Parcourir la source

Maj fonction de recherche des mentions

olivier.massot il y a 7 ans
Parent
commit
5207994317
1 fichiers modifiés avec 40 ajouts et 39 suppressions
  1. 40 39
      core.py

+ 40 - 39
core.py

@@ -27,6 +27,13 @@ def recurse(acc_obj):
 class InvalidFileExt(IOError):
     pass
 
+class Mention():
+    def __init__(self, line, objname, quote):
+        self.line = line
+        self.objname = objname
+        self.quote = quote
+        self.obj = None
+
 class AccessObject():
     type_ = "<unknown>"
     _valid_file_exts = (".bas")
@@ -35,9 +42,11 @@ class AccessObject():
         self.name_ = name_
         self.functions = []
         self.sourcefile = ""
-        self.deps = {}
         self._sourcecode = ""
 
+        self.mentions = []
+        self.deps = {}
+
     def __repr__(self):
         return "<{}: {}>".format(self.type_, self.name_)
 
@@ -49,6 +58,7 @@ class AccessObject():
         obj = cls(AccessObject.path_to_name(file))
         obj.sourcefile = file
         obj._sourcecode = file.text()
+        obj._sourcecode.replace("\r\n", "\n")
         return obj
 
     @property
@@ -95,12 +105,6 @@ class RelationObject(AccessObject):
     type_ = "Relation"
     _valid_file_exts = (".txt")
 
-class Mention():
-    def __init__(self, line, objname, quote):
-        self.line = line
-        self.objname = objname
-        self.quote = quote
-
 class Analyse():
     objects = []
     duplicated_names = []
@@ -142,30 +146,27 @@ class Analyse():
 
     @classmethod
     def find_deps(cls, subject):
-        for candidate in cls.objects:
-            if candidate is subject:
-                continue
-
-            mentions = []
-
-            if type(candidate) is ModuleObject:  # L'objet peut contenir des references aux fonctions de l'objet module
-                for fname in candidate.functions:
-                    rx = re.compile(r"((?:.*\n)*)(.*(?:^|\W)({})(?:$|\W).*)".format(fname), MULTILINE)
-                    for match in rx.finditer(subject.sourcecode):
-                        line = len(match.group(1).split("\n")) + 2 if match.group(1) is not None else 1
-                        quote = match.group(2)
-                        objname = match.group(3)
-                        mentions.append(Mention(line, objname, quote))
-
-            rx = re.compile("""((?:.*\n)*)(.*(?:^|\s|\[|\]|&|\(|\)|\.|!|"|')({})(?:^|\s|\[|\]|&|\(|\)|\.|!|"|').*)""".format(candidate.name_), MULTILINE)
-            for match in rx.finditer(subject.sourcecode):
-                line = len(match.group(1).split("\n")) if match.group(1) is not None else 1
-                quote = match.group(2)
-                objname = match.group(3)
-                mentions.append(Mention(line, objname, quote))
-
-            if mentions:
-                subject.deps[candidate] = mentions
+
+        # On cherche le nom de chaque autre objet, ainsi que le nom des fonctions issues des modules
+        look_for = [obj.name_ for obj in cls.objects if obj is not subject] + sum([obj.functions for obj in cls.objects if obj is not subject])
+
+        names = "|".join(list(set(look_for)))
+
+        rx = re.compile("""(.*(?:^|\t| |\[|\]|&|\(|\)|\.|!|"|')({})(?:$|\t| |\[|\]|&|\(|\)|\.|!|"|').*)""".format(names), MULTILINE)
+        mentions = []
+
+        # Indexe la position des lignes
+        line_ends = [m.end() for m in re.finditer('.*\n', subject.sourcecode)]
+
+        for match in rx.finditer(subject.sourcecode):
+            line = next(i for i in range(len(line_ends)) if line_ends[i] > match.start(1)) + 1
+            quote = match.group(1).strip()
+            objname = match.group(2)
+            mentions.append(Mention(line, objname, quote))
+        subject.mentions = mentions
+
+#             if mentions:
+#                 subject.deps[candidate] = mentions
 
     @classmethod
     def analyse_all(cls):
@@ -191,8 +192,6 @@ class Analyse():
 
         return cls.objects
 
-
-
 if __name__ == "__main__":
 
     here = Path(__file__).parent.abspath()
@@ -210,12 +209,14 @@ if __name__ == "__main__":
     with open(resultfile, "w+") as f:
         for obj in Analyse.objects:
             msg = "# L'objet [{}] '{}' mentionne dans son code-source:".format(obj.type_, obj.name_)
-            for dep, mentions in obj.deps.items():
-                msg += "\n\t* [{}] '{}'".format(dep.type_, dep.name_)
-                for mention in mentions:
-                    msg += "\n\t\tLine: {} >> {}".format(mention.line, mention.quote)
-            if not obj.deps:
-                msg += "\n\t (pas de dépendances)"
+            for mention in obj.mentions:
+                msg += "\n\t\t'{}'\t\tLine: {}\t>>\t{}".format(mention.objname, mention.line, mention.quote)
+#             for dep, mentions in obj.deps.items():
+#                 msg += "\n\t* [{}] '{}'".format(dep.type_, dep.name_)
+#                 for mention in mentions:
+#                     msg += "\n\t\tLine: {} >> {}".format(mention.line, mention.quote)
+#             if not obj.deps:
+#                 msg += "\n\t (pas de dépendances)"
 
             msg += "\n"
             f.write(msg)