MCPcopy Create free account
hub / github.com/scottrogowski/code2flow / Python

Class Python

code2flow/python.py:143–275  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

141
142
143class Python(BaseLanguage):
144 @staticmethod
145 def assert_dependencies():
146 pass
147
148 @staticmethod
149 def get_tree(filename, _):
150 """
151 Get the entire AST for this file
152
153 :param filename str:
154 :rtype: ast
155 """
156 try:
157 with open(filename) as f:
158 raw = f.read()
159 except ValueError:
160 with open(filename, encoding='UTF-8') as f:
161 raw = f.read()
162 return ast.parse(raw)
163
164 @staticmethod
165 def separate_namespaces(tree):
166 """
167 Given an AST, recursively separate that AST into lists of ASTs for the
168 subgroups, nodes, and body. This is an intermediate step to allow for
169 cleaner processing downstream
170
171 :param tree ast:
172 :returns: tuple of group, node, and body trees. These are processed
173 downstream into real Groups and Nodes.
174 :rtype: (list[ast], list[ast], list[ast])
175 """
176 groups = []
177 nodes = []
178 body = []
179 for el in tree.body:
180 if type(el) in (ast.FunctionDef, ast.AsyncFunctionDef):
181 nodes.append(el)
182 elif type(el) == ast.ClassDef:
183 groups.append(el)
184 elif getattr(el, 'body', None):
185 tup = Python.separate_namespaces(el)
186 groups += tup[0]
187 nodes += tup[1]
188 body += tup[2]
189 else:
190 body.append(el)
191 return groups, nodes, body
192
193 @staticmethod
194 def make_nodes(tree, parent):
195 """
196 Given an ast of all the lines in a function, create the node along with the
197 calls and variables internal to it.
198
199 :param tree ast:
200 :param parent Group:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected