Implement lib commands: install, uninstall, show, update, list

This commit is contained in:
Ivan Kravets
2014-09-04 18:58:12 +03:00
parent 3c030edc38
commit bac0a5ebb1
5 changed files with 234 additions and 26 deletions

View File

@@ -8,9 +8,14 @@ from platform import system, uname
from subprocess import PIPE, Popen
from time import sleep
from requests import get
from requests.exceptions import ConnectionError, HTTPError
from requests.utils import default_user_agent
from serial import Serial
from platformio.exception import GetSerialPortsError, NotPlatformProject
from platformio import __apiurl__, __version__
from platformio.exception import (APIRequestError, GetSerialPortsError,
NotPlatformProject)
try:
from configparser import ConfigParser
@@ -36,6 +41,17 @@ def get_home_dir():
return expanduser("~/.platformio")
def get_lib_dir():
try:
config = get_project_config()
if (config.has_section("platformio") and
config.has_option("platformio", "lib_dir")):
return config.get("platformio", "lib_dir")
except NotPlatformProject:
pass
return join(get_home_dir(), "lib")
def get_source_dir():
return dirname(realpath(__file__))
@@ -96,3 +112,27 @@ def get_serialports():
else:
raise GetSerialPortsError(os_name)
return[{"port": p, "description": d, "hwid": h} for p, d, h in comports()]
def get_api_result(path, params=None):
result = None
r = None
try:
headers = {"User-Agent": "PlatformIO/%s %s" % (
__version__, default_user_agent())}
r = get(__apiurl__ + path, params=params, headers=headers)
result = r.json()
r.raise_for_status()
except HTTPError as e:
if result and "errors" in result:
raise APIRequestError(result['errors'][0]['title'])
else:
raise APIRequestError(e)
except ConnectionError:
raise APIRequestError("Could not connect to PlatformIO API Service")
except ValueError:
raise APIRequestError("Invalid response: %s" % r.text)
finally:
if r:
r.close()
return result