def get_analyzer_config(ctx: ClickContext, re_match: str, as_json: bool, as_text: bool): console = Console() ctx.obj.logger.info("Requesting [italic blue]analyzer_config.json[/]..") try: res = ctx.obj.get_analyzer_configs() # filter resulset if a regex pattern was provided if re_match: pat = re.compile(re_match) res = {k: v for k, v in res.items() if pat.match(k) is not None} except IntelOwlClientException as e: ctx.obj.logger.fatal(str(e)) ctx.exit(0) if as_json: with console.pager(styles=True): console.print(json.dumps(res, indent=4)) elif as_text: click.echo(", ".join(res.keys())) else: # otherwise, print full table headers = [ "Name", "Type", "Description", "Supported\nTypes", "External\nService", "Leaks\nInfo", "Requires\nConfig", "Additional\nConfig\nParams", ] header_style = "bold blue" table = Table( show_header=True, title="Analyzer Configurations", box=box.DOUBLE_EDGE, show_lines=True, ) for h in headers: table.add_column(h, header_style=header_style, justify="center") for name, obj in res.items(): table.add_row( name, obj["type"], obj.get("description", ""), get_json_syntax( obj.get( "observable_supported", obj.get("supported_filetypes", []), )), get_success_text(obj.get("external_service", False)), get_success_text(obj.get("leaks_info", False)), get_success_text(obj.get("requires_configuration", False)), get_json_syntax(obj.get("additional_config_params", {})), ) with console.pager(styles=True): console.print(table)
def run(self): from rich.console import Console try: all_experiments = self.repo.experiments.show( all_branches=self.args.all_branches, all_tags=self.args.all_tags, all_commits=self.args.all_commits, sha_only=self.args.sha, num=self.args.num, param_deps=self.args.param_deps, ) if self.args.show_json: import json logger.info(json.dumps(all_experiments, default=_format_json)) return 0 if self.args.precision is None: precision = DEFAULT_PRECISION else: precision = self.args.precision table = _experiments_table( all_experiments, include_metrics=self.args.include_metrics, exclude_metrics=self.args.exclude_metrics, include_params=self.args.include_params, exclude_params=self.args.exclude_params, no_timestamp=self.args.no_timestamp, sort_by=self.args.sort_by, sort_order=self.args.sort_order, precision=precision, ) console = Console() if self.args.no_pager: console.print(table) else: from dvc.utils.pager import DvcPager # NOTE: rich does not have native support for unlimited width # via pager. we override rich table compression by setting # console width to the full width of the table console_options = console.options console_options.max_width = SHOW_MAX_WIDTH measurement = table.__rich_measure__(console, console_options) console._width = ( # pylint: disable=protected-access measurement.maximum) with console.pager(pager=DvcPager(), styles=True): console.print(table) except DvcException: logger.exception("failed to show experiments") return 1 return 0
def _display_single_job(data): console = Console() with console.pager(styles=True): # print job attributes attrs = _render_job_attributes(data) console.print(attrs) # construct job analysis table table = _render_job_analysis_table(data["analysis_reports"], verbose=True) console.print(table, justify="center")
def display_active_cases(data): console = Console() table = Table(title="Active Coronavirus Cases, University of Leeds") table.add_column("Period End") table.add_column("Staff", justify="right") table.add_column("Students", justify="right") table.add_column("Other", justify="right") table.add_column("Total", justify="right") for row in data: table.add_row(row[0], str(row[1]), str(row[2]), str(row[3]), str(row[4])) with console.pager(): console.print(table)
async def show(option): if not option: option = Prompt.ask( "What do you want to show?", choices=list(ShowOptions), default=ShowOptions.position, ) else: if option not in list(ShowOptions): raise NotAValidChoice("Selected option is not a valid choice") position = Position.load() if position: if option == "position": print(str(position)) elif option == "accounts": message = f"""Accounts: {"".join([f"{i + 1}) {account.position()}" for i, account in enumerate(position.accounts)])} """ print(message) elif option == "transactions": cards_lines = "".join([ f"{i + 1}) {account.transactions_menu(i + 1)}" for i, account in enumerate(position.accounts) ]) message = f"""Choose an account or card: {cards_lines} """ account_or_card_selection_choices = get_account_or_card_selection_choices( position) account_or_card_selection = Prompt.ask( message, choices=list(account_or_card_selection_choices.keys()), default=list(account_or_card_selection_choices.keys())[0], ) console = Console() with console.pager(): console.print(account_or_card_selection_choices[ account_or_card_selection])
def cli(self, runs): json_data = json.loads(self.json(*runs)) # 1. render the json_data into the terminal query_text = "" queries = json_data["queries"] docs = json_data["docs"] current_index = 0 console = Console() if len(runs) == 2: for current_index in range(len(queries)): self.cli_compare_one_query(console, queries[current_index], 0, None, docs, json_data["meta"]["run1_name"], json_data["meta"]["run2_name"]) ans = Confirm.ask("Want to see the next query?") if not ans: return else: with console.pager(): for current_index in range(len(queries)): self.cli_display_one_query(console, queries[current_index], 0, None, docs, json_data["meta"]["run1_name"])
class AbstractRichMaker(ABC): """ Abstract rich maker class """ def __init__(self, target_width, color_system, dic_style, filepath=None, file_contents=None, filetype=DIC_DEFAULT_VALUES['filetype']): """ Constructor Parameters ---------- target_width : float target_width color_system : str color system dic_style : dict style dict filepath : str filepath (default: None) file_contents : str file contents (default: None) filetype : str filetype (default: 'auto') See Also -------- richcat.richcat.interpret_style """ # Load file contents assert not (filepath is None and file_contents is None) if filepath is not None: file_contents = self._read_file(filepath) # Generate rich text self.rich_text = self._make_rich_text(file_contents, filetype, dic_style) # Instance console self.console = Console(color_system=color_system, width=self._decide_console_width( file_contents, target_width)) def print(self, use_pager): """ print rich text Parameters ---------- console : rich.console.Console console use_pager : bool flat whether use pager See Also -------- richcat.richcat.interpret_style """ if use_pager: with self.console.pager(styles=True): self.console.print(self.rich_text) else: self.console.print(self.rich_text) def _get_terminal_width(self): """ Getting terminal width method Returns ------- terminal_width : int terminal width """ try: terminal_width = int(os.popen('tput cols', 'r').read()) return terminal_width except ValueError: raise RichcatTerminalWidthGetError() def _decide_console_width(self, file_contents, target_width=DIC_DEFAULT_VALUES['width']): """ Deciding text width method Parameters ---------- file_contents : str file contents (default: 1.0) target_width : float target_width Returns ------- : int target width """ # Get terminal width terminal_width = self._get_terminal_width() # Decide target width if target_width <= DIC_DEFAULT_VALUES['width']: return int(float(terminal_width) * target_width) else: return int(target_width) @abstractmethod def _read_file(self, filepath): """ file reader method Parameters ---------- filepath : str filepath Returns ------- file_contents : str file contents """ pass @abstractmethod def _make_rich_text(self, file_contents, filetype, dic_style): """ rich maker method Paremters --------- file_contents : str file contents filetype : str filetype dic_style : dict style dict Returns ------- : str rich text See Also -------- richcat.rich_maker.AbstractRichMaker.print """ pass
def _remind(ctx, *args, **kwargs): def get_days_left(event): start = dt.datetime.now() cfp_days_left = (event.cfp_end_date - start).days event_days_left = (event.start_date - start).days if event.cfp_open and cfp_days_left >= 0: days_left = cfp_days_left elif event_days_left >= 0: days_left = event_days_left else: days_left = -1 return days_left, event.cfp_open initialize_conrad() _id = kwargs["id"] if _id is None: session = Session() reminders = list( session.query(Event, Reminder).filter(Event.id == Reminder.id).order_by( Event.start_date).all()) if len(reminders) > 0: console = Console() table = Table(show_header=True, header_style="bold magenta", show_lines=True) table.add_column("id") table.add_column("Name") table.add_column("Start Date") table.add_column("Days Left") reminders_output = [] for reminder, __ in reminders: days_left, cfp_open = get_days_left(reminder) if cfp_open and days_left >= 0: days_left_output = f"{days_left} days left to cfp deadline!" elif days_left >= 0: days_left_output = f"{days_left} days left!" else: days_left_output = "Event ended." style = "white" if days_left >= 30: style = "green" elif 30 > days_left >= 10: style = "yellow" elif 10 > days_left >= 0: style = "red" days_left_output = f"[bold][{style}]{days_left_output}[/{style}][/bold]" reminder_output = [ reminder.id, reminder.name, reminder.start_date.strftime("%Y-%m-%d"), days_left_output, ] table.add_row(*reminder_output) session.close() console_kwargs = {} if has_less(): console_kwargs["styles"] = True with console.pager(**console_kwargs): console.print(table) else: click.echo("No reminders found!") else: try: session = Session() event = session.query(Event).filter(Event.id == _id).first() if event is None: click.echo("Event not found!") else: days_left, __ = get_days_left(event) if days_left == -1: click.echo("Event ended.") else: reminder = Reminder(id=event.id) session.add(reminder) session.commit() session.close() click.echo("Reminder set!") except sqlalchemy.exc.IntegrityError: session.rollback() if click.confirm("Do you want to remove this reminder?"): session = Session() session.query(Reminder).filter(Reminder.id == _id).delete() session.commit() session.close() click.echo("Reminder removed!")
def _show(ctx, *args, **kwargs): # TODO: conrad show --new initialize_conrad() _id = kwargs["id"] kind = kwargs["kind"] cfp = kwargs["cfp"] tag = kwargs["tag"] name = kwargs["name"] date = list(kwargs["date"]) location = kwargs["location"] filters = [] if _id: filters.append(Event.id == _id) if kind: filters.append(Event.kind == kind) if cfp: filters.append(Event.cfp_open.is_(cfp)) if tag: filters.append(Event.tags.contains(tag)) if name: filters.append(Event.name.ilike(f"%{name}%")) if date: date_filters = [] for d in date: cmp, date = d.split(" ") if not (">" in cmp or "<" in cmp): raise click.UsageError("Wrong comparison operator!") try: __ = dt.datetime.strptime(date, "%Y-%m-%d") except ValueError: raise click.UsageError("Wrong date format!") if ">" in cmp: date_filters.append(Event.start_date >= date) elif "<" in cmp: date_filters.append(Event.start_date <= date) filters.append(sqlalchemy.and_(*date_filters)) if location: filters.append( sqlalchemy.or_( Event.name.ilike(f"%{location}%"), Event.city.ilike(f"%{location}%"), Event.state.ilike(f"%{location}%"), Event.country.ilike(f"%{location}%"), Event.location.ilike(f"%{location}%"), )) session = Session() try: events = list( session.query(Event).filter(*filters).order_by( Event.start_date).all()) except sqlalchemy.exc.OperationalError: refresh_conrad() events = list( session.query(Event).filter(*filters).order_by( Event.start_date).all()) if len(events) > 0: console = Console() table = Table(show_header=True, header_style="bold magenta", show_lines=True) table.add_column("id") table.add_column("Name") table.add_column("Website") table.add_column("City") table.add_column("Country") table.add_column("Start Date") table.add_column("End Date") events_output = [] rids = [r.id for r in session.query(Reminder).all()] for event in events: event_output = [ event.id, event.name, event.url, event.city, event.country, event.start_date.strftime("%Y-%m-%d"), event.end_date.strftime("%Y-%m-%d"), ] # highlight event which has a reminder set if event.id in rids: event_output = list( map( lambda x: f"[bold][green]{x}[/green][/bold]", event_output, )) table.add_row(*event_output) session.close() console_kwargs = {} if has_less(): console_kwargs["styles"] = True with console.pager(**console_kwargs): console.print(table) else: click.echo("No events found!")
def batch_add_groups(import_file: Path, yes: bool = False) -> List[Response]: console = Console(emoji=False) br = cli.central.BatchRequest data = config.get_file_data(import_file) # TODO handle csv if isinstance(data, dict) and "groups" in data: data = data["groups"] reqs, gw_reqs, ap_reqs = [], [], [] pre_cfgs = [] _pre_config_msg = "" cache_data = [] for group in data: if "allowed-types" in data[group]: data[group]["allowed_types"] = data[group]["allowed-types"] del data[group]["allowed-types"] try: g = GroupImport(**{"group": group, **data[group]}) except ValidationError as e: print(e) raise typer.Exit(1) reqs += [ br( cli.central.create_group, g.group, allowed_types=g.allowed_types, wired_tg=g.wired_tg, wlan_tg=g.wlan_tg, aos10=g.aos10, microbranch=g.microbranch, gw_role=g.gw_role, monitor_only_sw=g.monitor_only_sw, monitor_only_cx=g.monitor_only_cx, ) ] cache_data += [ {"name": g.group, "template group": {"Wired": g.wired_tg, "Wireless": g.wlan_tg}} ] for dev_type, cfg_file, var_file in zip(["gw", "ap"], [g.gw_config, g.ap_config], [g.gw_vars, g.ap_vars]): if cfg_file is not None: pc = _build_pre_config(g.group, dev_type=dev_type, cfg_file=cfg_file, var_file=var_file) pre_cfgs += [pc] _pre_config_msg += ( f" [bright_green]{len(pre_cfgs)}[/]. [cyan]{g.group}[/] {'gateways' if dev_type == 'gw' else 'AP'} " f"group level will be configured based on [cyan]{cfg_file.name}[/]\n" ) if dev_type == "gw": gw_reqs += [pc.request] else: ap_reqs += [pc.request] print(f"[bright_green]The following groups will be created:[/]") _ = [print(f" [cyan]{g}[/]") for g in data] _pre_config_msg = ( "\n[bright_green]Group level configurations will be sent:[/]\n" f"{_pre_config_msg}" f"\n[italic dark_olive_green2]{len(reqs) + len(gw_reqs) + len(ap_reqs)} API calls will be performed.[/]\n" ) print(_pre_config_msg) for idx in range(len(pre_cfgs) + 1): if idx > 0: print(_pre_config_msg) print(f"Select [bright_green]#[/] to display config to be sent or [bright_green]go[/] to continue.") ch = utils.ask( ">", console=console, choices=[*[str(idx) for idx in range(1, len(pre_cfgs) + 1)], "abort", "go"], ) if ch.lower() == "go": yes = True break else: pc: PreConfig = pre_cfgs[int(ch) - 1] console.rule(f"Config to be sent to {pc.name}") with console.pager(): console.print(pc.config) console.rule(f" End {pc.name} config ") if reqs and yes or typer.confirm("Proceed?", abort=True): resp = cli.central.batch_request(reqs) if all(r.ok for r in resp): cache_resp = asyncio.run(cli.cache.update_group_db(cache_data)) log.debug(f"batch add group cache resp: {cache_resp}") cli.display_results(resp) if gw_reqs: print("\n[bright_green]Results from Group level gateway config push (CLI commands)[/]") print("\n [italic]This can take some time.[/]") resp = cli.central.batch_request(gw_reqs) cli.display_results(resp, cleaner=cleaner.parse_caas_response) if ap_reqs: print("\n[bright_green]Results from Group level AP config push (Replaces entire group level)[/]\n") resp = cli.central.batch_request(ap_reqs) cli.display_results(resp, tablefmt="action")
class Render(FormatMixin): def __init__(self, config: Config): self.config = config self.node = None custom_theme = Theme({ "info": "dim cyan", "warning": "magenta", "danger": "bold red" }) if config.force_colors: self.console = Console(theme=custom_theme, force_terminal=config.force_colors) else: self.console = Console(theme=custom_theme) def parse_notebook(self): """Parse the notebook content.""" try: self.node = nbformat.read(self.config.input_file, as_version=4) except nbformat.reader.NotJSONError as exc: raise ParsingException( f"{self.config.input_file.name} is not a proper notebook") def iter_cells(self): block = None if self.config.full_display: block = self.node.cells start = 0 elif self.config.head: block = self.node.cells[:self.config.head] start = 0 elif self.config.tail: block = self.node.cells[-self.config.tail:] start = max(len(self.node.cells) - self.config.tail, 0) elif self.config.is_cell_range(): block = self.node.cells[self.config.start - 1:self.config.end] start = self.config.start - 1 for cell in block: start += 1 yield start, cell def format_cell(self, index, cell): cell_type = cell.get("cell_type") if cell_type == CellType.MARKDOWN.value: return self.format_markdown(index, cell, self.config) elif cell_type == CellType.CODE.value: return self.format_code(index, cell, self.config) elif cell_type == CellType.RAW.value: return self.format_raw(index, cell, self.config) def render_cell(self, panels): self.console.print(*panels) def _render_to_terminal(self): for index, cell in self.iter_cells(): panels = self.format_cell(index, cell) self.render_cell(panels) def render_to_terminal(self): if self.config.single_page: with self.console.pager(): self._render_to_terminal() else: self._render_to_terminal() def render(self): self.parse_notebook() self.render_to_terminal()