Skip to content

Commit 7d15fb9

Browse files
author
rootware
committed
working refactored hash_map
1 parent 069a1b1 commit 7d15fb9

File tree

2 files changed

+79
-65
lines changed

2 files changed

+79
-65
lines changed

pyredactkit/common_jobs.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,77 @@ def __init__(self) -> None:
2727
None
2828
"""
2929
return None
30+
31+
def write_hashmap(self, hash_map=dict, filename=str, savedir="./") -> dict:
32+
"""Function that writes a .hashshadow_file.txt.json to os directory.
33+
Args:
34+
hash_map (dictionary): dictionary object to be written to file.
35+
filename (str): name of supplied file
36+
37+
Returns:
38+
Writes .hashshadow_file.txt.json to os directory
39+
"""
40+
with open(f"{savedir}.hashshadow_{os.path.basename(filename)}.json", "w", encoding="utf-8") as file:
41+
json.dump(hash_map, file)
42+
43+
def process_report(self, filename, savedir="./"):
44+
"""Function to process calculate and generate report of man hour saved.
45+
Args:
46+
filename (str): File to count the words
47+
48+
Returns:
49+
Creates a report on estimated man hours/minutes saved.
50+
"""
51+
try:
52+
# Open a file read pointer as target_file
53+
with open(filename, encoding="utf-8") as target_file:
54+
if savedir != "./" and savedir[-1] != "/":
55+
savedir = savedir + "/"
56+
57+
# created the directory if not present
58+
if not os.path.exists(os.path.dirname(savedir)):
59+
print(
60+
"[+] "
61+
+ os.path.dirname(savedir)
62+
+ f"{self.dir_create}"
63+
)
64+
os.makedirs(os.path.dirname(savedir))
65+
66+
text_chunk = target_file.read()
67+
68+
# Words per minute
69+
WPM = 75
70+
71+
word_length = 5
72+
total_words = 0
73+
for current_text in text_chunk:
74+
total_words += len(current_text)/word_length
75+
76+
total_words = math.ceil(total_words)
77+
78+
# Divide total words by words per minute read to get minutes and hour estimate.
79+
reading_minutes = math.ceil(total_words/WPM)
80+
reading_hours = math.floor(reading_minutes/60)
81+
82+
word_report = f"[+] Estimated total words : {total_words}"
83+
minutes_saved = f"[+] Estimated total minutes saved : {reading_minutes}"
84+
man_hours_saved = f"[+] Estimated total man hours saved : {reading_hours}"
85+
86+
print(word_report)
87+
print(minutes_saved)
88+
print(man_hours_saved)
89+
# Open a file write pointer as result
90+
# with open(
91+
# f"{savedir}manhours_saved_{os.path.basename(filename)}",
92+
# "w",
93+
# encoding="utf-8",
94+
# ) as result:
95+
# result.write(word_report + "\n" +
96+
# minutes_saved + "\n" + man_hours_saved)
97+
# print(
98+
# f"[+] Estimated man hours saved report saved to {savedir}manhours_saved_{os.path.basename(filename)}")
99+
100+
except UnicodeDecodeError:
101+
os.remove(f"manhour_saved_report_{os.path.basename(filename)}")
102+
print("[-] Removed incomplete report")
103+
sys.exit("[-] Unable to read target file")

pyredactkit/core_redactor.py

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" Core redactor engine class implementation """
22

3+
from pyredactkit.common_jobs import CommonJobs
34
import mimetypes
45
import os
56
import sys
@@ -10,6 +11,7 @@
1011

1112
from pyredactkit.identifiers import Identifier
1213
id_object = Identifier()
14+
cj_object = CommonJobs()
1315
""" Coreredactor library """
1416

1517

@@ -158,7 +160,7 @@ def process_text(self, text=str, savedir="./"):
158160
kv_pairs = data[1]
159161
hash_map.update(kv_pairs)
160162
result.write(f"{redacted_line}\n")
161-
self.write_hashmap(hash_map, generated_file, savedir)
163+
cj_object.write_hashmap(hash_map, generated_file, savedir)
162164
print(
163165
f"[+] .hashshadow_{os.path.basename(generated_file)}.json file generated. Keep this safe if you need to undo the redaction.")
164166
print(
@@ -218,7 +220,7 @@ def process_custom_file(self, file_name, customfile=str, make_dir="./"):
218220
kv_pairs = data[1]
219221
secret_map.update(kv_pairs)
220222
result.write(redacted_line)
221-
self.write_hashmap(secret_map, file_name, make_dir)
223+
cj_object.write_hashmap(secret_map, file_name, make_dir)
222224
print(
223225
f"[+] .hashshadow_{os.path.basename(file_name)}.json file generated. Keep this safe if you need to undo the redaction.")
224226
print(f"[+] Redacted {redact_count} targets...")
@@ -282,7 +284,7 @@ def process_core_file(self, filename, savedir="./"):
282284
kv_pairs = data[1]
283285
hash_map.update(kv_pairs)
284286
result.write(redacted_line)
285-
self.write_hashmap(hash_map, filename, savedir)
287+
cj_object.write_hashmap(hash_map, filename, savedir)
286288
print(
287289
f"[+] .hashshadow_{os.path.basename(filename)}.json file generated. Keep this safe if you need to undo the redaction.")
288290
print(f"[+] Redacted {count} targets...")
@@ -293,65 +295,3 @@ def process_core_file(self, filename, savedir="./"):
293295
os.remove(f"{savedir}redacted_{os.path.basename(filename)}")
294296
print("[-] Removed incomplete redact file")
295297
sys.exit("[-] Unable to read file")
296-
297-
def process_report(self, filename, savedir="./"):
298-
"""Function to process calculate and generate report of man hour saved.
299-
Args:
300-
filename (str): File to count the words
301-
302-
Returns:
303-
Creates a report on estimated man hours/minutes saved.
304-
"""
305-
try:
306-
# Open a file read pointer as target_file
307-
with open(filename, encoding="utf-8") as target_file:
308-
if savedir != "./" and savedir[-1] != "/":
309-
savedir = savedir + "/"
310-
311-
# created the directory if not present
312-
if not os.path.exists(os.path.dirname(savedir)):
313-
print(
314-
"[+] "
315-
+ os.path.dirname(savedir)
316-
+ f"{self.dir_create}"
317-
)
318-
os.makedirs(os.path.dirname(savedir))
319-
320-
text_chunk = target_file.read()
321-
322-
# Words per minute
323-
WPM = 75
324-
325-
word_length = 5
326-
total_words = 0
327-
for current_text in text_chunk:
328-
total_words += len(current_text)/word_length
329-
330-
total_words = math.ceil(total_words)
331-
332-
# Divide total words by words per minute read to get minutes and hour estimate.
333-
reading_minutes = math.ceil(total_words/WPM)
334-
reading_hours = math.floor(reading_minutes/60)
335-
336-
word_report = f"[+] Estimated total words : {total_words}"
337-
minutes_saved = f"[+] Estimated total minutes saved : {reading_minutes}"
338-
man_hours_saved = f"[+] Estimated total man hours saved : {reading_hours}"
339-
340-
print(word_report)
341-
print(minutes_saved)
342-
print(man_hours_saved)
343-
# Open a file write pointer as result
344-
# with open(
345-
# f"{savedir}manhours_saved_{os.path.basename(filename)}",
346-
# "w",
347-
# encoding="utf-8",
348-
# ) as result:
349-
# result.write(word_report + "\n" +
350-
# minutes_saved + "\n" + man_hours_saved)
351-
# print(
352-
# f"[+] Estimated man hours saved report saved to {savedir}manhours_saved_{os.path.basename(filename)}")
353-
354-
except UnicodeDecodeError:
355-
os.remove(f"manhour_saved_report_{os.path.basename(filename)}")
356-
print("[-] Removed incomplete report")
357-
sys.exit("[-] Unable to read target file")

0 commit comments

Comments
 (0)