Allow to comunicate with unicode.

This commit is contained in:
Jakub Skiepko 2014-06-14 13:18:42 +02:00
parent 935bdcf107
commit 4366296703
4 changed files with 27 additions and 12 deletions

View file

@ -93,3 +93,16 @@ class ResponsePromise(object):
def get(self):
return self.communicator.receive_asynchronous(self.tag)
class UnicodeApiCommunicator(ApiCommunicator):
def receive_asynchronous(self, tag):
original = super(UnicodeApiCommunicator, self).receive_asynchronous(
tag)
response = []
for original_row in original:
row = {}
response.append(row)
for key, value in original_row.items():
row[key.decode()] = value.decode()
return response

View file

@ -1,13 +1,11 @@
from routeros_api import utils
class BasicQuery(object):
operator = None
def __init__(self, key, value):
if type(key) is str:
key = key.encode()
if type(value) is str:
value = value.encode()
self.key = key
self.value = value
self.key = utils.get_bytes(key)
self.value = utils.get_bytes(value)
def get_api_format(self):
return [self.operator + self.key + b'=' + self.value]
@ -27,9 +25,7 @@ class IsGreaterQuery(BasicQuery):
class HasValueQuery(object):
def __init__(self, key):
if type(key) is str:
key = key.encode()
self.key = key
self.key = utils.get_bytes(key)
def get_api_format(self):
return [b"?" + self.key]

View file

@ -1,6 +1,7 @@
import re
from routeros_api import exceptions
from routeros_api import utils
from routeros_api import query
@ -43,8 +44,8 @@ class ResponseSentence(object):
class CommandSentence(object):
def __init__(self, path, command, tag=None):
self.path = path
self.command = command
self.path = utils.get_bytes(path)
self.command = utils.get_bytes(command)
self.attributes = {}
self.api_attributes = {}
self.queries = set()
@ -61,7 +62,7 @@ class CommandSentence(object):
return formated
def set(self, key, value):
self.attributes[key] = value
self.attributes[utils.get_bytes(key)] = utils.get_bytes(value)
def filter(self, *args, **kwargs):
for arg in args:

5
routeros_api/utils.py Normal file
View file

@ -0,0 +1,5 @@
def get_bytes(string):
if hasattr(string, 'encode'):
return string.encode()
else:
return string