Exemple #1
0
 def data_received(self, data):
     conn = self._connection
     parser = conn.parser
     parser.feed(data)
     response = parser.get()
     request = self._request
     if len(request) == 2:
         if response is not False:
             if not isinstance(response, Exception):
                 cmnd = request[0][0]
                 response = self.parse_response(response, cmnd, request[1])
             else:
                 response = ResponseError(response)
             self.finished(response)
     else:   # pipeline
         commands, raise_on_error, responses = request
         error = None
         while response is not False:
             if (isinstance(response, Exception) and raise_on_error
                     and not error):
                 error = response
             responses.append(response)
             response = parser.get()
         if len(responses) == len(commands):
             response = []
             for cmds, resp in zip(commands[1:-1], responses[-1]):
                 args, options = cmds
                 if isinstance(resp, Exception) and not error:
                     error = resp
                 resp = self.parse_response(resp, args[0], options)
                 response.append(resp)
             if error and raise_on_error:
                 response = ResponseError(error)
             self.finished(response)
Exemple #2
0
 def execute_transaction(self, transaction):
     '''Execute a ``transaction``
     '''
     updates = []
     models = []
     for command in transaction.commands:
         action = command.action
         if not action:
             raise NotImplementedError
         else:
             model = command.args
             updates.append(dict(self.model_data(model, action)))
             models.append(model)
     #
     if updates:
         executed = yield self.update_documents(self._database, updates)
         errors = []
         for doc, model in zip(executed, models):
             if doc.get('ok'):
                 model['id'] = doc['id']
                 model['_rev'] = doc['rev']
                 model._modified.clear()
             elif doc.get('error'):
                 errors.append(CouchDbError(doc['error'], doc['reason']))
         if errors:
             raise errors[0]
     coroutine_return(models)
Exemple #3
0
 def data_received(self, data):
     conn = self._connection
     parser = conn.parser
     parser.feed(data)
     response = parser.get()
     request = self._request
     if len(request) == 2:
         if response is not False:
             if not isinstance(response, Exception):
                 cmnd = request[0][0]
                 response = self.parse_response(response, cmnd, request[1])
             else:
                 response = ResponseError(response)
             self.finished(response)
     else:  # pipeline
         commands, raise_on_error, responses = request
         error = None
         while response is not False:
             if (isinstance(response, Exception) and raise_on_error
                     and not error):
                 error = response
             responses.append(response)
             response = parser.get()
         if len(responses) == len(commands):
             response = []
             for cmds, resp in zip(commands[1:-1], responses[-1]):
                 args, options = cmds
                 if isinstance(resp, Exception) and not error:
                     error = resp
                 resp = self.parse_response(resp, args[0], options)
                 response.append(resp)
             if error and raise_on_error:
                 response = ResponseError(error)
             self.finished(response)
Exemple #4
0
 def execute_transaction(self, transaction):
     '''Execute a ``transaction``
     '''
     updates = []
     models = []
     for command in transaction.commands:
         action = command.action
         if not action:
             raise NotImplementedError
         else:
             model = command.args
             updates.append(dict(self.model_data(model, action)))
             models.append(model)
     #
     if updates:
         executed = yield self.update_documents(self._database, updates)
         errors = []
         for doc, model in zip(executed, models):
             if doc.get('ok'):
                 model['id'] = doc['id']
                 model['_rev'] = doc['rev']
                 model._modified.clear()
             elif doc.get('error'):
                 errors.append(CouchDbError(doc['error'], doc['reason']))
         if errors:
             raise errors[0]
Exemple #5
0
def sort_return_tuples(response, groups=None, **options):
    """
    If ``groups`` is specified, return the response as a list of
    n-element tuples with n being the value found in options['groups']
    """
    if not response or not groups:
        return response
    return list(zip(*[response[i::groups] for i in range(groups)]))
Exemple #6
0
def sort_return_tuples(response, groups=None, **options):
    """
    If ``groups`` is specified, return the response as a list of
    n-element tuples with n being the value found in options['groups']
    """
    if not response or not groups:
        return response
    return list(zip(*[response[i::groups] for i in range(groups)]))
Exemple #7
0
def pubsub_callback(response, subcommand=None):
    if subcommand == 'numsub':
        it = iter(response)
        return dict(((k, int(v)) for k, v in zip(it, it)))
        return pairs_to_object(response)
    elif subcommand == 'numpat':
        return int(response)
    else:
        return response
Exemple #8
0
def pubsub_callback(response, subcommand=None):
    if subcommand == 'numsub':
        it = iter(response)
        return dict(((k, int(v)) for k, v in zip(it, it)))
        return pairs_to_object(response)
    elif subcommand == 'numpat':
        return int(response)
    else:
        return response
Exemple #9
0
 def parse_response(self, response, command_name, **options):
     if self.transaction:
         if command_name != 'EXEC':
             return response
         else:
             data = []
             callbacks = self.response_callbacks
             for r, cmd in zip(response, self.command_stack):
                 if not isinstance(r, Exception):
                     args, opt = cmd
                     command_name = args[0]
                     if command_name in callbacks:
                         r = callbacks[command_name](r, **opt)
                 data.append(r)
             return data
     else:
         callbacks = self.response_callbacks
         if command_name in callbacks:
             response = callbacks[command_name](response, **options)
         if command_name in self.UNWATCH_COMMANDS:
             self.watching = False
         elif command_name == 'WATCH':
             self.watching = True
         return response
Exemple #10
0
 def parse_response(self, response, command_name, **options):
     if self.transaction:
         if command_name != 'EXEC':
             return response
         else:
             data = []
             callbacks = self.response_callbacks
             for r, cmd in zip(response, self.command_stack):
                 if not isinstance(r, Exception):
                     args, opt = cmd
                     command_name = args[0]
                     if command_name in callbacks:
                         r = callbacks[command_name](r, **opt)
                 data.append(r)
             return data
     else:
         callbacks = self.response_callbacks
         if command_name in callbacks:
             response = callbacks[command_name](response, **options)
         if command_name in self.UNWATCH_COMMANDS:
             self.watching = False
         elif command_name == 'WATCH':
             self.watching = True
         return response
Exemple #11
0
def values_to_zset(response, withscores=False, **kw):
    if withscores:
        it = iter(response)
        return Zset(((float(score), value) for value, score in zip(it, it)))
    else:
        return response
Exemple #12
0
def values_to_zset(response, withscores=False, **kw):
    if withscores:
        it = iter(response)
        return Zset(((float(score), value) for value, score in zip(it, it)))
    else:
        return response
Exemple #13
0
def pairs_to_object(response, factory=None):
    it = iter(response)
    return (factory or dict)(zip(it, it))
Exemple #14
0
def values_to_object(response, fields=None, factory=None):
    if fields is not None:
        return (factory or dict)(zip(fields, response))
    else:
        return response
Exemple #15
0
 def random(self, size=100, score_min=-10, score_max=10):
     scores = populate('float', size, score_min, score_max)
     data = populate('string', size, min_len=5, max_len=10)
     return self.skiplist(zip(scores, data))
Exemple #16
0
def pairs_to_object(response, factory=None):
    it = iter(response)
    return (factory or dict)(zip(it, it))
Exemple #17
0
def values_to_object(response, fields=None, factory=None):
    if fields is not None:
        return (factory or dict)(zip(fields, response))
    else:
        return response
Exemple #18
0
 def random(self, size=100, score_min=-10, score_max=10):
     scores = populate('float', size, score_min, score_max)
     data = populate('string', size, min_len=5, max_len=10)
     return self.skiplist(zip(scores, data))
Exemple #19
0
    def json(self):
        '''An ordered dictionary representation of this :class:`Column`.

        Used when serialising to json.'''
        return OrderedDict(((name, value) for name, value in
                            zip(self._fields, self) if value is not None))