From 63f1939c835c10eae2e553b78b6cc132c6a98a55 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Mon, 24 Oct 2016 21:03:54 +0300 Subject: [PATCH] PIO Account, Subscription, other improvements --- platformio/__init__.py | 2 +- platformio/commands/account.py | 55 ++++++++++++++++++++++++++++++++++ platformio/commands/run.py | 4 +-- platformio/pioplus.py | 2 +- platformio/util.py | 48 ++++++++++++++++++----------- 5 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 platformio/commands/account.py diff --git a/platformio/__init__.py b/platformio/__init__.py index 0840fa67..6c7aaeb4 100644 --- a/platformio/__init__.py +++ b/platformio/__init__.py @@ -14,7 +14,7 @@ import sys -VERSION = (3, 2, "0a7") +VERSION = (3, 2, "0a8") __version__ = ".".join([str(s) for s in VERSION]) __title__ = "platformio" diff --git a/platformio/commands/account.py b/platformio/commands/account.py new file mode 100644 index 00000000..eb4c923c --- /dev/null +++ b/platformio/commands/account.py @@ -0,0 +1,55 @@ +# Copyright 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# pylint: disable=unused-argument + +import sys + +import click + +from platformio.pioplus import pioplus_call + + +@click.group("account", short_help="Manage PIO Account") +def cli(): + pass + + +@cli.command("register", short_help="Create new PIO Account") +@click.option("-u", "--username") +def account_register(**kwargs): + pioplus_call(sys.argv[1:]) + + +@cli.command("login", short_help="Log in to PIO Account") +@click.option("-u", "--username") +@click.option("-p", "--password") +def account_login(**kwargs): + pioplus_call(sys.argv[1:]) + + +@cli.command("logout", short_help="Log out of PIO Account") +def account_logout(): + pioplus_call(sys.argv[1:]) + + +@cli.command("password", short_help="Change password") +def account_password(): + pioplus_call(sys.argv[1:]) + + +@cli.command("forgot", short_help="Forgot password") +@click.option("-u", "--username") +def account_forgot(**kwargs): + pioplus_call(sys.argv[1:]) diff --git a/platformio/commands/run.py b/platformio/commands/run.py index 652b204a..bd06ae22 100644 --- a/platformio/commands/run.py +++ b/platformio/commands/run.py @@ -203,8 +203,8 @@ class EnvironmentProcessor(object): # warn about unknown options if k not in self.KNOWN_OPTIONS: click.secho( - "Detected non-PlatformIO `%s` option in `[env:]` section" - % k, + "Detected non-PlatformIO `%s` option in `[env:]` section" % + k, fg="yellow") result[k] = v return result diff --git a/platformio/pioplus.py b/platformio/pioplus.py index 07704885..43d1eb40 100644 --- a/platformio/pioplus.py +++ b/platformio/pioplus.py @@ -23,7 +23,7 @@ from platformio.managers.package import PackageManager PACKAGE_DEPS = {"pysite": {"name": "pysite-pioplus", "requirements": ">=0.1.0"}, "tool": {"name": "tool-pioplus", - "requirements": ">=0.2.0"}} + "requirements": ">=0.3.0"}} class PioPlusPackageManager(PackageManager): diff --git a/platformio/util.py b/platformio/util.py index 235dea68..fe47326f 100644 --- a/platformio/util.py +++ b/platformio/util.py @@ -392,8 +392,10 @@ def get_logicaldisks(): match = disknamere.search(line.strip()) if not match: continue - disks.append({"disk": match.group(1), - "name": basename(match.group(1))}) + disks.append({ + "disk": match.group(1), + "name": basename(match.group(1)) + }) return disks @@ -407,33 +409,43 @@ def _api_request_session(): return requests.Session() -def _get_api_result( - path, # pylint: disable=too-many-branches - params=None, - data=None): +def _get_api_result(url, # pylint: disable=too-many-branches + params=None, + data=None, + auth=None): from platformio.app import get_setting result = None r = None + disable_ssl_check = sys.version_info < (2, 7, 9) headers = get_request_defheaders() - url = __apiurl__ - - if not get_setting("enable_ssl"): - url = url.replace("https://", "http://") + if not url.startswith("http"): + url = __apiurl__ + url + if not get_setting("enable_ssl"): + url = url.replace("https://", "http://") try: if data: r = _api_request_session().post( - url + path, params=params, data=data, headers=headers) + url, + params=params, + data=data, + headers=headers, + auth=auth, + verify=disable_ssl_check) else: - r = _api_request_session().get(url + path, + r = _api_request_session().get(url, params=params, - headers=headers) + headers=headers, + auth=auth, + verify=disable_ssl_check) result = r.json() r.raise_for_status() except requests.exceptions.HTTPError as e: - if result and "errors" in result: + if result and "message" in result: + raise exception.APIRequestError(result['message']) + elif result and "errors" in result: raise exception.APIRequestError(result['errors'][0]['title']) else: raise exception.APIRequestError(e) @@ -446,11 +458,11 @@ def _get_api_result( return result -def get_api_result(path, params=None, data=None, cache_valid=None): +def get_api_result(url, params=None, data=None, auth=None, cache_valid=None): from platformio.app import LocalCache total = 0 max_retries = 5 - cache_key = (LocalCache.key_from_args(path, params, data) + cache_key = (LocalCache.key_from_args(url, params, data, auth) if cache_valid else None) while total < max_retries: try: @@ -459,7 +471,7 @@ def get_api_result(path, params=None, data=None, cache_valid=None): result = lc.get(cache_key) if result is not None: return result - result = _get_api_result(path, params, data) + result = _get_api_result(url, params, data) if cache_valid: with LocalCache() as lc: lc.set(cache_key, result, cache_valid) @@ -478,7 +490,7 @@ def get_api_result(path, params=None, data=None, cache_valid=None): sleep(2 * total) raise exception.APIRequestError( - "Could not connect to PlatformIO Registry Service. " + "Could not connect to PlatformIO API Service. " "Please try later.")