1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#############################################################
## ##
## Copyright (c) 2003-2011 by The University of Queensland ##
## Earth Systems Science Computational Centre (ESSCC) ##
## http://www.uq.edu.au/esscc ##
## ##
## Primary Business: Brisbane, Queensland, Australia ##
## Licensed under the Open Software License version 3.0 ##
## http://www.opensource.org/licenses/osl-3.0.php ##
## ##
#############################################################
"""
Defines L{OptionParser} class which extends C{optparse.OptionParser} class
by adding 'string_list', 'int_list' and 'float_list' option types. Also
defines L{LogOptionParser} which extends L{OptionParser} and adds support
for L{esys.lsm.Logging} options.
"""
import optparse
import re
import string
import copy
def getListFromString(opt, str, mapCallable):
"""
Parses a given string to extract a list. The expected format of the
string is '[e1,e2,e3,...]'. Returns C{map(mapCallable, [e1,e2,e3,...])}.
@type opt: string
@param opt: Command line option, only used for for creating
messages when raising OptionValueException
@type str: string
@param str: The option value string
@type mapCallable: callable
@param mapCallable: Callable object which converts a string
list element into another type, eg int, float, etc
@rtype: list
@return: list of converted-type elements.
"""
regex = re.compile("\[(.*)\]")
match = regex.match(str)
if (match != None):
try:
return map(mapCallable, string.split(match.group(1), ","))
except ValueError:
raise \
optparse.OptionValueError(
(
"option %s: Could not convert string elements of"+\
" %r to type."
) % (opt, str)
)
else:
raise \
optparse.OptionValueError(
"option %s: invalid list value: %r" % (opt, str)
)
def checkIntList(option, opt, value):
"""
Extracts a list of integers from a specified value.
@param option: The option.
@type opt: string
@param opt: The command line option string.
@type value: string
@param value: The value passed to the option.
@rtype list
@return: list of ints
"""
return getListFromString(opt, value, int)
def checkFloatList(option, opt, value):
"""
Extracts a list of floats from a specified value.
@type opt: string
@param opt: The command line option string.
@type value: string
@param value: The value passed to the option.
@rtype list
@return: list of floats
"""
return getListFromString(opt, value, float)
def checkStringList(option, opt, value):
"""
Extracts a list of strings from a specified value.
@type opt: string
@param opt: The command line option string.
@type value: string
@param value: The value passed to the option.
@rtype list
@return: list of floats
"""
return getListFromString(opt, value, str)
class ListOption(optparse.Option):
"""
Extends optparse.Option class by adding 'string_list',
'int_list' and 'float_list' types
"""
TYPES = optparse.Option.TYPES + ("int_list", "float_list", "string_list")
TYPE_CHECKER = copy.copy(optparse.Option.TYPE_CHECKER)
TYPE_CHECKER["int_list"] = checkIntList
TYPE_CHECKER["float_list"] = checkFloatList
TYPE_CHECKER["string_list"] = checkStringList
class OptionParser(optparse.OptionParser):
"""
Command line option parser which extends C{optparse.OptionParser}
by adding "int_list", "float_list" and "string_list" types.
"""
def __init__(
self,
usage=None,
option_list=None,
option_class=ListOption,
version=None,
conflict_handler="error",
description=None,
formatter=None,
add_help_option=1,
prog=None
):
"""
Initialises this parser, arguments as per C{optparse.OptionParser}.
"""
optparse.OptionParser.__init__(
self,
usage=usage,
option_list=option_list,
option_class=option_class,
version=version,
conflict_handler=conflict_handler,
description=description,
formatter=formatter,
add_help_option=add_help_option,
prog=prog
)
from esys.lsm import Logging
class LogOptionParser(OptionParser):
"""
Command line option parser which extends L{OptionParser}
by adding a default logging option.
"""
def __init__(
self,
usage=None,
option_list=None,
option_class=ListOption,
version=None,
conflict_handler="error",
description=None,
formatter=None,
add_help_option=1,
prog=None
):
"""
Initialises this parser, arguments as per L{OptionParser}.
"""
OptionParser.__init__(
self,
usage=usage,
option_list=option_list,
option_class=option_class,
version=version,
conflict_handler=conflict_handler,
description=description,
formatter=formatter,
add_help_option=add_help_option,
prog=prog
)
self.addDefaultOptions()
def addDefaultOptions(self):
self.add_option(
"-l", "--log-level",
dest="logLevel",
type="string",
default="WARNING",
metavar="L",
help=\
"The level of logging output (default L=\"WARNING\")"
)
def initialiseLogging(self, options):
logLevel = options.logLevel
if ((logLevel != None) and (len(logLevel) > 0)):
Logging.basicConfig()
Logging.getLogger("").setLevel(Logging.getLevel(logLevel))
def parse_args (self, args=None, values=None):
(options,values) = OptionParser.parse_args(self, args, values)
self.initialiseLogging(options)
return (options,values)
|