def menu(menu_items, description): if not menu_items: return None # Clear terminal os.system('clear') # hide cursor sys.stdout.write("\033[?25l") sys.stdout.flush() try: term = Terminal() print description focus = 0 while True: for i, line in enumerate(menu_items): with term.location(0, len(menu_items) + i): if i == focus: print term.on_blue(term.bright_white(line)), else: print line, k = get_arrow_key_or_character() if k == 'down': focus += 1 elif k == 'up': focus -= 1 elif k == '\n': break # make sure we don't go outside menu if focus < 0: focus = 0 if focus == len(menu_items): focus = len(menu_items) - 1 finally: # Clear terminal os.system('clear') # show cursor again sys.stdout.write("\033[?25h") sys.stdout.flush() return menu_items[focus]
def menu(menu_items): if not menu_items: return None # hide cursor sys.stdout.write("\033[?25l") sys.stdout.flush() try: term = Terminal() print '\n' * (len(menu_items) - 2) focus = 0 while True: for i, line in enumerate(menu_items): with term.location(0, term.height - len(menu_items) + i): if i == focus: print term.on_red(term.bright_white(line)), else: print line, k = get_arrow_key_or_character() if k == 'down': focus += 1 elif k == 'up': focus -= 1 elif k == '\n': break # make sure we don't go outside menu if focus < 0: focus = 0 if focus == len(menu_items): focus = len(menu_items) - 1 finally: # show cursor again sys.stdout.write("\033[?25h") sys.stdout.flush() print '' # Write a newline to avoid next output writing over the last line of the menu return menu_items[focus]
class DefaultReader(object): """ Autoscaling status reader """ # FIXME: These values could be defined in forseti models COLORED_VALUES = { "OutOfService": "red", "InService": "green", "Healthy": "green", "UnHealthy": "red", "Terminating": "yellow", } def __init__(self, configuration, format=None, *args, **kwargs): self.configuration = configuration self.term = Terminal() format = format or "tree" self.formatter = self.get_formatter(format) def get_formatter(self, format): """ Get the formatter object based on a format string. 'tree' will return a `TreeFormatter` 'json' will return a `JsonFormatter` 'plain' will return a `DefaultFormatter` Any other value will raise a `ForsetiException` """ if format == "tree": return TreeFormatter() if format == "json": return JsonFormatter() if format == "plain": return DefaultFormatter() raise ForsetiException("Unknown format %s" % format) def _color_value(self, value): """ Add color to a value of a autoscale group status dictionary. If the value is defined in `COLORED_VALUES` then the color is added. """ if value in self.COLORED_VALUES.keys(): color = self.COLORED_VALUES[value] return getattr(self.term, color) + value + self.term.normal return value def _color_dictionary_list(self, array): """ Add color to a list of dictionaries. Each key is coloured in blue and each value is colored using `_color_value` """ coloured_list = [] for content in array: coloured_content = {} for key, value in content.iteritems(): key = "{t.blue}{0}{t.normal}".format(key, t=self.term) coloured_content[key] = self._color_value(value) coloured_list.append(coloured_content) return coloured_list def _color_status(self, status): """ Color the autoscale group `status` by setting a blue color to the keys and calling `_color_value` on each value. A new dictionary is returned. """ coloured_status = {} for key, value in status.iteritems(): if isinstance(value, list): value = self._color_dictionary_list(value) key = "{t.blue}{0}{t.normal}".format(key, t=self.term) coloured_status[key] = self._color_value(value) return coloured_status def _get_updated_status(self, group, max_activities): """ Updates the autoscale group status and limit the activities to `max_activities` """ status = group.status() status["Activities"] = status["Activities"][:max_activities] return status def _print_status(self, group, max_activities, color=True): """ Print the status and the current date. If `color` is `True` the output will be coloured by `_color_status` """ status = self._get_updated_status(group, max_activities) if color: status = self._color_status(status) print self.formatter.display(status) print self.term.bright_white(datetime.today().strftime("%Y-%m-%d %H:%M:%S")) def status(self, application, daemon=False, activities=3, *args, **kwargs): """ Print the current status of the autoscale group of an application. """ # activities can be read from args and must be converted. max_activities = int(activities) if activities else 3 group = EC2AutoScaleGroup( self.configuration.get_autoscale_group(application), application, self.configuration.get_autoscale_group_configuration(application), ) use_color = self.formatter.use_color if not daemon: return self._print_status(group, max_activities, color=use_color) # Daemon case will print the status in a fullscreen terminal forever running = True while running: try: with self.term.fullscreen(): self._print_status(group, max_activities, color=use_color) time.sleep(1) except (KeyboardInterrupt, SystemExit): running = False finally: self.term.exit_fullscreen() def list_autoscale_configurations(self, application): """ List all the launch configurations of the autoscaling group belonging to the application """ group = EC2AutoScaleGroup( self.configuration.get_autoscale_group(application), application, self.configuration.get_autoscale_group_configuration(application), ) configurations = group.get_all_launch_configurations() for configuration in configurations: ami = configuration.ami() print "- %s " % configuration.name print "\t- AMI: %s " % (ami.ami_id if ami.ami_id else "Unknown") print "\t- Snapshot: %s " % (ami.snapshot_id if ami.snapshot_id else "Unknown")
class DefaultReader(object): """ Autoscaling status reader """ # FIXME: These values could be defined in forseti models COLORED_VALUES = { 'OutOfService': 'red', 'InService': 'green', 'Healthy': 'green', 'UnHealthy': 'red', 'Terminating': 'yellow', } def __init__(self, configuration, format=None, *args, **kwargs): self.configuration = configuration self.term = Terminal() format = format or 'tree' self.formatter = self.get_formatter(format) def get_formatter(self, format): """ Get the formatter object based on a format string. 'tree' will return a `TreeFormatter` 'json' will return a `JsonFormatter` 'plain' will return a `DefaultFormatter` Any other value will raise a `ForsetiException` """ if format == 'tree': return TreeFormatter() if format == 'json': return JsonFormatter() if format == 'plain': return DefaultFormatter() raise ForsetiException("Unknown format %s" % format) def _color_value(self, value): """ Add color to a value of a autoscale group status dictionary. If the value is defined in `COLORED_VALUES` then the color is added. """ if value in self.COLORED_VALUES.keys(): color = self.COLORED_VALUES[value] return getattr(self.term, color) + value + self.term.normal return value def _color_dictionary_list(self, array): """ Add color to a list of dictionaries. Each key is coloured in blue and each value is colored using `_color_value` """ coloured_list = [] for content in array: coloured_content = {} for key, value in content.iteritems(): key = '{t.blue}{0}{t.normal}'.format(key, t=self.term) coloured_content[key] = self._color_value(value) coloured_list.append(coloured_content) return coloured_list def _color_status(self, status): """ Color the autoscale group `status` by setting a blue color to the keys and calling `_color_value` on each value. A new dictionary is returned. """ coloured_status = {} for key, value in status.iteritems(): if isinstance(value, list): value = self._color_dictionary_list(value) key = '{t.blue}{0}{t.normal}'.format(key, t=self.term) coloured_status[key] = self._color_value(value) return coloured_status def _get_updated_status(self, group, max_activities): """ Updates the autoscale group status and limit the activities to `max_activities` """ status = group.status() status['Activities'] = status['Activities'][:max_activities] return status def _print_status(self, group, max_activities, color=True): """ Print the status and the current date. If `color` is `True` the output will be coloured by `_color_status` """ status = self._get_updated_status(group, max_activities) if color: status = self._color_status(status) print self.formatter.display(status) print self.term.bright_white( datetime.today().strftime("%Y-%m-%d %H:%M:%S")) def status(self, application, daemon=False, activities=3, *args, **kwargs): """ Print the current status of the autoscale group of an application. """ # activities can be read from args and must be converted. max_activities = int(activities) if activities else 3 group = EC2AutoScaleGroup( self.configuration.get_autoscale_group(application), application, self.configuration.get_autoscale_group_configuration(application)) use_color = self.formatter.use_color if not daemon: return self._print_status(group, max_activities, color=use_color) # Daemon case will print the status in a fullscreen terminal forever running = True while running: try: with self.term.fullscreen(): self._print_status(group, max_activities, color=use_color) time.sleep(1) except (KeyboardInterrupt, SystemExit): running = False finally: self.term.exit_fullscreen() def list_autoscale_configurations(self, application): """ List all the launch configurations of the autoscaling group belonging to the application """ group = EC2AutoScaleGroup( self.configuration.get_autoscale_group(application), application, self.configuration.get_autoscale_group_configuration(application)) configurations = group.get_all_launch_configurations() for configuration in configurations: ami = configuration.ami() print "- %s " % configuration.name print "\t- AMI: %s " % (ami.ami_id if ami.ami_id else "Unknown") print "\t- Snapshot: %s " % (ami.snapshot_id if ami.snapshot_id else "Unknown")