Skip to content

Commit

Permalink
minor: pep8 compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Fiers committed Oct 15, 2012
1 parent 1eeb22e commit 7a03ab7
Showing 1 changed file with 49 additions and 43 deletions.
92 changes: 49 additions & 43 deletions lib/python/moa/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import moa.logger as l



class MoaHelpFormatter(argparse.HelpFormatter):
"""
Copy pasted some code from argparse.py - and made minor changes
Expand All @@ -35,17 +34,16 @@ def format(tuple_size):
return (result, ) * tuple_size
return format


def _format_action(self, action):
# determine the required width and the entry label
help_position = min(self._action_max_length + 2,
self._max_help_position)
help_width = self._width - help_position
action_width = help_position - self._current_indent - 2

if action.dest == 'command':
action_header = argparse.SUPPRESS
else:
else:
action_header = self._format_action_invocation(action)

# no nelp; start on same line and add a final newline
Expand All @@ -68,7 +66,7 @@ def _format_action(self, action):
parts = [action_header]

# if there was help for the action, add lines of help text
if action.help:
if action.help:
help_text = self._expand_help(action)
help_lines = self._split_lines(help_text, help_width)
if argparse.SUPPRESS in action_header:
Expand All @@ -90,14 +88,16 @@ def _format_action(self, action):
if sysConf.commands[subaction.dest]['private']:
continue
if sysConf.commands[subaction.dest].get('needsJob') and \
(not sysConf.job.isMoa()):
(not sysConf.job.isMoa()):
continue
if sysConf.commands[subaction.dest].get('source', '') == \
'template':
flag='*'

tsource = sysConf.commands[subaction.dest].get('source', '')

if tsource == 'template':
flag = '*'
elif sysConf.commands[subaction.dest].get('needsJob', False):
flag='j'
flag = 'j'

if not re.match(r'^[\*j.] .*$', subaction.help):
subaction.help = '%s %s' % (flag, subaction.help)

Expand All @@ -106,19 +106,20 @@ def _format_action(self, action):
# return a single string
return self._join_parts(parts)


class MoaArgumentParser(argparse.ArgumentParser):

def error(self, message):
"""error(message: string)
Prints a usage message incorporating the message to stderr and
exits.
If you override this in a subclass, it should not return -- it
should either exit or raise an exception.
"""
self.error_message = message
raise moa.exceptions.MoaInvalidCommandLine, self

def real_error(self):
"""
after argparser.error, but takes the message from self.error_meesage
Expand All @@ -127,13 +128,12 @@ def real_error(self):
"""

self.print_usage(argparse._sys.stderr)
self.exit(2, argparse._('%s: error: %s\n') % (self.prog, self.error_message))


self.exit(2, argparse._('%s: error: %s\n') %
(self.prog, self.error_message))


def getParser(reuse=True):

if sysConf.argParser:
if reuse:
#work on the current parser
Expand All @@ -145,21 +145,21 @@ def getParser(reuse=True):
sysConf.commandParser = newParser.commandParser
return newParser, newParser.commandParser
else:
parser = MoaArgumentParser(
parser = MoaArgumentParser(
prog='moa',
formatter_class=MoaHelpFormatter)

hlptxt = ("Command legend: '.' can be executed everywhere; "+
hlptxt = ("Command legend: '.' can be executed everywhere; " +
"'j' require a job; '*' are specified by the template")

commandParser = parser.add_subparsers(
title='command', help='Moa Command', dest='command',
description=hlptxt)

parser.commandParser = commandParser
sysConf.originalParser = parser
sysConf.argParser = parser
sysConf.commandParser = commandParser
sysConf.argParser = parser
sysConf.commandParser = commandParser
return parser, commandParser


Expand All @@ -169,12 +169,13 @@ def getParser(reuse=True):

def _commandify(f, name):
"""
Do the actual commandification of function f with specified name
Do the actual commandification of function f with specified name
"""
try:
assert(inspect.getargspec(f).args == ['job', 'args'])
except AssertionError:
moa.ui.exitError("Command function for %s seems invalid - contact a developer" % name)
moa.ui.exitError(("Command function for %s seems invalid " +
"- contact a developer") % name)

l.debug("registering command %s" % name)
_desc = [x.strip() for x in f.__doc__.strip().split("\n", 1)]
Expand All @@ -183,43 +184,42 @@ def _commandify(f, name):
else:
shortDesc = _desc[0]
longDesc = ''

parser, cparser = getParser()

cp = cparser.add_parser(
name, help= shortDesc,
description="%s %s" % ( shortDesc, longDesc))

name, help=shortDesc,
description="%s %s" % (shortDesc, longDesc))

cp.add_argument("-r", "--recursive", dest="recursive", action="store_true",
default="false", help="Run this job recursively")

cp.add_argument("-v", "--verbose", dest="verbose", action="store_true",
help="Show debugging output")


cp.add_argument("--profile", dest="profile", action="store_true",
help="Run the profiler")

sysConf.commands[name] = {
'desc' : shortDesc,
'long' : longDesc,
'recursive' : 'gbobal',
'logJob' : True,
'needsJob' : False,
'call' : f,
'cp' : cp,
}
'desc': shortDesc,
'long': longDesc,
'recursive': 'gbobal',
'logJob': True,
'needsJob': False,
'call': f,
'cp': cp,
}
f.arg_parser = cp
return f


def command(f):
"""
Decorator for any function in moa - name is derived from function name
"""
return _commandify(f, f.__name__)


def commandName(name):
"""
Decorate a function as a moa command with the option to specify a
Expand All @@ -229,6 +229,7 @@ def decorator(f):
return _commandify(f, name)
return decorator


def argument(*args, **kwargs):
"""
Add an argument to a function
Expand All @@ -238,36 +239,41 @@ def decorator(f):
return f
return decorator


def addFlag(*args, **kwargs):
"""
Add a flag to (default false - true if specified) any command
"""
def decorator(f):
if not kwargs.has_key('action'):
if not 'action' in kwargs:
kwargs['action'] = 'store_true'
if not kwargs.has_key('default'):
if not 'default' in kwargs:
kwargs['default'] = False
f.arg_parser.add_argument(*args, **kwargs)
return f
return decorator



def needsJob(f):
sysConf.commands[f.func_name]['needsJob'] = True
return f


def doNotLog(f):
sysConf.commands[f.func_name]['logJob'] = False
return f


def localRecursive(f):
sysConf.commands[f.func_name]['recursive'] = 'local'
return f


def private(f):
sysConf.commands[f.func_name]['private'] = True


def forceable(f):
f.arg_parser.add_argument('-f', '--force', action='store_true',
default=False, help='Force this action')
return f

0 comments on commit 7a03ab7

Please sign in to comment.