removed useless code, changed the way of parsing server response as it was way too much code that did not do anything

This commit is contained in:
Krzysztof Jagiello
2011-06-06 15:49:41 +02:00
committed by Andrew Williams
parent 0661e36fdc
commit a33d73bd91

View File

@@ -70,8 +70,8 @@ ts3_escape = [
class TS3Response(): class TS3Response():
def __init__(self, response, data): def __init__(self, response, data):
self.response = TS3Proto.parse_command(response) self.response = TS3Proto.parse_response(response)
self.data = TS3Proto.parse_command(data) self.data = TS3Proto.parse_data(data)
if isinstance(self.data, dict): if isinstance(self.data, dict):
if self.data: if self.data:
@@ -81,7 +81,7 @@ class TS3Response():
@property @property
def is_successful(self): def is_successful(self):
return self.response['keys']['msg'] == 'ok' return self.response['msg'] == 'ok'
class TS3Proto(): class TS3Proto():
@@ -174,46 +174,56 @@ class TS3Proto():
return " ".join(cstr) return " ".join(cstr)
@staticmethod @staticmethod
def parse_command(commandstr): def parse_response(response):
""" """
Parses a TS3 command string into command/keys/opts tuple Parses a TS3 command string into command/keys/opts tuple
@param commandstr: Command string @param command: Command string
@type commandstr: string @type command: string
""" """
if commandstr.strip() == "":
return {}
if len(commandstr.split('|')) > 1: # responses always begins with "error " so we may just skip it
vals = [] return TS3Server.parse_data(response[6:])
for cmd in commandstr.split('|'):
vals.append(TS3Proto.parse_command(cmd))
return vals
cmdlist = commandstr.strip().split(' ') @staticmethod
command = None def parse_data(data):
keys = {} """
opts = [] Parses data string consisting of key=value
for key in cmdlist: @param data: data string
v = key.strip().split('=') @type data: string
if len(v) > 1: """
# Key
if len > 2: data = data.strip()
# Fix the stupidities in TS3 escaping
v = [v[0], '='.join(v[1:])] multipart = data.split('|')
key, value = v
keys[key] = TS3Proto._unescape_str(value) if len(multipart) > 1:
elif v[0][0] == '-': values = []
# Option
opts.append(v[0][1:]) for part in multipart:
values.append(TS3Proto.parse_data(part))
return values
chunks = data.split(' ')
parsed_data = {}
for chunk in chunks:
chunk = chunk.strip().split('=')
if len(chunk) > 1:
if len(chunk) > 2:
# value can contain '=' which may confuse our parser
chunk = [v[0], '='.join(v[1:])]
key, value = chunk
parsed_data[key] = TS3Proto._unescape_str(value)
else: else:
command = v[0] # TS3 Query Server may sometimes return a key without any value
# and we default its value to None
parsed_data[chunk[0]] = None
d = {'keys': keys, 'opts': opts} return parsed_data
if command:
d['command'] = command
return d
@staticmethod @staticmethod
def _escape_str(value): def _escape_str(value):
@@ -269,7 +279,7 @@ class TS3Server(TS3Proto):
@property @property
def logger(self): def logger(self):
if not hasattr(self, _logger): if not hasattr(self, "_logger"):
self._logger = logging.getLogger(__name__) self._logger = logging.getLogger(__name__)
return self._logger return self._logger