Replace tabs with spaces. Remove trailing whitespace. Etc.
[fpdb-dooglus.git] / pyfpdb / Anonymise.py
blob6b00e3c60230038fe78bc1552029a172d128ccc4
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 #Copyright 2009-2011 Carl Gherardi
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU Affero General Public License as published by
7 #the Free Software Foundation, version 3 of the License.
9 #This program is distributed in the hope that it will be useful,
10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 #GNU General Public License for more details.
14 #You should have received a copy of the GNU Affero General Public License
15 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #In the "official" distribution you can find the license in agpl-3.0.txt.
18 import L10n
19 _ = L10n.get_translation()
21 import os
22 import re
23 import codecs
24 import Options
25 import HandHistoryConverter
26 import Configuration
27 import sys
29 # command line is:
30 # ./Anonymise.py -f <valid path to HH file> -k <name of input filter>
32 (options, argv) = Options.fpdb_options()
33 config = Configuration.Config()
35 filter = options.hhc
37 filter_name = filter.replace("ToFpdb", "")
39 mod = __import__(filter)
40 obj = getattr(mod, filter_name, None)
42 hhc = obj(config, autostart=False)
44 if os.path.exists(options.filename):
45 in_fh = codecs.open(options.filename, 'r', "utf8")
46 filecontents = in_fh.read()
47 in_fh.close()
48 else:
49 print _("Could not find file %s") % options.filename
50 exit(1)
52 m = hhc.re_PlayerInfo.finditer(filecontents)
54 outfile = options.filename+".anon"
55 print (_("Output being written to %s") % outfile)
57 savestdout = sys.stdout
58 fsock = open(outfile,"w")
59 sys.stdout = fsock
61 players = []
62 for a in m:
63 players = players + [a.group('PNAME')]
65 uniq = set(players)
67 for i, name in enumerate(uniq):
68 filecontents = filecontents.replace(name, 'Player%d' %i)
70 print filecontents
72 sys.stdout = savestdout
73 fsock.close()