Esempio n. 1
0
 def list(self):
     """List available templates."""
     
     print("Available blueprints:\n")
     
     # Load Python plugins.
     blueprints = dict([(i.name, i.load()) for i in pkg_resources.iter_entry_points('marrow.blueprint')])
     
     # Get the length of the longest plugin name.
     mlen = max([len(i) for i in blueprints])
     
     # Output one line per blueprint: name, then the first line of documentation (if available).
     for name in sorted(blueprints):
         doc = partitionhelp(getattr(blueprints[name], '__doc__', 'No description available.'))[0][0]
         print(" %-*s  %s" % (mlen, name, wrap(doc).replace("\n", "\n" + " " * (mlen + 3))))
     
     print("\nIf the last segment of the name is unambiguous you can omit the namespace.")
Esempio n. 2
0
 def help(self, master, value):
     width = 79
     
     # Determine current terminal width, if possible.
     if sys.stdout.isatty():
         try:
             # Use terminal control codes if possible.
             import fcntl, termios, struct
             width = struct.unpack(b'hh', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234'))[1] - 1
         
         except:
             # Fall back on environment variables.
             try:
                 width = int(os.environ['COLUMNS']) - 1
             except:
                 # TODO: Fall back on curses, then ANSI.
                 pass
     
     # Output the summary information.
     for current, spec in enumerate(master.callables):
         summary, description = partitionhelp(spec.doc)
         
         if current == 0 and summary: print(wrap(summary, width))
         
         if current == 0:
             print("Usage:", os.path.basename(sys.argv[0]), end=" ")
         
         else:
             print(spec.obj.__name__, end=" ")
         
         if spec.keywords:
             print("[OPTIONS]" if current == 0 else "[CMDOPTS]", end=" ")
         
         if spec.kwargs:
             print("[--name=value...]", end=" ")
         
         for arg in spec.arguments:
             print("<", arg, ">", sep="", end=" ")
         
         if spec.args:
             print("[value...]", end=" ")
         
         if not spec.simple and len(master.callables) == current + 1:
             print("<COMMAND> ...", end="")
     
     print()
     
     # Output the details.
     for current, spec in enumerate(master.callables):
         summary, description = partitionhelp(spec.doc)
         
         if current != 0:
             print("\nCommand:", spec.obj.__name__)
             if summary: print(wrap(summary, width))
         
         if spec.keywords:
             print("\n", "OPTIONS" if current == 0 else "CMDOPTS", " may be one or more of:", sep="", end="\n\n")
             
             strings = dict()
             abbreviation = dict(zip(spec.abbreviation.values(), spec.abbreviation.keys()))
             
             for name in spec.keywords:
                 default = spec.keywords[name]
                 
                 if spec.typecast.get(name, None) is boolean:
                     strings["-" + abbreviation[name] + ", --" + name] = \
                             spec.docstring.get(name, "Toggle this value.\nDefault: %r" % default)
                     continue
                 
                 strings["-" + abbreviation[name] + ", --" + name + "=VAL"] = \
                         spec.docstring.get(name, "Override this value.\nDefault: %r" % default)
             
             mlen = max([len(i) for i in strings])
             for name in sorted(strings):
                 print(" %-*s  %s" % (mlen, name, wrap(strings[name], width).replace("\n", "\n" + " " * (mlen + 3))))
         
         if not spec.simple and len(master.callables) == current + 1:
             print("\n", "COMMAND may be one of:", sep="", end="\n\n")
             
             cmds = dict()
             for cmd in dir(spec.obj):
                 if cmd[0] == '_' or not callable(getattr(spec.obj, cmd)): continue
                 cmds[cmd] = partitionhelp(getattr(spec.obj, cmd).__doc__ or "Undocumented command.")[0]
             
             mlen = max([len(i) for i in cmds])
             for name in sorted(cmds):
                 print(" %-*s  %s" % (mlen, name, wrap(cmds[name], width).replace("\n", "\n" + " " * (mlen + 3))))
             
             print("\n", wrap("For help on a specific command, call the command and pass --help in CMDOPTS.", width), sep="")
         
         if description: print("\n", wrap(description, width), sep="")
     
     print()
     raise ExitException(os.EX_USAGE, None)