Exemplo n.º 1
0
def _parseMethodDoc(text):
    funcDoc = []
    argsDoc = {}
    argsOrder = []
    if text:
        pat = re.compile(' +')
        text = text.strip()
        for line in text.split('\n'):
            line = line.strip()
            tmp = pat.split(line)
            pre = tmp.pop(0)
            if pre and pre[0] == '@':
                pre = pre[1:]
                if pre == 'param':
                    key = tmp.pop(0)
                    argsOrder.append(key)
                    if not argsDoc.has_key(key):
                        argsDoc[key] = {}
                    argsDoc[key]['key'] = key
                    argsDoc[key]['type'] = tmp.pop(0)
                    argsDoc[key]['info'] = ' '.join(tmp)
                    argsDoc[key]['required'] = True
                elif pre == 'valid':
                    key = tmp.pop(0)
                    if not argsDoc.has_key(key):
                        argsDoc[key] = {}
                    argsDoc[key]['valid'] = json.decode(' '.join(tmp))
                elif pre == 'optional':
                    key = tmp.pop(0)
                    if not argsDoc.has_key(key):
                        argsDoc[key] = {}
                    argsDoc[key]['required'] = False
            else:
                funcDoc.append(line)
    return ('\n'.join(funcDoc), argsDoc, argsOrder)
Exemplo n.º 2
0
    def call(self, key, data=None, timeout=5, block=None):
        log.debug('RPC-CALL: execute %s = %s' % (key, data))
        if block is None:
            block = True

        # @todo: remove consume again afterwards!
        if not timeout:
            # tuba - no_ack=True is also an option
            self.consumerTag = self.ch.basic_consume(self.queue, callback=self._callback)

        self.response = None
        self.correlationId = getUuid()  # str(uuid.uuid4())
        msg = amqp.Message(
            json.encode(data),
            content_type='text/json',
            correlation_id=self.correlationId,
            # reply_to = self.queue
        )
        self.ch.basic_publish(
            msg,
            self.exchange,
            key,
            # reply_to = self.queue,
            # correlation_id = self.correlationId
        )

        if not block:
            return
        log.notice('RPC-CALL: wait response')

        startTime = time.time()
        while self.response is None:
            if timeout:
                msg = self.ch.basic_get(self.queue)
                if msg:
                    if self._callback(msg):
                        break
                else:
                    time.sleep(0.01)
                    if (time.time() - startTime) >= timeout:
                        raise TimeoutException('Waited %s sec for rpc call %s' % (timeout, key))
            else:
                self.ch.wait()

        log.notice('RPC-CALL: finished waiting')
        try:
            body = json.decode(self.response.body)
        except Exception as e:
            raise Exception("json decoding error: %s - for raw response: %s" % (e, self.response.body))
        tmp = self.response.routing_key.split('.')
        if tmp[0][1] == 'x': # todo: use constants
            if tmp[0][0] == 'o': d = 'orchestrator' # here too
            elif tmp[0][0] == 's': d = 'service'
            elif tmp[0][0] == 'r': d = 'reactor'
            elif tmp[0][0] == 'm': d = 'manager'
            e = RemoteException('Error in %s %s when calling %s' % (d,tmp[1],tmp[2]))
            e.setResponse(body)
            raise e
        log.debug('RPC-CALL: respone %s = %s' % (self.response.routing_key, body))
        return body
Exemplo n.º 3
0
def _parseMethodDoc(text):
    funcDoc = []
    argsDoc = {}
    argsOrder = []
    if text:
        pat = re.compile(' +')
        text = text.strip()
        for line in text.split('\n'):
            line = line.strip()
            tmp = pat.split(line)
            pre = tmp.pop(0)
            if pre and pre[0] == '@':
                pre = pre[1:]
                if pre == 'param':
                    key = tmp.pop(0)
                    argsOrder.append(key)
                    if not argsDoc.has_key(key):
                        argsDoc[key] = {}
                    argsDoc[key]['key'] = key
                    argsDoc[key]['type'] = tmp.pop(0)
                    argsDoc[key]['info'] = ' '.join(tmp)
                    argsDoc[key]['required'] = True
                elif pre == 'valid':
                    key = tmp.pop(0)
                    if not argsDoc.has_key(key):
                        argsDoc[key] = {}
                    argsDoc[key]['valid'] = json.decode(' '.join(tmp))
                elif pre == 'optional':
                    key = tmp.pop(0)
                    if not argsDoc.has_key(key):
                        argsDoc[key] = {}
                    argsDoc[key]['required'] = False
            else:
                funcDoc.append(line)
    return ('\n'.join(funcDoc), argsDoc, argsOrder)
Exemplo n.º 4
0
def parse(_req):
    parser = OptionParser()
    parser.add_option("-r", "--raw", action="store_true", dest="raw", help="Output raw data (do not format it)")
    parser.add_option("-j", "--json", action="store_true", dest="json", help="Output data as json")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Be verbose (show error traces")
    parser.add_option(
        "-d",
        "--debug",
        action="store_true",
        dest="debug",
        help="Be very verbose (print log message for level debug and above)",
    )
    (options, req) = parser.parse_args(_req)
    try:
        section = req[0]
    except IndexError:
        raise InvalidParameterException("Missing section")
    try:
        command = req[1]
    except IndexError:
        raise InvalidParameterException("Missing command")
    id = None
    args = []
    if len(req) > 2:
        id = req[2]
        args = [id]
        if len(req) > 3:
            for a in req[3:]:
                if a[0] in ["{", "["]:
                    a = json.decode(a)
                args.append(a)
    options.section = section
    options.command = command
    options.args = args
    return options
Exemplo n.º 5
0
 def get(self):
     if self.deliveryTag is not None:
         raise Exception('You must call ack() before getting a new item')
     while not self.messages:
         self.ch.wait()
     msg = self.messages.pop(0)
     try:
         data = json.decode(msg.body)
     except Exception, e:
         raise Exception("Failed decoding JSON: %s\nOrig exception: %s" % (msg.body, e))
Exemplo n.º 6
0
def parse(_req):
    parser = OptionParser()
    parser.add_option('-r',
                      '--raw',
                      action='store_true',
                      dest='raw',
                      help='Output raw data (do not format it)')
    parser.add_option('-j',
                      '--json',
                      action='store_true',
                      dest='json',
                      help='Output data as json')
    parser.add_option('-v',
                      '--verbose',
                      action='store_true',
                      dest='verbose',
                      help='Be verbose (show error traces')
    parser.add_option(
        '-d',
        '--debug',
        action='store_true',
        dest='debug',
        help='Be very verbose (print log message for level debug and above)')
    (options, req) = parser.parse_args(_req)
    try:
        section = req[0]
    except IndexError:
        raise InvalidParameterException('Missing section')
    try:
        command = req[1]
    except IndexError:
        raise InvalidParameterException('Missing command')
    id = None
    args = []
    if len(req) > 2:
        id = req[2]
        args = [id]
        if len(req) > 3:
            for a in req[3:]:
                if a[0] in ['{', '[']:
                    a = json.decode(a)
                args.append(a)
    if section == 'help':
        options.section = command
        options.command = args[0]
        options.args = []
        options.help = True
    else:
        options.section = section
        options.command = command
        options.args = args
        options.help = False
    return options
Exemplo n.º 7
0
    def call(self, key, data=None, timeout=5, block=None):
        log.debug('RPC-CALL: execute %s = %s' % (key, data))
        if block == None:
            block = True

        # @todo: remove consume again afterwards!
        if not timeout:
            # tuba - no_ack=True is also an option
            self.consumerTag = self.ch.basic_consume(self.queue, callback=self._callback)

        self.response = None
        self.correlationId = getUuid() #str(uuid.uuid4())
        msg = amqp.Message(
            json.encode(data),
            content_type='text/json',
            correlation_id = self.correlationId,
            #reply_to = self.queue
        )
        self.ch.basic_publish(
            msg,
            self.exchange,
            key,
            #reply_to = self.queue,
            #correlation_id = self.correlationId
        )

        if not block:
            return
        log.notice('RPC-CALL: wait response')

        startTime = time.time()
        while self.response is None:
            if timeout:
                msg = self.ch.basic_get(self.queue)
                if msg:
                    if self._callback(msg):
                        break
                else:
                    time.sleep(0.01)
                    if (time.time()-startTime) >= timeout:
                        raise TimeoutException('Waited %s sec for rpc call %s' % (timeout, key))
            else:
                self.ch.wait()

        log.notice('RPC-CALL: finished waiting')
        try:
            body = json.decode(self.response.body)
        except Exception, e:
            raise Exception("json decoding error: %s - for raw response: %s" % (e, self.response.body))
Exemplo n.º 8
0
 def get(self):
     if self.deliveryTag is not None:
         raise Exception('You must call ack() before getting a new item')
     while not self.messages:
         self.ch.wait()
     msg = self.messages.pop(0)
     try:
         data = json.decode(msg.body)
     except Exception as e:
         raise Exception("Failed decoding JSON: %s\nOrig exception: %s" % (msg.body, repr(e)))
     self.deliveryTag = msg.delivery_tag
     correlationId = None
     if 'correlation_id' in msg.properties:
         correlationId = msg.properties['correlation_id']
     return (msg.routing_key, data, correlationId)
Exemplo n.º 9
0
 def get(self):
     if self.deliveryTag is not None:
         raise Exception('You must call ack() before getting a new item')
     while not self.messages:
         self.ch.wait()
     msg = self.messages.pop(0)
     try:
         data = json.decode(msg.body)
     except Exception as e:
         raise Exception("Failed decoding JSON: %s\nOrig exception: %s" %
                         (msg.body, repr(e)))
     self.deliveryTag = msg.delivery_tag
     correlationId = None
     if 'correlation_id' in msg.properties:
         correlationId = msg.properties['correlation_id']
     return (msg.routing_key, data, correlationId)
Exemplo n.º 10
0
def parse(_req):
    parser = OptionParser()
    parser.add_option('-r', '--raw', action='store_true', dest='raw', help='Output raw data (do not format it)')
    parser.add_option('-j', '--json', action='store_true', dest='json', help='Output data as json')
    parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='Be verbose (show error traces')
    parser.add_option('-d', '--debug', action='store_true', dest='debug', help='Be very verbose (print log message for level debug and above)')
    (options, req) = parser.parse_args(_req)
    try:
        section = req[0]
    except IndexError:
        raise InvalidParameterException('Missing section')
    try:
        command = req[1]
    except IndexError:
        raise InvalidParameterException('Missing command')
    id = None
    args = []
    if len(req) > 2:
        id = req[2]
        args = [id]
        if len(req) > 3:
            for a in req[3:]:
                if a[0] in ['{', '[']:
                    a = json.decode(a)
                args.append(a)
    if section == 'help':
        options.section = command
        options.command = args[0]
        options.args = []
        options.help = True
    else:
        options.section = section
        options.command = command
        options.args = args
        options.help = False
    return options
Exemplo n.º 11
0
    def call(self, key, data=None, timeout=5, block=None):
        log.debug('RPC-CALL: execute %s = %s' % (key, data))
        if block is None:
            block = True

        # @todo: remove consume again afterwards!
        if not timeout:
            # tuba - no_ack=True is also an option
            self.consumerTag = self.ch.basic_consume(self.queue,
                                                     callback=self._callback)

        self.response = None
        self.correlationId = getUuid()  # str(uuid.uuid4())
        msg = amqp.Message(
            json.encode(data),
            content_type='text/json',
            correlation_id=self.correlationId,
            # reply_to = self.queue
        )
        self.ch.basic_publish(
            msg,
            self.exchange,
            key,
            # reply_to = self.queue,
            # correlation_id = self.correlationId
        )

        if not block:
            return
        log.notice('RPC-CALL: wait response')

        startTime = time.time()
        while self.response is None:
            if timeout:
                msg = self.ch.basic_get(self.queue)
                if msg:
                    if self._callback(msg):
                        break
                else:
                    time.sleep(0.01)
                    if (time.time() - startTime) >= timeout:
                        raise TimeoutException(
                            'Waited %s sec for rpc call %s' % (timeout, key))
            else:
                self.ch.wait()

        log.notice('RPC-CALL: finished waiting')
        try:
            body = json.decode(self.response.body)
        except Exception as e:
            raise Exception("json decoding error: %s - for raw response: %s" %
                            (e, self.response.body))
        tmp = self.response.routing_key.split('.')
        if tmp[0][1] == 'x':  # todo: use constants
            if tmp[0][0] == 'o': d = 'orchestrator'  # here too
            elif tmp[0][0] == 's': d = 'service'
            elif tmp[0][0] == 'r': d = 'reactor'
            elif tmp[0][0] == 'm': d = 'manager'
            e = RemoteException('Error in %s %s when calling %s' %
                                (d, tmp[1], tmp[2]))
            e.setResponse(body)
            raise e
        log.debug('RPC-CALL: respone %s = %s' %
                  (self.response.routing_key, body))
        return body