Ejemplo n.º 1
0
    def get(self, id):
        if not os.path.exists(os.path.join(self.base_path, id)):
            raise IndexError

        with open(os.path.join(self.base_path, id), "rb") as f:
            msg = Message.from_json(f.read().decode('utf-8'))
            return {'id':id, 'state': self.get_message_state(id), 'message': msg}
Ejemplo n.º 2
0
    def search(self, order_by='timestamp'):

        for value in self.messages.values():

            resp = dict(value)
            resp['message'] = Message.from_dict(resp['message'])

            yield resp
Ejemplo n.º 3
0
    def test_message_json_conversion(self):
        m = create_complex_message()

        msg_json = m.to_json()

        compare_to = Message.from_json(msg_json)

        self.assertEqual(m.payload['answer'], compare_to.payload['answer'], "Payload not well decoded")
        self.assertEqual(m.uuid.hex, compare_to.uuid.hex, "Bad uuid")
        self.assertEqual(m.meta['question'], compare_to.meta['question'], "Bad meta")

        self.assertEqual(m.ctx['test'].payload['question'], compare_to.ctx['test'].payload['question'], "Bad ctx")
Ejemplo n.º 4
0
    def test_message_json_conversion(self):
        m = generate_msg(message_content={'answer': 42}, with_context=True)

        msg_json = m.to_json()

        compare_to = Message.from_json(msg_json)

        self.assertEqual(m.payload['answer'], compare_to.payload['answer'], "Payload not well decoded")
        self.assertEqual(m.uuid, compare_to.uuid, "Bad uuid")
        self.assertEqual(m.meta['question'], compare_to.meta['question'], "Bad meta")

        self.assertEqual(m.ctx['test']['payload']['question'], compare_to.ctx['test']['payload']['question'], "Bad ctx")
        self.assertEqual(m.ctx['test']['meta']['answer'], compare_to.ctx['test']['meta']['answer'], "Bad ctx")
Ejemplo n.º 5
0
    def test_message_dict_conversion(self):
        m = generate_msg(message_content={'answer': 42}, with_context=True)

        mdict = m.to_dict()

        self.assertTrue(isinstance(mdict, dict), "Message as_dict method is broken")

        compare_to = Message.from_dict(mdict)

        self.assertEqual(m.payload['answer'], compare_to.payload['answer'], "Payload not well decoded")
        self.assertEqual(m.uuid, compare_to.uuid, "Bad uuid")
        self.assertEqual(m.meta['question'], compare_to.meta['question'], "Bad meta")

        self.assertEqual(m.ctx['test']['payload']['question'], compare_to.ctx['test']['payload']['question'], "Bad ctx")
        self.assertEqual(m.ctx['test']['meta']['answer'], compare_to.ctx['test']['meta']['answer'], "Bad ctx")
Ejemplo n.º 6
0
    def test_message_dict_conversion(self):


        m = create_complex_message()

        mdict = m.to_dict()

        self.assertTrue(isinstance(mdict, dict), "Message as_dict method is broken")

        compare_to = Message.from_dict(mdict)

        self.assertEqual(m.payload['answer'], compare_to.payload['answer'], "Payload not well decoded")
        self.assertEqual(m.uuid.hex, compare_to.uuid.hex, "Bad uuid")
        self.assertEqual(m.meta['question'], compare_to.meta['question'], "Bad meta")

        self.assertEqual(m.ctx['test'].payload['question'], compare_to.ctx['test'].payload['question'], "Bad ctx")
Ejemplo n.º 7
0
def create_message():
    m = Message()

    m.payload = {'answer': 42}
    m.meta['question'] = 'unknown'

    mctx = Message()

    mctx.meta['answer'] = 42
    mctx.payload = {'question': 'unknown'}

    m.ctx['test'] = mctx

    return m
Ejemplo n.º 8
0
    async def search(self, start=0, count=10, order_by='timestamp'):

        if order_by.startswith('-'):
            reverse = True
            sort_key = order_by[1:]
        else:
            reverse = False
            sort_key = order_by

        result = []
        for value in sorted(self.messages.values(), key=lambda x: x[sort_key], reverse=reverse):

            resp = dict(value)
            resp['message'] = Message.from_dict(resp['message'])

            result.append(resp)

        return result[start: start + count]
Ejemplo n.º 9
0
 def get(self, id):
     resp = dict(self.messages[id])
     resp['message'] = Message.from_dict(resp['message'])
     return resp