| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,8 +6,6 @@ | |||
| 6 | 6 | import ast | |
| 7 | 7 | import os | |
| 8 | 8 | ||
| 9 | - CLASS_FUNCTION_SEPERATOR = ':' | ||
| 10 | - | ||
| 11 | 9 | def is_python_module(path): | |
| 12 | 10 | if os.path.splitext(path)[1] == '.py': | |
| 13 | 11 | return True | |
@@ -51,145 +49,9 @@ def get_project_module_names(path): | |||
| 51 | 49 | project_module_names.append(project_module[0]) | |
| 52 | 50 | return project_module_names | |
| 53 | 51 | ||
| 54 | - class Import(): | ||
| 55 | - def __init__(self, name, module, path): | ||
| 56 | - self.name = name | ||
| 57 | - self.module = module | ||
| 58 | - self.path = path | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - class Definition(): | ||
| 62 | - def __init__(self, name, path): | ||
| 63 | - self.name = name | ||
| 64 | - self.path = path | ||
| 65 | - | ||
| 66 | - def __str__(self): | ||
| 67 | - return ', '.join(('Definition: ', self.name, self.path)) | ||
| 68 | - | ||
| 69 | - class Module(): | ||
| 70 | - def __init__(self, path, imports, classes, functions): | ||
| 71 | - self.path = path | ||
| 72 | - self.imports = imports | ||
| 73 | - self.classes = classes | ||
| 74 | - self.functions = functions | ||
| 75 | - | ||
| 76 | - def __str__(self): | ||
| 77 | - return '\n'.join(('Module:', self.path, str([str(s) for s in self.imports]), str([str(s) for s in self.classes]), str([str(s) for s in self.functions]))) | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - class DefinitionVisitor(ast.NodeVisitor): | ||
| 81 | - def __init__(self, path): | ||
| 82 | - self.local_module_names = get_project_module_names(path) | ||
| 83 | - self.imports = list() | ||
| 84 | - self.functions = list() | ||
| 85 | - self.classes = list() | ||
| 86 | - self.latest_class = None | ||
| 87 | - | ||
| 88 | - def visit_FunctionDef(self, node): | ||
| 89 | - function_name = '' | ||
| 90 | - if self.latest_class: | ||
| 91 | - function_name = self.latest_class | ||
| 92 | - function_name += CLASS_FUNCTION_SEPERATOR | ||
| 93 | - function_name += node.name | ||
| 94 | - self.functions.append(function_name) | ||
| 95 | - | ||
| 96 | - def visit_ClassDef(self, node): | ||
| 97 | - self.latest_class = node.name | ||
| 98 | - self.classes.append(node.name) | ||
| 99 | - for stmt in node.body: | ||
| 100 | - self.visit(stmt) | ||
| 101 | - self.latest_class = None | ||
| 102 | - | ||
| 103 | - def visit_Import(self, node): | ||
| 104 | - for name in node.names: | ||
| 105 | - if name.name in self.local_module_names: | ||
| 106 | - self.imports.append(node) | ||
| 107 | - | ||
| 108 | - def visit_ImportFrom(self, node): | ||
| 109 | - if node.module.split('.')[-1] in self.local_module_names: | ||
| 110 | - self.imports.append(node) | ||
| 111 | - | ||
| 112 | - def generate_ast(path): | ||
| 113 | - """Generate an Abstract Syntax Tree using the ast module.""" | ||
| 114 | - with open(path, 'r') as f: | ||
| 115 | - return ast.parse(f.read()) | ||
| 116 | - | ||
| 117 | - | ||
| 118 | 52 | def is_directory(path): | |
| 119 | 53 | if os.path.isdir(path): | |
| 120 | 54 | return True | |
| 121 | 55 | elif is_python_module(path): | |
| 122 | 56 | return False | |
| 123 | 57 | raise Exception(path, ' has to be a python module or a directory.') | |
| 124 | - | ||
| 125 | - | ||
| 126 | - def module_name_handler(node, name): | ||
| 127 | - if isinstance(node, ast.Import): | ||
| 128 | - return '.'.join(name.split('.')[0:-1]) | ||
| 129 | - elif isinstance(node, ast.ImportFrom): | ||
| 130 | - return node.module | ||
| 131 | - else: | ||
| 132 | - raise Exception('Wrong type of node, ', node) | ||
| 133 | - | ||
| 134 | - | ||
| 135 | - def get_project_definitions_and_imports(path): | ||
| 136 | - project_modules = get_python_modules(path) | ||
| 137 | - modules = list() | ||
| 138 | - for module in project_modules: | ||
| 139 | - modules.append(get_definitions_and_imports(module[1])) | ||
| 140 | - return modules | ||
| 141 | - | ||
| 142 | - def get_definitions_and_imports(module_path): | ||
| 143 | - imports = list() | ||
| 144 | - functions = list() | ||
| 145 | - classes = list() | ||
| 146 | - definition_visitor = DefinitionVisitor(module_path) | ||
| 147 | - tree = generate_ast(module_path) | ||
| 148 | - definition_visitor.visit(tree) | ||
| 149 | - | ||
| 150 | - for i in definition_visitor.imports: | ||
| 151 | - for alias in i.names: | ||
| 152 | - _import = Import(alias.name, module_name_handler(i, alias.name), module_path) | ||
| 153 | - imports.append(_import) | ||
| 154 | - | ||
| 155 | - for function_name in definition_visitor.functions: | ||
| 156 | - functions.append(Definition(function_name, module_path)) | ||
| 157 | - | ||
| 158 | - for class_name in definition_visitor.classes: | ||
| 159 | - classes.append(Definition(class_name, module_path)) | ||
| 160 | - | ||
| 161 | - return Module(module_path, imports, classes, functions) | ||
| 162 | - | ||
| 163 | - def file_to_cfg(path): | ||
| 164 | - tree = generate_ast(path) | ||
| 165 | - cfg = CFG() | ||
| 166 | - cfg.create(tree) | ||
| 167 | - return cfg | ||
| 168 | - | ||
| 169 | - def file_to_cfgs(path, imports): | ||
| 170 | - tree = generate_ast(path) | ||
| 171 | - cfg = CFG(imports) | ||
| 172 | - cfg.create(tree) | ||
| 173 | - return cfg | ||
| 174 | - | ||
| 175 | - def get_cfgs(path): | ||
| 176 | - cfg_list = list() | ||
| 177 | - if is_directory(path): | ||
| 178 | - modules = get_project_definitions_and_imports(path) | ||
| 179 | - for module in modules: | ||
| 180 | - print(module) | ||
| 181 | - #cfg_list.append(file_to_cfgs(project_modules[index][1], imports)) | ||
| 182 | - else: | ||
| 183 | - cfg_list.append(file_to_cfg(path)) | ||
| 184 | - | ||
| 185 | - return cfg_list | ||
| 186 | - | ||
| 187 | - if __name__ == '__main__': | ||
| 188 | - import argparse | ||
| 189 | - | ||
| 190 | - parser = argparse.ArgumentParser() | ||
| 191 | - | ||
| 192 | - parser.add_argument('filename', help = 'Filename of the file that should be analysed.', type = str) | ||
| 193 | - | ||
| 194 | - args = parser.parse_args() | ||
| 195 | - print(get_cfgs(args.filename)) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments