Package bosco :: Module util
[hide private]
[frames] | no frames]

Source Code for Module bosco.util

 1  # 
 2  #    Copyright (C) 2012  Gaudenz Steinlin <gaudenz@durcheinandertal.ch> 
 3  # 
 4  #    This program is free software: you can redistribute it and/or modify 
 5  #    it under the terms of the GNU General Public License as published by 
 6  #    the Free Software Foundation, either version 3 of the License, or 
 7  #    (at your option) any later version. 
 8  # 
 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. 
13  # 
14  #    You should have received a copy of the GNU General Public License 
15  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
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   
25 -def load_config(name='conf'):
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
40 -class RankingOptionParser(OptionParser):
41
42 - def __init__(self, event, *args, **kwargs):
43 # super does not work here because OptionParser derives from OptionContainer 44 # which is an old style class (see optparse.py) 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
53 - def parse_args(self):
54 (options, args) = OptionParser.parse_args(self) 55 56 # process rankings arguments 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
73 -class RankingFileOptionParser(RankingOptionParser):
74
75 - def parse_args(self):
76 77 (options, args, ranking_list) = RankingOptionParser.parse_args(self) 78 79 # process file arguments 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