⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 misc.py

📁 Requirement =====================================================================================
💻 PY
📖 第 1 页 / 共 2 页
字号:
# Authors: David Goodger, Dethe Elza# Contact: goodger@users.sourceforge.net# Revision: $Revision: 4229 $# Date: $Date: 2005-12-23 00:46:16 +0100 (Fri, 23 Dec 2005) $# Copyright: This module has been placed in the public domain."""Miscellaneous directives."""__docformat__ = 'reStructuredText'import sysimport os.pathimport reimport timefrom docutils import io, nodes, statemachine, utilsfrom docutils.parsers.rst import directives, roles, statesfrom docutils.transforms import misctry:    import urllib2except ImportError:    urllib2 = Nonestandard_include_path = os.path.join(os.path.dirname(states.__file__),                                     'include')def include(name, arguments, options, content, lineno,            content_offset, block_text, state, state_machine):    """Include a reST file as part of the content of this reST file."""    if not state.document.settings.file_insertion_enabled:        warning = state_machine.reporter.warning(              '"%s" directive disabled.' % name,              nodes.literal_block(block_text, block_text), line=lineno)        return [warning]    source = state_machine.input_lines.source(        lineno - state_machine.input_offset - 1)    source_dir = os.path.dirname(os.path.abspath(source))    path = directives.path(arguments[0])    if path.startswith('<') and  path.endswith('>'):        path = os.path.join(standard_include_path, path[1:-1])    path = os.path.normpath(os.path.join(source_dir, path))    path = utils.relative_path(None, path)    encoding = options.get('encoding', state.document.settings.input_encoding)    try:        state.document.settings.record_dependencies.add(path)        include_file = io.FileInput(            source_path=path, encoding=encoding,            error_handler=state.document.settings.input_encoding_error_handler,            handle_io_errors=None)    except IOError, error:        severe = state_machine.reporter.severe(              'Problems with "%s" directive path:\n%s: %s.'              % (name, error.__class__.__name__, error),              nodes.literal_block(block_text, block_text), line=lineno)        return [severe]    try:        include_text = include_file.read()    except UnicodeError, error:        severe = state_machine.reporter.severe(              'Problem with "%s" directive:\n%s: %s'              % (name, error.__class__.__name__, error),              nodes.literal_block(block_text, block_text), line=lineno)        return [severe]    if options.has_key('literal'):        literal_block = nodes.literal_block(include_text, include_text,                                            source=path)        literal_block.line = 1        return literal_block    else:        include_lines = statemachine.string2lines(include_text,                                                  convert_whitespace=1)        state_machine.insert_input(include_lines, path)        return []include.arguments = (1, 0, 1)include.options = {'literal': directives.flag,                   'encoding': directives.encoding}def raw(name, arguments, options, content, lineno,        content_offset, block_text, state, state_machine):    """    Pass through content unchanged    Content is included in output based on type argument    Content may be included inline (content section of directive) or    imported from a file or url.    """    if ( not state.document.settings.raw_enabled         or (not state.document.settings.file_insertion_enabled             and (options.has_key('file') or options.has_key('url'))) ):        warning = state_machine.reporter.warning(              '"%s" directive disabled.' % name,              nodes.literal_block(block_text, block_text), line=lineno)        return [warning]    attributes = {'format': ' '.join(arguments[0].lower().split())}    encoding = options.get('encoding', state.document.settings.input_encoding)    if content:        if options.has_key('file') or options.has_key('url'):            error = state_machine.reporter.error(                  '"%s" directive may not both specify an external file and '                  'have content.' % name,                  nodes.literal_block(block_text, block_text), line=lineno)            return [error]        text = '\n'.join(content)    elif options.has_key('file'):        if options.has_key('url'):            error = state_machine.reporter.error(                  'The "file" and "url" options may not be simultaneously '                  'specified for the "%s" directive.' % name,                  nodes.literal_block(block_text, block_text), line=lineno)            return [error]        source_dir = os.path.dirname(            os.path.abspath(state.document.current_source))        path = os.path.normpath(os.path.join(source_dir, options['file']))        path = utils.relative_path(None, path)        try:            state.document.settings.record_dependencies.add(path)            raw_file = io.FileInput(                source_path=path, encoding=encoding,                error_handler=state.document.settings.input_encoding_error_handler,                handle_io_errors=None)        except IOError, error:            severe = state_machine.reporter.severe(                  'Problems with "%s" directive path:\n%s.' % (name, error),                  nodes.literal_block(block_text, block_text), line=lineno)            return [severe]        try:            text = raw_file.read()        except UnicodeError, error:            severe = state_machine.reporter.severe(                  'Problem with "%s" directive:\n%s: %s'                  % (name, error.__class__.__name__, error),                  nodes.literal_block(block_text, block_text), line=lineno)            return [severe]        attributes['source'] = path    elif options.has_key('url'):        if not urllib2:            severe = state_machine.reporter.severe(                  'Problems with the "%s" directive and its "url" option: '                  'unable to access the required functionality (from the '                  '"urllib2" module).' % name,                  nodes.literal_block(block_text, block_text), line=lineno)            return [severe]        source = options['url']        try:            raw_text = urllib2.urlopen(source).read()        except (urllib2.URLError, IOError, OSError), error:            severe = state_machine.reporter.severe(                  'Problems with "%s" directive URL "%s":\n%s.'                  % (name, options['url'], error),                  nodes.literal_block(block_text, block_text), line=lineno)            return [severe]        raw_file = io.StringInput(            source=raw_text, source_path=source, encoding=encoding,            error_handler=state.document.settings.input_encoding_error_handler)        try:            text = raw_file.read()        except UnicodeError, error:            severe = state_machine.reporter.severe(                  'Problem with "%s" directive:\n%s: %s'                  % (name, error.__class__.__name__, error),                  nodes.literal_block(block_text, block_text), line=lineno)            return [severe]        attributes['source'] = source    else:        error = state_machine.reporter.warning(            'The "%s" directive requires content; none supplied.' % (name),            nodes.literal_block(block_text, block_text), line=lineno)        return [error]    raw_node = nodes.raw('', text, **attributes)    return [raw_node]raw.arguments = (1, 0, 1)raw.options = {'file': directives.path,               'url': directives.uri,               'encoding': directives.encoding}raw.content = 1def replace(name, arguments, options, content, lineno,            content_offset, block_text, state, state_machine):    if not isinstance(state, states.SubstitutionDef):        error = state_machine.reporter.error(            'Invalid context: the "%s" directive can only be used within a '            'substitution definition.' % (name),            nodes.literal_block(block_text, block_text), line=lineno)        return [error]    text = '\n'.join(content)    element = nodes.Element(text)    if text:        state.nested_parse(content, content_offset, element)        if len(element) != 1 or not isinstance(element[0], nodes.paragraph):            messages = []            for node in element:                if isinstance(node, nodes.system_message):                    node['backrefs'] = []                    messages.append(node)            error = state_machine.reporter.error(                'Error in "%s" directive: may contain a single paragraph '                'only.' % (name), line=lineno)            messages.append(error)            return messages        else:            return element[0].children

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -