File: patch_modularized_graph_methods.py

package info (click to toggle)
python-igraph 0.11.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,480 kB
  • sloc: ansic: 24,545; python: 21,699; sh: 107; makefile: 35; sed: 2
file content (89 lines) | stat: -rw-r--r-- 2,834 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import inspect

import igraph


# FIXME: there must be a better way to do this
auxiliary_imports = [
    ("typing", "*"),
    ("igraph.io.files", "_identify_format"),
    ("igraph.community", "_optimal_cluster_count_from_merges_and_modularity"),
]


def main():

    # Get instance and classmethods
    g = igraph.Graph()
    methods = inspect.getmembers(g, predicate=inspect.ismethod)

    # Get the source code for each method and replace the method name
    # in the signature
    methodsources = {}
    underscore_functions = set()
    for mname, method in methods:
        # Source code of the function that uses the method
        source = inspect.getsourcelines(method)[0]

        # Find function name for this modularized method
        fname = source[0][source[0].find("def ") + 4 : source[0].find("(")]

        # FIXME: this also swaps in methods that are already there. While
        # that should be fine, we could check

        # Make new source code, which is the same but with the name swapped
        newsource = [source[0].replace(fname, mname)] + source[1:]
        methodsources[mname] = newsource

        # Prepare to delete the import for underscore functions
        if fname.startswith("_"):
            underscore_functions.add(fname)

    newmodule = igraph.__file__ + ".new"
    with open(newmodule, "wt") as fout:
        # FIXME: whitelisting all cases is not great, try to improve
        for (origin, value) in auxiliary_imports:
            fout.write(f"from {origin} import {value}\n")

        with open(igraph.__file__, "rt") as f:
            # Swap in the method sources
            for line in f:
                mtype = None

                for mname in methodsources:
                    # Class methods (constructors)
                    if " " + mname + " = classmethod(_" in line:
                        mtype = "class"
                        break

                    # Instance methods (excluding factories e.g. 3d layouts)
                    if (" " + mname + " = _" in line) and ("(" not in line):
                        mtype = "instance"
                        break

                else:
                    fout.write(line)
                    continue

                # Method found, substitute and remove from dict
                fout.write("\n")
                if mtype == "class":
                    fout.write("    @classmethod\n")
                for mline in methodsources[mname]:
                    # Correct indentation
                    fout.write("    " + mline)

                del methodsources[mname]

    # Move the new file back
    with open(igraph.__file__, "wt") as fout:
        with open(newmodule, "rt") as f:
            fout.write(f.read())

    # Delete .new file
    os.remove(newmodule)


if __name__ == "__main__":
    main()