Skip to content

Commit

Permalink
Addressed comments. Mostly style and comments, no functionality changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eoin Nugent committed Jan 13, 2016
1 parent d91ba25 commit 79ddbf4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
68 changes: 33 additions & 35 deletions babel/messages/extract.py
Expand Up @@ -144,30 +144,28 @@ def extract_from_dir(dirname=None, method_map=DEFAULT_MAPPING,
for filename in filenames:
filepath = os.path.join(root, filename).replace(os.sep, '/')

for (
extracted_filename,
lineno,
message,
comments,
context,
) in check_and_call_extract_file(
filepath,
method_map,
options_map,
callback,
keywords,
comment_tags,
strip_comment_tags,
dirpath=absname,
for message_tuple in check_and_call_extract_file(
filepath,
method_map,
options_map,
callback,
keywords,
comment_tags,
strip_comment_tags,
dirpath=absname,
):
yield extracted_filename, lineno, message, comments, context
yield message_tuple


def check_and_call_extract_file(filepath, method_map, options_map,
callback, keywords, comment_tags,
strip_comment_tags, dirpath=None):
"""Checks if the given file matches an extraction method mapping, and if so, calls extract_from_file.
Note that the extraction method mappings are based relative to dirpath.
So, given an absolute path to a file `filepath`, we want to check using
just the relative path from `dirpath` to `filepath`.
:param filepath: An absolute path to a file that exists.
:param method_map: a list of ``(pattern, method)`` tuples that maps of
extraction method names to extended glob patterns
Expand All @@ -187,29 +185,29 @@ def check_and_call_extract_file(filepath, method_map, options_map,
tags to be removed from the collected comments.
:param dirpath: the path to the directory to extract messages from.
"""
if dirpath is None:
dirpath = os.getcwd()

# filename is the relative path from dirpath to the actual file
filename = relpath(filepath, dirpath)

for pattern, method in method_map:
if pathmatch(pattern, filename):
options = {}
for opattern, odict in options_map.items():
if pathmatch(opattern, filename):
options = odict
if callback:
callback(filename, method, options)
for lineno, message, comments, context in \
extract_from_file(method, filepath,
keywords=keywords,
comment_tags=comment_tags,
options=options,
strip_comment_tags=
strip_comment_tags):
yield filename, lineno, message, comments, context
break
if not pathmatch(pattern, filename):
continue

options = {}
for opattern, odict in options_map.items():
if pathmatch(opattern, filename):
options = odict
if callback:
callback(filename, method, options)
for message_tuple in extract_from_file(
method, filepath,
keywords=keywords,
comment_tags=comment_tags,
options=options,
strip_comment_tags=strip_comment_tags
):
yield (filename, ) + message_tuple

break


def extract_from_file(method, filename, keywords=DEFAULT_KEYWORDS,
Expand Down
23 changes: 13 additions & 10 deletions babel/messages/frontend.py
Expand Up @@ -313,8 +313,8 @@ def finalize_options(self):
raise DistutilsOptionError("no input files or directories specified")

for path in self.input_paths:
if not os.path.isdir(path) and not os.path.isfile(path):
raise DistutilsOptionError("Input path: %s is not a file or directory" % path)
if not os.path.exists(path):
raise DistutilsOptionError("Input path: %s does not exist" % path)

if self.add_comments:
if isinstance(self.add_comments, string_types):
Expand Down Expand Up @@ -357,23 +357,26 @@ def callback(filename, method, options):
k, v in options.items()])
self.log.info('extracting messages from %s%s', filepath, optstr)

if os.path.isdir(path):
if os.path.isfile(path):
current_dir = os.getcwd()
extracted = check_and_call_extract_file(
path, method_map, options_map,
callback, self._keywords, self.add_comments,
self.strip_comments, current_dir
)
else:
extracted = extract_from_dir(
path, method_map, options_map,
keywords=self._keywords,
comment_tags=self.add_comments,
callback=callback,
strip_comment_tags=self.strip_comments
)
else:
extracted = check_and_call_extract_file(path, method_map,
options_map, callback, self._keywords,
self.add_comments, self.strip_comments)
for filename, lineno, message, comments, context in extracted:
if os.path.isdir(path):
filepath = os.path.normpath(os.path.join(path, filename))
if os.path.isfile(path):
filepath = filename # already normalized
else:
filepath = filename
filepath = os.path.normpath(os.path.join(path, filename))

catalog.add(message, None, [(filepath, lineno)],
auto_comments=comments, context=context)
Expand Down

0 comments on commit 79ddbf4

Please sign in to comment.