From c504001f04ec5545855dcd6200766c4f8be648d4 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Fri, 23 Dec 2016 21:57:11 +0200 Subject: [PATCH] PlatformIO Library Registry statistics with new `pio lib stats` --- HISTORY.rst | 9 +++- docs | 2 +- platformio/__init__.py | 2 +- platformio/app.py | 4 +- platformio/commands/lib.py | 84 ++++++++++++++++++++++++++++++++- platformio/commands/platform.py | 5 +- platformio/vcsclient.py | 5 +- 7 files changed, 98 insertions(+), 13 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 25a9d506..859153b4 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,9 +4,16 @@ Release Notes PlatformIO 3.0 -------------- -3.2.2 (2016-12-??) +3.3.0 (2016-12-??) ~~~~~~~~~~~~~~~~~~ +* PlatformIO Library Registry statistics with new + `pio lib stats `__ command + + - Recently updated and added libraries + - Recent and popular keywords + - Featured libraries (today, week, month) + * Added support for templated methods in ``*.ino to *.cpp`` convertor (`pull #858 `_) * Produce less noisy output when ``-s/--silent`` options are used for diff --git a/docs b/docs index c3780c69..fb1008d6 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit c3780c6913f16201da1c29643a8f4ae941d216df +Subproject commit fb1008d660f0d8a79306fe12319c6075f63b1690 diff --git a/platformio/__init__.py b/platformio/__init__.py index 7959277b..b5eb950a 100644 --- a/platformio/__init__.py +++ b/platformio/__init__.py @@ -14,7 +14,7 @@ import sys -VERSION = (3, 2, "2a1") +VERSION = (3, 3, "0a1") __version__ = ".".join([str(s) for s in VERSION]) __title__ = "platformio" diff --git a/platformio/app.py b/platformio/app.py index 8df17bb4..dae2141c 100644 --- a/platformio/app.py +++ b/platformio/app.py @@ -325,7 +325,7 @@ def get_cid(): except: # pylint: disable=bare-except pass cid = str( - uuid.UUID(bytes=hashlib.md5( - str(_uid if _uid else uuid.getnode())).digest())) + uuid.UUID(bytes=hashlib.md5(str(_uid if _uid else uuid.getnode())) + .digest())) set_state_item("cid", cid) return cid diff --git a/platformio/commands/lib.py b/platformio/commands/lib.py index 25cc749e..81f6c670 100644 --- a/platformio/commands/lib.py +++ b/platformio/commands/lib.py @@ -13,8 +13,10 @@ # limitations under the License. import json +from datetime import datetime from os.path import join from time import sleep +from urllib import quote import click @@ -44,7 +46,7 @@ from platformio.util import get_api_result @click.pass_context def cli(ctx, **options): # skip commands that don't need storage folder - if ctx.invoked_subcommand in ("search", "register") or \ + if ctx.invoked_subcommand in ("search", "register", "stats") or \ (len(ctx.args) == 2 and ctx.args[1] in ("-h", "--help")): return storage_dir = options['storage_dir'] @@ -116,7 +118,7 @@ def lib_update(lm, libraries, only_check): ####### -LIBLIST_TPL = ("[{id:^14}] {name:<25} {compatibility:<30} " +LIBLIST_TPL = ("[{id:^15}] {name:<25} {compatibility:<30} " "\"{authornames}\": {description}") @@ -317,3 +319,81 @@ def lib_register(config_url): result['message'], fg="green" if "successed" in result and result['successed'] else "red") + + +@cli.command("stats", short_help="Library Registry Statistics") +@click.option("--json-output", is_flag=True) +def lib_stats(json_output): + result = get_api_result("/lib/stats", cache_valid="1h") + + if json_output: + return click.echo(json.dumps(result)) + + printitem_tpl = "{name:<33} {url}" + printitemdate_tpl = "{name:<33} {date:23} {url}" + + def _print_title(title): + click.secho(title.upper(), bold=True) + click.echo("*" * len(title)) + + 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) + + def _print_lib_item(item): + click.echo((printitemdate_tpl + if "date" in item else printitem_tpl).format( + name=click.style( + item['name'], fg="cyan"), + date=str( + datetime.strptime(item['date'].replace("Z", "UTC"), + "%Y-%m-%dT%H:%M:%S%Z") + if "date" in item else ""), + url=click.style( + "http://platformio.org/lib/show/%s/%s" % (item[ + 'id'], quote(item['name'])), + fg="blue"))) + + def _print_tag_item(name): + click.echo( + printitem_tpl.format( + name=click.style( + name, fg="cyan"), + url=click.style( + "http://platformio.org/lib/search?query=" + quote( + "keyword:%s" % name), + fg="blue"))) + + for key in ("updated", "added"): + _print_title("Recently " + key) + _print_header(with_date=True) + for item in result.get(key, []): + _print_lib_item(item) + click.echo() + + _print_title("Recent keywords") + _print_header(with_date=False) + for item in result.get("lastkeywords"): + _print_tag_item(item) + click.echo() + + _print_title("Popular keywords") + _print_header(with_date=False) + for item in result.get("topkeywords"): + _print_tag_item(item) + click.echo() + + for key, title in (("dlday", "Today"), ("dlweek", "Week"), + ("dlmonth", "Month")): + _print_title("Featured: " + title) + _print_header(with_date=False) + for item in result.get(key, []): + _print_lib_item(item) + click.echo() diff --git a/platformio/commands/platform.py b/platformio/commands/platform.py index fc919405..f4a34241 100644 --- a/platformio/commands/platform.py +++ b/platformio/commands/platform.py @@ -141,9 +141,8 @@ def platform_list(json_output): "url": p.vendor_url, # "packages": p.packages.keys(), # dump all packages "packages": p.get_installed_packages().keys(), - 'forDesktop': any([ - p.name.startswith(n) for n in ("native", "linux", "windows") - ]) + 'forDesktop': + any([p.name.startswith(n) for n in ("native", "linux", "windows")]) }) if json_output: diff --git a/platformio/vcsclient.py b/platformio/vcsclient.py index 2f271968..c7519e61 100644 --- a/platformio/vcsclient.py +++ b/platformio/vcsclient.py @@ -177,9 +177,8 @@ class SvnClient(VCSClientBase): return self.run_cmd(args) def get_current_revision(self): - output = self.get_cmd_output([ - "info", "--non-interactive", "--trust-server-cert", "-r", "HEAD" - ]) + output = self.get_cmd_output( + ["info", "--non-interactive", "--trust-server-cert", "-r", "HEAD"]) for line in output.split("\n"): line = line.strip() if line.startswith("Revision:"):