Skip to content

Commit ef64e19

Browse files
author
rootware
committed
fixed directory and stable cli
1 parent 9ef48e7 commit ef64e19

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

pyredactkit/pyredactkit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,13 @@ def execute_file_arg() -> None:
119119
full_paths += glob.glob(path + '/*')
120120

121121
for file in files:
122-
if args.customfile:
122+
if args.customfile or (args.customfile and args.dirout):
123123
redact_obj.process_custom_file(file, args.customfile)
124124
redact_obj.process_report(file)
125+
elif args.customfile and args.dirout:
126+
redact_obj.process_custom_file(file, args.customfile, args.dirout)
125127
elif args.dirout:
126128
redact_obj.process_core_file(file, args.dirout)
127-
redact_obj.process_custom_file(file, args.custom, args.dirout)
128129
redact_obj.process_report(file, args.dirout)
129130
elif args.unredact:
130131
unredact_obj.unredact(file, args.unredact)

pyredactkit/redact.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ def process_text(self, text=str, savedir="./"):
209209
print(
210210
f"[+] Redacted and results saved to {os.path.basename(generated_file)}")
211211

212-
def process_custom_file(self, filename, customfile=str, savedir="./"):
212+
def process_custom_file(self, file_name, customfile=str, make_dir="./"):
213213
"""Function to process supplied file with custom regex file from cli.
214214
Args:
215-
filename (str): File to redact
215+
file_name (str): File to redact
216216
customfile (str): custom regex pattern for redaction
217-
savedir (str): [Optional] directory to place results
217+
make_dir (str): [Optional] directory to place results
218218
219219
Returns:
220220
Creates redacted file.
@@ -223,18 +223,18 @@ def process_custom_file(self, filename, customfile=str, savedir="./"):
223223
secret_map = {}
224224
try:
225225
# Open a file read pointer as target_file
226-
with open(filename, encoding="utf-8") as target_file:
227-
if savedir != "./" and savedir[-1] != "/":
228-
savedir = savedir + "/"
226+
with open(file_name, encoding="utf-8") as target_file:
227+
if make_dir != "./" and make_dir[-1] != "/":
228+
make_dir = make_dir + "/"
229229

230230
# created the directory if not present
231-
if not os.path.exists(os.path.dirname(savedir)):
231+
if not os.path.exists(os.path.dirname(make_dir)):
232232
print(
233233
"[+] "
234-
+ os.path.dirname(savedir)
234+
+ os.path.dirname(make_dir)
235235
+ f"{self.dir_create}"
236236
)
237-
os.makedirs(os.path.dirname(savedir))
237+
os.makedirs(os.path.dirname(make_dir))
238238

239239
print(
240240
"[+] Processing starts now. This may take some time "
@@ -244,7 +244,7 @@ def process_custom_file(self, filename, customfile=str, savedir="./"):
244244

245245
# Open a file write pointer as result
246246
with open(
247-
f"{savedir}redacted_{os.path.basename(filename)}",
247+
f"{make_dir}redacted_{os.path.basename(file_name)}",
248248
"w",
249249
encoding="utf-8",
250250
) as result:
@@ -263,15 +263,15 @@ def process_custom_file(self, filename, customfile=str, savedir="./"):
263263
kv_pairs = data[1]
264264
secret_map.update(kv_pairs)
265265
result.write(redacted_line)
266-
self.write_hashmap(secret_map, filename, savedir)
266+
self.write_hashmap(secret_map, file_name, make_dir)
267267
print(
268-
f"[+] .hashshadow_{os.path.basename(filename)}.json file generated. Keep this safe if you need to undo the redaction.")
268+
f"[+] .hashshadow_{os.path.basename(file_name)}.json file generated. Keep this safe if you need to undo the redaction.")
269269
print(f"[+] Redacted {redact_count} targets...")
270270
print(
271-
f"[+] Redacted results saved to {savedir}redacted_{os.path.basename(filename)}")
271+
f"[+] Redacted results saved to {make_dir}redacted_{os.path.basename(file_name)}")
272272

273273
except UnicodeDecodeError:
274-
os.remove(f"{savedir}redacted_{os.path.basename(filename)}")
274+
os.remove(f"{make_dir}redacted_{os.path.basename(file_name)}")
275275
print("[-] Removed incomplete redact file")
276276
sys.exit("[-] Unable to read file")
277277

0 commit comments

Comments
 (0)