luna.processing
extract_subclass
def extract_subclass(text: str, parent_cls: Type[T]) -> List[Type[T]]
Given a string containing Python code and a class type, this function returns any found class that inherits from the given class type.
For example, given parent_cls Template
, this function returns all
the classes which are subclasses of Template
.
unpack_repo
def unpack_repo(compressed_repo: bytes) -> str
Unpack a compressed repo and returns the path to the directory
unpack_repo_to_memory
def unpack_repo_to_memory(compressed_repo: bytes) -> dict
Unpack a compressed repo into an in-memory file system represented by a dictionary
SavedNode Objects
@dataclass
class SavedNode()
Node taken from parsing a module.
The node
is the AST node itself.
The index
is its position in the original AST.
ObjectVisitor Objects
class ObjectVisitor(ast.NodeVisitor)
This visitor parses any given object and gets all its relevant dependencies.
The dependencies must be given in the objects
dict.
FileVisitor Objects
class FileVisitor(ast.NodeVisitor)
Parses an AST and builds a dict with all the found nodes in it. The dict also contains information about the ordering of the nodes.
cls_to_source
def cls_to_source(cls: Type) -> str
Given a type, this function returns the source code of its definition in such a way that it is directly runnable. That is, not only the type's source is extracted but that also of all its dependencies, whether local or imported.
This function works in 3 steps. 1/ Process the entire file where the type is located. This step outputs a dict containing all the nodes contained within the file.
2/ Scan the type for dependencies. Using the dict of objects outputted in step 1, this step parses the type itself looking for dependencies. The output of this step is the dict of step 1 filtered down to only the nodes on which the type is dependent, thus removing all the unnecessary code around.
3/ Merge the ASTs. Finally, we take all the dependency nodes and merge them with the type's AST. This produces a fully runnable piece of code.