|
| 1 | +import os |
| 2 | + |
| 3 | +from specifipy.parsers.diagram_generator import DiagramGenerator |
| 4 | + |
| 5 | + |
| 6 | +class DirectoryScanner: |
| 7 | + scan_path: str = None |
| 8 | + full_dir_paths: list[str] = [] |
| 9 | + full_file_paths: list[str] = [] |
| 10 | + |
| 11 | + def __matches_file_classification(self, full_file_path) -> bool: |
| 12 | + file_name = full_file_path.split("/")[-1] |
| 13 | + return ( |
| 14 | + os.path.isfile(full_file_path) |
| 15 | + and file_name[0] != "." |
| 16 | + and file_name[-3:] == ".py" |
| 17 | + ) |
| 18 | + |
| 19 | + def __matches_directory_classification(self, full_dir_path) -> bool: |
| 20 | + dir_name: str = full_dir_path.split("/")[-1] |
| 21 | + return ( |
| 22 | + os.path.isdir(full_dir_path) |
| 23 | + and dir_name[0] != "." |
| 24 | + and not "venv" in dir_name |
| 25 | + and not "virtualenv" in dir_name |
| 26 | + ) |
| 27 | + |
| 28 | + def __init__(self, base_path: str): |
| 29 | + self.scan_path = os.path.abspath(base_path) |
| 30 | + for obj in os.listdir(self.scan_path): |
| 31 | + os.path.join(self.scan_path, obj) |
| 32 | + file_system_element: str |
| 33 | + |
| 34 | + # Perform initial directories scanning |
| 35 | + self.full_dir_paths = [ |
| 36 | + os.path.join(self.scan_path, file_system_element) |
| 37 | + for file_system_element in os.listdir(self.scan_path) |
| 38 | + if self.__matches_directory_classification( |
| 39 | + os.path.join(self.scan_path, file_system_element) |
| 40 | + ) |
| 41 | + ] |
| 42 | + |
| 43 | + # Perform initial files scanning |
| 44 | + self.full_file_paths = [ |
| 45 | + os.path.join(self.scan_path, file_system_element) |
| 46 | + for file_system_element in os.listdir(self.scan_path) |
| 47 | + if self.__matches_file_classification( |
| 48 | + os.path.join(self.scan_path, file_system_element) |
| 49 | + ) |
| 50 | + ] |
| 51 | + |
| 52 | + self.do_recursive_directory_scanning() |
| 53 | + |
| 54 | + def show_vars(self): |
| 55 | + print(self.full_dir_paths, self.full_file_paths) |
| 56 | + |
| 57 | + def make_diagrams(self): |
| 58 | + diagram_generator = DiagramGenerator() |
| 59 | + for f in self.full_file_paths: |
| 60 | + name = f.split("/")[-1] |
| 61 | + with open(f) as python_file: |
| 62 | + diagram_generator.generate_diagram(python_file.read(), name) |
| 63 | + |
| 64 | + def do_recursive_directory_scanning(self): |
| 65 | + new_found_directory_paths: list[str] = [] |
| 66 | + if self.full_dir_paths: |
| 67 | + directory_path: str |
| 68 | + for directory_path in self.full_dir_paths: |
| 69 | + for file_system_element in os.listdir(directory_path): |
| 70 | + full_file_path = os.path.join(directory_path, file_system_element) |
| 71 | + if os.path.isdir(full_file_path): |
| 72 | + new_found_directory_paths.append( |
| 73 | + os.path.join(directory_path, file_system_element) |
| 74 | + ) |
| 75 | + if os.path.isfile( |
| 76 | + full_file_path |
| 77 | + ) and self.__matches_file_classification(full_file_path): |
| 78 | + self.full_file_paths.append( |
| 79 | + os.path.join(directory_path, file_system_element) |
| 80 | + ) |
| 81 | + self.full_dir_paths = new_found_directory_paths |
| 82 | + self.do_recursive_directory_scanning() |
0 commit comments