You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
6.8 KiB
202 lines
6.8 KiB
#-*- coding: utf-8 -*-
|
|
|
|
# Copyright 2010 Mir Calculate Ltd. http://www.calculate-linux.org
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import optparse
|
|
import sys
|
|
import re
|
|
|
|
from cl_lang import lang
|
|
tr = lang()
|
|
tr.setLocalDomain('cl_lib')
|
|
tr.setLanguage(sys.modules[__name__])
|
|
optparse._ = _
|
|
|
|
def make_option(shortOption=None,longOption=None,
|
|
optVal=None, help=None, default=None,
|
|
action=None, type=None, choices=None):
|
|
"""Make option. Used for processing options parameter in opt __init__.
|
|
|
|
shortOption short option
|
|
longOption long option
|
|
optVal argument name, if specified then
|
|
option is store, else bool value
|
|
help help string
|
|
default default value of this option
|
|
action another action (at example append, for creating list)
|
|
type type of value
|
|
choices choice values for type 'choice'
|
|
|
|
Examples:
|
|
Simple bool option:
|
|
make_option(shortOption="q",help=_("quiet work mode"))
|
|
|
|
Simple value option:
|
|
make_option(longOption="file",help=_("log file"),optVal="LOGFILE",
|
|
default="/var/log/test.log")
|
|
|
|
Choice option:
|
|
make_option(shortOption="color",help=_("color mode"), type="choice",
|
|
choices=["auto","always","never"])
|
|
|
|
Many option:
|
|
make_option(longOption="set",help=_("set variable"),action="append",
|
|
optVal="VAR")
|
|
"""
|
|
# create option by arguments
|
|
args = []
|
|
dest = None
|
|
if not shortOption is None:
|
|
args.append("-%s"%shortOption)
|
|
dest = shortOption
|
|
if not longOption is None:
|
|
args.append("--%s"%longOption)
|
|
if type == "choice":
|
|
action = "store"
|
|
else:
|
|
if not optVal is None:
|
|
if not action:
|
|
action = "store"
|
|
type = "string"
|
|
else:
|
|
if not action:
|
|
action = "store_true"
|
|
default = False
|
|
type = None
|
|
return optparse.make_option(*args, action=action, type=type,
|
|
dest=dest, metavar=optVal,
|
|
help=help, default=default,
|
|
choices=choices)
|
|
|
|
class opt(optparse.OptionParser):
|
|
"""Class of options for calculate utilities
|
|
|
|
Example:
|
|
class new_utility(opt):
|
|
def __init__(self):
|
|
opt.__init__(self,
|
|
package=__app__,
|
|
version=__version__,
|
|
description=_("Some description"),\
|
|
option_list= [\
|
|
#Simple bool option:
|
|
{
|
|
'shortOption':'q',
|
|
'help':_("quiet work mode")
|
|
},
|
|
#Simple value option:
|
|
{
|
|
'longOption':"file",
|
|
'help':_("log file"),
|
|
'optVal':"LOGFILE",
|
|
"default":"/var/log/test.log")
|
|
},
|
|
#Choice option:
|
|
{
|
|
'shortOption':"color",
|
|
'help':_("color mode"),
|
|
'type':"choice",
|
|
'choices'=["auto","always","never"]
|
|
},
|
|
#Summary option:
|
|
{
|
|
'longOption':"set",
|
|
'help':_("set variable"),
|
|
'action':"append",
|
|
'optVal'="VAR"
|
|
}
|
|
]
|
|
)
|
|
"""
|
|
|
|
variable_control = \
|
|
[{'longOption':"set",
|
|
'optVal':"VAR=VALUE",
|
|
'action':'append',
|
|
'help':_("set value for variable (comma - delimeter)")
|
|
},
|
|
{'longOption':"vars",
|
|
'optVal':"TYPE_VAR",
|
|
'help':_("print variables") + " (TYPE_VAR - all: " + \
|
|
_("full variables list") +")"
|
|
}]
|
|
|
|
color_control = \
|
|
[
|
|
{'longOption':"color",
|
|
'optVal':"WHEN",
|
|
'type':'choice',
|
|
'choices':['never','always','auto'],
|
|
'help':_("control whether color is used. WHEN may be") +\
|
|
" 'never', 'always', "+ _("or") + " 'auto'"
|
|
}]
|
|
|
|
def __init__(self,
|
|
usage=None,
|
|
option_list=None,
|
|
option_class=optparse.Option,
|
|
version=None,
|
|
conflict_handler="error",
|
|
description=None,
|
|
formatter=None,
|
|
add_help_option=True,
|
|
prog=None,
|
|
epilog=None,
|
|
package=None):
|
|
self.package = package
|
|
optparse.OptionParser.__init__(self,
|
|
usage=usage,
|
|
option_list=[make_option(**i) for i in option_list],
|
|
option_class=option_class,
|
|
version=version,
|
|
conflict_handler=conflict_handler,
|
|
description=description,
|
|
formatter=formatter,
|
|
add_help_option=add_help_option,
|
|
prog=prog,
|
|
epilog=epilog)
|
|
self.formatter._long_opt_fmt = "%s %s"
|
|
|
|
def get_usage(self):
|
|
"""Replace standard help header"""
|
|
if self.usage:
|
|
return "%s %s\n\n%s"%(self.package,self.version,
|
|
self.formatter.format_usage(self.expand_prog_name(self.usage)))
|
|
else:
|
|
return ""
|
|
|
|
def format_help(self, formatter=None):
|
|
"""Decode format help from utf-8 to unicode"""
|
|
return \
|
|
optparse.OptionParser.format_help(self,formatter).decode('UTF-8')
|
|
|
|
def error(self, msg):
|
|
"""error(msg : string)
|
|
|
|
Print a usage message incorporating 'msg' to stderr and exit.
|
|
If you override this in a subclass, it should not return -- it
|
|
should either exit or raise an exception.
|
|
"""
|
|
self.print_usage(sys.stderr)
|
|
self.exit(2, "%s: %s: %s\n" % (self.get_prog_name(), _("error"), msg))
|
|
|
|
def checkVarSyntax(self,values):
|
|
"""Check value of parameter set, was used for change vars"""
|
|
reCheckSet = re.compile("^([a-z_]+=[^,]+)(,\s*[a-z_]+=[^,]+)*$")
|
|
if values.set:
|
|
for val in values.set:
|
|
if not reCheckSet.match(val):
|
|
self.error(_("wrong variable set %s")%val)
|