Generate toctree directive for rst file.

        :param conf_path: conf.py file absolute path
        :param docname: the rst file relpath from conf.py directory.
        :param source: rst content.
        :param maxdepth: int, max toc tree depth.
773010
def fly(cls,
            conf_path,
            docname,
            source,
            maxdepth=1):  # pragma: no cover
        
        msg = ("``.. articles::`` directive is going to be deprecated. "
               "use ``.. autodoctree`` instead.")
        warnings.warn(msg, FutureWarning)

        directive_pattern = ".. articles::"
        if directive_pattern not in source:
            return source

        af = ArticleFolder(
            dir_path=Path(Path(conf_path).parent, docname).parent.abspath)
        toc_directive = af.toc_directive(maxdepth)

        lines = list()
        for line in source.split("\n"):
            if directive_pattern in line.strip():
                if line.strip().startswith(directive_pattern):
                    line = line.replace(directive_pattern, toc_directive, 1)
                    lines.append(line)
                    continue
            lines.append(line)
        return "\n".join(lines)
1058172
def gen_rst(results):
    

    # make sure the destination directory exists
    try:
        os.mkdir(DOCPATH)
    except OSError as e:
        if e.args[0] != errno.EEXIST and e.args[0] != errno.EISDIR:
            raise

    toctree = []
    class_ = []
    for output in results:
        section = output.class_.split(',')
        for i in range(len(section)):
            section[i] = section[i].rstrip()
            if section[i] in CLASSMAP:
                section[i] = CLASSMAP[section[i]]
            if i >= len(class_) or section[i] != class_[i]:
                if i == 0:
                    filename = section[i].replace(' ', '_').lower()
                    rst = open(DOCPATH + filename + '.rst', 'w')
                    rst.write(HEADER)
                    rst.write(section[i] + '\n')
                    rst.write(RSTCHARS[0] * len(section[i]))
                    rst.write(time.strftime("\nGenerated %a %d %b %Y %X UTC\n\n", time.gmtime()))
                    toctree.append(filename)
                else:
                    rst.write(section[i] + '\n')
                    rst.write(RSTCHARS[min(i, len(RSTCHARS)-1)] * len(section[i]))
                    rst.write('\n\n')
        class_ = section
        rst.write('.. _cpydiff_%s:\n\n' % output.name.rsplit('.', 1)[0])
        rst.write(output.desc + '\n')
        rst.write('~' * len(output.desc) + '\n\n')
        if output.cause != 'Unknown':
            rst.write('**Cause:** ' + output.cause + '\n\n')
        if output.workaround != 'Unknown':
            rst.write('**Workaround:** ' + output.workaround + '\n\n')

        rst.write('Sample code::\n\n' + indent(output.code, TAB) + '\n')
        output_cpy = indent(''.join(output.output_cpy[0:2]), TAB).rstrip()
        output_cpy = ('::\n\n' if output_cpy != '' else '') + output_cpy
        output_upy = indent(''.join(output.output_upy[0:2]), TAB).rstrip()
        output_upy = ('::\n\n' if output_upy != '' else '') + output_upy
        table = gen_table([['CPy output:', output_cpy], ['uPy output:', output_upy]])
        rst.write(table)

    template = open(INDEXTEMPLATE, 'r')
    index = open(DOCPATH + INDEX, 'w')
    index.write(HEADER)
    index.write(template.read())
    for section in INDEXPRIORITY:
        if section in toctree:
            index.write(indent(section + '.rst', TAB))
            toctree.remove(section)
    for section in toctree:
        index.write(indent(section + '.rst', TAB))
760399
def source_roots(self, document_path):
        """"""
        files = _utils.find_parents(self._root_path, document_path, ['setup.py']) or []
        return [os.path.dirname(setup_py) for setup_py in files]
974843
def generate_docs(app):
    
    # Figure out the correct directories to use
    config = app.config
    config_dir = app.env.srcdir
    javascript_root = os.path.join(config_dir, config.jsdoc_source_root)
    if javascript_root[-1] != os.path.sep:
        javascript_root += os.path.sep
    if not javascript_root:
        return
    output_root = os.path.join(config_dir, config.jsdoc_output_root)
    execution_dir = os.path.join(config_dir, '..')
    exclude = config.jsdoc_exclude

    # Remove any files generated by earlier builds
    cleanup(output_root)

    # Generate the actual reST files
    jsdoc_toolkit_dir = os.path.join(SOURCE_PATH, 'jsdoc-toolkit')
    jsdoc_rst_dir = os.path.join(SOURCE_PATH, 'jsdoc-toolkit-rst-template')
    build_xml_path = os.path.join(jsdoc_rst_dir, 'build.xml')
    command = ['ant', '-f', build_xml_path,
               '-Djsdoc-toolkit.dir=%s' % jsdoc_toolkit_dir,
               '-Djs.src.dir=%s' % javascript_root,
               '-Djs.rst.dir=%s' % output_root]
    if exclude:
        exclude_args = ['--exclude=\\"%s\\"' % path for path in exclude]
        command.append('-Djs.exclude="%s"' % ' '.join(exclude_args))
    try:
        process = Popen(command, cwd=execution_dir)
        process.wait()
    except OSError:
        raise JSDocError('Error running ant; is it installed?')

    # Convert the absolute paths in the file listing to relative ones
    path = os.path.join(output_root, 'files.rst')
    with open(path, 'r') as f:
        content = f.read()
    content = content.replace(javascript_root, '')
    with open(path, 'w') as f:
        f.write(content)
623475
def fullPath(self):
        
        for ((dirTree, dirID, dirSeq), (dirPath, name)) in self.links.items():
            try:
                path = self.fileSystem.volumes[dirTree].fullPath
                if path is not None:
                    return path + ("/" if path[-1] != "/" else "") + dirPath + name
            except Exception:
                logging.debug("Haven't imported %d yet", dirTree)

        if self.id == BTRFS_FS_TREE_OBJECTID:
            return "/"
        else:
            return None
547639
def create_node(ctx, path):
    
    # Ensure given context object is BuildContext object
    _ensure_build_context(ctx)

    # Get top directory's relative path relative to `wscript` directory
    top_dir_relpath = os.path.relpath(
        # Top directory's absolute path
        ctx.top_dir,
        # `wscript` directory's absolute path
        ctx.run_dir,
    )

    # Convert given relative path to be relative to `wscript` directory
    node_path = os.path.join(top_dir_relpath, path)

    # Create node using the relative path relative to `wscript` directory
    node = ctx.path.make_node(node_path)

    # Return the created node
    return node
249937
def DoxySourceScan(node, env, path):
   
   default_file_patterns = [
      '*.c', '*.cc', '*.cxx', '*.cpp', '*.c++', '*.java', '*.ii', '*.ixx',
      '*.ipp', '*.i++', '*.inl', '*.h', '*.hh ', '*.hxx', '*.hpp', '*.h++',
      '*.idl', '*.odl', '*.cs', '*.php', '*.php3', '*.inc', '*.m', '*.mm',
      '*.py',
   ]

   default_exclude_patterns = [
      '*~',
   ]

   sources = []

   data = DoxyfileParse(node.get_contents())

   if data.get("RECURSIVE", "NO") == "YES":
      recursive = True
   else:
      recursive = False

   file_patterns = data.get("FILE_PATTERNS", default_file_patterns)
   exclude_patterns = data.get("EXCLUDE_PATTERNS", default_exclude_patterns)

   # We're running in the top-level directory, but the doxygen
   # configuration file is in the same directory as node; this means
   # that relative pathnames in node must be adjusted before they can
   # go onto the sources list
   conf_dir = os.path.dirname(str(node))
   
   for node in data.get("INPUT", []):
      if not os.path.isabs(node):
         node = os.path.join(conf_dir, node)
      if os.path.isfile(node):
         sources.append(node)
      elif os.path.isdir(node):
         if recursive:
            for root, dirs, files in os.walk(node):
               for f in files:
                  filename = os.path.join(root, f)

                  pattern_check = reduce(lambda x, y: x or bool(fnmatch(filename, y)), file_patterns, False)
                  exclude_check = reduce(lambda x, y: x and fnmatch(filename, y), exclude_patterns, True)

                  if pattern_check and not exclude_check:
                     sources.append(filename)
         else:
            for pattern in file_patterns:
               sources.extend(glob.glob("/".join([node, pattern])))

   # Add tagfiles to the list of source files:
   for node in data.get("TAGFILES", []):
      file = node.split("=")[0]
      if not os.path.isabs(file):
         file = os.path.join(conf_dir, file)
      sources.append(file)
   
   # Add additional files to the list of source files:
   def append_additional_source(option):
      file = data.get(option, "")
      if file != "":
         if not os.path.isabs(file):
            file = os.path.join(conf_dir, file)
         if os.path.isfile(file):
            sources.append(file)

   append_additional_source("HTML_STYLESHEET")
   append_additional_source("HTML_HEADER")
   append_additional_source("HTML_FOOTER")

   sources = map( lambda path: env.File(path), sources )
   return sources
631860
def generate_tree(root, paths, mock_os):
    
    tree = []
    dentries = mock_os.listdir(root)

    for d in dentries:
        full_path = mock_os.path.join(root, d)
        node = {
            'text': d,
            'href': '#%s' % d,
            'state': {
                'expanded': True
            }
        }

        if mock_os.path.isdir(full_path):
            node['nodes'] = generate_tree(full_path, paths, mock_os)
        else:
            node['icon'] = 'fa fa-file-code-o'
            node['href'] = '#'
            node['color'] = '#0088ce'
            node['dataAttr'] = {
                'toggle': 'modal',
                'target': '#file_modal',
                'load': paths[full_path] + '/'
            }
        tree.append(node)
    return tree
43714
def generate_contents(startpath, outfilename=DEFAULT_BUILDFILE):
    
    def _ignored_name(name):
        return (
            name.startswith('.') or
            name == PACKAGE_DIR_NAME or
            name.endswith('~') or
            name == outfilename
        )

    def _generate_contents(dir_path):
        safename_duplicates = defaultdict(list)
        for name in os.listdir(dir_path):
            if _ignored_name(name):
                continue

            path = os.path.join(dir_path, name)

            if os.path.isdir(path):
                nodename = name
                ext = None
            elif os.path.isfile(path):
                nodename, ext = splitext_no_dot(name)
            else:
                continue

            safename = to_identifier(nodename)
            safename_duplicates[safename].append((name, nodename, ext))

        safename_to_name = {}
        for safename, duplicates in iteritems(safename_duplicates):
            for name, nodename, ext in duplicates:
                if len(duplicates) > 1 and ext:
                    new_safename = to_identifier(name)  # Name with ext
                else:
                    new_safename = safename
                existing_name = safename_to_name.get(new_safename)
                if existing_name is not None:
                    message = "Duplicate node names in directory %r."
                    message += " %r was renamed to %r, which overlaps with %r"
                    raise BuildException(
                        message % (dir_path, name, new_safename, existing_name)
                    )
                safename_to_name[new_safename] = name

        contents = {}
        for safename, name in iteritems(safename_to_name):
            path = os.path.join(dir_path, name)

            if os.path.isdir(path):
                data = _generate_contents(path)
            else:
                rel_path = os.path.relpath(path, startpath)
                data = dict(file=rel_path)

            contents[safename] = data

        return contents

    return dict(
        contents=_generate_contents(startpath)
    )
158267
def document_path(cls, project, knowledge_base, document):
        """"""
        return google.api_core.path_template.expand(
            'projects/{project}/knowledgeBases/{knowledge_base}/documents/{document}',
            project=project,
            knowledge_base=knowledge_base,
            document=document,
        )
1242294
def _discover_toc(zf, opf_xmldoc, opf_filepath):
    '''
    
    '''
    toc = None

    # ePub 3.x
    tag = find_tag(opf_xmldoc, 'item', 'properties', 'nav')
    if tag and 'href' in tag.attributes.keys():
        filepath = unquote(tag.attributes['href'].value)
        # The xhtml file path is relative to the OPF file
        base_dir = os.path.dirname(opf_filepath)
        # print('- Reading Nav file: {}/{}'.format(base_dir, filepath))
        npath = os.path.normpath(os.path.join(base_dir, filepath))
        nav_content = zf.read(npath)
        toc_xmldoc = minidom.parseString(nav_content)

        _toc = []

        for n in toc_xmldoc.getElementsByTagName('a'):
            if n.firstChild and ('href' in n.attributes.keys()):
                href = unquote(n.attributes['href'].value)
                # Discarding CFI links
                if '.html' in href or '.xhtml' in href:
                    title = n.firstChild.nodeValue
                    # try the second node too (maybe the first child is an empty span)
                    if not title and n.firstChild.firstChild:
                        title = n.firstChild.firstChild.nodeValue

                    title = title.strip() if title else None

                    if title:
                        level = -1
                        parentNode = n.parentNode
                        avoid_infinite_loop = 0 # simple security issue to avoid infinite loop for bad epub files
                        while parentNode and parentNode.nodeName != 'nav' and avoid_infinite_loop < 50:
                            if parentNode.nodeName == 'ol': # count the depth of the a link related to ol items
                                level += 1
                            parentNode = parentNode.parentNode
                            avoid_infinite_loop += 1
                        level = max(level, 0) # root level is 0, not -1

                        _toc.append({'title': title, 'src': href, 'level': level})
        if _toc:
            toc = _toc

    if not toc:
        # ePub 2.x
        tag = find_tag(opf_xmldoc, 'item', 'id', 'ncx')
        if not tag:
            tag = find_tag(opf_xmldoc, 'item', 'id', 'ncxtoc')
        if tag and 'href' in tag.attributes.keys():
            filepath = unquote(tag.attributes['href'].value)
            # The ncx file path is relative to the OPF file
            base_dir = os.path.dirname(opf_filepath)
            # print('- Reading NCX file: {}/{}'.format(base_dir, filepath))
            npath = os.path.normpath(os.path.join(base_dir, filepath))
            ncx_content = zf.read(npath)

            toc_xmldoc = minidom.parseString(ncx_content)

            def read_nav_point(nav_point_node, level = 0):
                items = []
                item = {'title': None, 'src': None, 'level': level}
                children_points = []
                for item_node in nav_point_node.childNodes:
                    if item_node.nodeName in ('navLabel', 'ncx:navLabel'):
                        try:
                            text = item_node.getElementsByTagName('text')[0].firstChild
                        except IndexError:
                            try:
                                text = item_node.getElementsByTagName('ncx:text')[0].firstChild
                            except IndexError:
                                text = None

                        item['title'] = text.nodeValue.strip() if text and text.nodeValue else None
                    elif item_node.nodeName in ('content', 'ncx:content'):
                        if item_node.hasAttribute('src'):
                            item['src'] = item_node.attributes['src'].value
                    elif item_node.nodeName in ('navPoint', 'ncx:navPoint'):
                        children_points.append(item_node)

                if item['title']:
                    items.append(item)
                    for child_node in children_points:
                        subitems = read_nav_point(child_node, level=level + 1)
                        items.extend(subitems)
                return items

            def read_nav_map(toc_xmldoc, level=0):
                items = []
                try:
                    nav_map_node = toc_xmldoc.getElementsByTagName('navMap')[0]
                except IndexError:
                    # Some ebooks use the ncx: namespace so try that too
                    try:
                        nav_map_node = toc_xmldoc.getElementsByTagName('ncx:navMap')[0]
                    except IndexError:
                        print('Failed reading TOC')
                        return items

                for nav_point in nav_map_node.childNodes:
                    if nav_point.nodeName in ('navPoint', 'ncx:navPoint'):
                        subitems = read_nav_point(nav_point, level=level)
                        items.extend(subitems)
                return items

            toc = read_nav_map(toc_xmldoc)

    # add indexes
    if toc:
        for i, t in enumerate(toc):
            t['index'] = i

    return toc
567309
def _build_menu(docs):
    

    paths = []
    current = docs.filename
    while current != docs.root:
        current = current.parent
        paths.append(current)

    for path in reversed(paths):
        docs.add_menu(path.stem.title(), link=path / 'index.html')

    if docs.filename.stem != 'index':
        docs.add_menu(docs.title, link=docs.filename)

    docs.end_menu()
347891
def get_rendered_toctree(builder, docname, prune=False, collapse=True):
    
    fulltoc = build_full_toctree(builder,
                                 docname,
                                 prune=prune,
                                 collapse=collapse,
                                 )
    rendered_toc = builder.render_partial(fulltoc)['fragment']
    return rendered_toc
112069
def recurse_tree(rootpath, excludes, opts):
    # type: (unicode, List[unicode], Any) -> List[unicode]
    
    if INITPY in os.listdir(rootpath):
        path_prefix = path.sep.join(rootpath.split(path.sep)[:-1])
    else:
        path_prefix = rootpath

    toplevels = []
    followlinks = getattr(opts, 'followlinks', False)
    includeprivate = getattr(opts, 'includeprivate', False)
    implicit_namespaces = getattr(opts, 'implicit_namespaces', False)
    for root, subs, files in walk(rootpath, followlinks=followlinks):
        # document only Python module files (that aren't excluded)
        py_files = sorted(f for f in files
                          if path.splitext(f)[1] in PY_SUFFIXES and
                          not is_excluded(path.join(root, f), excludes))
        is_pkg = INITPY in py_files
        if is_pkg:
            py_files.remove(INITPY)
            py_files.insert(0, INITPY)
        elif root != rootpath:
            # only accept non-package at toplevel unless using implicit
            # namespaces
            if not implicit_namespaces:
                del subs[:]
                continue
        # remove hidden ('.') and private ('_') directories, as well as
        # excluded dirs
        if includeprivate:
            exclude_prefixes = ('.',)  # type: Tuple[unicode, ...]
        else:
            exclude_prefixes = ('.', '_')
        subs[:] = sorted(sub for sub in subs
                         if not sub.startswith(exclude_prefixes) and
                         not is_excluded(path.join(root, sub), excludes))

        pkg = root[len(path_prefix):].lstrip(path.sep).replace(path.sep, '.')
        for py_file in py_files:
            if not shall_skip(path.join(root, py_file), opts):
                if py_file == INITPY:
                    module = ''
                else:
                    module = path.splitext(py_file)[0]
                toplevels.append(makename(pkg, module))

    return toplevels
140645
def generate_docs(self, clspath, more_content):
        """"""
        obj = import_class(clspath)
        sourcename = 'docstring of %s' % clspath
        all_options = []
        indent = '    '
        config = obj.get_required_config()

        if config.options:
            # Go through options and figure out relevant information
            for option in config:
                if 'namespace' in self.options:
                    namespaced_key = self.options['namespace'] + '_' + option.key
                else:
                    namespaced_key = option.key

                if 'case' in self.options:
                    if self.options['case'] == 'upper':
                        namespaced_key = namespaced_key.upper()
                    elif self.options['case'] == 'lower':
                        namespaced_key = namespaced_key.lower()

                all_options.append({
                    'key': namespaced_key,
                    'parser': qualname(option.parser),
                    'doc': option.doc,
                    'default': option.default,
                })

        if 'hide-classname' not in self.options:
            modname, clsname = split_clspath(clspath)
            component_name = clspath
            component_index = clsname
        else:
            component_name = 'Configuration'
            component_index = 'Configuration'

        if all_options:
            # Add index entries for options first so they link to the right
            # place; we do it this way so that we don't have to make options a
            # real object type and then we don't get to use TypedField
            # formatting
            self.add_line('.. index::', sourcename)
            for option in all_options:
                self.add_line('   single: %s; (%s)' % (option['key'], component_index), sourcename)
            self.add_line('', '')

        # Add the classname or 'Configuration'
        self.add_line('.. everett:component:: %s' % component_name, sourcename)
        self.add_line('', sourcename)

        # Add the docstring if there is one and if show-docstring
        if 'show-docstring' in self.options:
            docstring_attr = self.options['show-docstring'] or '__doc__'
            docstring = getattr(obj, docstring_attr, None)
            if docstring:
                docstringlines = prepare_docstring(docstring, ignore=1)
                for i, line in enumerate(docstringlines):
                    self.add_line(indent + line, sourcename, i)
                self.add_line('', '')

        # Add content from the directive if there was any
        if more_content:
            for line, src in zip(more_content.data, more_content.items):
                self.add_line(indent + line, src[0], src[1])
            self.add_line('', '')

        if all_options:
            # Now list the options
            sourcename = 'class definition'

            for option in all_options:
                self.add_line(
                    '%s:option %s %s:' % (indent, option['parser'], option['key']),
                    sourcename
                )
                self.add_line('%s    %s' % (indent, option['doc']), sourcename)
                if option['default'] is not NO_VALUE:
                    self.add_line('', '')
                    self.add_line(
                        '%s    Defaults to ``%r``.' % (indent, option['default']),
                        sourcename
                    )
                self.add_line('', '')
46647
def tree(path, depth=2, topdown=True, followlinks=False, showhidden=False):
    """"""
    rt = []
    for root, dirs, files in os.walk(path, topdown=topdown, followlinks=followlinks):
        if not showhidden and File.is_hidden(root):
            continue

        current_depth = len(os.path.relpath(root, path).split(os.sep))
        if current_depth > depth:
            continue

        if showhidden:
            _tuple = (
                root,
                [File(os.path.join(root, _dir)) for _dir in dirs],
                [File(os.path.join(root, _file)) for _file in files]
            )
        else:
            _tuple = (
                root,
                [File(os.path.join(root, _dir)) for _dir in dirs if _dir[0] != '.'],
                [File(os.path.join(root, _file)) for _file in files if _file[0] != '.']
            )

        rt.append(_tuple)
    return rt
510029
def _get_src_path_line_nodes_clover(self, xml_document, src_path):
        

        files = [file_tree
                 for file_tree in xml_document.findall(".//file")
                 if GitPathTool.relative_path(file_tree.get('path')) == src_path
                 or []]
        if not files:
            return None
        lines = [file_tree.findall('./line[@type="stmt"]')
                 for file_tree in files]
        return [elem for elem in itertools.chain(*lines)]