def test_getpager_with_stdin_none(self): previous_stdin = sys.stdin try: sys.stdin = None pydoc.getpager() # Shouldn't fail. finally: sys.stdin = previous_stdin
def _dump(self, file=None): """ Pretty-printed look at all the registers in a peripheral """ dump = '\n'.join([r._dump_repr() for r in self._child_regs]) if file: file.write(dump) else: import pydoc pydoc.getpager()(dump)
def show(self, message): """ displays message to user TESTS:: sage: UI = sage.dev.user_interface.CmdLineInterface() sage: UI.show("I ate fillet mignon for dinner.") I ate fillet mignon for dinner. """ try: height, width = self._get_dimensions() except EnvironmentError: height, width = float('inf'), float('inf') message = message.strip().splitlines() message = [line.rstrip() for line in message] if (len(message)+2 <= height and max(len(line) for line in message) <= width): print(*message, sep='\n') else: message = '\n'.join(message)+'\n' try: self._pager(message) except AttributeError: import pydoc self._pager = pydoc.getpager() self._pager(message)
def main(): parser = argparse.ArgumentParser(prog=os.path.basename(__file__), description=__doc__) parser.add_argument('-D', '--docstrings', action='store_true', help='show all the docstrings using pydoc') parser.add_argument( '-v', '--verbose', action='store_true', help='turn on verbosity and debug-level debugging output', ) options = parser.parse_args() logging.basicConfig( format= '%(asctime)-15s %(levelname)s %(filename)s:%(lineno)d %(message)s', level=(logging.DEBUG if options.verbose else logging.INFO)) if options.docstrings: doc = pydoc.TextDoc() pager = pydoc.getpager() pager(doc.docmodule(sys.modules[__name__])) else: suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase) unittest.TextTestRunner( verbosity=(2 if options.verbose else 0)).run(suite)
def get_pager(_: "AppConfig") -> Callable[[str], None]: """ Decide what method to use for paging through text. todo: take pager from config? """ import pydoc return pydoc.getpager()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.buffer = [] self.pager = pydoc.getpager() self.stdout_write = sys.stdout.write self.stderr_write = sys.stderr.write sys.stdout.write = self.write sys.stderr.write = self.direct_write if 'LESS' not in os.environ: os.environ['LESS'] = '-FSRX' global PAGER PAGER = self
def print_out(self, txt): import curses txt = txt.replace("`$","\n") #txt = txt.replace("`$","") win=curses.initscr() max_x, max_y = win.getmaxyx() curses.endwin() if len(txt.split("\n")) > max_x: pager = pydoc.getpager() try: pager(txt) except (EOFError, KeyboardInterrupt), e: pass
def run(self): _stream_tracker.stdout = self.connection _stream_tracker.stderr = self.connection _stream_tracker.stdin = self.connection # Note that this is being called after we've swapped out stdout, so # it'll give us back a plain pager _stream_tracker.pager = pydoc.getpager() try: self.interact(BANNER) except SystemExit: print >>_former_stdout, "Remote console session disconnecting on a call to exit()" self.connection.close() self.connection = None
def show_sample(module): """Print sample modules.""" methods_info = _extract_methods_info(module) methods_info = sorted(methods_info, key=lambda x: x['name']) methods_info = sorted(methods_info, key=lambda x: x['category']) grouped_catergories = itertools.groupby(methods_info, key=lambda x: x['category']) text = _TEMPLATE_PREFIX.format(module_doc=_bold(module.__doc__.upper())) nb_categories = len( set(method_info['category'] for method_info in methods_info)) for current_category, ( category_name, category_methods_info) in enumerate(grouped_catergories): methods_text = '' for method_info in category_methods_info: codelines_text = '' for line in method_info['codelines']: codelines_text += _CODELINE_TEMPLATE.format(codeline=line) methods_text += _METHOD_TEMPLATE.format( method_upper=_bold(method_info['name'].upper()), method=_bold(method_info['name']), doc=_bold(method_info['doc']), result=method_info['method'](), codelines=codelines_text.rstrip()) text += _CATEGORY_TEMPLATE.format(step=_bold( _STEP_TEMPLATE.format(current_category=current_category + 1, nb_categories=nb_categories)), category=_underline( category_name.upper()), methods=methods_text) pydoc.getpager()(text)
def show_sample(module): """Print sample modules.""" methods_info = _extract_methods_info(module) methods_info = sorted(methods_info, key=lambda x: x['name']) methods_info = sorted(methods_info, key=lambda x: x['category']) grouped_catergories = itertools.groupby( methods_info, key=lambda x: x['category']) text = _TEMPLATE_PREFIX.format(module_doc=_bold(module.__doc__.upper())) nb_categories = len(set( method_info['category'] for method_info in methods_info)) for current_category, (category_name, category_methods_info) in enumerate( grouped_catergories): methods_text = '' for method_info in category_methods_info: codelines_text = '' for line in method_info['codelines']: codelines_text += _CODELINE_TEMPLATE.format(codeline=line) methods_text += _METHOD_TEMPLATE.format( method_upper=_bold(method_info['name'].upper()), method=_bold(method_info['name']), doc=_bold(method_info['doc']), result=method_info['method'](), codelines=codelines_text.rstrip()) text += _CATEGORY_TEMPLATE.format( step=_bold(_STEP_TEMPLATE.format( current_category=current_category+1, nb_categories=nb_categories)), category=_underline(category_name.upper()), methods=methods_text) pydoc.getpager()(text)
def print_help(self, errors=None): buf = cStringIO.StringIO() sys.stdout = buf print '='*80 optparse.OptionParser.print_help(self) print """ examples: """ if errors: print errors sys.stdout = sys.__stdout__ pager = pydoc.getpager() pager(buf.getvalue())
def _show(self, message, log_level, *args): r""" Display ``message``. .. SEEALSO:: :meth:`sage.dev.user_interface.UserInterface.show` TESTS:: sage: from sage.dev.test.config import DoctestConfig sage: from sage.dev.cmd_line_interface import CmdLineInterface sage: UI = CmdLineInterface(DoctestConfig()["UI"]) sage: UI.info("I ate {0} for dinner, a whole {1} for dessert, and then took a nice walk around the lake.", 'filet mignon', 'apple pie') # indirect doctest # I ate filet mignon for dinner, a whole apple pie for dessert, and then took a # nice walk around the lake. """ if len(args) > 0: message = message.format(*args) height, width = self._get_dimensions() kwds = {'width': width} if log_level == INFO: kwds['initial_indent'] = kwds['subsequent_indent'] = '# ' wrap = textwrap.TextWrapper(**kwds).wrap message = list( itertools.chain.from_iterable( wrap(line) for line in message.splitlines())) if len(message) <= height: print( self._color_code(log_level) + '\n'.join(message) + self._color_code()) else: message = '\n'.join(message) + '\n' try: self._pager(message) except AttributeError: import pydoc self._pager = pydoc.getpager() self._pager(message)
def license_acceptor() -> bool: """ Shows the license and asks the user to accept it. :returns: Whether the user accepted the license. """ title = "Before using this software you must accept the following license:" pager = getpager() pager(f"{title}\n\n{EULA}") resp = input("\nIf you accept these license terms, type 'I accept' and press Enter.\n> ") if not resp: for attempt in range(10): resp = input("> ") if resp: break return resp.lower().strip() == "i accept"
def _show(self, message, log_level, *args): r""" Display ``message``. .. SEEALSO:: :meth:`sage.dev.user_interface.UserInterface.show` TESTS:: sage: from sage.dev.test.config import DoctestConfig sage: from sage.dev.cmd_line_interface import CmdLineInterface sage: UI = CmdLineInterface(DoctestConfig()["UI"]) sage: UI.info("I ate {0} for dinner, a whole {1} for dessert, and then took a nice walk around the lake.", 'filet mignon', 'apple pie') # indirect doctest # I ate filet mignon for dinner, a whole apple pie for dessert, and then took a # nice walk around the lake. """ if len(args) > 0: message = message.format(*args) height, width = self._get_dimensions() kwds = {'width': width} if log_level == INFO: kwds['initial_indent'] = kwds['subsequent_indent'] = '# ' wrap = textwrap.TextWrapper(**kwds).wrap message = list(itertools.chain.from_iterable( wrap(line) for line in message.splitlines())) if len(message) <= height: print(self._color_code(log_level) + '\n'.join(message) + self._color_code()) else: message = '\n'.join(message)+'\n' try: self._pager(message) except AttributeError: import pydoc self._pager = pydoc.getpager() self._pager(message)
def __init__(self): self.pager = pydoc.getpager() self.command_map = self.as_map()
def lookfor(what, module=None, import_modules=True, regenerate=False, output=None): """ Do a keyword search on docstrings. A list of objects that matched the search is displayed, sorted by relevance. All given keywords need to be found in the docstring for it to be returned as a result, but the order does not matter. Parameters ---------- what : str String containing words to look for. module : str or list, optional Name of module(s) whose docstrings to go through. import_modules : bool, optional Whether to import sub-modules in packages. Default is True. regenerate : bool, optional Whether to re-generate the docstring cache. Default is False. output : file-like, optional File-like object to write the output to. If omitted, use a pager. See Also -------- source, info Notes ----- Relevance is determined only roughly, by checking if the keywords occur in the function name, at the start of a docstring, etc. Examples -------- >>> np.lookfor('binary representation') # doctest: +SKIP Search results for 'binary representation' ------------------------------------------ numpy.binary_repr Return the binary representation of the input number as a string. numpy.core.setup_common.long_double_representation Given a binary dump as given by GNU od -b, look for long double numpy.base_repr Return a string representation of a number in the given base system. ... """ import pydoc # Cache cache = _lookfor_generate_cache(module, import_modules, regenerate) # Search # XXX: maybe using a real stemming search engine would be better? found = [] whats = str(what).lower().split() if not whats: return for name, (docstring, kind, index) in cache.items(): if kind in ('module', 'object'): # don't show modules or objects continue doc = docstring.lower() if all(w in doc for w in whats): found.append(name) # Relevance sort # XXX: this is full Harrison-Stetson heuristics now, # XXX: it probably could be improved kind_relevance = { 'func': 1000, 'class': 1000, 'module': -1000, 'object': -1000 } def relevance(name, docstr, kind, index): r = 0 # do the keywords occur within the start of the docstring? first_doc = "\n".join(docstr.lower().strip().split("\n")[:3]) r += sum([200 for w in whats if w in first_doc]) # do the keywords occur in the function name? r += sum([30 for w in whats if w in name]) # is the full name long? r += -len(name) * 5 # is the object of bad type? r += kind_relevance.get(kind, -1000) # is the object deep in namespace hierarchy? r += -name.count('.') * 10 r += max(-index / 100, -100) return r def relevance_value(a): return relevance(a, *cache[a]) found.sort(key=relevance_value) # Pretty-print s = "Search results for '%s'" % (' '.join(whats)) help_text = [s, "-" * len(s)] for name in found[::-1]: doc, kind, ix = cache[name] doclines = [ line.strip() for line in doc.strip().split("\n") if line.strip() ] # find a suitable short description try: first_doc = doclines[0].strip() if _function_signature_re.search(first_doc): first_doc = doclines[1].strip() except IndexError: first_doc = "" help_text.append("%s\n %s" % (name, first_doc)) if not found: help_text.append("Nothing found.") # Output if output is not None: output.write("\n".join(help_text)) elif len(help_text) > 10: pager = pydoc.getpager() pager("\n".join(help_text)) else: print("\n".join(help_text))
def lookfor(what, module=None, import_modules=True, regenerate=False, output=None): """ Do a keyword search on docstrings. A list of of objects that matched the search is displayed, sorted by relevance. All given keywords need to be found in the docstring for it to be returned as a result, but the order does not matter. Parameters ---------- what : str String containing words to look for. module : str or list, optional Name of module(s) whose docstrings to go through. import_modules : bool, optional Whether to import sub-modules in packages. Default is True. regenerate : bool, optional Whether to re-generate the docstring cache. Default is False. output : file-like, optional File-like object to write the output to. If omitted, use a pager. See Also -------- source, info Notes ----- Relevance is determined only roughly, by checking if the keywords occur in the function name, at the start of a docstring, etc. Examples -------- >>> np.lookfor('binary representation') Search results for 'binary representation' ------------------------------------------ numpy.binary_repr Return the binary representation of the input number as a string. numpy.core.setup_common.long_double_representation Given a binary dump as given by GNU od -b, look for long double numpy.base_repr Return a string representation of a number in the given base system. ... """ import pydoc # Cache cache = _lookfor_generate_cache(module, import_modules, regenerate) # Search # XXX: maybe using a real stemming search engine would be better? found = [] whats = str(what).lower().split() if not whats: return for name, (docstring, kind, index) in cache.iteritems(): if kind in ('module', 'object'): # don't show modules or objects continue ok = True doc = docstring.lower() for w in whats: if w not in doc: ok = False break if ok: found.append(name) # Relevance sort # XXX: this is full Harrison-Stetson heuristics now, # XXX: it probably could be improved kind_relevance = {'func': 1000, 'class': 1000, 'module': -1000, 'object': -1000} def relevance(name, docstr, kind, index): r = 0 # do the keywords occur within the start of the docstring? first_doc = "\n".join(docstr.lower().strip().split("\n")[:3]) r += sum([200 for w in whats if w in first_doc]) # do the keywords occur in the function name? r += sum([30 for w in whats if w in name]) # is the full name long? r += -len(name) * 5 # is the object of bad type? r += kind_relevance.get(kind, -1000) # is the object deep in namespace hierarchy? r += -name.count('.') * 10 r += max(-index / 100, -100) return r def relevance_value(a): return relevance(a, *cache[a]) found.sort(key=relevance_value) # Pretty-print s = "Search results for '%s'" % (' '.join(whats)) help_text = [s, "-"*len(s)] for name in found[::-1]: doc, kind, ix = cache[name] doclines = [line.strip() for line in doc.strip().split("\n") if line.strip()] # find a suitable short description try: first_doc = doclines[0].strip() if _function_signature_re.search(first_doc): first_doc = doclines[1].strip() except IndexError: first_doc = "" help_text.append("%s\n %s" % (name, first_doc)) if not found: help_text.append("Nothing found.") # Output if output is not None: output.write("\n".join(help_text)) elif len(help_text) > 10: pager = pydoc.getpager() pager("\n".join(help_text)) else: print "\n".join(help_text)
def lookfor(what, module=None, import_modules=True, regenerate=False): """ Do a keyword search on docstrings. A list of of objects that matched the search is displayed, sorted by relevance. Parameters ---------- what : str String containing words to look for. module : str, module Module whose docstrings to go through. import_modules : bool Whether to import sub-modules in packages. Will import only modules in ``__all__``. regenerate : bool Whether to re-generate the docstring cache. Examples -------- >>> np.lookfor('binary representation') Search results for 'binary representation' ------------------------------------------ numpy.binary_repr Return the binary representation of the input number as a string. """ import pydoc # Cache cache = _lookfor_generate_cache(module, import_modules, regenerate) # Search # XXX: maybe using a real stemming search engine would be better? found = [] whats = str(what).lower().split() if not whats: return for name, (docstring, kind, index) in cache.iteritems(): if kind in ('module', 'object'): # don't show modules or objects continue ok = True doc = docstring.lower() for w in whats: if w not in doc: ok = False break if ok: found.append(name) # Relevance sort # XXX: this is full Harrison-Stetson heuristics now, # XXX: it probably could be improved kind_relevance = {'func': 1000, 'class': 1000, 'module': -1000, 'object': -1000} def relevance(name, docstr, kind, index): r = 0 # do the keywords occur within the start of the docstring? first_doc = "\n".join(docstr.lower().strip().split("\n")[:3]) r += sum([200 for w in whats if w in first_doc]) # do the keywords occur in the function name? r += sum([30 for w in whats if w in name]) # is the full name long? r += -len(name) * 5 # is the object of bad type? r += kind_relevance.get(kind, -1000) # is the object deep in namespace hierarchy? r += -name.count('.') * 10 r += max(-index / 100, -100) return r def relevance_sort(a, b): dr = relevance(b, *cache[b]) - relevance(a, *cache[a]) if dr != 0: return dr else: return cmp(a, b) found.sort(relevance_sort) # Pretty-print s = "Search results for '%s'" % (' '.join(whats)) help_text = [s, "-"*len(s)] for name in found: doc, kind, ix = cache[name] doclines = [line.strip() for line in doc.strip().split("\n") if line.strip()] # find a suitable short description try: first_doc = doclines[0].strip() if _function_signature_re.search(first_doc): first_doc = doclines[1].strip() except IndexError: first_doc = "" help_text.append("%s\n %s" % (name, first_doc)) # Output if len(help_text) > 10: pager = pydoc.getpager() pager("\n".join(help_text)) else: print "\n".join(help_text)
def lookfor_f(self, what, modules=None, import_modules=True, regenerate=False): """ Search for objects whose documentation contains all given words. Shows a summary of matching objects, sorted roughly by relevance. Parameters ---------- what : str String containing words to look for. module : str, module Module whose docstrings to go through. import_modules : bool Whether to import sub-modules in packages. Will import only modules in __all__ regenerate: bool Re-generate the docstring cache """ # Cache cache = {} for module in modules: try: c = self._lookfor_generate_cache(module, import_modules, regenerate) cache.update(c) except ImportError: pass # Search # XXX: maybe using a real stemming search engine would be better? found = [] whats = str(what).lower().split() if not whats: return for name, (docstring, kind, index) in cache.items(): if kind in ('module', 'object'): # don't show modules or objects continue ok = True doc = docstring.lower() for w in whats: if w not in doc: ok = False break if ok: found.append(name) # Relevance sort # XXX: this is full Harrison-Stetson heuristics now, # XXX: it probably could be improved kind_relevance = {'func': 1000, 'class': 1000, 'module': -1000, 'object': -1000} def relevance(name, docstr, kind, index): r = 0 # do the keywords occur within the start of the docstring? first_doc = "\n".join(docstr.lower().strip().split("\n")[:3]) r += sum([200 for w in whats if w in first_doc]) # do the keywords occur in the function name? r += sum([30 for w in whats if w in name]) # is the full name long? r += -len(name) * 5 # is the object of bad type? r += kind_relevance.get(kind, -1000) # is the object deep in namespace hierarchy? r += -name.count('.') * 10 r += max(-index / 100, -100) return r def relevance_sort(a, b): dr = relevance(b, *cache[b]) - relevance(a, *cache[a]) if dr != 0: return dr else: return (a > b) - (a < b) # sort found according to relevance for i in range(len(found)): for j in range(i): if (relevance_sort(found[j],found[j+1]) > 0): tmp = found[j] found[j] = found[j+1] found[j+1] = tmp # Pretty-print s = "Search results for '%s'" % (' '.join(whats)) help_text = [s, "-"*len(s)] for name in found: doc, kind, ix = cache[name] doclines = [line.strip() for line in doc.strip().split("\n") if line.strip()] # find a suitable short description try: first_doc = doclines[0].strip() if _function_signature_re.search(first_doc): first_doc = doclines[1].strip() except IndexError: first_doc = "" help_text.append("%s\n %s" % (name, first_doc)) # Output if len(help_text) > 10: pager = pydoc.getpager() pager("\n".join(help_text)) else: print("\n".join(help_text))
def lookfor(what, module=None, import_modules=True, regenerate=False): """ Do a keyword search on docstrings. A list of of objects that matched the search is displayed, sorted by relevance. Parameters ---------- what : str String containing words to look for. module : str, module Module whose docstrings to go through. import_modules : bool Whether to import sub-modules in packages. Will import only modules in ``__all__``. regenerate : bool Whether to re-generate the docstring cache. Examples -------- >>> np.lookfor('binary representation') Search results for 'binary representation' ------------------------------------------ numpy.binary_repr Return the binary representation of the input number as a string. """ import pydoc # Cache cache = _lookfor_generate_cache(module, import_modules, regenerate) # Search # XXX: maybe using a real stemming search engine would be better? found = [] whats = str(what).lower().split() if not whats: return for name, (docstring, kind, index) in cache.iteritems(): if kind in ('module', 'object'): # don't show modules or objects continue ok = True doc = docstring.lower() for w in whats: if w not in doc: ok = False break if ok: found.append(name) # Relevance sort # XXX: this is full Harrison-Stetson heuristics now, # XXX: it probably could be improved kind_relevance = { 'func': 1000, 'class': 1000, 'module': -1000, 'object': -1000 } def relevance(name, docstr, kind, index): r = 0 # do the keywords occur within the start of the docstring? first_doc = "\n".join(docstr.lower().strip().split("\n")[:3]) r += sum([200 for w in whats if w in first_doc]) # do the keywords occur in the function name? r += sum([30 for w in whats if w in name]) # is the full name long? r += -len(name) * 5 # is the object of bad type? r += kind_relevance.get(kind, -1000) # is the object deep in namespace hierarchy? r += -name.count('.') * 10 r += max(-index / 100, -100) return r def relevance_sort(a, b): dr = relevance(b, *cache[b]) - relevance(a, *cache[a]) if dr != 0: return dr else: return cmp(a, b) found.sort(relevance_sort) # Pretty-print s = "Search results for '%s'" % (' '.join(whats)) help_text = [s, "-" * len(s)] for name in found: doc, kind, ix = cache[name] doclines = [ line.strip() for line in doc.strip().split("\n") if line.strip() ] # find a suitable short description try: first_doc = doclines[0].strip() if _function_signature_re.search(first_doc): first_doc = doclines[1].strip() except IndexError: first_doc = "" help_text.append("%s\n %s" % (name, first_doc)) # Output if len(help_text) > 10: pager = pydoc.getpager() pager("\n".join(help_text)) else: print "\n".join(help_text)
def update_event(self, inp=-1): self.set_output_val(0, pydoc.getpager())
import itertools import sre_constants import webbrowser import subprocess import traceback from code import InteractiveConsole from operator import itemgetter from os.path import abspath, dirname, join import logbook from clint.textui import puts, indent, colored from clint.textui.colored import red, green, cyan, magenta, yellow logger = logbook.Logger('batshell') HERE = dirname(abspath(__file__)) pager = pydoc.getpager() try: subprocess.check_call("echo 'test' | xsel -pi", shell=True) except subprocess.CalledProcessError: xsel_enabled = False logger.warning(u'✄ Proceeding without xsel ☹') logger.info('Please install xsel to automatically ' 'copy tested regexes to the clipboard.') else: xsel_enabled = True logger.info(u'✄ xsel is enabled! ☺') def command(*aliases, **kwargs): def decorator(f):
def lookfor_f(self, what, modules=None, import_modules=True, regenerate=False): """ Search for objects whose documentation contains all given words. Shows a summary of matching objects, sorted roughly by relevance. Parameters ---------- what : str String containing words to look for. module : str, module Module whose docstrings to go through. import_modules : bool Whether to import sub-modules in packages. Will import only modules in __all__ regenerate: bool Re-generate the docstring cache """ # Cache cache = {} for module in modules: try: c = self._lookfor_generate_cache(module, import_modules, regenerate) cache.update(c) except ImportError: pass # Search # XXX: maybe using a real stemming search engine would be better? found = [] whats = str(what).lower().split() if not whats: return for name, (docstring, kind, index) in cache.items(): if kind in ('module', 'object'): # don't show modules or objects continue ok = True doc = docstring.lower() for w in whats: if w not in doc: ok = False break if ok: found.append(name) # Relevance sort # XXX: this is full Harrison-Stetson heuristics now, # XXX: it probably could be improved kind_relevance = { 'func': 1000, 'class': 1000, 'module': -1000, 'object': -1000 } def relevance(name, docstr, kind, index): r = 0 # do the keywords occur within the start of the docstring? first_doc = "\n".join(docstr.lower().strip().split("\n")[:3]) r += sum([200 for w in whats if w in first_doc]) # do the keywords occur in the function name? r += sum([30 for w in whats if w in name]) # is the full name long? r += -len(name) * 5 # is the object of bad type? r += kind_relevance.get(kind, -1000) # is the object deep in namespace hierarchy? r += -name.count('.') * 10 r += max(-index / 100, -100) return r def relevance_sort(a, b): dr = relevance(b, *cache[b]) - relevance(a, *cache[a]) if dr != 0: return dr else: return (a > b) - (a < b) # sort found according to relevance for i in range(len(found)): for j in range(i): if (relevance_sort(found[j], found[j + 1]) > 0): tmp = found[j] found[j] = found[j + 1] found[j + 1] = tmp # Pretty-print s = "Search results for '%s'" % (' '.join(whats)) help_text = [s, "-" * len(s)] for name in found: doc, kind, ix = cache[name] doclines = [ line.strip() for line in doc.strip().split("\n") if line.strip() ] # find a suitable short description try: first_doc = doclines[0].strip() if _function_signature_re.search(first_doc): first_doc = doclines[1].strip() except IndexError: first_doc = "" help_text.append("%s\n %s" % (name, first_doc)) # Output if len(help_text) > 10: pager = pydoc.getpager() pager("\n".join(help_text)) else: print("\n".join(help_text))
import site site._Helper = _Helper # This will be used for paging by IPython help. def IPython_pager(self, text, start=0, screen_lines=0): if isinstance(text, Mapping): text = text['text/plain'] terminal.page(text) # This will be used for paging by pydoc help. import pydoc def pydoc_pager(text): terminal.page(pydoc.plain(text)) pydoc.getpager() # this call creates the global variable pydoc.pager pydoc.pager = pydoc_pager # This sets the "system menu" icon in the title bar to be the SnapPy # icon (in Windows and ??KDE??) def set_icon(window): if sys.platform == 'win32': try: import snappy ico = os.path.join(os.path.dirname(snappy.__file__), 'SnapPy.ico') window.iconbitmap(default=ico) except: pass if sys.platform == 'darwin': if not sys.executable.endswith('SnapPy.app/Contents/MacOS/python'):
def get_cmdline(loneclass, rootdir, luser, lhost, args=sys.argv): """ Parse the command line returning the verb, pk and YAML @type loneclass: C{laf.lone_interface.Lone} @param loneclass: Lone for which we are parsing the command line. @rtype: dict @return: A dictionary of the 'verb', 'pk' and 'yaml' found on the command line. """ lonename = loneclass.format_name() rest = args[1:] # Get framework level options (fw_options, rest) = _get_framework_opts(lonename, rest) ############################# ## # If --status then len(rest) is not checked and verb = get # and immediately return ###################### if 'status' in fw_options: return {'verb': 'get', 'pk': None, 'input': None, 'options': fw_options, 'path': None, 'body': None} if len(rest) < 1: # No verb were given on the command line, no need to go further raise UsageException(lonename, reason='usage <verb> <pk>') # The verb is always the first argument verb = rest[0] # Help shortcut: get the documentation from the lone if verb == 'help': import pydoc # pylint: disable=C0415 pydoc.getpager()(loneclass.help()) sys.exit(0) # Read provided data from STDIN if it is NOT a TTY stdin_input = _get_yaml_from_stdin(lonename, verb) # If we received an error on STDIN, do not do input merging if isinstance(stdin_input, dict) and '_error' in stdin_input: raise UsageException(lonename, reason=stdin_input) # Get the lone's CLI configuration lone_config = _get_lone_cli_conf(lonename, rootdir) # Look in the lone configuration for verbs' default input # (this is hardcoded for 'get' and 'delete') verbs_default_input = lone_config.get('default_input', {}) verbs_default_input.update({'get': {}, 'delete': {}}) if verb not in HTTP_VERBS: verbs_default_input.update({verb: {}}) default_input = verbs_default_input.get(verb, None) body = None # Get input from getopt style options (getopt_input, rest) = _get_yaml_from_getopt(lonename, verb, lone_config, rest) # From the rest, extract the YAML given on the command line try: (yaml_input, rest) = _get_yaml_from_cmdline(rest) except Exception as err: raise UsageException( lonename, reason='Error parsing command line YAML:\n%s' % repr(err), verb=verb) body = yaml_input # We are expecting rest to be either 'verb' or 'verb pk' if len(rest) == 0: # pylint: disable=R1720 # Something wrong happened raise UsageException( lonename, reason='Error parsing command line: %s' % rest, verb=verb) elif len(rest) == 1: pk = None path = None elif len(rest) == 2: # We have a primary key pk_info = _get_pk_path(rest[1]) if not pk_info: raise UsageException( lonename, reason="Unparseable primary key: '%s'" % (rest[1]), verb=verb) # Apply input expansion pk[some/xpath] pk = pk_info['pk'] path = pk_info['path'] if path: getopt_input = _expand_path(path, getopt_input) yaml_input = _expand_path(path, yaml_input) else: # We remain with more than two items, # Something went wrong parsing options raise UsageException( lonename, reason="Unrecognized elements on the command line: '{0}'".format( rest[2:]), verb=verb, pk=rest[1]) # We now have all input sources covered, merge them all into one if verb in HTTP_VERBS: try: obj = _merge_inputs([default_input, stdin_input, getopt_input, yaml_input]) except Exception as ex: raise UsageException( lonename, reason='Error merging inputs: {0}\n{1}'.format( repr(ex), traceback.format_exc()), verb=verb, pk=pk) else: if body: obj = [body] else: return { 'verb': verb, 'pk': pk, 'input': None, 'options': fw_options, 'path': path, 'body': body } # Check now if we need to go into interactive mode # If obj is None of if we do not have a pk given on the command line if (obj is None) or (pk is '-' and isinstance( # pylint: disable=R0123 obj[0], dict) and '_id' not in obj[0]): if cmdutils.is_body_required(rootdir, pk, verb, lonename, path, obj, luser, lhost): msg = ( """Enter YAML input and type """ """Ctrl-D (i.e. EOF) to submit:\n\n""" ) stdin_input = _get_yaml_from_stdin( lonename, verb, message=msg, ask_tty=True) # If we received an error on STDIN, do not do input merging if isinstance(stdin_input, dict) and '_error' in stdin_input: raise UsageException(lonename, reason=stdin_input) # Re-merge one more time with the new STDIN input obj = _merge_inputs([default_input, stdin_input, getopt_input, yaml_input]) # TODO: Sanity checks from those ? return { 'verb': verb, 'pk': pk, 'input': obj, 'options': fw_options, 'path': path, 'body': body }