-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcensor.py
More file actions
executable file
·55 lines (50 loc) · 2.02 KB
/
Copy pathcensor.py
File metadata and controls
executable file
·55 lines (50 loc) · 2.02 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
import sys
import time
import re
import hashlib
import ConfigParser
import os
import argparse
cfg_name = os.path.expanduser("~/.censor.cfg")
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--profile",
action="store",
dest="profile",
default="default",
help="Profile to use")
args, unknown_args = parser.parse_known_args()
hash_table = {}
translations = open("translations.txt", "w")
config = ConfigParser.RawConfigParser()
try:
config.readfp(open(cfg_name))
except IOError:
print "Config file", cfg_name, "not found."
print "A default is created but you'll need to add some stuff to it"
with open(cfg_name, "w") as f:
f.write("[default]\n(peos.*) =\n#(soumada\w*) =\n")
f.write("# Remember to surround your regexs with parenthesis\n\n")
f.write("#or create your own_profile with\n#[own_profile]\n# and use with \"-p own_profile\"\n")
sys.exit(1)
print "Censoring with profile", args.profile, "\n", config.items(args.profile)
for arg in unknown_args:
print "Checking file", arg
with open(arg, "r") as f:
with open(arg + ".txt", "w") as out:
for line in f.xreadlines():
for regex in config.items(args.profile):
match = re.search(regex[0], line, re.IGNORECASE)
if match:
try:
hash_table[match.group(1)]
except IndexError:
print "index error [", line, "]", regex[0]
except KeyError:
# first time we saw one of those, store
# it for later.
hash_table[match.group(1)] = hashlib.sha224(match.group(1)).hexdigest()
translations.write(match.group(1) + " " + hash_table[match.group(1)] + "\n")
line = re.sub(match.group(1), hash_table[match.group(1)], line)
out.write(line)
translations.close()