Beispiel #1
0
def instances_ps(project):
    """
    Lists all instances.
    """
    instances = project.host.ps_instances()
    width, _ = click.get_terminal_size()
    click.echo(instance_table(instances, width))
Beispiel #2
0
 def list_examples(self):
     if util.check_package(
         self.name,
         self.version,
         self.spec_version,
         self.examples_dir
     ):
         # examples = sorted(os.listdir(self.examples_dir))
         examples = [dirname(y).replace(self.examples_dir + sep, '')
                     for x in os.walk(self.examples_dir)
                     for y in glob.glob(util.safe_join(x[0], 'info'))]
         click.secho('')
         for example in examples:
             example_dir = util.safe_join(self.examples_dir, example)
             if isdir(example_dir):
                 info_path = util.safe_join(example_dir, 'info')
                 info = ''
                 if isfile(info_path):
                     with codecs.open(info_path, 'r', 'utf-8') as info_file:
                         info = info_file.read().replace('\n', '')
                 click.secho(' ' + example, fg='blue', bold=True)
                 click.secho('-' * click.get_terminal_size()[0])
                 click.secho(' ' + info)
                 click.secho('')
         click.secho(EXAMPLE_DIR_FILE, fg='green')
         click.secho(EXAMPLE_OF_USE_CAD, fg='green')
     else:
         return 1
     return 0
Beispiel #3
0
 def log_func(x):
     # 63 is the total length of progressbar, label, percentage, etc
     available_length = get_terminal_size()[0] - 64
     if x and available_length > 10:
         return x.name[:available_length]
     else:
         return ""
Beispiel #4
0
def print_boards(boards):
    terminal_width, _ = click.get_terminal_size()
    BOARDLIST_TPL = ("{type:<30} {mcu:<14} {frequency:<8} "
                     " {flash:<7} {ram:<6} {name}")
    click.echo(
        BOARDLIST_TPL.format(
            type=click.style("ID", fg="cyan"),
            mcu="MCU",
            frequency="Frequency",
            flash="Flash",
            ram="RAM",
            name="Name"))
    click.echo("-" * terminal_width)

    for board in boards:
        ram_size = board['ram']
        if ram_size >= 1024:
            if ram_size % 1024:
                ram_size = "%.1fkB" % (ram_size / 1024.0)
            else:
                ram_size = "%dkB" % (ram_size / 1024)
        else:
            ram_size = "%dB" % ram_size

        click.echo(
            BOARDLIST_TPL.format(
                type=click.style(board['id'], fg="cyan"),
                mcu=board['mcu'],
                frequency="%dMhz" % (board['fcpu'] / 1000000),
                flash="%dkB" % (board['rom'] / 1024),
                ram=ram_size,
                name=board['name']))
Beispiel #5
0
def print_banner(fname, percent, fill=None):
    """
    Prints out a coloured banner showing coverage percent

    :param fill: the width of the banner; if None, uses 80 or the
        terminal width, whichever is less.
    """

    if fill is None:
        fill = min(click.get_terminal_size()[0], 80)

    click.echo(colors.color('-' * fill, bg=226, fg=236), color=True)
    maxsize = fill - len('coverage: ') - 3
    truncfname = fname[-maxsize:]
    if len(truncfname) != len(fname):
        truncfname = u'...{}'.format(truncfname)
    click.echo(colors.color(u'coverage: {}'.format(truncfname).ljust(fill), bg=226, fg=236), color=True)
    grsize = int(fill * percent)
    if grsize >= 5:
        prcnt_formatted = u'%3d%%' % int(percent * 100.0)
        gr = colors.color(prcnt_formatted + (u' ' * (grsize - 4)), fg=255, bg=22)
    else:
        gr = colors.color(u' ' * grsize, bg=22)
    red = colors.color(u' ' * int(math.ceil(fill * (1.0 - percent))), bg=52)
    click.echo(gr + red, color=True)
    click.echo(colors.color(u'-' * fill, bg=226, fg=236), color=True)
Beispiel #6
0
def process_environment(ctx, name, options, targets, upload_port):
    terminal_width, _ = click.get_terminal_size()
    start_time = time()

    click.echo("[%s] Processing %s (%s)" % (
        datetime.now().strftime("%c"),
        click.style(name, fg="cyan", bold=True),
        ", ".join(["%s: %s" % (k, v) for k, v in options.iteritems()])
    ))
    click.secho("-" * terminal_width, bold=True)

    result = _run_environment(ctx, name, options, targets, upload_port)

    is_error = result['returncode'] != 0
    summary_text = " Took %.2f seconds " % (time() - start_time)
    half_line = "=" * ((terminal_width - len(summary_text) - 10) / 2)
    click.echo("%s [%s]%s%s" % (
        half_line,
        (click.style(" ERROR ", fg="red", bold=True)
         if is_error else click.style("SUCCESS", fg="green", bold=True)),
        summary_text,
        half_line
    ), err=is_error)

    return not is_error
Beispiel #7
0
    def after_method(self, bench, method):
        click.echo('\r', nl=False)  # Clear the line

        results = bench.results[method]
        mean = results.total / bench.times
        ref = self.ref(bench, method)
        duration = self.duration(total=results.total, mean=mean, ref=ref)

        if results.has_success and results.has_errors:
            status = ' '.join((yellow(WARNING), duration))
        elif results.has_success:
            status = ' '.join((green(OK), duration))
        else:
            status = ' '.join((red(KO), duration))

        width, _ = click.get_terminal_size()
        size = width - len(click.unstyle(status))

        label = bench.label_for(method)

        click.echo('{label:.<{size}} {status}'.format(label=cyan(label),
                                                      size=size,
                                                      status=status))
        if self.debug and results.error:
            exc = results.error
            click.echo(yellow('Error: {0}'.format(type(exc))))
            click.echo('\t{0}'.format(exc.message if hasattr(exc, 'message') else exc))
Beispiel #8
0
    def update_progress(cls, progress, starttime):
        """ Display an ascii progress bar while processing operation. """
        width, _height = click.get_terminal_size()
        if not width:
            return

        duration = datetime.utcnow() - starttime
        hours, remainder = divmod(duration.seconds, 3600)
        minutes, seconds = divmod(remainder, 60)

        size = int(width * .6)
        status = ""
        if isinstance(progress, int):
            progress = float(progress)
        if not isinstance(progress, float):
            progress = 0
            status = 'error: progress var must be float\n'
            cls.echo(type(progress))
        if progress < 0:
            progress = 0
            status = 'Halt...\n'
        if progress >= 1:
            progress = 1
            # status = 'Done...\n'
        block = int(round(size * progress))
        text = ('\rProgress: [{0}] {1:.2%} {2} {3:0>2}:{4:0>2}:{5:0>2}  '
                ''.format('#' * block + '-' * (size - block), progress,
                          status, hours, minutes, seconds))
        sys.stdout.write(text)
        sys.stdout.flush()
Beispiel #9
0
def report(ctx):
    """Print out a report of all packages"""
    db = ctx.obj['db']
    columns, rows = click.get_terminal_size()
    collections = list(db.query(tables.Collection).order_by(tables.Collection.order))

    print_collection_header(collections)

    print('│  ' * len(collections))
    query = db.query(tables.Package)
    query = query.order_by(func.lower(tables.Package.name))
    query = query.options(eagerload(tables.Package.by_collection))
    query = query.options(subqueryload(tables.Package.requirements))
    for package in query:
        print_collection_info(package, collections)
        print(' ' + package.name, end=' ')
        reqs = []
        for req in package.requirements:
            for cp in req.by_collection.values():
                if cp.status != 'released':
                    reqs.append(req.name)
                    break
        if reqs:
            print('({})'.format(', '.join(reqs)), end='')
        print()

    print('│  ' * len(collections))

    print_collection_header(collections, foot=True)
Beispiel #10
0
def display_bugs(__bugs: List[Dict[str, str]], __order: str, **extras) -> str:
    """Display bugs to users.

    Args:
        __bugs: Bugs to display
        __order: Sorting order for displaying bugs
        extras: Additional values to pass to templates

    Returns:
        Rendered template output
    """
    if not __bugs:
        return success('No bugs found!')

    # Match ordering method to bug attribute
    if __order == 'updated':
        attr = 'updated_at'
    else:
        attr = __order

    __bugs = sorted(__bugs, key=operator.attrgetter(attr))

    # Default to 80 columns, when stdout is not a tty
    columns = click.get_terminal_size()[0]

    template = get_template('view', 'list.txt')

    max_id = max(i.number for i in __bugs)
    id_len = len(str(max_id))
    spacer = ' ' * (id_len - 2)

    return template.render(bugs=__bugs, spacer=spacer, id_len=id_len,
                           max_title=columns - id_len - 2, **extras)
Beispiel #11
0
def print_filler(fill, color=Color.WHITE, err=False):
    width, _ = click.get_terminal_size()
    if len(fill) > 1:
        fill = fill[0]

    out = sys.stderr if err else sys.stdout
    print(clr(color, fill * width), file=out)
Beispiel #12
0
def quickstart():
    """Quickstart wizard for setting up twtxt."""
    width = click.get_terminal_size()[0]
    width = width if width <= 79 else 79

    click.secho("twtxt - quickstart", fg="cyan")
    click.secho("==================", fg="cyan")
    click.echo()

    help_text = "This wizard will generate a basic configuration file for twtxt with all mandatory options set. " \
                "Have a look at the README.rst to get information about the other available options and their meaning."
    click.echo(textwrap.fill(help_text, width))

    click.echo()
    nick = click.prompt("➤ Please enter your desired nick", default=os.environ.get("USER", ""))
    twtfile = click.prompt("➤ Please enter the desired location for your twtxt file", "~/twtxt.txt", type=click.Path())
    disclose_identity = click.confirm("➤ Do you want to disclose your identity? Your nick and URL will be shared", default=False)

    click.echo()
    add_news = click.confirm("➤ Do you want to follow the twtxt news feed?", default=True)

    conf = Config.create_config(nick, twtfile, disclose_identity, add_news)
    open(os.path.expanduser(twtfile), "a").close()

    click.echo()
    click.echo("✓ Created config file at '{0}'.".format(click.format_filename(conf.config_file)))
Beispiel #13
0
    def list_boards(self):
        """Return a list with all the supported boards"""

        # Print table
        click.echo('\nSupported boards:\n')

        BOARDLIST_TPL = ('{board:25} {fpga:20} {type:<5} {size:<5} {pack:<10}')
        terminal_width, _ = click.get_terminal_size()

        click.echo('-' * terminal_width)
        click.echo(BOARDLIST_TPL.format(
            board=click.style('Board', fg='cyan'), fpga='FPGA', type='Type',
            size='Size', pack='Pack'))
        click.echo('-' * terminal_width)

        for board in self.boards:
            fpga = self.boards.get(board).get('fpga')
            click.echo(BOARDLIST_TPL.format(
                board=click.style(board, fg='cyan'),
                fpga=fpga,
                type=self.fpgas.get(fpga).get('type'),
                size=self.fpgas.get(fpga).get('size'),
                pack=self.fpgas.get(fpga).get('pack')))

        click.secho(BOARDS_MSG, fg='green')
Beispiel #14
0
    def _render(self):
        cols, rows = click.get_terminal_size()
        second_col_width = cols - self.first_col_width

        clear_cmd = ''
        if self.last_lines > 0:
            # Move cursor up `last_lines` times.
            clear_cmd = f'\r\033[{self.last_lines}A'

        lines = []
        for mod, progress in self.buffer.items():
            line = self._render_modname(mod).ljust(self.first_col_width, ' ')
            while progress:
                second_col = progress[:second_col_width]
                second_col = second_col.ljust(second_col_width, ' ')

                progress = progress[second_col_width:]

                # Apply styles *after* slicing and padding the string
                # (otherwise ANSI codes could be sliced in half).
                second_col = re.sub(
                    r'\S',
                    lambda x: self.styles_map[x[0]](x[0]),
                    second_col)

                lines.append(f'{line}{second_col}')

                if line[0] != ' ':
                    line = ' ' * self.first_col_width

        lines.append(' ' * cols)
        lines.append(
            f'Progress: {self.completed_tests}/{self.total_tests} tests.')

        if self.completed_tests != self.total_tests:
            # If it's not the last test, check if our render buffer
            # requires more rows than currently visible.
            if len(lines) + 1 > rows:
                # Scroll the render buffer to the bottom and
                # cut the lines from the beginning, so that it
                # will fit the screen.
                #
                # We need to do this because we can't move the
                # cursor past the visible screen area, so if we
                # render more data than the screen can fit, we
                # will have lot's of garbage output.
                lines = lines[len(lines) + 1 - rows:]
                lines[0] = '^' * cols

        # Hide cursor.
        print('\033[?25l', end='', flush=True, file=self.stream)
        try:
            # Use `print` (not `click.echo`) because we want to
            # precisely control when the output is flushed.
            print(clear_cmd + '\n'.join(lines), flush=False, file=self.stream)
        finally:
            # Show cursor.
            print('\033[?25h', end='', flush=True, file=self.stream)

        self.last_lines = len(lines)
Beispiel #15
0
def after_upgrade(ctx):
    last_version = app.get_state_item("last_version", "0.0.0")
    if last_version == __version__:
        return

    if last_version == "0.0.0":
        app.set_state_item("last_version", __version__)
    else:
        click.secho("Please wait while upgrading PlatformIO ...", fg="yellow")
        clean_cache()
        u = Upgrader(last_version, __version__)
        if u.run(ctx):
            app.set_state_item("last_version", __version__)

            # update development platforms
            pm = PlatformManager()
            for manifest in pm.get_installed():
                # pm.update(manifest['name'], "^" + manifest['version'])
                pm.update(manifest['name'])

            # update PlatformIO Plus tool if installed
            pioplus_update()

            click.secho(
                "PlatformIO has been successfully upgraded to %s!\n" %
                __version__,
                fg="green")

            telemetry.on_event(
                category="Auto",
                action="Upgrade",
                label="%s > %s" % (last_version, __version__))
        else:
            raise exception.UpgradeError("Auto upgrading...")
        click.echo("")

    # PlatformIO banner
    terminal_width, _ = click.get_terminal_size()
    click.echo("*" * terminal_width)
    click.echo("If you like %s, please:" % (click.style(
        "PlatformIO", fg="cyan")))
    click.echo("- %s us on Twitter to stay up-to-date "
               "on the latest project news > %s" % (click.style(
                   "follow", fg="cyan"), click.style(
                       "https://twitter.com/PlatformIO_Org", fg="cyan")))
    click.echo("- %s it on GitHub > %s" % (click.style(
        "star", fg="cyan"), click.style(
            "https://github.com/platformio/platformio", fg="cyan")))
    if not getenv("PLATFORMIO_IDE"):
        click.echo("- %s PlatformIO IDE for IoT development > %s" %
                   (click.style(
                       "try", fg="cyan"), click.style(
                           "http://platformio.org/platformio-ide", fg="cyan")))
    if not util.is_ci():
        click.echo("- %s us with PlatformIO Plus > %s" % (click.style(
            "support", fg="cyan"), click.style(
                "https://pioplus.com", fg="cyan")))

    click.echo("*" * terminal_width)
    click.echo("")
Beispiel #16
0
    def process(self):
        terminal_width, _ = click.get_terminal_size()
        start_time = time()

        click.echo(
            "[%s] Processing %s (%s)"
            % (
                datetime.now().strftime("%c"),
                click.style(self.name, fg="cyan", bold=True),
                ", ".join(["%s: %s" % (k, v) for k, v in self.options.iteritems()]),
            )
        )
        click.secho("-" * terminal_width, bold=True)

        result = self._run()

        is_error = result["returncode"] != 0
        summary_text = " Took %.2f seconds " % (time() - start_time)
        half_line = "=" * ((terminal_width - len(summary_text) - 10) / 2)
        click.echo(
            "%s [%s]%s%s"
            % (
                half_line,
                (
                    click.style(" ERROR ", fg="red", bold=True)
                    if is_error
                    else click.style("SUCCESS", fg="green", bold=True)
                ),
                summary_text,
                half_line,
            ),
            err=is_error,
        )

        return not is_error
Beispiel #17
0
def check_platformio_upgrade():
    last_check = app.get_state_item("last_check", {})
    interval = int(app.get_setting("check_platformio_interval")) * 3600 * 24
    if (time() - interval) < last_check.get("platformio_upgrade", 0):
        return

    last_check['platformio_upgrade'] = int(time())
    app.set_state_item("last_check", last_check)

    latest_version = get_latest_version()
    if (latest_version == __version__ or
            Upgrader.version_to_int(latest_version) <
            Upgrader.version_to_int(__version__)):
        return

    terminal_width, _ = click.get_terminal_size()

    click.echo("")
    click.echo("*" * terminal_width)
    click.secho("There is a new version %s of PlatformIO available.\n"
                "Please upgrade it via " % latest_version,
                fg="yellow", nl=False)
    click.secho("platformio upgrade", fg="cyan", nl=False)
    click.secho(" command.\nChanges: ", fg="yellow", nl=False)
    click.secho("http://docs.platformio.org/en/latest/history.html",
                fg="cyan")
    click.echo("*" * terminal_width)
    click.echo("")
Beispiel #18
0
    def process(self):
        terminal_width, _ = click.get_terminal_size()
        start_time = time()
        env_dump = []

        for k, v in self.options.items():
            self.options[k] = self.options[k].strip()
            if self.verbose or k in self.DEFAULT_DUMP_OPTIONS:
                env_dump.append(
                    "%s: %s" % (k, ", ".join(util.parse_conf_multi_values(v))))

        if not self.silent:
            click.echo("Processing %s (%s)" % (click.style(
                self.name, fg="cyan", bold=True), "; ".join(env_dump)))
            click.secho("-" * terminal_width, bold=True)

        self.options = self._validate_options(self.options)
        result = self._run()
        is_error = result['returncode'] != 0

        if self.silent and not is_error:
            return True

        if is_error or "piotest_processor" not in self.cmd_ctx.meta:
            print_header(
                "[%s] Took %.2f seconds" % (
                    (click.style("ERROR", fg="red", bold=True) if is_error else
                     click.style("SUCCESS", fg="green", bold=True)),
                    time() - start_time),
                is_error=is_error)

        return not is_error
Beispiel #19
0
 def log_func(x):
     # 63 is the total length of progressbar, label, percentage, etc
     available_length = get_terminal_size()[0] - 64
     if x and available_length > 10:
         return text_type(x.name)[:available_length].encode('utf-8')
     else:
         return ""
Beispiel #20
0
def sdks(sdk, version):
    """Print a list of all installed SDKs and a breakdown of the symbols
    contained within.  This queries the system symbol database and reports
    all SDKs and versions that symbols exist for.  The output is broken down
    by minor versions, builds and cpu architectures.  For each of those a
    count of the stored bundles is returned.  (A bundle in this case is a
    single binary)
    """
    from sentry.models import DSymSDK
    last_prefix = None
    click.secho('  %-8s  %-10s  %-12s %-8s %s' % (
        'SDK',
        'Version',
        'Build',
        'CPU',
        'Bundles',
    ), fg='cyan')
    click.secho('-' * click.get_terminal_size()[0], fg='yellow')
    for sdk in DSymSDK.objects.enumerate_sdks(sdk=sdk, version=version):
        prefix = '  %-8s  %-10s  ' % (
            sdk['sdk_name'],
            sdk['version']
        )
        if prefix == last_prefix:
            prefix = ' ' * len(prefix)
        else:
            last_prefix = prefix
        click.echo('%s%-12s %-8s %d' % (
            prefix,
            sdk['build'],
            sdk['cpu_name'],
            sdk['bundle_count'],
        ))
Beispiel #21
0
def platforms_search(query, json_output):

    data = []
    platforms = PlatformFactory.get_platforms().keys()
    platforms.sort()
    for platform in platforms:
        p = PlatformFactory.newPlatform(platform)
        type_ = p.get_type()
        description = p.get_description()

        if query == "all":
            query = ""

        search_data = "%s %s %s" % (type_, description, p.get_packages())
        if query and query.lower() not in search_data.lower():
            continue

        data.append({
            "type": type_,
            "description": description,
            "packages": p.get_packages()
        })

    if json_output:
        click.echo(json.dumps(data))
    else:
        terminal_width, _ = click.get_terminal_size()
        for item in data:
            click.secho(item['type'], fg="cyan", nl=False)
            click.echo(" (available packages: %s)" % ", ".join(
                item.get("packages").keys()))
            click.echo("-" * terminal_width)
            click.echo(item['description'])
            click.echo()
Beispiel #22
0
def settings_get(name):

    list_tpl = "{name:<40} {value:<35} {description}"
    terminal_width, _ = click.get_terminal_size()

    click.echo(
        list_tpl.format(
            name=click.style("Name", fg="cyan"),
            value=(click.style("Value", fg="green") + click.style(" [Default]", fg="yellow")),
            description="Description",
        )
    )
    click.echo("-" * terminal_width)

    for _name, _data in sorted(app.DEFAULT_SETTINGS.items()):
        if name and name != _name:
            continue
        _value = app.get_setting(_name)

        _value_str = str(_value)
        if isinstance(_value, bool):
            _value_str = "Yes" if _value else "No"
        _value_str = click.style(_value_str, fg="green")

        if _value != _data["value"]:
            _defvalue_str = str(_data["value"])
            if isinstance(_data["value"], bool):
                _defvalue_str = "Yes" if _data["value"] else "No"
            _value_str += click.style(" [%s]" % _defvalue_str, fg="yellow")
        else:
            _value_str += click.style(" ", fg="yellow")

        click.echo(
            list_tpl.format(name=click.style(_name, fg="cyan"), value=_value_str, description=_data["description"])
        )
Beispiel #23
0
def show():
    click.clear()

    term_width, term_height = click.get_terminal_size()
    canvas = make_canvas(term_width, term_height - 1)

    chart_width = min(20, term_width - 20)

    m = metrics.get_all_metrics()
    max_count = max(m.value for m in m.values())

    scale = get_scale(max_count)
    ratio = chart_width * 1.0 / scale

    t = []
    headers = ['param', 'value', 'chart']
    keys = sorted(m.keys())
    for key in keys:
        metric = m[key]
        count = metric.value
        color = green if metric.mood == metrics.MOOD_HAPPY else red
        chart = color('|' + u'█' * int(ratio * count))

        t.append([key, count, chart])

    place(canvas, LOGO2, 0, 0)

    s = tabulate(t, headers=headers, tablefmt='simple')
    place(canvas, s, 25, 0)

    render(canvas)

    time.sleep(cfg.CONF.interval)
Beispiel #24
0
def show_queues(queues, raw, by_queue):
    if queues:
        qs = list(map(Queue, queues))
    else:
        qs = Queue.all()

    num_jobs = 0
    termwidth, _ = click.get_terminal_size()
    chartwidth = min(20, termwidth - 20)

    max_count = 0
    counts = dict()
    for q in qs:
        count = q.count
        counts[q] = count
        max_count = max(max_count, count)
    scale = get_scale(max_count)
    ratio = chartwidth * 1.0 / scale

    for q in qs:
        count = counts[q]
        if not raw:
            chart = green('|' + '█' * int(ratio * count))
            line = '%-12s %s %d' % (q.name, chart, count)
        else:
            line = 'queue %s %d' % (q.name, count)
        click.echo(line)

        num_jobs += count

    # print summary when not in raw mode
    if not raw:
        click.echo('%d queues, %d jobs total' % (len(qs), num_jobs))
Beispiel #25
0
def ps(project):
    """
    List all services running across the cluster.

    """
    services = project.host.ps_services()
    width, _ = click.get_terminal_size()
    click.echo(service_table(services, width))
Beispiel #26
0
    def _print_header(with_date=False):
        click.echo((printitemdate_tpl if with_date else printitem_tpl).format(
            name=click.style("Name", fg="cyan"),
            date="Date",
            url=click.style("Url", fg="blue")))

        terminal_width, _ = click.get_terminal_size()
        click.echo("-" * terminal_width)
Beispiel #27
0
def echo(text='', prepend='', initial_indent='', subsequent_indent='', fg=''):
    window_width, _ = click.get_terminal_size()
    wrapped = click.wrap_text(text,
                              width=window_width - len(initial_indent),
                              initial_indent=prepend + initial_indent,
                              subsequent_indent=prepend + subsequent_indent,
                              preserve_paragraphs=False)
    click.secho(wrapped, fg=fg)
Beispiel #28
0
def print_header(artist, album, current, total):
    click.clear()
    title = u"{0} - {1}".format(artist, album)
    progress = u"{0}/{1}".format(current + 1, total)
    term_width, term_height = click.get_terminal_size()
    pad_width = (term_width - len(title) - len(progress)) - 10
    header_template = u"=== {title} {pad} {progress} ==="
    click.secho(header_template.format(title=title, pad="=" * pad_width, progress=progress), bg="blue", fg="white")
Beispiel #29
0
def list_cmd(cfg, ctx, query):
    """List items by date

    Examples:

    \b
    * francis list
    * francis list today
    * francis list tomorrow
    * francis list friday
    * francis list "july 22"
    * francis list "over due"

    """
    api = todoist.api.TodoistAPI(cfg['auth_token'])
    api.sync()

    if not query:
        query = ['today']

    resp = api.query(query)
    for item in resp:
        if item['type'] == 'overdue':
            item['query'] = 'Over due'

        click.echo('[%s]' % item['query'])
        click.echo('')

        if not item['data']:
            continue

        table = [
            ('id', 'pri', 'content', 'proj', 'due date')
        ]
        data = sorted(
            item['data'],
            key=lambda item: (item['due_date'], item.get('date_string'))
        )

        for task in data:
            table.append(
                (
                    task['id'],
                    display_priority(task['priority']),
                    task['content'],
                    display_project(api.projects.get_by_id(task['project_id'])),
                    task['date_string'],
                )
            )

        table = prettytable(click.get_terminal_size()[0], table)
        for i, line in enumerate(table.splitlines()):
            if i < 2:
                click.echo(line)
            elif i % 2 == 0:
                click.secho(line, fg='cyan', dim=True)
            else:
                click.echo(line)
Beispiel #30
0
def cli(query, json_output):  # pylint: disable=R0912

    if json_output:
        return ouput_boards_json(query)

    BOARDLIST_TPL = ("{type:<30} {mcu:<14} {frequency:<8} "
                     " {flash:<7} {ram:<6} {name}")
    terminal_width, _ = click.get_terminal_size()

    grpboards = {}
    for type_, data in get_boards().items():
        if data['platform'] not in grpboards:
            grpboards[data['platform']] = {}
        grpboards[data['platform']][type_] = data

    for (platform, boards) in sorted(grpboards.items()):
        if query:
            search_data = json.dumps(boards).lower()
            if query.lower() not in search_data.lower():
                continue

        click.echo("")
        click.echo("Platform: ", nl=False)
        click.secho(platform, bold=True)
        click.echo("-" * terminal_width)
        click.echo(BOARDLIST_TPL.format(
            type=click.style("Type", fg="cyan"), mcu="MCU",
            frequency="Frequency", flash="Flash", ram="RAM", name="Name"))
        click.echo("-" * terminal_width)

        for type_, data in sorted(boards.items(), key=lambda b: b[1]['name']):
            if query:
                search_data = "%s %s" % (type_, json.dumps(data).lower())
                if query.lower() not in search_data.lower():
                    continue

            flash_size = ""
            if "maximum_size" in data.get("upload", None):
                flash_size = int(data['upload']['maximum_size'])
                flash_size = "%dKb" % (flash_size / 1024)

            ram_size = ""
            if "maximum_ram_size" in data.get("upload", None):
                ram_size = int(data['upload']['maximum_ram_size'])
                if ram_size >= 1024:
                    if ram_size % 1024:
                        ram_size = "%.1fKb" % (ram_size / 1024.0)
                    else:
                        ram_size = "%dKb" % (ram_size / 1024)
                else:
                    ram_size = "%dB" % ram_size

            click.echo(BOARDLIST_TPL.format(
                type=click.style(type_, fg="cyan"), mcu=data['build']['mcu'],
                frequency="%dMhz" % (
                    int(data['build']['f_cpu'][:-1]) / 1000000),
                flash=flash_size, ram=ram_size, name=data['name']))
Beispiel #31
0
def after_upgrade(ctx):
    terminal_width, _ = click.get_terminal_size()
    last_version = app.get_state_item("last_version", "0.0.0")
    if last_version == __version__:
        return

    if last_version == "0.0.0":
        app.set_state_item("last_version", __version__)
    elif semantic_version.Version.coerce(util.pepver_to_semver(
            last_version)) > semantic_version.Version.coerce(
                util.pepver_to_semver(__version__)):
        click.secho("*" * terminal_width, fg="yellow")
        click.secho(
            "Obsolete PIO Core v%s is used (previous was %s)" % (__version__,
                                                                 last_version),
            fg="yellow")
        click.secho(
            "Please remove multiple PIO Cores from a system:", fg="yellow")
        click.secho(
            "http://docs.platformio.org/page/faq.html"
            "#multiple-pio-cores-in-a-system",
            fg="cyan")
        click.secho("*" * terminal_width, fg="yellow")
        return
    else:
        click.secho("Please wait while upgrading PlatformIO...", fg="yellow")
        app.clean_cache()

        # Update PlatformIO's Core packages
        update_core_packages(silent=True)

        u = Upgrader(last_version, __version__)
        if u.run(ctx):
            app.set_state_item("last_version", __version__)
            click.secho(
                "PlatformIO has been successfully upgraded to %s!\n" %
                __version__,
                fg="green")
            telemetry.on_event(
                category="Auto",
                action="Upgrade",
                label="%s > %s" % (last_version, __version__))
        else:
            raise exception.UpgradeError("Auto upgrading...")
        click.echo("")

    # PlatformIO banner
    click.echo("*" * terminal_width)
    click.echo(
        "If you like %s, please:" % (click.style("PlatformIO", fg="cyan")))
    click.echo("- %s us on Twitter to stay up-to-date "
               "on the latest project news > %s" %
               (click.style("follow", fg="cyan"),
                click.style("https://twitter.com/PlatformIO_Org", fg="cyan")))
    click.echo(
        "- %s it on GitHub > %s" %
        (click.style("star", fg="cyan"),
         click.style("https://github.com/platformio/platformio", fg="cyan")))
    if not getenv("PLATFORMIO_IDE"):
        click.echo(
            "- %s PlatformIO IDE for IoT development > %s" %
            (click.style("try", fg="cyan"),
             click.style("https://platformio.org/platformio-ide", fg="cyan")))
    if not util.is_ci():
        click.echo("- %s us with PlatformIO Plus > %s" % (click.style(
            "support", fg="cyan"), click.style(
                "https://pioplus.com", fg="cyan")))

    click.echo("*" * terminal_width)
    click.echo("")
Beispiel #32
0
def print_processing_header(test, env):
    click.echo("Processing %s in %s environment" % (click.style(
        test, fg="yellow", bold=True), click.style(env, fg="cyan", bold=True)))
    terminal_width, _ = click.get_terminal_size()
    click.secho("-" * terminal_width, bold=True)
Beispiel #33
0
    def _render(self):
        def print_line(line):
            if len(line) < cols:
                line += ' ' * (cols - len(line))
            lines.append(line)

        def print_empty_line():
            print_line(' ')

        last_render = self.completed_tests == self.total_tests
        cols, rows = click.get_terminal_size()
        second_col_width = cols - self.first_col_width

        clear_cmd = ''
        if self.last_lines > 0:
            # Move cursor up `last_lines` times.
            clear_cmd = f'\r\033[{self.last_lines}A'

        lines = []
        for mod, progress in self.buffer.items():
            line = self._render_modname(mod).ljust(self.first_col_width, ' ')
            while progress:
                second_col = progress[:second_col_width]
                second_col = second_col.ljust(second_col_width, ' ')

                progress = progress[second_col_width:]

                # Apply styles *after* slicing and padding the string
                # (otherwise ANSI codes could be sliced in half).
                second_col = re.sub(r'\S', lambda x: self.styles_map[x[0]]
                                    (x[0]), second_col)

                lines.append(f'{line}{second_col}')

                if line[0] != ' ':
                    line = ' ' * self.first_col_width

        if (not last_render and self.failed_tests
                and self.FT_LABEL_LEN <= self.first_col_width
                and cols - self.first_col_width > 40):

            print_empty_line()

            line = (self.FT_LABEL + ' ' *
                    (self.first_col_width - self.FT_LABEL_LEN))

            failed_tests_lines = 1
            for testi, test in enumerate(self.failed_tests, 1):
                last = testi == len(self.failed_tests)

                if not last:
                    test += ', '

                test_name_len = len(test)

                if len(line) + test_name_len < cols:
                    line += test

                else:
                    if failed_tests_lines == self.FT_MAX_LINES:
                        if len(line) + 3 < cols:
                            line += '...'
                        break

                    else:
                        line += (cols - len(line)) * ' '
                        line = self._color_second_column(
                            line, styles.marker_errored)
                        lines.append(line)

                        failed_tests_lines += 1
                        line = self.first_col_width * ' '

                        if len(line) + test_name_len > cols:
                            continue

                        line += test

            line += (cols - len(line)) * ' '
            line = self._color_second_column(line, styles.marker_errored)
            lines.append(line)

        print_empty_line()
        print_line(
            f'Progress: {self.completed_tests}/{self.total_tests} tests.')

        if last_render:
            if self.max_lines > len(lines):
                for _ in range(self.max_lines - len(lines)):
                    lines.insert(0, ' ' * cols)
        else:
            # If it's not the last test, check if our render buffer
            # requires more rows than currently visible.
            if len(lines) + 1 > rows:
                # Scroll the render buffer to the bottom and
                # cut the lines from the beginning, so that it
                # will fit the screen.
                #
                # We need to do this because we can't move the
                # cursor past the visible screen area, so if we
                # render more data than the screen can fit, we
                # will have lot's of garbage output.
                lines = lines[len(lines) + 1 - rows:]
                lines[0] = '^' * cols

        # Hide cursor.
        print('\033[?25l', end='', flush=True, file=self.stream)
        try:
            # Use `print` (not `click.echo`) because we want to
            # precisely control when the output is flushed.
            print(clear_cmd + '\n'.join(lines), flush=False, file=self.stream)
        finally:
            # Show cursor.
            print('\033[?25h', end='', flush=True, file=self.stream)

        self.last_lines = len(lines)
        self.max_lines = max(self.last_lines, self.max_lines)
Beispiel #34
0
def get_content_trimmed(toot):
    (width, _) = click.get_terminal_size()
    trimlen = width*4//7  # very scientific
    return _ts_wrapper.shorten(collapse(get_content(toot)), trimlen)
Beispiel #35
0
    'blocking':      '\U0000274c', # thumbsdown '\U0001f44e', big X '\U0000274c', stopsign '\U0001f6d1'
    'muting':        '\U0001f6ab', # mute-spkr '\U0001f507', mute-bell '\U0001f515', prohibited '\U0001f6ab'
    'requested':     '\U00002753', # hourglass '\U0000231b', question '\U00002753'
    # catchall
    'unknown':       '\U0001f34d' }

_indent = "  "
_ts_wrapper = TootWrapper( width=75, tabsize=4,
                           initial_indent=_indent, subsequent_indent=_indent,
                           #replace_whitespace=False,
                           drop_whitespace=False,
                           break_long_words=False )
toot_parser = TootParser(indent='  ', width=75)
#_current_width = 0
#_current_height = 0
(_current_width, _current_height) = click.get_terminal_size()
random.seed()


### text wrapping
#def _format_paragraph(text):
#    width = click.get_terminal_size()[0]
#    print("width is "+str(width))
#    return text
#    return click.wrap_text(text, width=width-5, initial_indent=_indent, subsequent_indent=_indent, preserve_paragraphs=True)
#    #return click.wrap_text(text, width=width-5, initial_indent=_indent, subsequent_indent=_indent)

#####################################
######## STRING UTILITIES    ########
#####################################
### handy string collapser
Beispiel #36
0
 def __init__(self):
     self.question = 0
     self.options = {}
     self.term_width = min(click.get_terminal_size()[0], 78)
     self.e = click.secho
     self.w = partial(click.wrap_text, width=self.term_width)
Beispiel #37
0
def format_table(
    rows: Optional[List[List[str]]] = None,
    columns: Optional[List[List[str]]] = None,
    headers: Optional[List[str]] = None,
    padding: int = 2,
    alignment: Optional[Union[int, List[int]]] = None,
    wrap: bool = True,
):
    """
    Prints given data as a pretty table. Either rows or columns must be given.

    :param rows: List of strings for table rows.
    :param columns: List of strings for table columns.
    :param headers: List of strings for column titles.
    :param padding: Padding between columns.
    :param alignment: List of alignments for every column. Values can be ``LEFT``,
        ``CENTER``, ``RIGHT``. If not given, defaults to left alignment.
    :param wrap: If ``True``, wrap cell content to fit the terminal width.
    :returns: Formatted multi-line string.
    """

    if rows and columns:
        raise ValueError("Cannot give both rows and columns as input.")

    if not columns:
        if rows:
            columns = list(columns) if columns else _transpose(rows)
        else:
            raise ValueError("Must give either rows or columns as input.")

    if headers:
        for i, col in enumerate(columns):
            col.insert(0, headers[i])

    # return early if all columns are empty (including headers)
    if all(len(col) == 0 for col in columns):
        return ""

    # default to left alignment
    if not alignment:
        alignment = [LEFT] * len(columns)
    elif isinstance(alignment, int):
        alignment = [alignment] * len(columns)
    elif len(alignment) != len(columns):
        raise ValueError("Must give an alignment for every column.")

    # determine column widths from terminal width and padding

    col_widths = tuple(max(len(cell) for cell in col) for col in columns)

    if wrap:

        terminal_width, terminal_height = click.get_terminal_size()
        available_width = terminal_width - padding * len(columns)

        n = 3
        sum_widths = sum(w ** n for w in col_widths)
        subtract = max([sum(col_widths) - available_width, 0])
        col_widths = tuple(
            round(w - subtract * w ** n / sum_widths) for w in col_widths
        )

    # wrap strings to fit column

    wrapped_columns: List[List[List[str]]] = []

    for column, width in zip(columns, col_widths):
        wrapped_columns.append([textwrap.wrap(cell, width=width) for cell in column])

    wrapped_rows = _transpose(wrapped_columns)

    # generate lines by filling columns

    lines = []

    for row in wrapped_rows:
        n_lines = max(len(cell) for cell in row)
        for cell in row:
            cell += [""] * (n_lines - len(cell))

        for i in range(n_lines):
            line = ""
            for cell, width, align in zip(row, col_widths, alignment):

                if align == LEFT:
                    line += cell[i].ljust(width)
                elif align == RIGHT:
                    line += cell[i].rjust(width)
                elif align == CENTER:
                    line += cell[i].center(width)
                else:
                    raise ValueError(
                        "Alignment must be LEFT = 0, CENTER = 1, RIGHT = 2"
                    )

                line += " " * padding

            lines.append(line)

    return "\n".join(lines)
Beispiel #38
0
def ls(long: bool, dropbox_path: str, include_deleted: bool, config_name: str) -> None:

    from datetime import datetime
    from maestral.utils import natural_size

    if not dropbox_path.startswith("/"):
        dropbox_path = "/" + dropbox_path

    with MaestralProxy(config_name, fallback=True) as m:

        entries = m.list_folder(
            dropbox_path, recursive=False, include_deleted=include_deleted
        )
        entries.sort(key=lambda x: cast(str, x["name"]).lower())

    if long:

        names = []
        types = []
        sizes = []
        shared = []
        last_modified = []
        excluded = []

        to_short_type = {
            "FileMetadata": "file",
            "FolderMetadata": "folder",
            "DeletedMetadata": "deleted",
        }

        for e in entries:

            long_type = cast(str, e["type"])
            name = cast(str, e["name"])
            path_lower = cast(str, e["path_lower"])

            types.append(to_short_type[long_type])
            names.append(name)

            shared.append("shared" if "sharing_info" in e else "private")
            excluded.append(m.excluded_status(path_lower))

            if "size" in e:
                size = cast(float, e["size"])
                sizes.append(natural_size(size))
            else:
                sizes.append("-")

            if "client_modified" in e:
                cm = cast(str, e["client_modified"])
                dt = datetime.strptime(cm, "%Y-%m-%dT%H:%M:%S%z").astimezone()
                last_modified.append(dt.strftime("%d %b %Y %H:%M"))
            else:
                last_modified.append("-")

        click.echo("")
        click.echo(
            format_table(
                headers=["Name", "Type", "Size", "Shared", "Syncing", "Last modified"],
                columns=[names, types, sizes, shared, excluded, last_modified],
                alignment=[LEFT, LEFT, RIGHT, LEFT, LEFT, LEFT],
                wrap=False,
            ),
        )
        click.echo("")

    else:

        from maestral.utils import chunks

        names = []
        colors = []
        formatted_names = []
        max_len = 0

        for e in entries:
            name = cast(str, e["name"])

            max_len = max(max_len, len(name))
            names.append(name)
            colors.append("blue" if e["type"] == "DeletedMetadata" else None)

        max_len += 2  # add 2 spaces padding

        for name, color in zip(names, colors):
            formatted_names.append(click.style(name.ljust(max_len), fg=color))

        width, height = click.get_terminal_size()
        n_columns = max(width // max_len, 1)

        rows = chunks(formatted_names, n_columns)

        for row in rows:
            click.echo("".join(row))
Beispiel #39
0
import click

FRUITCONFIG_NAME = "fruitconfig.py"
FRUIT_DIR_NAME = ".fruit"
FRUIT_DIR_CONFIG = f"{FRUIT_DIR_NAME}/__init__.py"

WIDTH = (click.get_terminal_size()[0] - 10)

SEP_TARGET = "=" * WIDTH
SEP_STEP = "-" * WIDTH

FMT_STEPHEADER = "🥝 Step {number}: {name}\n" + "-" * WIDTH
FMT_TARGETHEADER = "🍉 Making '{target}' ...\n" + "=" * WIDTH
FMT_SUBTARGETHEADER = "🍎 Making sub-target '{target}' ..." + ">" * WIDTH

SHELLCHAR = '$ '

ICON_SUCC = "✅"
ICON_FAIL = "❌"
ICON_SKIP = "⏭"
ICON_BANANA = "🍌"

FMT_SKIPMESSAGE = ICON_SKIP + "  The step was skipped. {reason}"
FMT_FAILMESSAGE = ICON_BANANA + "  The step failed! {reason}"
Beispiel #40
0
def print_header(label, is_error=False):
    terminal_width, _ = click.get_terminal_size()
    width = len(click.unstyle(label))
    half_line = "=" * ((terminal_width - width - 2) / 2)
    click.echo("%s %s %s" % (half_line, label, half_line), err=is_error)
Beispiel #41
0
    def list_packages(self, installed=True, notinstalled=True):
        """Return a list with all the installed/notinstalled packages"""

        self.profile = Profile()

        # Classify packages
        installed_packages = []
        notinstalled_packages = []

        for package in self.packages:
            data = {
                'name': package,
                'version': None,
                'description': self.packages[package]['description']
            }
            if self.profile.check_package(package):
                data['version'] = self.profile.get_version(package)
                installed_packages += [data]
            else:
                notinstalled_packages += [data]

        # Print tables
        terminal_width, _ = click.get_terminal_size()

        if installed and installed_packages:

            # - Print installed packages table
            click.echo('\nInstalled packages:\n')

            PACKAGELIST_TPL = ('{name:20} {description:30} {version:<8}')

            click.echo('-' * terminal_width)
            click.echo(
                PACKAGELIST_TPL.format(name=click.style('Name', fg='cyan'),
                                       version='Version',
                                       description='Description'))
            click.echo('-' * terminal_width)

            for package in installed_packages:
                click.echo(
                    PACKAGELIST_TPL.format(name=click.style(package['name'],
                                                            fg='cyan'),
                                           version=package['version'],
                                           description=package['description']))

        if notinstalled and notinstalled_packages:

            # - Print not installed packages table
            click.echo('\nNot installed packages:\n')

            PACKAGELIST_TPL = ('{name:20} {description:30}')

            click.echo('-' * terminal_width)
            click.echo(
                PACKAGELIST_TPL.format(name=click.style('Name', fg='yellow'),
                                       description='Description'))
            click.echo('-' * terminal_width)

            for package in notinstalled_packages:
                click.echo(
                    PACKAGELIST_TPL.format(name=click.style(package['name'],
                                                            fg='yellow'),
                                           description=package['description']))
Beispiel #42
0
def cli(ctx, cache_seconds, log_level, context, namespace,
        table_format, maximum, no_headers, wide, match):
    '''Simple wrapper to help find specific Kubernetes pods and containers and run asynchronous
    commands (default is to list those that matched).

    \b
    MATCH       [<NODE>/]<POD>[/<CONTAINER>]
    \b
    NODE        provide a regular expression to select one or more nodes
    POD         provide a regular expression to select one or more pods
    CONTAINER   provide a regular expression to select one or more containers
    \b
    Partial match using just node or just node and pod, provide trailing slash:
        my-node//          match all pods and containers hosted in my-node
        my-node/my-pod/    match all containers hosted in my-node/my-pod
    '''

    logging.basicConfig(
        level=getattr(logging, log_level.upper()),
        format='[%(asctime)s #%(process)d] %(levelname)-8s %(name)-12s %(message)s',
        datefmt='%Y-%m-%dT%H:%M:%S%z'
    )
    global _logger
    _logger = logging.getLogger(__name__)

    if not wide:
        width, height = click.get_terminal_size()
        wide = width > 160

    highlight = sys.stdout.isatty()

    def highlight_with(color):
        if not highlight:
            return str

        def colorizer(obj):
            return click.style(str(obj), bold=True, fg=color)
        return colorizer

    hard_percent_limit = 80     # TODO: consider making cfg'abl
    soft_percent_limit = hard_percent_limit * (hard_percent_limit / 100.0)

    ctx.obj = OpenStruct(
        highlight_ok=highlight_with('green'),
        highlight_warn=highlight_with('yellow'),
        highlight_error=highlight_with('red'),
        hard_percent_limit=hard_percent_limit,
        soft_percent_limit=soft_percent_limit,
        cache_path=os.path.expanduser('~'),
        cache_seconds=cache_seconds,
        context=context,
        namespace=namespace,
        table_format=table_format,
        no_headers=no_headers,
        wide=wide,
        maximum=maximum,
        match=match,
    )
    ctx.obj.kubey = Kubey(ctx.obj)

    def handle_interrupt(signal, _frame):
        ctx.obj.kubey.kubectl.kill(signal)
        ctx.exit(22)
    signal.signal(signal.SIGINT, handle_interrupt)
    signal.signal(signal.SIGTERM, handle_interrupt)

    if not ctx.invoked_subcommand:
        ctx.invoke(list_pods)
Beispiel #43
0
def check_internal_updates(ctx, what):  # pylint: disable=too-many-branches
    last_check = app.get_state_item("last_check", {})
    interval = int(app.get_setting("check_%s_interval" % what)) * 3600 * 24
    if (time() - interval) < last_check.get(what + "_update", 0):
        return

    last_check[what + "_update"] = int(time())
    app.set_state_item("last_check", last_check)

    http.ensure_internet_on(raise_exception=True)

    outdated_items = []
    pm = PlatformPackageManager() if what == "platforms" else LibraryPackageManager()
    for pkg in pm.get_installed():
        if pkg.metadata.name in outdated_items:
            continue
        conds = [
            pm.outdated(pkg).is_outdated(),
            what == "platforms" and PlatformFactory.new(pkg).are_outdated_packages(),
        ]
        if any(conds):
            outdated_items.append(pkg.metadata.name)

    if not outdated_items:
        return

    terminal_width, _ = click.get_terminal_size()

    click.echo("")
    click.echo("*" * terminal_width)
    click.secho(
        "There are the new updates for %s (%s)" % (what, ", ".join(outdated_items)),
        fg="yellow",
    )

    if not app.get_setting("auto_update_" + what):
        click.secho("Please update them via ", fg="yellow", nl=False)
        click.secho(
            "`platformio %s update`"
            % ("lib --global" if what == "libraries" else "platform"),
            fg="cyan",
            nl=False,
        )
        click.secho(" command.\n", fg="yellow")
        click.secho(
            "If you want to manually check for the new versions "
            "without updating, please use ",
            fg="yellow",
            nl=False,
        )
        click.secho(
            "`platformio %s update --dry-run`"
            % ("lib --global" if what == "libraries" else "platform"),
            fg="cyan",
            nl=False,
        )
        click.secho(" command.", fg="yellow")
    else:
        click.secho("Please wait while updating %s ..." % what, fg="yellow")
        if what == "platforms":
            ctx.invoke(cmd_platform_update, platforms=outdated_items)
        elif what == "libraries":
            ctx.meta[CTX_META_STORAGE_DIRS_KEY] = [pm.package_dir]
            ctx.invoke(cmd_lib_update, libraries=outdated_items)
        click.echo()

        telemetry.send_event(category="Auto", action="Update", label=what.title())

    click.echo("*" * terminal_width)
    click.echo("")
Beispiel #44
0
 def get_rows(self):
     self.columns = click.get_terminal_size()[0]
Beispiel #45
0
 def __init__(self):
     self.rows = []
     self.width, self.height = click.get_terminal_size()
Beispiel #46
0
def print_nl():
    click.echo("-" * click.get_terminal_size()[0], nl=True)
Beispiel #47
0
def _format_spoiler_trimmed(toot):
    (width, _) = click.get_terminal_size()
    trimlen = width*2//7  # very scientific
    return _ts_wrapper.shorten(_format_spoiler(toot), width=trimlen)
Beispiel #48
0
def hr() -> None:
    """Prints a horizontal line."""
    width, _ = click.get_terminal_size()
    click.echo('-' * width)
Beispiel #49
0
 def _set_columns():
     w, _h = click.get_terminal_size()
     return ["export COLUMNS=%i" % w]
Beispiel #50
0
def print_per_tag_report(metricses):
    """Given a list of results from multiple ``per_tag_metrics()`` calls,
    return a human-readable report."""
    THIN_COLORS = {True: {'fg': 'green'}, False: {'fg': 'red'}}

    max_filename_len = max(len(metrics['filename']) for metrics in metricses)

    max_tag_len = max_default((len(tag['markup']) for metrics in metricses
                               for tag in metrics['tags']), inf)
    template_width_minus_tag = max_filename_len + 2 + 3 + 2 + 3 + 10
    tag_max_width = min(get_terminal_size()[0] - template_width_minus_tag,
                        max_tag_len)

    template = '{file_style}{file: >' + str(
        max_filename_len
    ) + '}{style_reset}  {tag_style}{tag_and_padding}   {error_type: >2}{style_reset}   {score}'
    style_reset = style('', reset=True)
    for metrics in sorted(metricses, key=lambda m: m['filename']):
        first = True
        true_negative_count = metrics['true_negative_count']
        all_right = not any(t['error_type'] for t in metrics['tags'])
        any_right = not all(t['error_type']
                            for t in metrics['tags']) or true_negative_count
        file_color = 'good' if all_right else (
            'medium' if any_right else 'bad')
        for tag in metrics['tags']:
            print(
                template.format(
                    file=metrics['filename'] if first else '',
                    file_style=style('', **FAT_COLORS[file_color],
                                     reset=False),
                    style_reset=style_reset,
                    tag_and_padding=fit_unicode(tag['markup'], tag_max_width),
                    tag_style=style('',
                                    **THIN_COLORS[not bool(tag['error_type'])],
                                    reset=False),
                    error_type=tag['error_type'],
                    score='pruned' if tag['score'] == 'pruned' else
                    thermometer(tag['score'])))
            first = False
        if first:
            # There were no targets and no errors, so we didn't print tags.
            print(
                template.format(file=metrics['filename'],
                                file_style=style('',
                                                 **FAT_COLORS['good'],
                                                 reset=False),
                                style_reset=style_reset,
                                tag_and_padding=fit_unicode(
                                    'No targets found.', tag_max_width),
                                tag_style=style('', fg='green', reset=False),
                                error_type='',
                                score=''))
        else:
            # We printed some tags. Also show the TNs so we get credit for them.
            if true_negative_count:
                print(
                    template.format(
                        file='',
                        file_style=style('',
                                         **FAT_COLORS[file_color],
                                         reset=False),
                        style_reset=style_reset,
                        tag_and_padding=fit_unicode(
                            f'   ...and {true_negative_count} correct negative'
                            + ('s' if true_negative_count > 1 else ''),
                            tag_max_width),
                        tag_style=style('', fg='green', reset=False),
                        error_type='',
                        score=''))
Beispiel #51
0
 def _get_term_width(self):
     return click.get_terminal_size()[0] or 70
Beispiel #52
0
  * {{ ingredient['name'] }}, **{{ ingredient['amount'].amount }} {{ ingredient['amount'].unit.symbol }}**
  {% else %}
  * {{ ingredient['name'] }}
  {% endif %}
{% endfor %}

---

### Steps

{% for step in RECIPE['steps'] %}
  {{ loop.index }}. {{ '**({:2d} minutes)**'.format(step['time']) if 'time' in step else '' }} {{ step['task'] }}
{% endfor %}
"""

mdv.term_columns = click.get_terminal_size()[0]


@cli.command(name='recipe', help='Display various aspects of a recipe.')
@click.argument('recipe_name', required=False)
@click.option('--recipe-id', default=None)
@click.option('--action', default='show', type=click.Choice(['show']))
@click.option('--scale', default=None)
@click.pass_context
def cmd_recipe(ctx, recipe_name, recipe_id, action, scale):
    logger = ctx.obj.logger

    if recipe_name is None and recipe_id is None:
        logger.error('Either pass recipe name, or use --recipe-id option.')
        return
Beispiel #53
0
def hr(char="-", width=None, **kwargs):
    if width is None:
        width = click.get_terminal_size()[0]
    click.secho(char * width, **kwargs)
Beispiel #54
0
def diff_color(input_file, cfg):
    """
    colorizes a diff file
    """
    cov = cfg.data

    with paged_echo() as pager:
        term_width = click.get_terminal_size()[0]
        modified = []
        measured = cov.get_data().measured_files()
        diff = PatchSet(input_file)
        for thing in diff:
            if thing.is_modified_file or thing.is_added_file:
                target = thing.target_file
                if target.startswith('b/') or target.startswith('a/'):
                    target = target[2:]
                if abspath(target) in measured:
                    covdata = cov._analyze(abspath(target))
                    modified.append((thing, covdata))
#                    pager.echo(abspath(target))
                else:
                    msg = "skip: {}".format(target)
                    msg = msg + (' ' * (term_width - len(msg)))
                    pager.echo(colors.color(msg, bg='yellow', fg='black'))

        total_added_lines = 0
        total_covered_lines = 0

        for (patch, covdata) in modified:
            fname = str(patch.target_file) + (' ' * (term_width - len(patch.target_file)))
            pager.echo(colors.color(fname, bg='cyan', fg='black'))
            for hunk in patch:
                for line in hunk:
                    kw = dict()
                    if line.is_added:
                        total_added_lines += 1
                        if line.target_line_no in covdata.missing:
                            pager.echo(colors.color(u'\u258c', fg='red', bg=52), nl=False, color=True)
                            kw['bg'] = 52
                        else:
                            total_covered_lines += 1
                            pager.echo(colors.color(u'\u258f', fg='green'), nl=False, color=True)
                    else:
                        pager.echo(' ', nl=False)
                    out = u"{}".format(line).strip()
                    if line.is_added:
                        kw['fg'] = 'green'
                    elif line.is_removed:
                        kw['fg'] = 'red'
                    pager.echo(colors.color(out, **kw))

        if total_added_lines == 0:
            raise click.ClickException("No covered lines at all")

        percent_covered = (total_covered_lines / float(total_added_lines))
        msg = u"{} covered of {} added lines".format(total_covered_lines, total_added_lines)
        print_banner(msg, percent_covered, pager=pager)
        return

        target_fname = abspath(target_fname)
        for fname in cov.get_data().measured_files():
            if target_fname == abspath(fname):
                match.append(fname)

        if len(match) != 1:
            if len(match) == 0:
                # this file wasn't in the coverage data, so we just dump
                # it to stdout as-is. (FIXME: ideally, also
                # syntax-highlighted anyway)
                with open(target_fname, 'r') as f:
                    for line in f.readlines():
                        sys.stdout.write(line)
                return
            else:
                raise RuntimeError("Multiple matches: %s" % ', '.join(match))

        fname = match[0]
        covdata = cov._analyze(fname)

        percent = 1.0  # if no statements, it's all covered, right?
        if covdata.numbers.n_statements:
            percent = float(covdata.numbers.n_statements - covdata.numbers.n_missing) / covdata.numbers.n_statements
        total_statements = covdata.numbers.n_statements
        total_missing = covdata.numbers.n_missing

        fill = min(click.get_terminal_size()[0], 80)
        print_banner(fname, percent, fill)

        # it was tempting to write/override/wrap this Formatter and mess
        # with the background color based on our coverage data -- and
        # that's not a terrible idea, but the way TerminalFormatter is
        # written, it's not very nice. Basically, we'd have to wrap the
        # output stream, look for ANSI reset codes, and re-do the
        # background color after each reset (for "uncovered" lines)...  so
        # I didn't do that. Instead we just hack it by splitting on the
        # reset codes (see below)

        formatter = TerminalFormatter(style=style)
        lines = highlight(
            open(fname).read(), get_lexer_by_name('python'),
            formatter=formatter,
        )
        lines = lines.split(u'\n')

        for (i, line) in enumerate(lines):
    #        assert type(line) is unicode
            spaces = fill - len(colors.strip_color(line))
            spaces = u' ' * spaces
            if (i + 1) not in covdata.missing:
                if (i + 1) in covdata.excluded:
                    line = colors.strip_color(line)
                    pager.echo(colors.color(u'\u258f', fg=46, bg=236) + colors.color(line + spaces, bg=236, fg=242), color=True)
                elif cfg.branch and (i + 1) in covdata.branch_lines():
                    line = colors.strip_color(line)
                    pager.echo(colors.color(u'\u258f', bg=52, fg=160) + colors.color(line + spaces, bg=52), color=True)
                else:
                    pager.echo(u'{}{}{}'.format(colors.color(u'\u258f', fg=46), line, spaces), color=True)
            else:
                # HACK-O-MATIC, uhm. Yeah, so what we're doing here is
                # splitting the output from the formatter on the ANSI
                # "reset" code, and the re-assembling it with the "52"
                # (dark red) background color. I appoligize in advance;
                # PRs with improvements encouraged!
                reset_code = u"\x1b[39;49;00m"
                segments = (line + spaces).split(reset_code)
                reset_plus_bg = u"\x1b[39;49;00m\x1b[39;49;48;5;52m"
                out = u"\x1b[39;49;48;5;52m" + reset_plus_bg.join(segments)
                pager.echo(colors.color(u'\u258f', bg=52, fg=160) + out, color=True)
Beispiel #55
0
 def get_reserved_space(self):
     """Get the number of lines to reserve for the completion menu."""
     reserved_space_ratio = 0.45
     max_reserved_space = 8
     _, height = click.get_terminal_size()
     return min(int(round(height * reserved_space_ratio)), max_reserved_space)
Beispiel #56
0
def print_processing_header(tool, envname, envdump):
    click.echo(
        "Checking %s > %s (%s)" %
        (click.style(envname, fg="cyan", bold=True), tool, "; ".join(envdump)))
    terminal_width, _ = click.get_terminal_size()
    click.secho("-" * terminal_width, bold=True)
Beispiel #57
0
def check_internal_updates(ctx, what):
    last_check = app.get_state_item("last_check", {})
    interval = int(app.get_setting("check_%s_interval" % what)) * 3600 * 24
    if (time() - interval) < last_check.get(what + "_update", 0):
        return

    last_check[what + '_update'] = int(time())
    app.set_state_item("last_check", last_check)

    util.internet_on(raise_exception=True)

    pm = PlatformManager() if what == "platforms" else LibraryManager()
    outdated_items = []
    for manifest in pm.get_installed():
        if manifest['name'] in outdated_items:
            continue
        conds = [
            pm.outdated(manifest['__pkg_dir']), what == "platforms"
            and PlatformFactory.newPlatform(
                manifest['__pkg_dir']).are_outdated_packages()
        ]
        if any(conds):
            outdated_items.append(manifest['name'])

    if not outdated_items:
        return

    terminal_width, _ = click.get_terminal_size()

    click.echo("")
    click.echo("*" * terminal_width)
    click.secho(
        "There are the new updates for %s (%s)" % (what,
                                                   ", ".join(outdated_items)),
        fg="yellow")

    if not app.get_setting("auto_update_" + what):
        click.secho("Please update them via ", fg="yellow", nl=False)
        click.secho(
            "`platformio %s update`" % ("lib --global" if what == "libraries"
                                        else "platform"),
            fg="cyan",
            nl=False)
        click.secho(" command.\n", fg="yellow")
        click.secho(
            "If you want to manually check for the new versions "
            "without updating, please use ",
            fg="yellow",
            nl=False)
        click.secho(
            "`platformio %s update --only-check`" %
            ("lib --global" if what == "libraries" else "platform"),
            fg="cyan",
            nl=False)
        click.secho(" command.", fg="yellow")
    else:
        click.secho("Please wait while updating %s ..." % what, fg="yellow")
        if what == "platforms":
            ctx.invoke(cmd_platform_update, platforms=outdated_items)
        elif what == "libraries":
            ctx.obj = pm
            ctx.invoke(cmd_lib_update, libraries=outdated_items)
        click.echo()

        telemetry.on_event(
            category="Auto", action="Update", label=what.title())

    click.echo("*" * terminal_width)
    click.echo("")
Beispiel #58
0
    def format_lines(self, width: Optional[int] = None) -> Iterator[str]:
        """
        Iterator over formatted lines of the table. Fields may span multiple lines if
        they are set to wrap instead of truncate.

        :param width: Width to fit the table.
        :returns: Iterator over lines which can be printed to the terminal.
        """

        # get terminal width if no width is given
        if not width:
            width, height = click.get_terminal_size()

        available_width = width - self.padding * len(self.columns)
        raw_col_widths = [col.display_width for col in self.columns]

        # Allocate column width from available width,
        # weighted by the raw width of each column.
        n = 3
        sum_widths = sum(w**n for w in raw_col_widths)
        subtract = max([sum(raw_col_widths) - available_width, 0])
        allocated_col_widths = tuple(
            round(w - subtract * w**n / sum_widths) for w in raw_col_widths)

        spacer = " " * self.padding

        # generate line for titles
        if any(col.has_title for col in self.columns):
            titles: List[str] = []

            for col, width in zip(self.columns, allocated_col_widths):
                if col.title:
                    titles.append(col.title.format(width)[0])
                else:
                    titles.append(adjust("", width))

            line = spacer.join(titles)
            yield line.rstrip()

        # generate lines for rows
        for row in self.rows():
            cells = []

            for field, alloc_width in zip(row, allocated_col_widths):
                cells.append(field.format(alloc_width))

            n_lines = max(len(cell) for cell in cells)

            for i in range(n_lines):

                line_parts = []

                for cell_lines, alloc_width in zip(cells,
                                                   allocated_col_widths):
                    try:
                        line_parts.append(cell_lines[i])
                    except IndexError:
                        line_parts.append(adjust("", width=alloc_width))

                line = spacer.join(line_parts)
                yield line.rstrip()
Beispiel #59
0
    def generate_summary(self):

        _CONVERSION_VALUE = _BYTE_TO_KILO_BYTE_RATIO

        if self._converter == 'MB':
            _CONVERSION_VALUE = _BYTE_TO_MEGA_BYTE_RATIO
        if self._converter == 'GB':
            _CONVERSION_VALUE = _BYTE_TO_GIGA_BYTE_RATIO

        if self._input_size is None or self._model is None:
            raise Exception("Input Size {} or Model {} is not defined".format(
                self._input_size, self._model))

        sm1 = self.network_summary()

        total = 0
        trainable_params = 0
        total_output = 0
        layers = []
        input_shapes = []
        output_shapes = []
        param_items = []
        param_items_bytes = []
        trainable_param_items_bytes = []
        trainable_params_list = []
        # append for table print
        row_data = []

        save_str = ""
        '''
        Generate Header of the stats file
        Use default delimiter or any other symbol other than "[" or ","  or "("
        "," Is used within internal data structures, it could complicate file reading with Excel, LibCalc, etc
        '''

        if self._delimiter == ",":
            raise Exception(
                " ',' Is used within internal data structures, it could complicate file reading with Excel, LibCalc, etc"
            )

        if self._save:
            for col_id, col_name in enumerate(self._header):
                save_str += col_name + self._delimiter
            save_str += "\n"

        ################################################################################################################
        ########################################## Iterate Through Layers ##############################################
        ################################################################################################################

        for layer in sm1:
            layer_name = layer
            input_shape = str(sm1[layer]["input_shape"])
            output_value = sm1[layer]["output_shape"]
            output_shape = str(output_value)
            params = sm1[layer]["nb_params"]
            layers.append(layer_name)
            input_shapes.append(input_shape)
            output_shapes.append(output_shape)
            trainable_param = 0

            if isinstance(params, th.Tensor):
                params = params.item()

            param_items.append(params)
            params_in_bytes = params * self._bytes_per_param / _CONVERSION_VALUE
            param_items_bytes.append(params_in_bytes)

            if "trainable" in sm1[layer]:
                if sm1[layer]["trainable"] == True:
                    trainable_param = params
                    trainable_params += trainable_param

            trainable_param_items_bytes.append(trainable_param)
            trainable_param_in_bytes = trainable_param * self._bytes_per_param / _CONVERSION_VALUE
            total += params

            total_output += th.prod(th.Tensor(output_value)).item()
            row_data.append([
                layer_name, input_shape, output_shape, params, params_in_bytes,
                trainable_param, trainable_param_in_bytes
            ])

            if self._save:
                save_str += layer_name + self._delimiter + str(input_shape) + self._delimiter + str(
                    output_shape) + self._delimiter + str(
                    params) + self._delimiter + str(
                    params_in_bytes) + self._delimiter + str(trainable_param) + self._delimiter + str(
                    trainable_param_in_bytes) \
                            + "\n"

            alignment = ""

        ################################################################################################################
        ################################################################################################################
        ################################################################################################################

        # total number of parameters
        self._total_params = total
        # total memory of parameter
        self._total_params_memory = total * self._bytes_per_param / _CONVERSION_VALUE
        # total trainable number of parameters
        self._total_trainable_params = trainable_params
        # total memory of trainable parameters
        self._total_trainable_params_memory = trainable_params * self._bytes_per_param / _CONVERSION_VALUE
        # total number forward parameters
        self._forward_params = total_output
        # total memory of forward parameters
        self._forward_params_memory = total_output * self._bytes_per_param / _CONVERSION_VALUE
        # backward and forward params are equal
        self._backward_params = self._forward_params
        self._backward_params_memory = self._forward_params_memory
        self._total_activation_parameters = self._forward_params + self._backward_params
        self._total_activation_memory = self._forward_params_memory + self._backward_params_memory
        self._total_input_params = th.prod(th.Tensor(
            self._input_size)).item() * self._batch_size
        self._total_input_memory = self._total_input_params * self._bytes_per_param / (
            _CONVERSION_VALUE)

        row_data.append([
            'Total Values', '', '',
            str(self._total_params),
            str(self._total_params_memory),
            str(self._total_trainable_params),
            str(self._total_trainable_params_memory)
        ])

        if self._save:
            save_str += 'Total Values' + self._delimiter + '' + self._delimiter + '' + self._delimiter \
                        + str(self._total_params) + self._delimiter + str(self._total_params_memory) + self._delimiter \
                        + str(self._total_trainable_params) + self._delimiter + str(
                self._total_trainable_params_memory) + "\n"

        if self._console:
            for _, _ in enumerate(self._header):
                alignment += "c"

            tt.print(row_data,
                     header=self._header,
                     style=tt.styles.ascii_thin,
                     padding=(1, 1, 1, 1),
                     alignment=alignment)

        summary_statement = "" + "\n"

        if self._console or self._save:
            self._delimiter = " = "
            summary_statement += "MiniBatch Size" + self._delimiter + str(
                self._batch_size) + "\n"
            summary_statement += "Total Input Parameters " + self._delimiter + str(
                self._total_input_params) + "\n"
            summary_statement += "Total Input Memory " + self._delimiter + str(
                self._total_input_memory) + " " + self._converter + "\n"
            summary_statement += 'Forward Parameters' + self._delimiter + str(
                self._forward_params) + "\n"
            summary_statement += 'Forward Parameters Memory' + self._delimiter + str(
                self._forward_params_memory) + " " + self._converter + "\n"
            summary_statement += 'Backward Parameters' + self._delimiter + str(
                self._backward_params) + "\n"
            summary_statement += 'Backward Parameters Memory' + self._delimiter + str(
                self._backward_params_memory) + " " + self._converter + "\n"
            summary_statement += 'Total Activation Parameters' + self._delimiter + str(
                self._forward_params + self._backward_params) + "\n"
            summary_statement += 'Total Activation Parameters Memory' + self._delimiter + str(
                (self._forward_params_memory +
                 self._backward_params_memory)) + " " + self._converter + "\n"
            width, _ = click.get_terminal_size()
            click.echo('*' * width)
            print(summary_statement)
            click.echo('*' * width)
            save_str += summary_statement

        if self._save_path:
            with open(self._save_path, "w") as fp:
                fp.write(save_str)
        else:
            raise Exception("Save Path not specified")
Beispiel #60
0
def print_separator():
    click.echo("=" * (min(40, click.get_terminal_size()[0])))