예제 #1
0
파일: Babel.py 프로젝트: JPilarr/tapyr
    def babel_extract(fileobj, keywords, comment_tags, options):
        """Babel extraction method for Jinja templates.

        :param fileobj: the file-like object the messages should be extracted from
        :param keywords: a list of keywords (i.e. function names) that should be
                         recognized as translation functions
        :param comment_tags: a list of translator tags to search for and include
                             in the results.  (Unused)
        :param options: a dictionary of additional options (optional)
        :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
                 (comments will be empty currently)
        """
        extensions = set()
        for extension in options.get("extensions", "").split(","):
            extension = extension.strip()
            if not extension:
                continue
            extensions.add(ext.import_string(extension))
        for extension in JNJ.Environment.Default_Extensions:
            if isinstance(extension, pyk.string_types):
                extension = ext.import_string(extension)
            extensions.add(extension)
        if ext.InternationalizationExtension not in extensions:
            extensions.add(ext.InternationalizationExtension)

        def getbool(options, key, default=False):
            return options.get (key, str (default)).lower () in \
                ('1', 'on', 'yes', 'true')

        environment = ext.Environment \
            ( options.get ("block_start_string",    ext.BLOCK_START_STRING)
            , options.get ("block_end_string",      ext.BLOCK_END_STRING)
            , options.get ("variable_start_string", ext.VARIABLE_START_STRING)
            , options.get ("variable_end_string",   ext.VARIABLE_END_STRING)
            , options.get ("comment_start_string",  ext.COMMENT_START_STRING)
            , options.get ("comment_end_string",    ext.COMMENT_END_STRING)
            , options.get ("line_statement_prefix") or ext.LINE_STATEMENT_PREFIX
            , options.get ("line_comment_prefix") or ext.LINE_COMMENT_PREFIX
            , getbool (options, "trim_blocks",   ext.TRIM_BLOCKS)
            , getbool (options, "lstrip_blocks", ext.LSTRIP_BLOCKS)
            , ext.NEWLINE_SEQUENCE
            , getbool
                (options, "keep_trailing_newline", ext.KEEP_TRAILING_NEWLINE)
            , frozenset (extensions)
            , cache_size  = 0
            , auto_reload = False
            )

        source = fileobj.read().decode(options.get("encoding", "utf-8"))
        node = environment.parse(source)
        for lineno, func, message in extract_from_ast(node, keywords):
            yield lineno, func, message, []
예제 #2
0
파일: Babel.py 프로젝트: Tapyr/tapyr
    def babel_extract (fileobj, keywords, comment_tags, options) :
        """Babel extraction method for Jinja templates.

        :param fileobj: the file-like object the messages should be extracted from
        :param keywords: a list of keywords (i.e. function names) that should be
                         recognized as translation functions
        :param comment_tags: a list of translator tags to search for and include
                             in the results.  (Unused)
        :param options: a dictionary of additional options (optional)
        :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
                 (comments will be empty currently)
        """
        extensions = set ()
        for extension in options.get ("extensions", "").split (",") :
            extension = extension .strip ()
            if not extension:
                continue
            extensions.add (ext.import_string (extension))
        for extension in JNJ.Environment.Default_Extensions :
            if isinstance (extension, pyk.string_types) :
                extension = ext.import_string (extension)
            extensions.add (extension)
        if ext.InternationalizationExtension not in extensions :
            extensions.add (ext.InternationalizationExtension)

        def getbool(options, key, default=False):
            return options.get (key, str (default)).lower () in \
                ('1', 'on', 'yes', 'true')

        environment = ext.Environment \
            ( options.get ("block_start_string",    ext.BLOCK_START_STRING)
            , options.get ("block_end_string",      ext.BLOCK_END_STRING)
            , options.get ("variable_start_string", ext.VARIABLE_START_STRING)
            , options.get ("variable_end_string",   ext.VARIABLE_END_STRING)
            , options.get ("comment_start_string",  ext.COMMENT_START_STRING)
            , options.get ("comment_end_string",    ext.COMMENT_END_STRING)
            , options.get ("line_statement_prefix") or ext.LINE_STATEMENT_PREFIX
            , options.get ("line_comment_prefix") or ext.LINE_COMMENT_PREFIX
            , getbool (options, "trim_blocks",   ext.TRIM_BLOCKS)
            , getbool (options, "lstrip_blocks", ext.LSTRIP_BLOCKS)
            , ext.NEWLINE_SEQUENCE
            , getbool (options, "keep_trailing_newline", ext.KEEP_TRAILING_NEWLINE)
            , frozenset (extensions)
            , cache_size  = 0
            , auto_reload = False
            )

        source = fileobj.read ().decode (options.get ("encoding", "utf-8"))
        node   = environment.parse (source)
        for lineno, func, message in extract_from_ast (node, keywords) :
            yield lineno, func, message, []
예제 #3
0
파일: Babel.py 프로젝트: JPilarr/tapyr
    def babel_extract(fileobj, keywords, comment_tags, options):
        """Babel extraction method for Jinja templates.

        :param fileobj: the file-like object the messages should be extracted from
        :param keywords: a list of keywords (i.e. function names) that should be
                         recognized as translation functions
        :param comment_tags: a list of translator tags to search for and include
                             in the results.  (Unused)
        :param options: a dictionary of additional options (optional)
        :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
                 (comments will be empty currently)
        """
        extensions = set()
        for extension in options.get("extensions", "").split(","):
            extension = extension.strip()
            if not extension:
                continue
            extensions.add(ext.import_string(extension))
        for extension in JNJ.Environment.Default_Extensions:
            if isinstance(extension, pyk.string_types):
                extension = ext.import_string(extension)
            extensions.add(extension)
        if ext.InternationalizationExtension not in extensions:
            extensions.add(ext.InternationalizationExtension)

        environment = get_spontaneous_environment \
            ( options.get ("block_start_string",    ext.BLOCK_START_STRING)
            , options.get ("block_end_string",      ext.BLOCK_END_STRING)
            , options.get ("variable_start_string", ext.VARIABLE_START_STRING)
            , options.get ("variable_end_string",   ext.VARIABLE_END_STRING)
            , options.get ("comment_start_string",  ext.COMMENT_START_STRING)
            , options.get ("comment_end_string",    ext.COMMENT_END_STRING)
            , options.get ("line_statement_prefix") or ext.LINE_STATEMENT_PREFIX
            , options.get ("line_comment_prefix") or ext.LINE_COMMENT_PREFIX
            , str (options.get ("trim_blocks", ext.TRIM_BLOCKS)).lower()
              in ("1", "on", "yes", "true")
            , ext.NEWLINE_SEQUENCE
            , frozenset (extensions)
            # fill with defaults so that environments are shared
            # with other spontaneus environments.  The rest of the
            # arguments are optimizer, undefined, finalize, autoescape,
            # loader, cache size, auto reloading setting and the
            # bytecode cache
            , True, Undefined, None, False, None, 0, False, None
            )

        source = fileobj.read().decode(options.get("encoding", "utf-8"))
        node = environment.parse(source)
        for lineno, func, message in extract_from_ast(node, keywords):
            yield lineno, func, message, []
예제 #4
0
파일: Babel.py 프로젝트: Tapyr/tapyr
    def babel_extract (fileobj, keywords, comment_tags, options) :
        """Babel extraction method for Jinja templates.

        :param fileobj: the file-like object the messages should be extracted from
        :param keywords: a list of keywords (i.e. function names) that should be
                         recognized as translation functions
        :param comment_tags: a list of translator tags to search for and include
                             in the results.  (Unused)
        :param options: a dictionary of additional options (optional)
        :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
                 (comments will be empty currently)
        """
        extensions = set ()
        for extension in options.get ("extensions", "").split (",") :
            extension = extension .strip ()
            if not extension:
                continue
            extensions.add (ext.import_string (extension))
        for extension in JNJ.Environment.Default_Extensions :
            if isinstance (extension, pyk.string_types) :
                extension = ext.import_string (extension)
            extensions.add (extension)
        if ext.InternationalizationExtension not in extensions :
            extensions.add (ext.InternationalizationExtension)

        environment = get_spontaneous_environment \
            ( options.get ("block_start_string",    ext.BLOCK_START_STRING)
            , options.get ("block_end_string",      ext.BLOCK_END_STRING)
            , options.get ("variable_start_string", ext.VARIABLE_START_STRING)
            , options.get ("variable_end_string",   ext.VARIABLE_END_STRING)
            , options.get ("comment_start_string",  ext.COMMENT_START_STRING)
            , options.get ("comment_end_string",    ext.COMMENT_END_STRING)
            , options.get ("line_statement_prefix") or ext.LINE_STATEMENT_PREFIX
            , options.get ("line_comment_prefix") or ext.LINE_COMMENT_PREFIX
            , str (options.get ("trim_blocks", ext.TRIM_BLOCKS)).lower()
              in ("1", "on", "yes", "true")
            , ext.NEWLINE_SEQUENCE
            , frozenset (extensions)
            # fill with defaults so that environments are shared
            # with other spontaneus environments.  The rest of the
            # arguments are optimizer, undefined, finalize, autoescape,
            # loader, cache size, auto reloading setting and the
            # bytecode cache
            , True, ext.Undefined, None, False, None, 0, False, None
            )

        source = fileobj.read ().decode (options.get ("encoding", "utf-8"))
        node   = environment.parse (source)
        for lineno, func, message in extract_from_ast (node, keywords) :
            yield lineno, func, message, []