Add 'Currencies' filter to the ring player stats viewer.
[fpdb-dooglus.git] / pyfpdb / ScriptAddStatToRegression.py
blobd0eeabbe98188a5dc4e6d31e74ec84cc1308bb01
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 #Copyright 2008-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 """A script for adding new stats to the regression test library"""
20 import Options
22 import logging, os, sys, string
23 import re, urllib2, datetime, pytz
24 import codecs
25 import pprint
26 pp = pprint.PrettyPrinter(indent=4)
28 def write_file(filename, data):
29 print data
30 f = open(filename, 'w')
31 f.write(data)
32 f.close()
33 print f
35 def update(leaf, file_type, stat, default):
36 filename = leaf
37 #print "DEBUG: fileanme: %s" % filename
39 # Test if this is a hand history file
40 if filename.endswith(file_type):
41 in_fh = codecs.open(filename, 'r', 'utf8')
42 whole_file = in_fh.read()
43 in_fh.close()
45 hash = eval(whole_file)
46 if file_type==".hp":
47 for player in hash:
48 print "player:", player, "<end>"
49 hash[player][stat] = default
50 elif file_type==".hands":
51 hash[stat] = default
53 out_string = pp.pformat(hash)
54 out_string = string.replace(out_string, "<UTC>", "pytz.utc")
55 write_file(filename, out_string)
57 def walk_test_files(dir, file_type, stat, default):
58 """Walks a directory, and executes a callback on each file
59 dir: directory of to search
60 file_type: .hands or .hp
61 stat: stat to add
62 default: value to insert"""
63 dir = os.path.abspath(dir)
64 for file in [file for file in os.listdir(dir) if not file in [".",".."]]:
65 nfile = os.path.join(dir,file)
66 if os.path.isdir(nfile):
67 walk_test_files(nfile, file_type, stat, default)
68 else:
69 update(nfile, file_type, stat, default)
71 def usage():
72 print "USAGE:"
73 print "Edit this script to activate walk_test_files in main(). Parameters explained in comment of walk_test_files."
74 print "\t./ScriptAddStatToRegression.py"
75 sys.exit(0)
77 def main(argv=None):
78 if argv is None:
79 argv = sys.argv[1:]
81 (options, argv) = Options.fpdb_options()
83 if options.usage == True: # or (len(argv) < 1):
84 usage()
86 print "WARNING:"
87 print "This script will modify many files in the regression test suite"
88 print "As a safety precaution, you need to edit the file manually to run it"
89 #walk_test_files('regression-test-files/', '.hp', 'NEW_STAT', "DEFAULT_VALUE")
91 if __name__ == '__main__':
92 main()