Last active
March 26, 2018 08:57
-
-
Save ityoung/b26c396c61b3ae5ef5374ba8923927ff to your computer and use it in GitHub Desktop.
analysis a python file of module calls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ModuleFinder(object): | |
def __init__(self): | |
self.find_next_line = False | |
self.father_module = None | |
self.modules = [] | |
def find_modules_next_line(self, code, module=None): | |
"""code: line to find modules""" | |
if '\\' not in code[-1]: | |
self.find_next_line = False | |
code = code.strip("\\").replace(" ", "") | |
if ',' in code: | |
modules = code.replace(" ", "").split(",") | |
if module: | |
modules = map(lambda x:module + "." + x,modules) | |
self.modules += modules | |
else: | |
if module: | |
code = module + "." + code | |
self.modules.append(code) | |
def find_modules(self, code): | |
#TODO: relative path | |
#TODO: judge module in Path | |
code = code.splitlines() | |
for item in code: | |
if self.find_next_line is True: | |
self.find_modules_next_line(item, self.father_module) | |
item_import_head = item[:7] | |
item_import_tail = item[7:] | |
item_from_head = item[:5] | |
item_from_tail = item[5:] | |
if item_import_head == "import ": | |
self.father_module = None | |
if '\\' in item[-1]: | |
self.find_next_line = True | |
if ',' in item_import_tail: | |
self.modules += item_import_tail.strip("\\").replace(" ", "").split(",") | |
else: | |
self.modules.append(item_import_tail.strip("\\").replace(" ", "")) | |
if item_from_head == "from ": | |
if '\\' in item[-1]: | |
self.find_next_line = True | |
module = item_from_tail.split('import') | |
self.father_module = module[0].strip(" ") | |
item_from_tail = module[1] | |
if ',' in item_from_tail: | |
modules = item_from_tail.strip("\\").replace(" ", "").split(",") | |
self.modules += map(lambda item:self.father_module + "." + item, modules) | |
else: | |
self.modules.append(self.father_module + "." + item_from_tail.strip("\\").replace(" ", "")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aa | |
from bb.bbb import cc, ccc\ | |
cccc | |
import dd.dd, ee \ | |
eee, eeee | |
import ff \ | |
gg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment