# HG changeset patch # User cmlenz # Date 1181302095 0 # Node ID a6183d300a6e626620378b1eaacfc92ef68ac248 # Parent 27fba894d3ca66122d9030d4d16ee48f9215b114 * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories. * Add a pseudo extractor called ?ignore? for #10. diff --git a/babel/messages/extract.py b/babel/messages/extract.py --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -117,6 +117,12 @@ if options_map is None: options_map = {} + # Sort methods by pattern length + # FIXME: we'll probably need to preserve the user-supplied order in the + # frontends + methods = method_map.items() + methods.sort(lambda a,b: -cmp(len(a[0]), len(b[0]))) + absname = os.path.abspath(dirname) for root, dirnames, filenames in os.walk(absname): for subdir in dirnames: @@ -127,7 +133,7 @@ os.path.join(root, filename).replace(os.sep, '/'), dirname ) - for pattern, method in method_map.items(): + for pattern, method in methods: if pathmatch(pattern, filename): filepath = os.path.join(absname, filename) options = {} @@ -135,11 +141,12 @@ if pathmatch(opattern, filename): options = odict if callback: - callback(filename, options) + callback(filename, method, options) for lineno, message in extract_from_file(method, filepath, keywords=keywords, options=options): yield filename, lineno, message + break def extract_from_file(method, filename, keywords=DEFAULT_KEYWORDS, options=None): @@ -216,6 +223,12 @@ raise ValueError('Unknown extraction method %r' % method) +def extract_nothing(fileobj, keywords, options): + """Pseudo extractor that does not actually extract anything, but simply + returns an empty list. + """ + return [] + def extract_genshi(fileobj, keywords, options): """Extract messages from Genshi templates. diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -77,6 +77,8 @@ ('no-wrap', None, 'do not break long message lines, longer than the output line width, ' 'into several lines') + ('input-dirs=', + 'directories that should be scanned for messages'), ] boolean_options = [ 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap' @@ -90,6 +92,7 @@ self.no_location = False self.omit_header = False self.output_file = None + self.input_dirs = None self.width = 76 self.no_wrap = False @@ -111,6 +114,11 @@ else: self.width = int(self.width) + if not self.input_dirs: + self.input_dirs = dict.fromkeys([k.split('.',1)[0] + for k in self.distribution.packages + ]).keys() + def run(self): if self.mapping_file: fileobj = open(self.mapping_file, 'U') @@ -136,21 +144,25 @@ outfile = open(self.output_file, 'w') try: - def callback(filename, options): - optstr = '' - if options: - optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for k, v - in options.items()]) - log.info('extracting messages from %s%s' % (filename, optstr)) + catalog = Catalog() + for dirname in self.input_dirs: + def callback(filename, method, options): + if method == 'ignore': + return + filepath = os.path.normpath(os.path.join(dirname, filename)) + optstr = '' + if options: + optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for + k, v in options.items()]) + log.info('extracting messages from %s%s' + % (filepath, optstr)) - catalog = Catalog() - extracted = extract_from_dir(method_map=method_map, - options_map=options_map, - keywords=self.keywords, - callback=callback) - for filename, lineno, message in extracted: - filepath = os.path.normpath(filename) - catalog.add(message, None, [(filepath, lineno)]) + extracted = extract_from_dir(dirname, method_map, options_map, + keywords=self.keywords, + callback=callback) + for filename, lineno, message in extracted: + filepath = os.path.normpath(os.path.join(dirname, filename)) + catalog.add(message, None, [(filepath, lineno)]) log.info('writing PO template file to %s' % self.output_file) write_pot(outfile, catalog, project=self.distribution.get_name(), diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -139,6 +139,7 @@ [babel.extractors] genshi = babel.messages.extract:extract_genshi python = babel.messages.extract:extract_python + ignore = babel.messages.extract:extract_nothing """, cmdclass = {'build_doc': build_doc, 'test_doc': test_doc}