Use debian 2.7 only
[fpbd-bostik.git] / pyfpdb / Anonymise.py
blobce67bb3fec7cec36d54e47a3003b3fdadc9b2106
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 if options.config:
34 config = Configuration.Config(options.config)
35 else:
36 config = Configuration.Config()
38 filter = options.hhc
40 filter_name = filter.replace("ToFpdb", "")
42 mod = __import__(filter)
43 obj = getattr(mod, filter_name, None)
45 hhc = obj(config, autostart=False)
47 for kodec in ("utf8", "cp1252", "utf-16"):
48 if os.path.exists(options.filename):
49 in_fh = codecs.open(options.filename, 'r', kodec)
50 filecontents = in_fh.read()
51 in_fh.close()
52 break
53 else:
54 print(_("Could not find file %s") % options.filename)
55 exit(1)
57 m = hhc.re_PlayerInfo.finditer(filecontents)
59 outfile = options.filename+".anon"
60 print(_("Output being written to %s") % outfile)
62 savestdout = sys.stdout
63 fsock = open(outfile,"w")
64 sys.stdout = fsock
66 players = []
67 for a in m:
68 players = players + [a.group('PNAME')]
70 uniq = set(players)
72 for i, name in enumerate(uniq):
73 filecontents = filecontents.replace(name, 'Player%d' %i)
75 print(filecontents)
77 sys.stdout = savestdout
78 fsock.close()