Package lib
[hide private]
[frames] | no frames]

Source Code for Package lib

  1  import time 
  2   
  3  _version = "0.4.1" 
  4  _copyright = """Copyright (C) 2012-2014 George M. Tzoumas 
  5  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
  6  This is free software: you are free to change and redistribute it. 
  7  There is NO WARRANTY, to the extent permitted by law.""" 
  8   
9 -class Abort(Exception):
10 pass
11
12 -def extract_parser_args(arglist, parser, pos = -1):
13 """extract options belonging to I{parser} along with I{pos} positional arguments 14 15 @param arglist: argument list to extract 16 @param parser: parser object to be used for extracting 17 @param pos: number of positional options to be extracted 18 19 if I{pos}<0 then all positional arguments are extracted, otherwise, 20 only I{pos} arguments are extracted. arglist[0] (usually sys.argv[0]) is also positional 21 argument! 22 23 @rtype: ([str,...],[str,...]) 24 @return: tuple (argv1,argv2) with extracted argument list and remaining argument list 25 """ 26 argv = [[],[]] 27 posc = 0 28 push_value = None 29 for x in arglist: 30 if push_value: 31 push_value.append(x) 32 push_value = None 33 continue 34 # get option name (long options stop at '=') 35 y = x[0:x.find('=')] if '=' in x else x 36 if x[0] == '-': 37 if parser.has_option(y): 38 argv[0].append(x) 39 if not x.startswith('--') and parser.get_option(y).takes_value(): 40 push_value = argv[0] 41 else: 42 argv[1].append(x) 43 else: 44 if pos < 0: 45 argv[0].append(x) 46 else: 47 argv[posc >= pos].append(x) 48 posc += 1 49 return tuple(argv)
50
51 -def atoi(s, lower_bound=None, upper_bound=None, prefix=''):
52 """convert string to integer, exiting on error (for cmdline parsing) 53 54 @param lower_bound: perform additional check so that value >= I{lower_bound} 55 @param upper_bound: perform additional check so that value <= I{upper_bound} 56 @param prefix: output prefix for error reporting 57 @rtype: int 58 """ 59 try: 60 k = int(s); 61 if lower_bound is not None: 62 if k < lower_bound: 63 raise Abort(prefix + "value '" + s +"' out of range: should not be less than %d" % lower_bound) 64 if upper_bound is not None: 65 if k > upper_bound: 66 raise Abort(prefix + "value '" + s +"' out of range: should not be greater than %d" % upper_bound) 67 except ValueError as e: 68 raise Abort(prefix + "invalid integer value '" + s +"'") 69 return k
70
71 -def _parse_month(mstr):
72 """get a month value (0-12) from I{mstr}, exiting on error (for cmdline parsing) 73 74 @rtype: int 75 """ 76 m = atoi(mstr,lower_bound=0,upper_bound=12,prefix='month: ') 77 if m == 0: m = time.localtime()[1] 78 return m
79
80 -def parse_month_range(s):
81 """return (Month,Span) by parsing range I{Month}, I{Month1}-I{Month2} or I{Month}:I{Span} 82 83 @rtype: (int,int) 84 """ 85 if ':' in s: 86 t = s.split(':') 87 if len(t) != 2: raise Abort("invalid month range '" + s + "'") 88 Month = _parse_month(t[0]) 89 MonthSpan = atoi(t[1],lower_bound=0,prefix='month span: ') 90 elif '-' in s: 91 t = s.split('-') 92 if len(t) != 2: raise Abort("invalid month range '" + s + "'") 93 Month = _parse_month(t[0]) 94 MonthSpan = atoi(t[1],lower_bound=Month+1,prefix='month range: ') - Month + 1 95 else: 96 Month = _parse_month(s) 97 MonthSpan = 1 98 return (Month,MonthSpan)
99
100 -def parse_year(ystr):
101 """get a year value (>=0) from I{ystr}, exiting on error (for cmdline parsing) 102 103 @rtype: int 104 """ 105 y = atoi(ystr,lower_bound=0,prefix='year: ') 106 if y == 0: y = time.localtime()[0] 107 return y
108