|
16 | 16 | import sys |
17 | 17 |
|
18 | 18 | # Creating instances of redact and unredact classes |
19 | | -redact_obj = CoreRedactorEngine() |
20 | | -customrd_obj = CustomRedactorEngine() |
| 19 | +core_redact = CoreRedactorEngine() |
| 20 | +custom_redact = CustomRedactorEngine() |
21 | 21 | unredact_obj = Unredactor() |
22 | 22 |
|
23 | 23 |
|
@@ -61,12 +61,6 @@ def arg_helper() -> argparse.Namespace: |
61 | 61 | print(help_menu) |
62 | 62 | parser.print_help(sys.stderr) |
63 | 63 | sys.exit(1) |
64 | | - # parser.add_argument( |
65 | | - # "-f", |
66 | | - # "--file", |
67 | | - # nargs="+", |
68 | | - # help="""Path of a file or a directory of files.""" |
69 | | - # ) |
70 | 64 | parser.add_argument( |
71 | 65 | "-u", |
72 | 66 | "--unredact", |
@@ -109,51 +103,52 @@ def arg_helper() -> argparse.Namespace: |
109 | 103 | return args |
110 | 104 |
|
111 | 105 |
|
112 | | -def execute_file_arg() -> None: |
113 | | - args = arg_helper() |
114 | | - print(args.text) |
| 106 | +def is_it_text(file_path: str) -> bool: |
| 107 | + return not os.path.isfile(file_path) or not os.path.isdir(file_path) |
115 | 108 |
|
116 | | - if not os.path.isfile(args.text[0]) or not os.path.isdir(args.text[0]): |
117 | | - redact_obj.process_text(args.text) |
118 | 109 |
|
119 | | - full_paths = [os.path.join(os.getcwd(), path) for path in args.text] |
120 | | - print(full_paths) |
| 110 | +def recursive_file_search(full_path: str, extension: str, recursive: bool) -> set: |
| 111 | + full_paths = [os.path.join(os.getcwd(), path) for path in full_path] |
121 | 112 | files = set() |
122 | 113 | for path in full_paths: |
123 | 114 | if os.path.isfile(path): |
124 | 115 | file_name, file_ext = os.path.splitext(path) |
125 | | - if args.extension in ('', file_ext): |
| 116 | + if extension in ('', file_ext): |
126 | 117 | files.add(path) |
127 | | - elif args.recursive: |
| 118 | + elif recursive: |
128 | 119 | full_paths += glob.glob(path + '/*') |
| 120 | + return files |
| 121 | + |
| 122 | + |
| 123 | +def execute_redact_logic() -> None: |
| 124 | + args = arg_helper() |
| 125 | + |
| 126 | + is_text = is_it_text(args.text[0]) |
| 127 | + if not is_text: |
| 128 | + core_redact.process_text(args.text) |
| 129 | + |
| 130 | + files = recursive_file_search(args.text, args.extension, args.recursive) |
129 | 131 |
|
130 | 132 | for file in files: |
131 | 133 | if args.customfile and args.dirout: |
132 | | - customrd_obj.process_custom_file(file, args.customfile, args.dirout) |
| 134 | + custom_redact.process_custom_file(file, args.customfile, args.dirout) |
133 | 135 | elif args.customfile: |
134 | | - customrd_obj.process_custom_file(file, args.customfile) |
| 136 | + custom_redact.process_custom_file(file, args.customfile) |
135 | 137 | elif args.dirout: |
136 | | - redact_obj.process_core_file(file, args.dirout) |
| 138 | + core_redact.process_core_file(file, args.dirout) |
137 | 139 | elif args.unredact: |
138 | 140 | unredact_obj.unredact(file, args.unredact) |
139 | 141 | else: |
140 | | - redact_obj.process_core_file(file) |
141 | | - |
| 142 | + core_redact.process_core_file(file) |
142 | 143 |
|
143 | 144 |
|
144 | 145 | def main(): |
145 | 146 | print(banner) |
146 | | - |
147 | | - args = arg_helper() |
148 | | - execute_file_arg() |
149 | | - # if args.file or (args.file and args.dirout): |
150 | | - # execute_file_arg() |
151 | | - # else: |
152 | | - # redact_obj.process_text(args.text) |
| 147 | + execute_redact_logic() |
153 | 148 |
|
154 | 149 |
|
155 | 150 | def api_identify_sensitive_data(text: str) -> list: |
156 | | - return redact_obj.identify_data(text) |
| 151 | + return core_redact.identify_data(text) |
157 | 152 |
|
158 | 153 |
|
159 | 154 | if __name__ == "__main__": |
|
0 commit comments