Example #1
0
def fake_extract_command(filename, fileobj, method,
                         options=generate_options_map(),
                         keywords=get_setting('KEYWORDS'),
                         comment_tags=get_setting('COMMENT_TAGS')):
    catalog = Catalog(charset='utf-8')
    extracted = fake_extract_from_dir(filename, fileobj, method, options, keywords, comment_tags)
    for filename, lineno, msg, cmts, ctxt in extracted:
        catalog.add(msg, None, [(filename, lineno)], auto_comments=cmts,
                    context=ctxt)

    po_out = StringIO()
    write_po(po_out, catalog, width=80, omit_header=True)
    return unicode(po_out.getvalue())
Example #2
0
def extract_from_files(filenames,
                       method_map=METHODS,
                       options_map=generate_options_map(),
                       keywords=get_setting('KEYWORDS'),
                       comment_tags=get_setting('COMMENT_TAGS'),
                       callback=extract_callback,
                       strip_comment_tags=False):
    """Extract messages from any source files found in the given iterable.

    This function generates tuples of the form:

        ``(filename, lineno, message, comments)``

    Which extraction method is used per file is determined by the `method_map`
    parameter, which maps extended glob patterns to extraction method names.
    For example, the following is the default mapping:

    >>> method_map = [
    ...     ('**.py', 'python')
    ... ]

    This basically says that files with the filename extension ".py"
    should be processed by the "python" extraction
    method. Files that don't match any of the mapping patterns are ignored. See
    the documentation of the `pathmatch` function for details on the pattern
    syntax.

    The following extended mapping would also use the "genshi" extraction
    method on any file in "templates" subdirectory:

    >>> method_map = [
    ...     ('**/templates/**.*', 'genshi'),
    ...     ('**.py', 'python')
    ... ]

    The dictionary provided by the optional `options_map` parameter augments
    these mappings. It uses extended glob patterns as keys, and the values are
    dictionaries mapping options names to option values (both strings).

    The glob patterns of the `options_map` do not necessarily need to be the
    same as those used in the method mapping. For example, while all files in
    the ``templates`` folders in an application may be Genshi applications, the
    options for those files may differ based on extension:

    >>> options_map = {
    ...     '**/templates/**.txt': {
    ...         'template_class': 'genshi.template:TextTemplate',
    ...         'encoding': 'latin-1'
    ...     },
    ...     '**/templates/**.html': {
    ...         'include_attrs': ''
    ...     }
    ... }

    :param filenames: an iterable of filenames relative to the ROOT of
                      the project
    :param method_map: a list of ``(pattern, method)`` tuples that maps of
                       extraction method names to extended glob patterns
    :param options_map: a dictionary of additional options (optional)
    :param keywords: a dictionary mapping keywords (i.e. names of functions
                     that should be recognized as translation functions) to
                     tuples that specify which of their arguments contain
                     localizable strings
    :param comment_tags: a list of tags of translator comments to search for
                         and include in the results
    :param callback: a function that is called for every file that message are
                     extracted from, just before the extraction itself is
                     performed; the function is passed the filename, the name
                     of the extraction method and and the options dictionary as
                     positional arguments, in that order
    :param strip_comment_tags: a flag that if set to `True` causes all comment
                               tags to be removed from the collected comments.
    :return: an iterator over ``(filename, lineno, funcname, message)`` tuples
    :rtype: ``iterator``
    :see: `pathmatch`
    """
    # adapted from babel.messages.extract.extract_from_dir
    for filename in filenames:
        matched = False
        for pattern, method in method_map:
            if pathmatch(pattern, filename):
                matched = True
                filepath = os.path.join(settings.ROOT, filename)
                if not os.path.exists(filepath):
                    print('! %s does not exist!' % filename)
                    break
                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 matched:
            print('! %s does not match any domain methods!' % filename)
Example #3
0
def extract_from_files(filenames,
                       method_map=METHODS,
                       options_map=generate_options_map(),
                       keywords=get_setting('KEYWORDS'),
                       comment_tags=get_setting('COMMENT_TAGS'),
                       callback=extract_callback,
                       strip_comment_tags=False):
    """Extract messages from any source files found in the given iterable.

    This function generates tuples of the form:

        ``(filename, lineno, message, comments)``

    Which extraction method is used per file is determined by the `method_map`
    parameter, which maps extended glob patterns to extraction method names.
    For example, the following is the default mapping:

    >>> method_map = [
    ...     ('**.py', 'python')
    ... ]

    This basically says that files with the filename extension ".py"
    should be processed by the "python" extraction
    method. Files that don't match any of the mapping patterns are ignored. See
    the documentation of the `pathmatch` function for details on the pattern
    syntax.

    The following extended mapping would also use the "genshi" extraction
    method on any file in "templates" subdirectory:

    >>> method_map = [
    ...     ('**/templates/**.*', 'genshi'),
    ...     ('**.py', 'python')
    ... ]

    The dictionary provided by the optional `options_map` parameter augments
    these mappings. It uses extended glob patterns as keys, and the values are
    dictionaries mapping options names to option values (both strings).

    The glob patterns of the `options_map` do not necessarily need to be the
    same as those used in the method mapping. For example, while all files in
    the ``templates`` folders in an application may be Genshi applications, the
    options for those files may differ based on extension:

    >>> options_map = {
    ...     '**/templates/**.txt': {
    ...         'template_class': 'genshi.template:TextTemplate',
    ...         'encoding': 'latin-1'
    ...     },
    ...     '**/templates/**.html': {
    ...         'include_attrs': ''
    ...     }
    ... }

    :param filenames: an iterable of filenames relative to the ROOT of
                      the project
    :param method_map: a list of ``(pattern, method)`` tuples that maps of
                       extraction method names to extended glob patterns
    :param options_map: a dictionary of additional options (optional)
    :param keywords: a dictionary mapping keywords (i.e. names of functions
                     that should be recognized as translation functions) to
                     tuples that specify which of their arguments contain
                     localizable strings
    :param comment_tags: a list of tags of translator comments to search for
                         and include in the results
    :param callback: a function that is called for every file that message are
                     extracted from, just before the extraction itself is
                     performed; the function is passed the filename, the name
                     of the extraction method and and the options dictionary as
                     positional arguments, in that order
    :param strip_comment_tags: a flag that if set to `True` causes all comment
                               tags to be removed from the collected comments.
    :return: an iterator over ``(filename, lineno, funcname, message)`` tuples
    :rtype: ``iterator``
    :see: `pathmatch`
    """
    # adapted from babel.messages.extract.extract_from_dir
    for filename in filenames:
        matched = False
        for pattern, method in method_map:
            if pathmatch(pattern, filename):
                matched = True
                filepath = os.path.join(settings.ROOT, filename)
                if not os.path.exists(filepath):
                    print '! %s does not exist!' % filename
                    break
                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 matched:
            print '! %s does not match any domain methods!' % filename