-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredact.py
More file actions
38 lines (28 loc) · 1.15 KB
/
redact.py
File metadata and controls
38 lines (28 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import re, yaml, sys
class Redactor:
def __init__(self, filepath='./rules.yml', replacement_string='[redacted]'):
self.filepath = filepath
self.rules = self.read_rules()
self.replacement_string = replacement_string
def read_rules(self):
rules = {}
with open(self.filepath) as rule_file:
raw_rules = yaml.load(rule_file)
for rule in raw_rules['Rules']:
rules.update(rule)
return rules
def combine_rules(self):
return '|'.join(self.rules.values())
def apply_rule(self, string: str, rule_name: str):
redacted = re.sub(self.rules[rule_name], self.replacement_string, string)
return redacted
def apply_rules(self, string: str): #applied in the order declared in dict, important for phone_number/zipcode interference
redacted = re.sub(self.combine_rules(), self.replacement_string, string)
return redacted
if __name__ == '__main__':
redactor = Redactor()
if(len(sys.argv)>2):
redacted = redactor.apply_rule(sys.argv[1], sys.argv[2])
else:
redacted = redactor.apply_rules(sys.argv[1])
print(redacted)