def pack_command(self, *args): "Pack a series of arguments into the ssdb protocol" output = [] # the client might have included 1 or more literal arguments in # the command name, e.g., 'CONFIG GET'. The ssdb server expects these # arguments to be sent separately, so split the first argument # manually. All of these arguements get wrapped in the Token class # to prevent them from being encoded. command = args[0] if ' ' in command: args = tuple([Token.get_token(s) for s in command.split()]) + args[1:] else: args = (Token.get_token(command), ) + args[1:] buff = SYM_EMPTY for arg in imap(self.encoder.encode, args): # to avoid large string mallocs, chunk the command into the # output list if we're sending large values if len(buff) > 6000 or len(arg) > 6000: buff = SYM_EMPTY.join((buff, b(str(len(arg))), SYM_LF)) output.append(buff) output.append(arg) buff = SYM_LF else: buff = SYM_EMPTY.join( (buff, b(str(len(arg))), SYM_LF, arg, SYM_LF)) output.append(buff) output.append(SYM_LF) return output
def encode(self, value): "Return a bytestring representation of the value" if isinstance(value, Token): return value.encoded_value elif isinstance(value, bytes): return value elif isinstance(value, (int, long)): value = b(str(value)) elif isinstance(value, float): value = b(repr(value)) elif not isinstance(value, basestring): # an object we don't know how to deal with. default to unicode() value = unicode(value) if isinstance(value, unicode): value = value.encode(self.encoding, self.encoding_errors) return value
def encode(self, value): """ Return a bytestring representation of the value """ if isinstance(value, Token): return b(value.value) if isinstance(value, bytes): return value elif isinstance(value, (int, long)): value = b(str(value)) elif isinstance(value, float): value = repr(value) elif not isinstance(value, basestring): value = str(value) if isinstance(value, unicode): value = value.encode(self.encoding, self.encoding_errors) return value
def pack_command(self, *args): """ Pack a series of arguments into a value SSDB command """ # the client might have included 1 or more literal arguments in # the command name, e.g., 'CONFIG GET'. The SSDB server expects # these arguments to be sent separately, so split the first # argument manually. All of these arguements get wrapped # in the Token class to prevent them from being encoded. command = args[0] if ' ' in command: args = tuple([Token(s) for s in command.split(' ')]) + args[1:] else: args = (Token(command),) + args[1:] args_output = SYM_EMPTY.join([ SYM_EMPTY.join(( b(str(len(k))), SYM_LF, k, SYM_LF )) for k in imap(self.encode, args) ]) output = "%s%s" % (args_output,SYM_LF) return output
from ssdb.utils import get_integer from ssdb.exceptions import ( RES_STATUS_MSG, RES_STATUS, SSDBError, TimeoutError, ConnectionError, BusyLoadingError, ResponseError, InvalidResponse, AuthenticationError, NoScriptError, ExecAbortError, ) SYM_LF = b('\n') SYM_EMPTY = b('') SERVER_CLOSED_CONNECTION_ERROR = "Connection closed by server." class Token(object): """ Literal strings in SSDB commands, such as the command names and any hard-coded arguments are wrapped in this class so we know not to apply and encoding rules on them. """ def __init__(self, value): if isinstance(value, Token): value = value.value self.value = value
# coding=utf-8 from itertools import starmap from ssdb._compat import b from ssdb.exceptions import ( ConnectionError, DataError, SSDBError, ResponseError, WatchError, NoScriptError, ExecAbortError, ) SYM_EMPTY = b("") class BaseBatch(object): def __init__(self, connection_pool, response_callbacks): self.connection_pool = connection_pool self.connection = None self.response_callbacks = response_callbacks self.reset() def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.reset() def __del__(self): try:
def __init__(self, value): if isinstance(value, Token): value = value.value self.value = value self.encoded_value = b(value)
#coding=utf-8 from itertools import starmap from ssdb._compat import b from ssdb.exceptions import ( ConnectionError, DataError, SSDBError, ResponseError, WatchError, NoScriptError, ExecAbortError, ) SYM_EMPTY = b('') class BaseBatch(object): def __init__(self, connection_pool, response_callbacks): self.connection_pool = connection_pool self.connection = None self.response_callbacks = response_callbacks self.reset() def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.reset() def __del__(self): try:
from ssdb._compat import (b, basestring, bytes, imap, iteritems, iterkeys, itervalues, izip, long, nativestr, urlparse, unicode, OrderedDict) from ssdb.connection import ConnectionPool from ssdb.batch import BaseBatch from ssdb.exceptions import ( ConnectionError, DataError, SSDBError, ResponseError, WatchError, NoScriptError, ExecAbortError, ) SYM_EMPTY = b('') def get_integer(name, num): if not isinstance(num, int): raise ValueError('``%s`` must be a integer' % name) return num def get_integer_or_emptystring(name, num): if not isinstance(num, int) and num != '': raise ValueError('``%s`` must be a integer or an empty string' % name) return num def get_nonnegative_integer(name, num): is_valid = isinstance(num, int) and num >= 0 if not is_valid: raise ValueError('``%s`` must be a nonnegative integer' % name)