def get_language(language_code, reporter=None):
    """Return module with language localizations.

    `language_code` is a "BCP 47" language tag.
    If there is no matching module, warn and fall back to English.
    """
    # TODO: use a dummy module returning emtpy strings?, configurable?
    for tag in normalize_language_tag(language_code):
        tag = tag.replace('-','_') # '-' not valid in module names
        if tag in _languages:
            return _languages[tag]
        try:
            module = __import__(tag, globals(), locals(), level=0)
        except ImportError:
            try:
                module = __import__(tag, globals(), locals(), level=1)
            except ImportError:
                continue
        _languages[tag] = module
        return module
    if reporter is not None:
        reporter.warning(
            'language "%s" not supported: ' % language_code +
            'Docutils-generated text will be in English.')
    module = __import__('en', globals(), locals(), level=1)
    _languages[tag] = module # warn only one time!
    return module
Example #2
0
def get_language(language_code, reporter=None):
    """Return module with language localizations.

    `language_code` is a "BCP 47" language tag.
    If there is no matching module, warn and fall back to English.
    """
    # TODO: use a dummy module returning emtpy strings?, configurable?
    for tag in normalize_language_tag(language_code):
        tag = tag.replace('-','_') # '-' not valid in module names
        if tag in _languages:
            return _languages[tag]
        try:
            module = __import__(tag, globals(), locals(), level=1)
        except ImportError:
            try:
                module = __import__(tag, globals(), locals(), level=0)
            except ImportError:
                continue
        _languages[tag] = module
        return module
    if reporter is not None:
        reporter.warning(
            'language "%s" not supported: ' % language_code +
            'Docutils-generated text will be in English.')
    module = __import__('en', globals(), locals(), level=1)
    _languages[tag] = module # warn only one time!
    return module
Example #3
0
def get_writer_class(writer_name):
    """Return the Writer class from the `writer_name` module."""
    writer_name = writer_name.lower()
    if writer_name in _writer_aliases:
        writer_name = _writer_aliases[writer_name]
    try:
        module = __import__(writer_name, globals(), locals(), level=1)
    except ImportError:
        module = __import__(writer_name, globals(), locals(), level=0)
    return module.Writer
def get_parser_class(parser_name):
    """Return the Parser class from the `parser_name` module."""
    parser_name = parser_name.lower()
    if parser_name in _parser_aliases:
        parser_name = _parser_aliases[parser_name]
    try:
        module = __import__(parser_name, globals(), locals(), level=1)
    except ImportError:
        module = __import__(parser_name, globals(), locals(), level=0)
    return module.Parser
def get_writer_class(writer_name):
    """Return the Writer class from the `writer_name` module."""
    writer_name = writer_name.lower()
    if writer_name in _writer_aliases:
        writer_name = _writer_aliases[writer_name]
    try:
        module = __import__(writer_name, globals(), locals(), level=1)
    except ImportError:
        module = __import__(writer_name, globals(), locals(), level=0)
    return module.Writer
Example #6
0
def get_reader_class(reader_name):
    """Return the Reader class from the `reader_name` module."""
    reader_name = reader_name.lower()
    if reader_name in _reader_aliases:
        reader_name = _reader_aliases[reader_name]
    try:
        module = __import__(reader_name, globals(), locals(), level=1)
    except ImportError:
        module = __import__(reader_name, globals(), locals(), level=0)
    return module.Reader
def get_reader_class(reader_name):
    """Return the Reader class from the `reader_name` module."""
    reader_name = reader_name.lower()
    if reader_name in _reader_aliases:
        reader_name = _reader_aliases[reader_name]
    try:
        module = __import__(reader_name, globals(), locals(), level=0)
    except ImportError:
        module = __import__(reader_name, globals(), locals(), level=1)
    return module.Reader
Example #8
0
def get_parser_class(parser_name):
    """Return the Parser class from the `parser_name` module."""
    parser_name = parser_name.lower()
    if parser_name in _parser_aliases:
        parser_name = _parser_aliases[parser_name]
    try:
        module = __import__(parser_name, globals(), locals(), level=1)
    except ImportError:
        module = __import__(parser_name, globals(), locals(), level=0)
    return module.Parser
Example #9
0
def get_language(language_code):
    for tag in normalize_language_tag(language_code):
        tag = tag.replace('-','_') # '-' not valid in module names
        if tag in _languages:
            return _languages[tag]
        try:
            module = __import__(tag, globals(), locals(), level=1)
        except ImportError:
            try:
                module = __import__(tag, globals(), locals(), level=0)
            except ImportError:
                continue
        _languages[tag] = module
        return module
    return None
Example #10
0
def get_language(language_code):
    for tag in normalize_language_tag(language_code):
        tag = tag.replace('-', '_')  # '-' not valid in module names
        if tag in _languages:
            return _languages[tag]
        try:
            module = __import__(tag, globals(), locals(), level=1)
        except ImportError:
            try:
                module = __import__(tag, globals(), locals(), level=0)
            except ImportError:
                continue
        _languages[tag] = module
        return module
    return None
def directive(directive_name, language_module, document):
    """
    Locate and return a directive function from its language-dependent name.
    If not found in the current language, check English.  Return None if the
    named directive cannot be found.
    """
    normname = directive_name.lower()
    messages = []
    msg_text = []
    if normname in _directives:
        return _directives[normname], messages
    canonicalname = None
    try:
        canonicalname = language_module.directives[normname]
    except AttributeError as error:
        msg_text.append('Problem retrieving directive entry from language '
                        'module %r: %s.' % (language_module, error))
    except KeyError:
        msg_text.append('No directive entry for "%s" in module "%s".' %
                        (directive_name, language_module.__name__))
    if not canonicalname:
        try:
            canonicalname = _fallback_language_module.directives[normname]
            msg_text.append('Using English fallback for directive "%s".' %
                            directive_name)
        except KeyError:
            msg_text.append('Trying "%s" as canonical directive name.' %
                            directive_name)
            # The canonical name should be an English name, but just in case:
            canonicalname = normname
    if msg_text:
        message = document.reporter.info('\n'.join(msg_text),
                                         line=document.current_line)
        messages.append(message)
    try:
        modulename, classname = _directive_registry[canonicalname]
    except KeyError:
        # Error handling done by caller.
        return None, messages
    try:
        module = __import__(modulename, globals(), locals(), level=1)
    except ImportError as detail:
        messages.append(
            document.reporter.error(
                'Error importing directive module "%s" (directive "%s"):\n%s' %
                (modulename, directive_name, detail),
                line=document.current_line))
        return None, messages
    try:
        directive = getattr(module, classname)
        _directives[normname] = directive
    except AttributeError:
        messages.append(
            document.reporter.error(
                'No directive class "%s" in module "%s" (directive "%s").' %
                (classname, modulename, directive_name),
                line=document.current_line))
        return None, messages
    return directive, messages
Example #12
0
def directive(directive_name, language_module, document):
    """
    Locate and return a directive function from its language-dependent name.
    If not found in the current language, check English.  Return None if the
    named directive cannot be found.
    """
    normname = directive_name.lower()
    messages = []
    msg_text = []
    if normname in _directives:
        return _directives[normname], messages
    canonicalname = None
    try:
        canonicalname = language_module.directives[normname]
    except AttributeError as error:
        msg_text.append('Problem retrieving directive entry from language '
                        'module %r: %s.' % (language_module, error))
    except KeyError:
        msg_text.append('No directive entry for "%s" in module "%s".'
                        % (directive_name, language_module.__name__))
    if not canonicalname:
        try:
            canonicalname = _fallback_language_module.directives[normname]
            msg_text.append('Using English fallback for directive "%s".'
                            % directive_name)
        except KeyError:
            msg_text.append('Trying "%s" as canonical directive name.'
                            % directive_name)
            # The canonical name should be an English name, but just in case:
            canonicalname = normname
    if msg_text:
        message = document.reporter.info(
            '\n'.join(msg_text), line=document.current_line)
        messages.append(message)
    try:
        modulename, classname = _directive_registry[canonicalname]
    except KeyError:
        # Error handling done by caller.
        return None, messages
    try:
        module = __import__(modulename, globals(), locals(), level=1)
    except ImportError as detail:
        messages.append(document.reporter.error(
            'Error importing directive module "%s" (directive "%s"):\n%s'
            % (modulename, directive_name, detail),
            line=document.current_line))
        return None, messages
    try:
        directive = getattr(module, classname)
        _directives[normname] = directive
    except AttributeError:
        messages.append(document.reporter.error(
            'No directive class "%s" in module "%s" (directive "%s").'
            % (classname, modulename, directive_name),
            line=document.current_line))
        return None, messages
    return directive, messages
Example #13
0
def get_language(language_code):
    for tag in normalize_language_tag(language_code):
        if tag in _languages:
            return _languages[tag]
        try:
            module = __import__(tag, globals(), locals(), level=1)
        except ImportError:
            continue
        _languages[tag] = module
        return module
    return None
Example #14
0
     except KeyError:
         msg_text.append('Trying "%s" as canonical directive-js name.'
                         % directive_name)
         # The canonical name should be an English name, but just in case:
         canonicalname = normname
 if msg_text:
     message = document.reporter.info(
         '\n'.join(msg_text), line=document.current_line)
     messages.append(message)
 try:
     modulename, classname = _directive_registry[canonicalname]
 except KeyError:
     # Error handling done by caller.
     return None, messages
 try:
     module = __import__(modulename, globals(), locals(), level=1)
 except ImportError, detail:
     messages.append(document.reporter.error(
         'Error importing directive-js module "%s" (directive-js "%s"):\n%s'
         % (modulename, directive_name, detail),
         line=document.current_line))
     return None, messages
 try:
     directive = getattr(module, classname)
     _directives[normname] = directive
 except AttributeError:
     messages.append(document.reporter.error(
         'No directive-js class "%s" in module "%s" (directive-js "%s").'
         % (classname, modulename, directive_name),
         line=document.current_line))
     return None, messages
Example #15
0
     except KeyError:
         msg_text.append('Trying "%s" as canonical directive name.' %
                         directive_name)
         # The canonical name should be an English name, but just in case:
         canonicalname = normname
 if msg_text:
     message = document.reporter.info('\n'.join(msg_text),
                                      line=document.current_line)
     messages.append(message)
 try:
     modulename, classname = _directive_registry[canonicalname]
 except KeyError:
     # Error handling done by caller.
     return None, messages
 try:
     module = __import__(modulename, globals(), locals(), level=1)
 except ImportError, detail:
     messages.append(
         document.reporter.error(
             'Error importing directive module "%s" (directive "%s"):\n%s' %
             (modulename, directive_name, detail),
             line=document.current_line))
     return None, messages
 try:
     directive = getattr(module, classname)
     _directives[normname] = directive
 except AttributeError:
     messages.append(
         document.reporter.error(
             'No directive class "%s" in module "%s" (directive "%s").' %
             (classname, modulename, directive_name),