1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 util.py - Utility functions
19 """
20
21 import os, sys
22 from imp import find_module, load_module
23 from optparse import OptionParser
24
26 oldpath = sys.path
27 try:
28 sys.path = [os.getcwd(), ]
29 fp, pathname, description = find_module(name)
30 finally:
31 sys.path = oldpath
32
33 try:
34 return load_module(name, fp, pathname, description)
35 finally:
36 if fp:
37 fp.close()
38
39
41
42 - def __init__(self, event, *args, **kwargs):
43
44
45 self._event = event
46 OptionParser.__init__(self, *args, **kwargs)
47
48 self.add_option('-r', '--rankings', action='store', default=None,
49 help='Comma separted list of rankings to show.')
50 self.add_option('-l', '--list', action='store_true', default=False,
51 help='List all available rankings.')
52
54 (options, args) = OptionParser.parse_args(self)
55
56
57 if options.list:
58 print 'Available rankings:',
59 for (desc,r) in self._event.list_rankings():
60 print "%s," % desc,
61 print
62 sys.exit()
63
64 if options.rankings:
65 ranking_codes = options.rankings.split(',')
66 ranking_list = [ (desc,r) for desc, r in self._event.list_rankings() if desc in ranking_codes ]
67 else:
68 ranking_list = self._event.list_rankings()
69
70 return (options, args, ranking_list)
71
72
74
76
77 (options, args, ranking_list) = RankingOptionParser.parse_args(self)
78
79
80 if len(args) > 1:
81 print "Can't write to more than one file!"
82 sys.exit(1)
83 if len(args) == 0:
84 f = sys.stdout
85 else:
86 f = open(args[0], 'wb')
87
88 return (options, args, ranking_list, f)
89