From 5c4996873a5042ec0894205fd55e86586ddcc2d6 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 25 Jan 2018 20:54:13 -0500 Subject: [PATCH] Start using click for cli --- piker/brokers/__init__.py | 22 ---------------------- piker/brokers/cli.py | 29 +++++++++++++++++++++++++++++ piker/brokers/questrade.py | 18 ++++++++++++------ setup.py | 6 ++++-- 4 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 piker/brokers/cli.py diff --git a/piker/brokers/__init__.py b/piker/brokers/__init__.py index a78fb558..54afc783 100644 --- a/piker/brokers/__init__.py +++ b/piker/brokers/__init__.py @@ -1,25 +1,3 @@ """ Broker clients, daemons and general back end machinery. """ -import sys -import trio -from pprint import pformat -from .questrade import serve_forever -from ..log import get_console_log - - -def main() -> None: - log = get_console_log('info', name='questrade') - argv = sys.argv[1:] - - refresh_token = None - if argv: - refresh_token = argv[0] - - # main loop - try: - client = trio.run(serve_forever, refresh_token) - except Exception as err: - log.exception(err) - else: - log.debug(f"Exiting with last access info:\n{pformat(client.access_data)}\n") diff --git a/piker/brokers/cli.py b/piker/brokers/cli.py new file mode 100644 index 00000000..2980d785 --- /dev/null +++ b/piker/brokers/cli.py @@ -0,0 +1,29 @@ +""" +Console interface to broker client/daemons. +""" +from pprint import pformat +import click +import trio +from ..log import get_console_log + + +def run(loglevel, main): + log = get_console_log(loglevel) + + # main loop + try: + client = trio.run(main) + except Exception as err: + log.exception(err) + else: + log.debug( + f"Exiting with last access info:\n{pformat(client.access_data)}\n") + + +@click.command() +@click.option('--broker', default='questrade', help='Broker backend to use') +@click.option('--loglevel', '-l', default='warning', help='Logging level') +def pikerd(broker, loglevel): + # import broker module daemon entry point + from .questrade import serve_forever + run(loglevel, serve_forever) diff --git a/piker/brokers/questrade.py b/piker/brokers/questrade.py index 3e9839b8..6d65d44f 100644 --- a/piker/brokers/questrade.py +++ b/piker/brokers/questrade.py @@ -5,6 +5,7 @@ from . import config from ..log import get_logger from pprint import pformat import time +import datetime from async_generator import asynccontextmanager # TODO: move to urllib3/requests once supported @@ -113,14 +114,19 @@ class Client: """ access_token = self.access_data.get('access_token') expires = float(self.access_data.get('expires_at', 0)) + expires_stamp = datetime.datetime.fromtimestamp( + expires).strftime('%Y-%m-%d %H:%M:%S') if not access_token or (expires < time.time()) or force_refresh: log.info(f"Refreshing access token {access_token} which expired at" - f" {expires}") + f" {expires_stamp}") data = await self._new_auth_token() # store absolute token expiry time self.access_data['expires_at'] = time.time() + float( data['expires_in']) + else: + log.info(f"\nCurrent access token {access_token} expires at" + f" {expires_stamp}\n") self._prep_sess() return self.access_data @@ -141,7 +147,7 @@ def get_config() -> "configparser.ConfigParser": @asynccontextmanager -async def get_client(refresh_token: str = None) -> Client: +async def get_client() -> Client: """Spawn a broker client. """ conf = get_config() @@ -150,8 +156,8 @@ async def get_client(refresh_token: str = None) -> Client: await client.enable_access() try: - try: # do a test ping to ensure the access token works - log.debug("Check time to ensure access token is valid") + log.debug("Check time to ensure access token is valid") + try: await client.api.time() except Exception as err: # access token is likely no good @@ -169,10 +175,10 @@ async def get_client(refresh_token: str = None) -> Client: config.write(conf) -async def serve_forever(refresh_token: str = None) -> None: +async def serve_forever() -> None: """Start up a client and serve until terminated. """ - async with get_client(refresh_token) as client: + async with get_client() as client: # pretty sure this doesn't work # await client._revoke_auth_token() return client diff --git a/setup.py b/setup.py index ca2daa3e..ae606424 100755 --- a/setup.py +++ b/setup.py @@ -28,10 +28,12 @@ setup( ], entry_points={ 'console_scripts': [ - 'pikerd = piker.brokers:main', + 'pikerd = piker.brokers.cli:pikerd', ] }, - install_requires=['click', 'colorlog', 'trio', 'attrs'], + install_requires=[ + 'click', 'colorlog', 'trio', 'attrs', 'async_generator' + ], extras_require={ 'questrade': ['asks'], },