Implement "lib search" command

This commit is contained in:
Ivan Kravets
2014-09-03 23:03:49 +03:00
parent 69b73935d0
commit 3d179fe1a7
3 changed files with 47 additions and 0 deletions

View File

@@ -14,4 +14,5 @@ __email__ = "me@ikravets.com"
__license__ = "MIT Licence"
__copyright__ = "Copyright (C) 2014 Ivan Kravets"
__apiurl__ = "http://api.platformio.ikravets.com"
__pkgmanifesturl__ = "http://platformio.ikravets.com/packages/manifest.json"

View File

@@ -0,0 +1,41 @@
# Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.
from click import argument, group, echo, style, secho
from requests import get
from requests.exceptions import ConnectionError
from platformio import __apiurl__
from platformio.exception import APIRequestError
def get_api_result(query):
result = None
r = None
try:
r = get(__apiurl__ + query)
result = r.json()
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
@group(short_help="Library Manager")
def cli():
pass
@cli.command("search", short_help="Search for library")
@argument("query")
def lib_search(query):
result = get_api_result("/lib/search?query=%s" % query)
secho("Found [ %d ] libraries:" % result['total'],
fg="green" if result['total'] else "yellow")
for item in result['items']:
echo("{name:<30} {info}".format(name=style(item['name'], fg="cyan"),
info=item['description']))

View File

@@ -105,3 +105,8 @@ class GetSerialPortsError(PlatformioException):
class GetLatestVersionError(PlatformioException):
MESSAGE = "Can't retrieve latest PlatformIO version"
class APIRequestError(PlatformioException):
MESSAGE = "[API] %s"