Commits

Antonio Cuni  committed d30fc75 Draft

add support for running extract()/compile() from command line by using python -m i18n ...

  • Participants
  • Parent commits 69a5ae0

Comments (0)

Files changed (4)

File i18n/__main__.py

+import sys
+from i18n.cmdline import main
+
+main(sys.argv)

File i18n/cmdline.py

+import py
+import argparse
+
+def get_parser():
+    parser = argparse.ArgumentParser(prog='i18n',
+                                     description='Manage the translations of a project.')
+    parser.add_argument('--root', metavar='DIR', type=py.path.local, default='.',
+                        help='The root directory of the project (default: .)')
+    parser.add_argument('--languages', metavar='LANG,LANG,...', default='',
+                        help='Comma separated list of languages to be supported')
+    add_subparsers(parser)
+    return parser
+
+def add_subparsers(parser):
+    subparsers = parser.add_subparsers(dest='command')
+    subparsers.add_parser('extract',
+                          help='Extract the messages from the python files ' + 
+                          'and merge them with the existing catalogs in languages/')
+    subparsers.add_parser('compile', help='Compile the catalogs')
+
+
+def main(argv):
+    from i18n.translator import Translator
+    parser = get_parser()
+    args = parser.parse_args(argv[1:])
+    if args.root.check(dir=False):
+        parser.error('Not a directory: %s' % args.root)
+    languages = map(str.strip, args.languages.split(','))
+    tr = Translator(args.root, languages, autocompile=False)
+    if args.command == 'extract':
+        tr.extract()
+    elif args.command == 'compile':
+        tr.compile()
+    else:
+        parser.error('Invalid command: %s' % args.command)

File i18n/testing/test_translator.py

     tr = Translator(path, ['it_IT'])
     assert tr.rootdir.strpath == path
     
+
+def test_cmdline():
+    class MyTranslator(Translator):
+        def __init__(self):
+            pass
+
+        def compile(self):
+            self.cmd = 'compile'
+
+        def extract(self):
+            self.cmd = 'extract'
+
+    tr = MyTranslator()
+    tr.cmdline(['foo.py', 'compile'])
+    assert tr.cmd == 'compile'
+    tr.cmdline(['foo.py', 'extract'])
+    assert tr.cmd == 'extract'
+    py.test.raises(SystemExit, "tr.cmdline(['foo.py', 'foobar'])")
+    
+    

File i18n/translator.py

             self.langdir.join(lang, 'LC_MESSAGES').ensure(dir=1)
             print 'compiling language %s' % po.basename
             self._do_compile(po, lang)
+
+    def cmdline(self, argv):
+        import argparse
+        from i18n.cmdline import add_subparsers
+        parser = argparse.ArgumentParser(
+            description='Manage the translations of a project.')
+        add_subparsers(parser)
+        args = parser.parse_args(argv[1:])
+        if args.command == 'extract':
+            self.extract()
+        elif args.command == 'compile':
+            self.compile()
+        else:
+            parser.error('Invalid command: %s' % args.command)