Commit eb9d624f authored by Stavros Sachtouris's avatar Stavros Sachtouris Committed by Giorgos Korfiatis
Browse files

Replace CLI assertions with UI-specific errors

parent f64fe66c
......@@ -40,6 +40,10 @@ NOTIFICATION = protocol.COMMON['NOTIFICATION']
remaining = lambda st: st['unsynced'] - (st['synced'] + st['failed'])
class ConfigError(protocol_client.UIClientError):
"""Error with config settings"""
class ConfigCommands(object):
"""Commands for handling Agkyra config options"""
cnf = config.AgkyraConfig()
......@@ -64,7 +68,8 @@ class ConfigCommands(object):
def list_section_type(self, section):
"""print the contents of a configuration section"""
names = ['', ] if section in ('global', ) else self.cnf.keys(section)
assert names, 'Section %s not found' % section
if not names:
ConfigError('Section %s not found' % section)
for name in names:
print section, name
self.list_section(section, name)
......@@ -75,12 +80,14 @@ class ConfigCommands(object):
self.list_section_type(section)
def set_global_setting(self, section, option, value):
assert section in ('global'), 'Syntax error'
if section not in ('global'):
raise ConfigError('Section "%s" not in global' % section)
self.cnf.set(section, option, value)
self.cnf.write()
def set_setting(self, section, name, option, value):
assert section in self.cnf.sections(), 'Syntax error'
if section not in self.cnf.sections():
raise ConfigError('"%s" is not a section')
self.cnf.set('%s.%s' % (section, name), option, value)
self.cnf.write()
......@@ -95,7 +102,8 @@ class ConfigCommands(object):
def delete_section_option(self, section, name, option, yes=False):
"""Delete a section (sync or cloud) option"""
assert section in self.cnf.sections(), 'Syntax error'
if section not in self.cnf.sections():
raise ConfigError('"%s" not a section' % section)
if (not yes and 'y' != raw_input(
'Delete %s of %s "%s"? [y|N]: ' % (option, section, name))):
sys.stderr.write('Aborted\n')
......@@ -326,8 +334,8 @@ class AgkyraCLI(cmd.Cmd):
try:
client.wait_until_syncing()
sys.stderr.write('OK\n')
except AssertionError as ae:
sys.stderr.write('%s\n' % ae)
except protocol_client.UIClientError as uice:
sys.stderr.write('%s\n' % uice)
else:
sys.stderr.write('Already ')
sys.stderr.flush()
......@@ -348,8 +356,8 @@ class AgkyraCLI(cmd.Cmd):
try:
client.wait_until_paused()
sys.stderr.write('OK\n')
except AssertionError as ae:
sys.stderr.write('%s\n' % ae)
except protocol_client.UIClientError as uice:
sys.stderr.write('%s\n' % uice)
sys.stderr.flush()
self.do_status(line)
......
......@@ -17,12 +17,29 @@ from ws4py.client.threadedclient import WebSocketClient
import json
import time
import logging
import random
from protocol import STATUS
LOG = logging.getLogger(__name__)
class UIClientError(Exception):
"""UIClient Exception class"""
class TimeOutError(UIClientError):
"""A client request timed out"""
class UnexpectedResponseError(UIClientError):
"""The protocol server response was not as expected"""
def __init__(self, *args, **kw):
""":param response: a keyword argument containing the repsonse"""
self.response = kw.pop('response', None)
super(UnexpectedResponseError, self).__init__(*args, **kw)
class UIClient(WebSocketClient):
"""Web Socket Client for Agkyra"""
buf, authenticated, ready = {}, False, False
......@@ -47,7 +64,8 @@ class UIClient(WebSocketClient):
while timeout and not self.ready:
time.sleep(1)
timeout -= 1
assert self.ready, 'UI client timed out while waiting to be ready'
if not self.ready:
raise TimeOutError('UI client timed out while waiting to be ready')
return self.ready
def wait_until_syncing(self, timeout=20):
......@@ -57,8 +75,8 @@ class UIClient(WebSocketClient):
time.sleep(1)
status = self.get_status()
timeout -= 1
msg = 'Timed out, still not syncing'
assert status['code'] == STATUS['SYNCING'], msg
if status['code'] != STATUS['SYNCING']:
raise TimeOutError('Still not syncing')
def wait_until_paused(self, timeout=20):
"""Wait until session reaches paused status"""
......@@ -67,8 +85,8 @@ class UIClient(WebSocketClient):
time.sleep(1)
status = self.get_status()
timeout -= 1
msg = 'Timed out, still not paused'
assert status['code'] == STATUS['PAUSED'], msg
if status['code'] != STATUS['PAUSED']:
raise TimeOutError('Still not paused')
def received_message(self, m):
"""handle server responces according to the protocol"""
......@@ -88,20 +106,25 @@ class UIClient(WebSocketClient):
# Receive handlers
def recv_authenticate(self, msg):
"""Receive: client authentication response"""
assert 'ACCEPTED' in msg, json.dumps(msg)
if 'ACCEPTED' not in msg:
raise UnexpectedResponseError(
'Client authentication failed', response=msg)
self.ready = True
def recv_start(self, msg):
"""Receive: start response"""
assert 'OK' in msg, json.dumps(msg)
if 'OK' not in msg:
raise UnexpectedResponseError('Start failed', response=msg)
def recv_pause(self, msg):
"""Receive: start response"""
assert 'OK' in msg, json.dumps(msg)
if 'OK' not in msg:
raise UnexpectedResponseError('Pause failed', response=msg)
def recv_get_status(self, msg):
"""Receive: GET STATUS"""
assert 'code' in msg, json.dumps(msg)
if 'code' not in msg:
raise UnexpectedResponseError('Get status failed', response=msg)
self.buf[msg['action']] = msg
# API methods
......@@ -110,7 +133,7 @@ class UIClient(WebSocketClient):
self.wait_until_ready()
self.send_get_status()
while 'get status' not in self.buf:
time.sleep(1)
time.sleep(random.random())
return self.buf.pop('get status')
def _post(self, path):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment