Skip to content

Commit 45e8ca5

Browse files
author
rootware
committed
working redaction logics
1 parent 74a787c commit 45e8ca5

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

pyredactkit/runner.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import sys
1717

1818
# Creating instances of redact and unredact classes
19-
redact_obj = CoreRedactorEngine()
20-
customrd_obj = CustomRedactorEngine()
19+
core_redact = CoreRedactorEngine()
20+
custom_redact = CustomRedactorEngine()
2121
unredact_obj = Unredactor()
2222

2323

@@ -61,12 +61,6 @@ def arg_helper() -> argparse.Namespace:
6161
print(help_menu)
6262
parser.print_help(sys.stderr)
6363
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-
# )
7064
parser.add_argument(
7165
"-u",
7266
"--unredact",
@@ -109,51 +103,52 @@ def arg_helper() -> argparse.Namespace:
109103
return args
110104

111105

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)
115108

116-
if not os.path.isfile(args.text[0]) or not os.path.isdir(args.text[0]):
117-
redact_obj.process_text(args.text)
118109

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]
121112
files = set()
122113
for path in full_paths:
123114
if os.path.isfile(path):
124115
file_name, file_ext = os.path.splitext(path)
125-
if args.extension in ('', file_ext):
116+
if extension in ('', file_ext):
126117
files.add(path)
127-
elif args.recursive:
118+
elif recursive:
128119
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)
129131

130132
for file in files:
131133
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)
133135
elif args.customfile:
134-
customrd_obj.process_custom_file(file, args.customfile)
136+
custom_redact.process_custom_file(file, args.customfile)
135137
elif args.dirout:
136-
redact_obj.process_core_file(file, args.dirout)
138+
core_redact.process_core_file(file, args.dirout)
137139
elif args.unredact:
138140
unredact_obj.unredact(file, args.unredact)
139141
else:
140-
redact_obj.process_core_file(file)
141-
142+
core_redact.process_core_file(file)
142143

143144

144145
def main():
145146
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()
153148

154149

155150
def api_identify_sensitive_data(text: str) -> list:
156-
return redact_obj.identify_data(text)
151+
return core_redact.identify_data(text)
157152

158153

159154
if __name__ == "__main__":

0 commit comments

Comments
 (0)