summaryrefslogtreecommitdiff
path: root/plac/plac_ext.py
diff options
context:
space:
mode:
Diffstat (limited to 'plac/plac_ext.py')
-rw-r--r--plac/plac_ext.py97
1 files changed, 59 insertions, 38 deletions
diff --git a/plac/plac_ext.py b/plac/plac_ext.py
index e7b4ab3..ae665b0 100644
--- a/plac/plac_ext.py
+++ b/plac/plac_ext.py
@@ -130,6 +130,54 @@ class ReadlineInput(object):
def __iter__(self):
return iter(self.readline, '')
+################### help functionality in plac interpreters ###################
+
+class HelpSummary(object):
+ "Build the help summary consistently with the cmd module"
+
+ @classmethod
+ def add(cls, obj, specialcommands):
+ p = plac_core.parser_from(obj)
+ c = cmd.Cmd(stdout=cls())
+ c.stdout.write('\n')
+ c.print_topics('special commands',
+ sorted(specialcommands), 15, 80)
+ c.print_topics('custom commands',
+ sorted(obj.syncommands), 15, 80)
+ c.print_topics('commands run in external processes',
+ sorted(obj.mpcommands), 15, 80)
+ c.print_topics('threaded commands',
+ sorted(obj.thcommands), 15, 80)
+ p.helpsummary = unicode(c.stdout)
+
+ def __init__(self):
+ self._ls = []
+
+ def write(self, s):
+ self._ls.append(s)
+
+ def __str__(self):
+ return ''.join(self._ls)
+
+def format_help(self):
+ "Attached to plac_core.ArgumentParser for plac interpreters"
+ try:
+ return self.helpsummary
+ except AttributeError:
+ return super(plac_core.ArgumentParser, self).format_help()
+plac_core.ArgumentParser.format_help = format_help
+
+def default_help(obj, cmd=None):
+ "An utility for implementing the help functionality in plac interpreters"
+ parser = plac_core.parser_from(obj)
+ if cmd is None:
+ yield parser.format_help()
+ subp = parser.subparsers._name_parser_map.get(cmd)
+ if subp is None:
+ yield _('Unknown command %s' % cmd)
+ else:
+ yield subp.format_help()
+
########################### import management ################################
try:
@@ -139,7 +187,6 @@ except:
def partial_call(factory, arglist):
"Call a container factory with the arglist and return a plac object"
-
a = plac_core.parser_from(factory).argspec
if a.defaults or a.varargs or a.varkw:
raise TypeError('Interpreter.call must be invoked on '
@@ -155,6 +202,7 @@ def partial_call(factory, arglist):
dic = dict(factory=factory)
exec code in dic
makeobj = dic['makeobj']
+ makeobj.add_help = False
if inspect.isclass(factory):
makeobj.__annotations__ = getattr(
factory.__init__, '__annotations__', {})
@@ -389,46 +437,21 @@ class MPTask(BaseTask):
######################### Task Manager #######################
-class HelpSummary(object):
- "Build the help summary consistently with the cmd module"
-
- @classmethod
- def make(cls, obj, specialcommands):
- c = cmd.Cmd(stdout=cls())
- c.stdout.write('\n')
- c.print_topics('special commands',
- sorted(specialcommands), 15, 80)
- c.print_topics('custom commands',
- sorted(obj.syncommands), 15, 80)
- c.print_topics('commands run in external processes',
- sorted(obj.mpcommands), 15, 80)
- c.print_topics('threaded commands',
- sorted(obj.thcommands), 15, 80)
- return c.stdout
-
- def __init__(self):
- self._ls = []
-
- def write(self, s):
- self._ls.append(s)
-
- def __str__(self):
- return ''.join(self._ls)
-
class TaskManager(object):
"""
Store the given commands into a task registry. Provides methods to
manage the submitted tasks.
"""
cmdprefix = '.'
- specialcommands = set(['.help', '.last_tb'])
+ specialcommands = set(['.last_tb'])
def __init__(self, obj):
self.obj = obj
self.registry = {} # {taskno : task}
if obj.mpcommands or obj.thcommands:
self.specialcommands.update(['.kill', '.list', '.output'])
- self.helpsummary = HelpSummary.make(obj, self.specialcommands)
+ self.parser = plac_core.parser_from(obj)
+ HelpSummary.add(obj, self.specialcommands)
self.man = Manager() if obj.mpcommands else None
signal.signal(signal.SIGTERM, terminatedProcess)
@@ -520,13 +543,6 @@ class TaskManager(object):
else:
yield 'Nothing to show'
- def help(self, cmd=None):
- "show help about a given command"
- if cmd is None:
- yield unicode(self.helpsummary)
- else:
- yield plac_core.parser_from(self.obj).help_cmd(cmd)
-
########################### SyncProcess ##############################
class Process(subprocess.Popen):
@@ -756,7 +772,7 @@ class Interpreter(object):
self._set_commands(obj)
self.tm = TaskManager(obj)
self.man = self.tm.man
- self.parser = plac_core.parser_from(obj, prog='', add_help=False)
+ self.parser = plac_core.parser_from(obj, prog='')
if self.commands:
self.commands.update(self.tm.specialcommands)
self.parser.addsubcommands(
@@ -941,7 +957,8 @@ class Interpreter(object):
readline_present = False
if stdin is sys.stdin and readline_present: # use readline
histfile = os.path.expanduser('~/.%s.history' % self.name)
- self.stdin = ReadlineInput(self.commands, histfile=histfile)
+ completions = list(self.commands) + ['help']
+ self.stdin = ReadlineInput(completions, histfile=histfile)
else:
self.stdin = stdin
self.prompt = prompt
@@ -998,6 +1015,10 @@ class Interpreter(object):
interpreter, else start an interactive session.
"""
obj = partial_call(factory, arglist)
+ if not hasattr(obj, 'help'):
+ # help is recognized as an alias for --help
+ aliases = dict(help='--help')
+ plac_core.parser_from(obj).alias = lambda a: aliases.get(a, a)
i = cls(obj, commentchar, split)
if i.obj._args_:
with i: