def __call__(self): w = self.shell.stdout.write console = Console(self.shell.pty) def run(): # Print nodes if Inspector(self.node).get_childnodes(): yield 'Child nodes:' def column_iterator(): for c in Inspector(self.node).get_childnodes(): name = Inspector(c).get_name() yield termcolor.colored(name, type_of_node(c).color), len(name) for line in console.in_columns(column_iterator()): yield line # Print actions if Inspector(self.node).get_actions(): yield 'Actions:' def column_iterator(): for a in Inspector(self.node).get_actions(): yield termcolor.colored(a.name, type_of_action(a).color), len(a.name) for line in console.in_columns(column_iterator()): yield line console.lesspipe(run())
def __call__(self): w = self.shell.stdout.write console = Console(self.shell.pty) def run(): # Print nodes if Inspector(self.node).get_childnodes(): yield 'Child nodes:' def column_iterator(): for c in Inspector(self.node).get_childnodes(): name = Inspector(c).get_name() yield termcolor.colored( name, type_of_node(c).color), len(name) for line in console.in_columns(column_iterator()): yield line # Print actions if Inspector(self.node).get_actions(): yield 'Actions:' def column_iterator(): for a in Inspector(self.node).get_actions(): yield termcolor.colored(a.name, type_of_action(a).color), len( a.name) for line in console.in_columns(column_iterator()): yield line console.lesspipe(run())
def _inspect_query_attribute(self): console = Console(self.shell.pty) query = Inspector(self.node).get_query(self.attr_name).query def run(): yield termcolor.colored(' Node: ', 'cyan') + \ termcolor.colored(Inspector(self.node).get_full_name(), 'yellow') yield termcolor.colored(' Filename: ', 'cyan') + \ termcolor.colored(query._filename, 'yellow') yield termcolor.colored(' Line: ', 'cyan') + \ termcolor.colored(query._line, 'yellow') yield termcolor.colored(' Expression: ', 'cyan') + \ termcolor.colored(repr(query.query), 'yellow') yield '' # Execute query in sandboxed environment. yield 'Trace query:' try: insp = Inspector(self._get_env()) result = insp.trace_query(self.attr_name) except Exception as e: yield 'Failed to execute query: %r' % e return # Print query and all subqueries with their results. for subquery in result.walk_through_subqueries(): yield termcolor.colored(repr(subquery[0]), 'cyan') yield ' %s' % subquery[1] console.lesspipe(run())
def _inspect_action(self): console = Console(self.shell.pty) action = Inspector(self.node).get_action(self.attr_name) def run(): yield termcolor.colored(' Action name: ', 'cyan') + \ termcolor.colored(self.attr_name, 'yellow') yield termcolor.colored(' __repr__: ', 'cyan') + \ termcolor.colored(repr(action._func), 'yellow') yield termcolor.colored(' Node: ', 'cyan') + \ termcolor.colored(repr(self.node), 'yellow') console.lesspipe(run())
def __call__(self): host = get_host_func(self.shell) files = host.listdir() def iterator(): for f in files: if host.stat(f).is_dir: yield colored(f, DirectoryType.color), len(f) else: yield f, len(f) console = Console(self.shell.pty) console.lesspipe(console.in_columns(iterator()))
def lview(shell, path): """ View local file. """ console = Console(shell.pty) with shell.localhost.open(path, 'r') as f: def reader(): while True: line = f.readline() if line: yield line.rstrip('\n') else: return # EOF console.lesspipe(reader())
def print_all_completions(self, all_completions): self.stdout.write('\r\n') # Create an iterator which yields all the comments (in their color), # and pass it through in_columns/lesspipe def column_items(): for w in all_completions: handler_type = w[1].handler_type text = '%s%s' % (termcolor.colored(w[0], handler_type.color), termcolor.colored(handler_type.postfix, handler_type.postfix_color)) length = len(w[0]) + len(handler_type.postfix) yield text, length c = Console(self.pty) c.lesspipe(c.in_columns(column_items()))
def print_all_completions(self, all_completions): self.stdout.write('\r\n') # Create an iterator which yields all the comments (in their color), # and pass it through in_columns/lesspipe def column_items(): for w in all_completions: handler_type = w[1].handler_type text = '%s%s' % ( termcolor.colored(w[0], handler_type.color), termcolor.colored(handler_type.postfix, handler_type.postfix_color)) length = len(w[0]) + len(handler_type.postfix) yield text, length c = Console(self.pty) c.lesspipe(c.in_columns(column_items()))
def _inspect_property_attribute(self): console = Console(self.shell.pty) action = Inspector(self.node).get_property(self.attr_name) def run(): yield termcolor.colored(' Property name: ', 'cyan') + \ termcolor.colored(self.attr_name, 'yellow') yield termcolor.colored(' __repr__: ', 'cyan') + \ termcolor.colored(repr(action._func), 'yellow') yield termcolor.colored(' Node: ', 'cyan') + \ termcolor.colored(repr(self.node), 'yellow') # Value try: value = getattr(self._get_env(), self.attr_name) yield termcolor.colored(' Value: ', 'cyan') + \ termcolor.colored(repr(value), 'yellow') except Exception as e: yield termcolor.colored(' Value: ', 'cyan') + \ termcolor.colored('Failed to evaluate value...', 'yellow') console.lesspipe(run())
def _inspect_node(self): console = Console(self.shell.pty) def inspect(): # Print full name yield termcolor.colored(' Node: ', 'cyan') + \ termcolor.colored(Inspector(self.node).get_full_name(), 'yellow') # Print mro yield termcolor.colored(' Mro:', 'cyan') i = 1 for m in self.node.__class__.__mro__: if m.__module__ != 'deployer.node' and m != object: yield termcolor.colored(' %i ' % i, 'cyan') + \ termcolor.colored('%s.' % m.__module__, 'red') + \ termcolor.colored('%s' % m.__name__, 'yellow') i += 1 # File names yield termcolor.colored(' Files:', 'cyan') i = 1 for m in self.node.__class__.__mro__: if m.__module__ != 'deployer.node' and m != object: yield termcolor.colored(' %i ' % i, 'cyan') + \ termcolor.colored(getfile(m), 'red') i += 1 # Print host mappings yield termcolor.colored(' Hosts:', 'cyan') for role in sorted(self.node.hosts._hosts.keys()): items = self.node.hosts._hosts[role] yield termcolor.colored(' "%s"' % role, 'yellow') i = 1 for host in sorted(items, key=lambda h: h.slug): yield termcolor.colored(' %3i ' % i, 'cyan') + \ termcolor.colored('%-25s (%s)' % (host.slug, getattr(host, 'address', '')), 'red') i += 1 # Print the first docstring (look to the parents) for m in self.node.__class__.__mro__: if m.__module__ != 'deployer.node' and m != object and m.__doc__: yield termcolor.colored(' Docstring:\n', 'cyan') + \ termcolor.colored(m.__doc__ or '<None>', 'red') break # Actions yield termcolor.colored(' Actions:', 'cyan') def item_iterator(): for a in Inspector(self.node).get_actions(): yield termcolor.colored(a.name, 'red'), len(a.name) for line in console.in_columns(item_iterator(), margin_left=13): yield line # Nodes yield termcolor.colored(' Sub nodes:', 'cyan') # Group by node group grouper = lambda c: Inspector(c).get_group() for group, nodes in groupby( sorted(Inspector(self.node).get_childnodes(), key=grouper), grouper): yield termcolor.colored(' "%s"' % group.name, 'yellow') # Create iterator for all the items in this group def item_iterator(): for n in nodes: name = Inspector(n).get_name() if n.parent == self.node: text = termcolor.colored(name, type_of_node(n).color) length = len(name) else: full_name = Inspector(n).get_full_name() text = termcolor.colored( '%s -> %s' % (name, full_name), type_of_node(n).color) length = len('%s -> %s' % (name, full_name)) yield text, length # Show in columns for line in console.in_columns(item_iterator(), margin_left=13): yield line console.lesspipe(inspect())
def __call__(self): files = self.shell.localhost.listdir() console = Console(self.shell.pty) console.lesspipe(console.in_columns(files))
def _inspect_node(self): console = Console(self.shell.pty) def inspect(): # Print full name yield termcolor.colored(' Node: ', 'cyan') + \ termcolor.colored(Inspector(self.node).get_full_name(), 'yellow') # Print mro yield termcolor.colored(' Mro:', 'cyan') i = 1 for m in self.node.__class__.__mro__: if m.__module__ != 'deployer.node' and m != object: yield termcolor.colored(' %i ' % i, 'cyan') + \ termcolor.colored('%s.' % m.__module__, 'red') + \ termcolor.colored('%s' % m.__name__, 'yellow') i += 1 # File names yield termcolor.colored(' Files:', 'cyan') i = 1 for m in self.node.__class__.__mro__: if m.__module__ != 'deployer.node' and m != object: yield termcolor.colored(' %i ' % i, 'cyan') + \ termcolor.colored(getfile(m), 'red') i += 1 # Print host mappings yield termcolor.colored(' Hosts:', 'cyan') for role in sorted(self.node.hosts._hosts.keys()): items = self.node.hosts._hosts[role] yield termcolor.colored(' "%s"' % role, 'yellow') i = 1 for host in sorted(items, key=lambda h:h.slug): yield termcolor.colored(' %3i ' % i, 'cyan') + \ termcolor.colored('%-25s (%s)' % (host.slug, getattr(host, 'address', '')), 'red') i += 1 # Print the first docstring (look to the parents) for m in self.node.__class__.__mro__: if m.__module__ != 'deployer.node' and m != object and m.__doc__: yield termcolor.colored(' Docstring:\n', 'cyan') + \ termcolor.colored(m.__doc__ or '<None>', 'red') break # Actions yield termcolor.colored(' Actions:', 'cyan') def item_iterator(): for a in Inspector(self.node).get_actions(): yield termcolor.colored(a.name, 'red'), len(a.name) for line in console.in_columns(item_iterator(), margin_left=13): yield line # Nodes yield termcolor.colored(' Sub nodes:', 'cyan') # Group by node group grouper = lambda c:Inspector(c).get_group() for group, nodes in groupby(sorted(Inspector(self.node).get_childnodes(), key=grouper), grouper): yield termcolor.colored(' "%s"' % group.name, 'yellow') # Create iterator for all the items in this group def item_iterator(): for n in nodes: name = Inspector(n).get_name() if n.parent == self.node: text = termcolor.colored(name, type_of_node(n).color) length = len(name) else: full_name = Inspector(n).get_full_name() text = termcolor.colored('%s -> %s' % (name, full_name), type_of_node(n).color) length = len('%s -> %s' % (name, full_name)) yield text, length # Show in columns for line in console.in_columns(item_iterator(), margin_left=13): yield line console.lesspipe(inspect())