Commit d02436db authored by Marco Mariani's avatar Marco Mariani

naming consistency (args/options/config..)

parent 5d6e6b96
...@@ -11,10 +11,10 @@ class BangCommand(ConfigCommand): ...@@ -11,10 +11,10 @@ class BangCommand(ConfigCommand):
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(BangCommand, self).get_parser(prog_name) ap = super(BangCommand, self).get_parser(prog_name)
parser.add_argument('-m', '--message', ap.add_argument('-m', '--message',
help='Message for bang') help='Message for bang')
return parser return ap
def take_action(self, args): def take_action(self, args):
config = self.fetch_config(args) config = self.fetch_config(args)
......
...@@ -11,11 +11,11 @@ class CacheLookupCommand(ConfigCommand): ...@@ -11,11 +11,11 @@ class CacheLookupCommand(ConfigCommand):
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(CacheLookupCommand, self).get_parser(prog_name) ap = super(CacheLookupCommand, self).get_parser(prog_name)
# XXX this argument could use a better name # XXX this argument could use a better name
parser.add_argument('software_url', ap.add_argument('software_url',
help='Your software url or MD5 hash') help='Your software url or MD5 hash')
return parser return ap
def take_action(self, args): def take_action(self, args):
config = self.fetch_config(args) config = self.fetch_config(args)
......
...@@ -21,9 +21,9 @@ class ConfigCommand(Command): ...@@ -21,9 +21,9 @@ class ConfigCommand(Command):
default_config_path = '/etc/opt/slapos/slapos.cfg' default_config_path = '/etc/opt/slapos/slapos.cfg'
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(ConfigCommand, self).get_parser(prog_name) ap = super(ConfigCommand, self).get_parser(prog_name)
parser.add_argument('--cfg', help='SlapOS configuration file') ap.add_argument('--cfg', help='SlapOS configuration file')
return parser return ap
def _get_config(self, cfg_path, required=False): def _get_config(self, cfg_path, required=False):
""" """
......
...@@ -74,6 +74,7 @@ class RegisterCommand(Command): ...@@ -74,6 +74,7 @@ class RegisterCommand(Command):
return_code = do_register(config) return_code = do_register(config)
except SystemExit, err: except SystemExit, err:
# Catch exception raised by optparse # Catch exception raised by optparse
# XXX returning exception with sys.exit?
return_code = err return_code = err
sys.exit(return_code) sys.exit(return_code)
...@@ -15,11 +15,11 @@ class SupervisorctlCommand(ConfigCommand): ...@@ -15,11 +15,11 @@ class SupervisorctlCommand(ConfigCommand):
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(SupervisorctlCommand, self).get_parser(prog_name) ap = super(SupervisorctlCommand, self).get_parser(prog_name)
parser.add_argument('supervisor_args', ap.add_argument('supervisor_args',
nargs=argparse.REMAINDER, nargs=argparse.REMAINDER,
help='parameters passed to supervisorctl') help='parameters passed to supervisorctl')
return parser return ap
def take_action(self, args): def take_action(self, args):
config = self.fetch_config(args) config = self.fetch_config(args)
......
...@@ -34,17 +34,14 @@ from slapos.bang import do_bang ...@@ -34,17 +34,14 @@ from slapos.bang import do_bang
def main(*args): def main(*args):
parser = argparse.ArgumentParser() ap = argparse.ArgumentParser()
parser.add_argument('-m', '--message', default='', help='Message for bang.') ap.add_argument('-m', '--message', default='', help='Message for bang.')
parser.add_argument('configuration_file', nargs=1, type=argparse.FileType(), ap.add_argument('configuration_file', type=argparse.FileType(),
help='SlapOS configuration file.') help='SlapOS configuration file.')
if len(args) == 0: if args:
argument = parser.parse_args() args = ap.parse_args(list(args))
else: else:
argument = parser.parse_args(list(args)) args = ap.parse_args()
configuration_file = argument.configuration_file[0]
message = argument.message
# Loads config (if config specified)
config = ConfigParser.SafeConfigParser() config = ConfigParser.SafeConfigParser()
config.readfp(configuration_file) config.readfp(args.configuration_file)
do_bang(config, message) do_bang(config, args.message)
...@@ -7,10 +7,10 @@ from slapos.cache import do_lookup ...@@ -7,10 +7,10 @@ from slapos.cache import do_lookup
def cache_lookup(): def cache_lookup():
parser = argparse.ArgumentParser() ap = argparse.ArgumentParser()
parser.add_argument("configuration_file", help="SlapOS configuration file") ap.add_argument("configuration_file", help="SlapOS configuration file")
parser.add_argument("software_url", help="Your software url or MD5 hash") ap.add_argument("software_url", help="Your software url or MD5 hash")
args = parser.parse_args() args = ap.parse_args()
config = ConfigParser.SafeConfigParser() config = ConfigParser.SafeConfigParser()
config.read(args.configuration_file) config.read(args.configuration_file)
......
...@@ -40,12 +40,12 @@ examples : ...@@ -40,12 +40,12 @@ examples :
ap.add_argument('configuration_file', ap.add_argument('configuration_file',
help='path to slapos.cfg') help='path to slapos.cfg')
options = ap.parse_args() args = ap.parse_args()
if not os.path.isfile(options.configuration_file): if not os.path.isfile(args.configuration_file):
ap.error("%s: Not found or not a regular file." % options.configuration_file) ap.error("%s: Not found or not a regular file." % args.configuration_file)
config = ClientConfig(options, get_config_parser(options.configuration_file)) config = ClientConfig(args, get_config_parser(args.configuration_file))
local = init(config) local = init(config)
do_console(local) do_console(local)
...@@ -72,9 +72,9 @@ def checkSlaposCfg(): ...@@ -72,9 +72,9 @@ def checkSlaposCfg():
for element in sys.argv: for element in sys.argv:
if '.cfg' in element: if '.cfg' in element:
if os.path.exists(element): if os.path.exists(element):
configuration = ConfigParser.SafeConfigParser() config = ConfigParser.SafeConfigParser()
configuration.read(element) config.read(element)
if configuration.has_section('slapos'): if config.has_section('slapos'):
return True return True
return False return False
...@@ -207,23 +207,23 @@ Node subcommands usage: ...@@ -207,23 +207,23 @@ Node subcommands usage:
# Parse arguments # Parse arguments
# XXX remove the "positional arguments" from help message # XXX remove the "positional arguments" from help message
parser = argparse.ArgumentParser(usage=usage) ap = argparse.ArgumentParser(usage=usage)
parser.add_argument('command') ap.add_argument('command')
parser.add_argument('argument_list', nargs=argparse.REMAINDER) ap.add_argument('argument_list', nargs=argparse.REMAINDER)
namespace = parser.parse_args() args = ap.parse_args()
# Set sys.argv for the sub-entry point that we will call # Set sys.argv for the sub-entry point that we will call
command_line = [namespace.command] command_line = [args.command]
command_line.extend(namespace.argument_list) command_line.extend(args.argument_list)
sys.argv = command_line sys.argv = command_line
try: try:
if not dispatch(namespace.command, is_node): if not dispatch(args.command, is_node):
parser.print_help() ap.print_help()
sys.exit(1) sys.exit(1)
except EntryPointNotImplementedError, exception: except EntryPointNotImplementedError, exception:
print ('The command %s does not exist or is not yet implemented. Please ' print ('The command %s does not exist or is not yet implemented. Please '
'have a look at http://community.slapos.org to read documentation or ' 'have a look at http://community.slapos.org to read documentation or '
'forum. Please also make sure that SlapOS Node is up to ' 'forum. Please also make sure that SlapOS Node is up to '
'date.' % exception) 'date.' % exception)
sys.exit(1) sys.exit(1)
...@@ -38,8 +38,6 @@ from slapos.format import FormatConfig, UsageError, tracing_monkeypatch, do_form ...@@ -38,8 +38,6 @@ from slapos.format import FormatConfig, UsageError, tracing_monkeypatch, do_form
def main(*args): def main(*args):
"Run default configuration." "Run default configuration."
# Parse arguments
ap = argparse.ArgumentParser(usage='usage: %s [options] CONFIGURATION_FILE' % sys.argv[0]) ap = argparse.ArgumentParser(usage='usage: %s [options] CONFIGURATION_FILE' % sys.argv[0])
ap.add_argument('-x', '--computer_xml', ap.add_argument('-x', '--computer_xml',
...@@ -96,14 +94,14 @@ def main(*args): ...@@ -96,14 +94,14 @@ def main(*args):
help='path to slapos.cfg') help='path to slapos.cfg')
if args: if args:
options = ap.parse_args(list(args)) args = ap.parse_args(list(args))
else: else:
options = ap.parse_args() args = ap.parse_args()
logger = logging.getLogger("slapformat") logger = logging.getLogger("slapformat")
logger.addHandler(logging.StreamHandler()) logger.addHandler(logging.StreamHandler())
if options.verbose: if args.verbose:
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
logger.debug("Verbose mode enabled.") logger.debug("Verbose mode enabled.")
else: else:
...@@ -112,13 +110,13 @@ def main(*args): ...@@ -112,13 +110,13 @@ def main(*args):
config = FormatConfig(logger=logger) config = FormatConfig(logger=logger)
configuration_parser = ConfigParser.SafeConfigParser() configuration_parser = ConfigParser.SafeConfigParser()
if configuration_parser.read(options.configuration_file) != [options.configuration_file]: if configuration_parser.read(args.configuration_file) != [args.configuration_file]:
raise UsageError('Cannot find or parse configuration file: %s' % options.configuration_file) raise UsageError('Cannot find or parse configuration file: %s' % args.configuration_file)
try: try:
config.setConfig(options, configuration_parser) config.setConfig(args, configuration_parser)
except UsageError as err: except UsageError as exc:
sys.stderr.write(err.message + '\n') sys.stderr.write(exc.message + '\n')
sys.stderr.write("For help use --help\n") sys.stderr.write("For help use --help\n")
sys.exit(1) sys.exit(1)
......
...@@ -35,8 +35,6 @@ from slapos.register.register import do_register, RegisterConfig ...@@ -35,8 +35,6 @@ from slapos.register.register import do_register, RegisterConfig
def main(): def main():
"Run default configuration."
ap = argparse.ArgumentParser(usage='usage: slapos node %s NODE_NAME [options]' % sys.argv[0]) ap = argparse.ArgumentParser(usage='usage: slapos node %s NODE_NAME [options]' % sys.argv[0])
ap.add_argument('node_name', ap.add_argument('node_name',
...@@ -86,9 +84,9 @@ def main(): ...@@ -86,9 +84,9 @@ def main():
default=False, default=False,
action='store_true') action='store_true')
options = ap.parse_args() args = ap.parse_args()
if options.password and not options.login: if args.password and not args.login:
ap.error('Please enter your login with your password') ap.error('Please enter your login with your password')
logger = logging.getLogger('Register') logger = logging.getLogger('Register')
...@@ -100,10 +98,11 @@ def main(): ...@@ -100,10 +98,11 @@ def main():
try: try:
config = RegisterConfig(logger=logger) config = RegisterConfig(logger=logger)
config.setConfig(options) config.setConfig(args)
return_code = do_register(config) return_code = do_register(config)
except SystemExit, err: except SystemExit as exc:
# Catch exception raised by optparse # Catch exception raised by optparse
return_code = err # XXX returning exception with sys.exit?
return_code = exc
sys.exit(return_code) sys.exit(return_code)
...@@ -7,17 +7,14 @@ from slapos.cli_legacy.util import get_config_parser ...@@ -7,17 +7,14 @@ from slapos.cli_legacy.util import get_config_parser
def remove(): def remove():
""" ap = argparse.ArgumentParser()
Run when invoking slapos remove. Mostly argument parsing. ap.add_argument('configuration_file',
""" help='SlapOS configuration file')
parser = argparse.ArgumentParser() ap.add_argument('software_url',
parser.add_argument('configuration_file', help='Your software url')
help='SlapOS configuration file') ap.add_argument('node',
parser.add_argument('software_url', help='Target node')
help='Your software url') args = ap.parse_args()
parser.add_argument('node',
help='Target node')
args = parser.parse_args()
config = ClientConfig(args, get_config_parser(args.configuration_file)) config = ClientConfig(args, get_config_parser(args.configuration_file))
local = init(config) local = init(config)
......
...@@ -10,14 +10,14 @@ def supply(): ...@@ -10,14 +10,14 @@ def supply():
""" """
Run when invoking slapos supply. Mostly argument parsing. Run when invoking slapos supply. Mostly argument parsing.
""" """
parser = argparse.ArgumentParser() ap = argparse.ArgumentParser()
parser.add_argument('configuration_file', ap.add_argument('configuration_file',
help='SlapOS configuration file') help='SlapOS configuration file')
parser.add_argument('software_url', ap.add_argument('software_url',
help='Your software url') help='Your software url')
parser.add_argument('node', ap.add_argument('node',
help='Target node') help='Target node')
args = parser.parse_args() args = ap.parse_args()
config = ClientConfig(args, get_config_parser(args.configuration_file)) config = ClientConfig(args, get_config_parser(args.configuration_file))
local = init(config) local = init(config)
......
...@@ -11,5 +11,3 @@ def get_config_parser(path): ...@@ -11,5 +11,3 @@ def get_config_parser(path):
raise OSError('Specified configuration file %s does not exist. Exiting.' % path) raise OSError('Specified configuration file %s does not exist. Exiting.' % path)
configuration_parser.read(path) configuration_parser.read(path)
return configuration_parser return configuration_parser
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment