From: Christopher Lenz Date: Fri, 8 Jun 2007 11:28:15 +0000 (+0000) Subject: * The `extract_messages` distutils command now operators on configurable input direc... X-Git-Tag: 1.0~572 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3368f82b4fea612f2c49ecfb6281b986e9737272;p=thirdparty%2Fbabel.git * 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 index 96bcc29e..b0d90e1d 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -117,6 +117,12 @@ def extract_from_dir(dirname=os.getcwd(), method_map=DEFAULT_MAPPING, 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 @@ def extract_from_dir(dirname=os.getcwd(), method_map=DEFAULT_MAPPING, 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 @@ def extract_from_dir(dirname=os.getcwd(), method_map=DEFAULT_MAPPING, 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 @@ def extract(method, fileobj, keywords=DEFAULT_KEYWORDS, options=None): 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 index c937ec2b..3459662b 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -77,6 +77,8 @@ class extract_messages(Command): ('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 @@ class extract_messages(Command): 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 @@ class extract_messages(Command): 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 @@ class extract_messages(Command): 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() - 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)]) + 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)) + + 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 index 12949290..a6e1fccf 100755 --- a/setup.py +++ b/setup.py @@ -139,6 +139,7 @@ setup( [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}