示例#1
0
class InheritanceTree(XmlElement):
  
  def __init__(self, path):
    super(InheritanceTree, self).__init__("root")
    print "Retriving XML tree..."
    self.regex_parser = RegexParser()
    nodes = self.get_nodes(path)
    self.extend(nodes)
    self.extend(self.get_edges(nodes))

  def get_edges(self, nodes):
    edges = []
    for target in nodes:
      for source in nodes:
        if self.are_connected(target, source):
          edges.append(Edge(source, target))
          print "Connecting: %s ---> %s"  % (source.text, target.text)
    return edges

  def are_connected(self, target, source):
    regex = re.compile(r'[\s\{\}\,\;]'+ source.text + r'[\s\{\}\,\;]+', re.MULTILINE|re.DOTALL)
    return self.regex_parser.is_in_text(target.tail, regex)

  def search_for_classes(self, file_abs_path, regexes = {"template":re.compile(".*"), "class":re.compile(".*"), "comment":re.compile(".*")}):
    clean_text = self.regex_parser.get_text_without_from_file(file_abs_path, regexes["comment"])
    clean_text = self.regex_parser.get_text_without(clean_text, regexes["template"])
    return self.regex_parser.search_in_text(clean_text, regexes["class"])

  def get_nodes(self, path):
    stored_classes = set()
    regexes = {
      "class": re.compile(r'class\s+(?P<name>\w+)[\s\:]+(?P<tail>.*?[\n\{\}\;])', re.MULTILINE),
      "template": re.compile(r'template\s*<\s*[^\>]*\s*\>', re.MULTILINE), 
      "comment":re.compile(r'(\/\/.*?\n)|(\/\*.*?\*\/)', re.MULTILINE|re.DOTALL),
      "file_name": re.compile(r'\w+\.(cxx|hxx|cpp|h|c)')
    }
    result = []
    counter = 0
    for root, dirs, files in os.walk(path):
      for _file in files:
        if not self.regex_parser.is_in_text(_file, regexes["file_name"]):
          continue
        print "Searching in %s..." % _file
        file_abs_path = "/".join([root, _file])
        for record in self.search_for_classes(file_abs_path, regexes):
          if record["name"] in stored_classes:
            continue
          args = {"name":record["name"], "id":counter, "tail":record["tail"]}
          result.append(Node(args))
          stored_classes.add(record["name"])
          counter+=1
          print "  Found %s class" % record["name"]
        print "Done"
    return result