Example #1
0
    def run(self):
        if Environment is None:
            logging.critical(_('package jinja2 is required.'))
            return 1
        env = Environment(loader=PackageLoader('starterpyth.commands', 'templates'))

        def write_template(template_, path, context):
            """
            Write a template file.

            :param template_: Jinja2 template
            :type template_: :class:`Template`
            :param path: destination path
            :type path: basestring
            :param context: context
            :type context: :class:`dict`
            """
            dirname = os.path.dirname(path)
            if not os.path.isdir(dirname):
                os.makedirs(dirname)
            if not os.path.isfile(path) or self.overwrite:
                tpl_fd = codecs.open(path, 'w', encoding='utf-8')
                tpl_fd.write(template_.render(context))
                tpl_fd.close()
                logging.info('writing %s' % path)
        src_module_names = find_packages()
        if self.pre_rm and os.path.isdir(self.api_dir):
            logging.info('removing %s' % self.api_dir)
            shutil.rmtree(self.api_dir)
        module_names = []
        excluded_module_names = set([x.strip() for x in self.modules_to_exclude.split(',') if x.strip()])

        for module_name in src_module_names:
            module = load_module(module_name)
            logging.warning('Processing %s.' % module_name)
            if not any(fnmatch.fnmatch(module_name, x) for x in excluded_module_names):
                module_names.append(module_name)
            module_root = os.path.dirname(module.__file__)
            for filename in os.listdir(module_root):
                basename, sep, ext = filename.rpartition('.')
                if ext not in ('pyx', 'py', 'so') or filename == '__init__.py':
                    continue
                submodule_name = '%s.%s' % (module_name, basename)
                try:
                    load_module(submodule_name)
                    if not any(fnmatch.fnmatch(submodule_name, x) for x in excluded_module_names):
                        module_names.append(submodule_name)
                except ImportError as e:
                    msg = 'Unable to import %s [%s].' % (submodule_name, e)
                    logging.warning(msg)
        template = env.get_template('index.rst_tpl')
        all_module_names = [mod_name.replace('.', '/') for mod_name in module_names]
        all_module_names.sort()
        write_template(template, os.path.join(self.api_dir, 'index.rst'),
                       {'module_paths': all_module_names})
        template = env.get_template('module.rst_tpl')
        for mod_name in module_names:
            path_components = mod_name.split('.')
            path_components[-1] += '.rst'
            write_template(template, os.path.join(self.api_dir, *path_components), {'module_name': mod_name})
Example #2
0
    def run(self):
        if polib is None:
            print(red(_('package polib is required.')))
            return 1
        module_names = find_packages()
        dst_rel_path = 'locale' if self.dest is None else self.dest
        # group by top-level packages and compute their directories:
        top_levels_modules = {}
        for module_name in module_names:
            top_level = module_name.partition('.')[0]
            if top_level not in top_levels_modules:
                locale_dir = os.path.join(
                    os.path.dirname(load_module(top_level).__file__),
                    dst_rel_path)
                top_levels_modules[top_level] = locale_dir

        for module_name, locale_dir in top_levels_modules.items():
            po_filename = os.path.join(locale_dir, self.language,
                                       'LC_MESSAGES', '%s.po' % module_name)
            if not os.path.isfile(po_filename):
                print(
                    yellow(
                        _('Missing file: %(filename)s. Please run the makemessages -l xx_XX command first.'
                          ) % {'filename': po_filename}))
                continue
            po_content = polib.pofile(po_filename)
            for entry in po_content:
                entry.msgstr = translate_string(entry.msgid)
            print(
                green(
                    _('Processed file: %(filename)s.') %
                    {'filename': po_filename}))
            po_content.save(po_filename)
Example #3
0
    def run(self):
        if polib is None:
            print(red(_('package polib is required.')))
            return 1
        module_names = find_packages()
        dst_rel_path = 'locale' if self.dest is None else self.dest
        # group by top-level packages and compute their directories:
        top_levels_modules = {}
        for module_name in module_names:
            top_level = module_name.partition('.')[0]
            if top_level not in top_levels_modules:
                locale_dir = os.path.join(os.path.dirname(load_module(top_level).__file__), dst_rel_path)
                top_levels_modules[top_level] = locale_dir

        for module_name, locale_dir in top_levels_modules.items():
            po_filename = os.path.join(locale_dir, self.language, 'LC_MESSAGES', '%s.po' % module_name)
            if not os.path.isfile(po_filename):
                print(yellow(_('Missing file: %(filename)s. Please run the makemessages -l xx_XX command first.')
                             % {'filename': po_filename}))
                continue
            po_content = polib.pofile(po_filename)
            for entry in po_content:
                entry.msgstr = translate_string(entry.msgid)
            print(green(_('Processed file: %(filename)s.') % {'filename': po_filename}))
            po_content.save(po_filename)
Example #4
0
    def run(self):
        module_names = find_packages()
        dst_rel_path = 'locale' if self.dest is None else self.dest
        # group by top-level packages and compute their directories:
        top_levels_modules = {}
        for module_name in module_names:
            tl, sep, bl = module_name.partition('.')
            if tl not in top_levels_modules:
                locale_dir = os.path.join(
                    os.path.dirname(load_module(tl).__file__), dst_rel_path)
                top_levels_modules[tl] = locale_dir

        for module_name, locale_dir in top_levels_modules.items():
            mo_file = os.path.join(locale_dir, self.language, 'LC_MESSAGES',
                                   '%s.mo' % module_name)
            po_file = os.path.join(locale_dir, self.language, 'LC_MESSAGES',
                                   '%s.po' % module_name)
            if not os.path.isdir(os.path.dirname(mo_file)):
                os.makedirs(os.path.dirname(mo_file))
            if os.path.isfile(po_file):
                cmd = 'msgfmt --output-file %s %s' % (mo_file, po_file)
                display(_('Processing file %(filename)s.') %
                        {'filename': po_file},
                        color=GREEN)
                subprocess.check_call(cmd, shell=True, stderr=subprocess.PIPE)
Example #5
0
 def run(self):
     module_names = find_packages()
     for module_name in module_names:
         module = load_module(module_name)
         display('Processing %s.' % module_name, color=YELLOW)
         doctest.testmod(module)
         module_root = os.path.dirname(module.__file__)
         for filename in os.listdir(module_root):
             basename, sep, ext = filename.rpartition('.')
             if ext != 'py' or filename == '__init__.py':
                 continue
             submodule_name = '%s.%s' % (module_name, basename)
             try:
                 module = load_module(submodule_name)
                 display('Processing %s.' % submodule_name, color=GREEN)
                 doctest.testmod(module)
             except ImportError:
                 display('Unable to import %s.' % submodule_name, color=YELLOW)
Example #6
0
 def run(self):
     module_names = find_packages()
     for module_name in module_names:
         module = load_module(module_name)
         display('Processing %s.' % module_name, color=YELLOW)
         doctest.testmod(module)
         module_root = os.path.dirname(module.__file__)
         for filename in os.listdir(module_root):
             basename, sep, ext = filename.rpartition('.')
             if ext != 'py' or filename == '__init__.py':
                 continue
             submodule_name = '%s.%s' % (module_name, basename)
             try:
                 module = load_module(submodule_name)
                 display('Processing %s.' % submodule_name, color=GREEN)
                 doctest.testmod(module)
             except ImportError:
                 display('Unable to import %s.' % submodule_name,
                         color=YELLOW)
Example #7
0
    def run(self):
        module_names = find_packages()
        dst_rel_path = 'locale' if self.dest is None else self.dest
        # group by top-level packages and compute their directories:
        top_levels_modules = {}
        for module_name in module_names:
            tl, sep, bl = module_name.partition('.')
            if tl not in top_levels_modules:
                locale_dir = os.path.join(os.path.dirname(load_module(tl).__file__), dst_rel_path)
                top_levels_modules[tl] = locale_dir

        for module_name, locale_dir in top_levels_modules.items():
            mo_file = os.path.join(locale_dir, self.language, 'LC_MESSAGES', '%s.mo' % module_name)
            po_file = os.path.join(locale_dir, self.language, 'LC_MESSAGES', '%s.po' % module_name)
            if not os.path.isdir(os.path.dirname(mo_file)):
                os.makedirs(os.path.dirname(mo_file))
            if os.path.isfile(po_file):
                cmd = 'msgfmt --output-file %s %s' % (mo_file, po_file)
                display(_('Processing file %(filename)s.') % {'filename': po_file}, color=GREEN)
                subprocess.check_call(cmd, shell=True, stderr=subprocess.PIPE)
Example #8
0
 def run(self):
     if Environment is None:
         display(_('package jinja2 is required.'), color=RED)
         return 1
     module_names = find_packages()
     dst_rel_path = 'locale' if self.dest is None else self.dest
     # group by top-level packages and compute their directories:
     all_modules = {}
     top_levels_modules = {}
     for module_name in module_names:
         top_module = module_name.partition('.')[0]
         all_modules.setdefault(top_module, []).append(module_name)
         if top_module not in top_levels_modules:
             top_levels_modules[top_module] = os.path.dirname(load_module(top_module).__file__)
     env = Environment(loader=PackageLoader('starterpyth.commands.makemessages', 'templates'))
     template = env.get_template('lang.po')
     context = {
         'description': self.distribution.get_description(),
         'copyright': self.distribution.get_author(),
         'package': None,
         'author': self.distribution.get_author(),
         'version': self.distribution.get_version(),
         'email': self.distribution.get_author_email(),
         'year': datetime.datetime.now().year,
     }
     for tl_name in top_levels_modules.keys():
         dst_abs_path = os.path.join(top_levels_modules[tl_name], dst_rel_path)
         pot_filename = os.path.join(dst_abs_path, '%s.pot' % tl_name)
         po_filename = os.path.join(dst_abs_path, self.language, 'LC_MESSAGES', '%s.po' % tl_name)
         if not os.path.isdir(os.path.dirname(po_filename)):
             os.makedirs(os.path.dirname(po_filename))
         for filename in (pot_filename, po_filename):
             if not os.path.isfile(filename):
                 context['package'] = tl_name
                 po_fd = codecs.open(filename, 'w', encoding='utf-8')
                 po_fd.write(template.render(context))
                 po_fd.close()
     for tl_name, module_names in all_modules.items():
         dst_abs_path = os.path.join(top_levels_modules[tl_name], dst_rel_path)
         root_path = os.path.dirname(top_levels_modules[tl_name])
         print(root_path)
         pot_filename = os.path.join(dst_abs_path, '%s.pot' % tl_name)
         po_filename = os.path.join(dst_abs_path, self.language, 'LC_MESSAGES', '%s.po' % tl_name)
         # build the list of files to examine, for each top-level module
         filenames = []
         for module_name in module_names:
             init_filename = load_module(module_name).__file__
             local_root = os.path.dirname(init_filename)
             for filename in os.listdir(local_root):
                 filename = os.path.join(local_root, filename)
                 basename, sepa, ext = filename.rpartition('.')
                 if ext not in ('py', 'pyx', 'c'):
                     continue
                 try:
                     po_fd = codecs.open(filename, 'r', encoding='utf-8')
                     po_fd.read()
                     po_fd.close()
                     filenames.append(os.path.relpath(filename, root_path))
                     msg = _('%(filename)s added.') % {'filename': filename}
                     display(msg, color=GREEN)
                 except UnicodeDecodeError:
                     msg = _('Encoding of %(filename)s is not UTF-8.') % {'filename': filename}
                     display(msg, color=GREEN)
         cmd = ['xgettext', '--language=Python', '--keyword=_', u('--output=%s') % pot_filename,
                '--from-code=UTF-8', '--add-comments=Translators', ] + filenames
         subprocess.check_call(cmd, stdout=subprocess.PIPE)
         if os.path.isfile(po_filename):
             cmd = ['msgmerge', '--update', '--backup=off', po_filename, pot_filename, ]
         else:
             cmd = ['msginit', '--no-translator', '-l', self.language, u('--input=%s') % pot_filename,
                    u('--output=%s') % po_filename, ]
         subprocess.check_call(cmd, stderr=subprocess.PIPE)
         msg = _('Please translate strings in %(filename)s') % {'filename': po_filename}
         display(msg, color=YELLOW)
         msg = _('Then run setup.py compilemessages -l %(lang)s') % {'lang': self.language}
         display(msg, color=YELLOW)
Example #9
0
    def run(self):
        if Environment is None:
            logging.critical(_('package jinja2 is required.'))
            return 1
        env = Environment(
            loader=PackageLoader('starterpyth.commands', 'templates'))

        def write_template(template_, path, context):
            """
            Write a template file.

            :param template_: Jinja2 template
            :type template_: :class:`Template`
            :param path: destination path
            :type path: basestring
            :param context: context
            :type context: :class:`dict`
            """
            dirname = os.path.dirname(path)
            if not os.path.isdir(dirname):
                os.makedirs(dirname)
            if not os.path.isfile(path) or self.overwrite:
                tpl_fd = codecs.open(path, 'w', encoding='utf-8')
                tpl_fd.write(template_.render(context))
                tpl_fd.close()
                logging.info('writing %s' % path)

        src_module_names = find_packages()
        if self.pre_rm and os.path.isdir(self.api_dir):
            logging.info('removing %s' % self.api_dir)
            shutil.rmtree(self.api_dir)
        module_names = []
        for module_name in src_module_names:
            module = load_module(module_name)
            logging.warning('Processing %s.' % module_name)
            module_names.append(module_name)
            module_root = os.path.dirname(module.__file__)
            for filename in os.listdir(module_root):
                basename, sep, ext = filename.rpartition('.')
                if ext not in ('pyx', 'py', 'so') or filename == '__init__.py':
                    continue
                submodule_name = '%s.%s' % (module_name, basename)
                try:
                    load_module(submodule_name)
                    module_names.append(submodule_name)
                except ImportError:
                    msg = 'Unable to import %s.' % submodule_name
                    logging.warning(msg)
        template = env.get_template('index.rst_tpl')
        write_template(
            template, os.path.join(self.api_dir, 'index.rst'), {
                'module_paths':
                [mod_name.replace('.', '/') for mod_name in module_names]
            })
        template = env.get_template('module.rst_tpl')
        for mod_name in module_names:
            path_components = mod_name.split('.')
            path_components[-1] += '.rst'
            write_template(template,
                           os.path.join(self.api_dir, *path_components),
                           {'module_name': mod_name})
Example #10
0
 def run(self):
     module_names = find_packages()
     dst_rel_path = 'locale' if self.dest is None else self.dest
     # group by top-level packages and compute their directories:
     all_modules = {}
     top_levels_modules = {}
     for module_name in module_names:
         top_module = module_name.partition('.')[0]
         all_modules.setdefault(top_module, []).append(module_name)
         if top_module not in top_levels_modules:
             top_levels_modules[top_module] = os.path.dirname(load_module(top_module).__file__)
     env = Environment(loader=PackageLoader('starterpyth.commands.makemessages', 'templates'))
     template = env.get_template('lang.po')
     context = {
         'description': self.distribution.get_description(),
         'copyright': self.distribution.get_author(),
         'package': None,
         'author': self.distribution.get_author(),
         'version': self.distribution.get_version(),
         'email': self.distribution.get_author_email(),
         'year': datetime.datetime.now().year,
     }
     for tl_name in top_levels_modules.keys():
         dst_abs_path = os.path.join(top_levels_modules[tl_name], dst_rel_path)
         pot_filename = os.path.join(dst_abs_path, '%s.pot' % tl_name)
         po_filename = os.path.join(dst_abs_path, self.language, 'LC_MESSAGES', '%s.po' % tl_name)
         if not os.path.isdir(os.path.dirname(po_filename)):
             os.makedirs(os.path.dirname(po_filename))
         for filename in (pot_filename, po_filename):
             if not os.path.isfile(filename):
                 context['package'] = tl_name
                 po_fd = codecs.open(filename, 'w', encoding='utf-8')
                 po_fd.write(template.render(context))
                 po_fd.close()
     for tl_name, module_names in all_modules.items():
         dst_abs_path = os.path.join(top_levels_modules[tl_name], dst_rel_path)
         root_path = os.path.dirname(top_levels_modules[tl_name])
         print(root_path)
         pot_filename = os.path.join(dst_abs_path, '%s.pot' % tl_name)
         po_filename = os.path.join(dst_abs_path, self.language, 'LC_MESSAGES', '%s.po' % tl_name)
         # build the list of files to examine, for each top-level module
         filenames = []
         for module_name in module_names:
             init_filename = load_module(module_name).__file__
             local_root = os.path.dirname(init_filename)
             for filename in os.listdir(local_root):
                 filename = os.path.join(local_root, filename)
                 basename, sepa, ext = filename.rpartition('.')
                 if ext not in ('py', 'pyx', 'c'):
                     continue
                 try:
                     po_fd = codecs.open(filename, 'r', encoding='utf-8')
                     po_fd.read()
                     po_fd.close()
                     filenames.append(os.path.relpath(filename, root_path))
                     msg = _('%(filename)s added.') % {'filename': filename}
                     logging.info(msg)
                 except UnicodeDecodeError:
                     msg = _('Encoding of %(filename)s is not UTF-8.') % {'filename': filename}
                     logging.error(msg)
         cmd = ['xgettext', '--language=Python', '--keyword=_', py3k_unicode('--output=%s') % pot_filename,
                '--from-code=UTF-8', '--add-comments=Translators', ] + filenames
         subprocess.check_call(cmd, stdout=subprocess.PIPE)
         if os.path.isfile(po_filename):
             cmd = ['msgmerge', '--update', '--backup=off', po_filename, pot_filename, ]
         else:
             cmd = ['msginit', '--no-translator', '-l', self.language, py3k_unicode('--input=%s') % pot_filename,
                    py3k_unicode('--output=%s') % po_filename, ]
         subprocess.check_call(cmd, stderr=subprocess.PIPE)
         msg = _('Please translate strings in %(filename)s') % {'filename': po_filename}
         logging.warning(msg)
         msg = _('Then run setup.py compilemessages -l %(lang)s') % {'lang': self.language}
         logging.warning(msg)