Package osh :: Package command :: Module help
[frames] | no frames]

Source Code for Module osh.command.help

  1  # osh 
  2  # Copyright (C) Jack Orenstein <jao@geophile.com> 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published by 
  6  # the Free Software Foundation; either version 2 of the License, or 
  7  # (at your option) any later version. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with this program; if not, write to the Free Software 
 16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 17   
 18  """C{help [OSH_COMMAND]} 
 19   
 20  If C{OSH_COMMAND} is specified, print usage information for the named osh command. Otherwise, list 
 21  the builtin commands. 
 22  """ 
 23   
 24  import sys 
 25   
 26  import osh.core 
 27   
 28  # CLI 
29 -def _help():
30 return _Help()
31 32 # API
33 -def help(osh_command = None):
34 """Prints usage information on osh commands. If C{osh_command} is specified, 35 print usage information for the named osh command. Otherwise, list the builtin commands. 36 """ 37 return _Help()
38
39 -class _Help(osh.core.Generator):
40 41 _command_name = None 42 43 44 # object interface 45
46 - def __init__(self):
47 osh.core.Generator.__init__(self, '', (0, 1))
48 49 50 # OshCommand interface 51
52 - def doc(self):
53 return __doc__
54
55 - def setup(self):
56 args = self.args() 57 if args.has_next(): 58 self._command_name = args.next_string() 59 if args.has_next(): 60 self.usage()
61 62 # Generator interface 63
64 - def execute(self):
65 if self._command_name: 66 try: 67 top_level_module = __import__('osh.command.' + self._command_name) 68 module = getattr(top_level_module, 'command') 69 module = getattr(module, self._command_name) 70 print _remove_epydoc_markup(module.__doc__) 71 except Exception, e: 72 print >>sys.stderr, "Can't find documentation for %s: %s" % (self._command_name, e) 73 else: 74 import osh.command 75 import osh.command.help 76 print _remove_epydoc_markup(__doc__) 77 print 'Builtin commands:' 78 for command in osh.command.cli: 79 print '\t', command
80 81 # Translation of epydoc markup to plain text. This is pretty weak -- it doesn't 82 # account for nested markup or markup inside quotes. 83 84 _MARKUP = ['B{', 'C{', 'i{', '::'] 85
86 -def _nearest_markup(text, start):
87 nearest_markup = None 88 nearest_start = len(text) 89 for i in xrange(len(_MARKUP)): 90 markup = _MARKUP[i] 91 position = text.find(markup, start) 92 if position != -1 and position < nearest_start: 93 nearest_markup = markup 94 nearest_start = position 95 if nearest_markup: 96 if '{' in nearest_markup: 97 markup_end = text.find('}', nearest_start) 98 assert markup_end > nearest_start 99 else: 100 markup_end = None 101 return nearest_start, nearest_markup, markup_end 102 else: 103 return None, None, None
104
105 -def _remove_epydoc_markup(text):
106 plain = [] 107 plain_start = 0 108 while plain_start < len(text): 109 markup_start, markup, markup_end = _nearest_markup(text, plain_start) 110 if markup_start is None: 111 # No more markup 112 plain.append(text[plain_start :]) 113 plain_start = len(text) 114 else: 115 # Get everything up to markup 116 plain.append(text[plain_start : markup_start]) 117 if '{' in markup: 118 # If markup is x{...} get text between the braces. 119 assert markup_end is not None, markup_end 120 plain.append(text[markup_start + len(markup) : markup_end]) 121 plain_start = markup_end + 1 122 else: # must be :: 123 plain.append(':') 124 plain_start = markup_start + len(markup) 125 return ''.join(plain)
126