diff --git a/agkyra/cli.py b/agkyra/cli.py index 463caf2b7bec5a29f61bae555e7fdc659478704a..5f55e67bb5175b2e4b809f1cb52a004353d18bc8 100644 --- a/agkyra/cli.py +++ b/agkyra/cli.py @@ -20,6 +20,21 @@ from agkyra import config, protocol, protocol_client LOG = logging.getLogger(__name__) +STATUS = protocol.STATUS +NOTIFICATION = { + 0: 'Not initialized', + 1: 'Initializing ...', + 2: 'Shutting down', + 100: 'Syncing', + 101: 'Pausing', + 102: 'Paused', + 200: 'Settings are incomplete', + 201: 'Cloud URL error', + 202: 'Authentication error', + 203: 'Local directory error', + 204: 'Remote container error', + 1000: 'Critical error' +} class ConfigCommands: @@ -209,17 +224,6 @@ class AgkyraCLI(cmd.Cmd): except AttributeError: self.do_help('config') - @staticmethod - def _status_string(status=None): - """Get the status string (Syncing, Paused, Not running)""" - if status: - remain = status.get('unsynced', 0) - status.get('synced', 0) - if status['paused']: - return ('Pausing, %s remain' % remain) if remain else 'Paused' - else: - return 'Syncing, %s remain' % remain - return 'Not running' - def do_status(self, line): """Get Agkyra client status. Status may be one of the following: Syncing There is a process syncing right now @@ -227,8 +231,13 @@ class AgkyraCLI(cmd.Cmd): Not running No active processes """ client = self.client - status = client.get_status() if client else None - sys.stdout.write('%s\n' % self._status_string(status)) + status, msg = client.get_status() if client else None, 'Not running' + if status: + msg = NOTIFICATION[status['code']] + diff = status['unsynced'] - status['synced'] + if diff: + msg = '%s, %s remaining' % (msg, diff) + sys.stdout.write('%s\n' % msg) sys.stdout.flush() # def do_start_daemon(self, line): @@ -254,15 +263,14 @@ class AgkyraCLI(cmd.Cmd): sys.stderr.write('OK\n') else: status = client.get_status() - if status['paused']: + if status['code'] == STATUS['PAUSED']: client.start() sys.stderr.write('Starting syncer ... ') try: client.wait_until_syncing() - except AssertionError: - sys.stderr.write('\n') - raise - sys.stderr.write('OK\n') + sys.stderr.write('OK\n') + except AssertionError as ae: + sys.stderr.write('%s\n' % ae) else: sys.stderr.write('Already ') sys.stderr.flush() @@ -273,17 +281,16 @@ class AgkyraCLI(cmd.Cmd): client = self.client if client: status = client.get_status() - if status['paused']: + if status['code'] == STATUS['PAUSED']: sys.stderr.write('Already ') else: client.pause() sys.stderr.write('Pausing syncer ... ') try: client.wait_until_paused() - except AssertionError: - sys.stderr.write('Failed\n') - raise - sys.stderr.write('OK\n') + sys.stderr.write('OK\n') + except AssertionError as ae: + sys.stderr.write('%s\n' % ae) sys.stderr.flush() self.do_status(line) @@ -299,3 +306,7 @@ class AgkyraCLI(cmd.Cmd): else: sys.stderr.write('Not running\n') sys.stderr.flush() + + def do_florist(self, line): + """RUN FLORIST, RUN!""" + sys.stderr.write('RUN: %s\n' % STATUS) diff --git a/agkyra/protocol.py b/agkyra/protocol.py index 91b37c57df80bf22838bfa46946df80d66030941..16ce932aca1c1dd008a885d36779c7e7ff9597b8 100644 --- a/agkyra/protocol.py +++ b/agkyra/protocol.py @@ -38,7 +38,6 @@ with open(os.path.join(CURPATH, 'ui_common.json')) as f: UI_COMMON = json.load(f) STATUS = UI_COMMON['STATUS'] - def retry_on_locked_db(method, *args, **kwargs): """If DB is locked, wait and try again""" wait = kwargs.get('wait', 0.2) diff --git a/agkyra/protocol_client.py b/agkyra/protocol_client.py index 0e11a7d0436ed75835a360a1a8eeb77946954d7e..7132af231d780f08b1450a6947fb875eedf9e414 100644 --- a/agkyra/protocol_client.py +++ b/agkyra/protocol_client.py @@ -17,6 +17,7 @@ from ws4py.client.threadedclient import WebSocketClient import json import time import logging +from protocol import STATUS LOG = logging.getLogger(__name__) @@ -52,20 +53,22 @@ class UIClient(WebSocketClient): def wait_until_syncing(self, timeout=20): """Wait until session reaches syncing status""" status = self.get_status() - while timeout and status['paused']: + while timeout and status['code'] != STATUS['SYNCING']: time.sleep(1) status = self.get_status() timeout -= 1 - assert not status['paused'], 'Timed out, still in paused status' + msg = 'Timed out, still not syncing' + assert status['code'] == STATUS['SYNCING'], msg def wait_until_paused(self, timeout=20): """Wait until session reaches paused status""" status = self.get_status() - while timeout and not status['paused']: + while timeout and status['code'] != STATUS['PAUSED']: time.sleep(1) status = self.get_status() timeout -= 1 - assert status['paused'], 'Timed out, still in syncing status' + msg = 'Timed out, still not paused' + assert status['code'] == STATUS['PAUSED'], msg def received_message(self, m): """handle server responces according to the protocol""" @@ -98,7 +101,7 @@ class UIClient(WebSocketClient): def recv_get_status(self, msg): """Receive: GET STATUS""" - assert 'can_sync' in msg, json.dumps(msg) + assert 'code' in msg, json.dumps(msg) self.buf[msg['action']] = msg # API methods