rule_extractor.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:skydoc 作者: bazelbuild 项目源码 文件源码
def _extract_docstrings(self, bzl_file):
    """Extracts the docstrings for all public rules in the .bzl file.

    This function parses the .bzl file and extracts the docstrings for all
    public rules in the file that were extracted in _process_skylark. It calls
    _add_rule_doc for to parse the attribute documentation in each docstring
    and associate them with the extracted rules and attributes.

    Args:
      bzl_file: The .bzl file to extract docstrings from.
    """
    try:
      tree = ast.parse(open(bzl_file).read(), bzl_file)
      key = None
      for node in ast.iter_child_nodes(tree):
        if isinstance(node, ast.Assign):
          name = node.targets[0].id
          if not name.startswith("_"):
            key = name
          continue
        elif isinstance(node, ast.Expr) and key:
          # Python itself does not treat strings defined immediately after a
          # global variable definition as a docstring. Only extract string and
          # parse as docstring if it is defined.
          if hasattr(node.value, 's'):
            self._add_rule_doc(key, node.value.s.strip())
        key = None
    except IOError:
      print("Failed to parse {0}: {1}".format(bzl_file, e.strerror))
      pass
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号