我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用docutils.parsers.rst.directives.path()。
def run(self): env = self.state.document.settings.env relpath, abspath = env.relfn2path(directives.path(self.arguments[0])) # Add OpenAPI spec as a dependency to the current document. That means # the document will be rebuilt if the spec is changed. env.note_dependency(relpath) # Read the spec using encoding passed to the directive or fallback to # the one specified in Sphinx's config. encoding = self.options.get('encoding', env.config.source_encoding) with io.open(abspath, 'rt', encoding=encoding) as stream: spec = yaml.load(stream, _YamlOrderedLoader) # URI parameter is crucial for resolving relative references. So # we need to set this option properly as it's used later down the # stack. self.options.setdefault('uri', 'file://%s' % abspath) # reStructuredText DOM manipulation is pretty tricky task. It requires # passing dozen arguments which is not easy without well-documented # internals. So the idea here is to represent OpenAPI spec as # reStructuredText in-memory text and parse it in order to produce a # real DOM. viewlist = ViewList() for line in openapi2httpdomain(spec, **self.options): viewlist.append(line, '<openapi>') # Parse reStructuredText contained in `viewlist` and return produced # DOM nodes. node = nodes.section() node.document = self.state.document nested_parse_with_titles(self.state, viewlist, node) return node.children
def run(self): rst_file = self.state_machine.document.attributes['source'] rst_dir = os.path.abspath(os.path.dirname(rst_file)) script_fn = directives.path(self.arguments[0]) script_bn = os.path.basename(script_fn) script_name = os.path.basename(self.arguments[0]).split(".")[0] # This magic is from matplotlib dest_dir = os.path.abspath(os.path.join(setup.app.builder.outdir, os.path.dirname(script_fn))) if not os.path.exists(dest_dir): os.makedirs(dest_dir) # no problem here for me, but just use built-ins rel_dir = os.path.relpath(rst_dir, setup.confdir) place = os.path.join(dest_dir, rel_dir) if not os.path.isdir(place): os.makedirs(place) shutil.copyfile(os.path.join(rst_dir, script_fn), os.path.join(place, script_bn)) im_path = os.path.join(rst_dir, "_static") images = sorted(glob.glob(os.path.join(im_path, "*.png"))) lines = [] for im in images: im_name = os.path.join("_static", os.path.basename(im)) lines.append(".. image:: %s" % im_name) lines.append(" :width: 400") lines.append(" :target: ../../_images/%s" % os.path.basename(im)) lines.append("\n") lines.append("\n") self.state_machine.insert_input(lines, rst_file) return []
def _httpresource(endpoint, method, properties): parameters = properties.get('parameters', []) responses = properties['responses'] indent = ' ' yield '.. http:{0}:: {1}'.format(method, endpoint) yield ' :synopsis: {0}'.format(properties.get('summary', 'null')) yield '' if 'summary' in properties: for line in properties['summary'].splitlines(): yield '{indent}**{line}**'.format(**locals()) yield '' if 'description' in properties: for line in properties['description'].splitlines(): yield '{indent}{line}'.format(**locals()) yield '' # print request's route params for param in filter(lambda p: p['in'] == 'path', parameters): yield indent + ':param {type} {name}:'.format(**param) for line in param.get('description', '').splitlines(): yield '{indent}{indent}{line}'.format(**locals()) # print request's query params for param in filter(lambda p: p['in'] == 'query', parameters): yield indent + ':query {type} {name}:'.format(**param) for line in param.get('description', '').splitlines(): yield '{indent}{indent}{line}'.format(**locals()) # print response status codes for status, response in responses.items(): yield '{indent}:status {status}:'.format(**locals()) for line in response['description'].splitlines(): yield '{indent}{indent}{line}'.format(**locals()) # print request header params for param in filter(lambda p: p['in'] == 'header', parameters): yield indent + ':reqheader {name}:'.format(**param) for line in param.get('description', '').splitlines(): yield '{indent}{indent}{line}'.format(**locals()) # print response headers for status, response in responses.items(): for headername, header in response.get('headers', {}).items(): yield indent + ':resheader {name}:'.format(name=headername) for line in header['description'].splitlines(): yield '{indent}{indent}{line}'.format(**locals()) yield ''
def run(self): rst_file = self.state_machine.document.attributes['source'] rst_dir = os.path.abspath(os.path.dirname(rst_file)) script_fn = directives.path(self.arguments[0]) script_bn = os.path.basename(script_fn) script_name = os.path.basename(self.arguments[0]).split(".")[0] # This magic is from matplotlib dest_dir = os.path.abspath(os.path.join(setup.app.builder.outdir, os.path.dirname(script_fn))) if not os.path.exists(dest_dir): os.makedirs(dest_dir) # no problem here for me, but just use built-ins rel_dir = os.path.relpath(rst_dir, setup.confdir) place = os.path.join(dest_dir, rel_dir) if not os.path.isdir(place): os.makedirs(place) shutil.copyfile(os.path.join(rst_dir, script_fn), os.path.join(place, script_bn)) im_path = os.path.join(rst_dir, "_static") images = sorted(glob.glob(os.path.join(im_path, "%s__*.png" % script_name))) lines = [] lines.append("(`%s <%s>`__)" % (script_bn, script_fn)) lines.append("\n") lines.append("\n") lines.append(".. literalinclude:: %s" % self.arguments[0]) lines.append("\n") lines.append("\n") for im in images: im_name = os.path.join("_static", os.path.basename(im)) lines.append(".. image:: %s" % im_name) lines.append(" :width: 400") lines.append(" :target: ../_images/%s" % os.path.basename(im)) lines.append("\n") lines.append("\n") for ext in data_patterns: data_files = sorted(glob.glob(os.path.join( im_path, "%s__*.%s" % (script_name, ext)))) for df in data_files: df_bn = os.path.basename(df) shutil.copyfile(os.path.join(rst_dir, df), os.path.join(dest_dir, rel_dir, df_bn)) lines.append(" * Data: `%s <%s>`__)" % (df_bn, df)) lines.append("\n") self.state_machine.insert_input(lines, rst_file) return []