Esempio n. 1
0
 def execute(self, cmd: Command, msg: ReliableMessage) -> Optional[Content]:
     assert isinstance(cmd, SearchCommand), 'command error: %s' % cmd
     # message
     message = cmd.get('message')
     print('search response: %s' % message)
     # users
     users = cmd.get('users')
     if users is not None:
         print('      users:', json_encode(users))
     # results
     results = cmd.get('results')
     if results is not None:
         print('      results:', json_encode(results))
     return None
Esempio n. 2
0
 def store_message(self, msg: ReliableMessage) -> bool:
     path = self.__message_path(msg=msg)
     if self.__message_exists(msg=msg, path=path):
         self.error('message duplicated: %s' % msg)
         return False
     self.debug('Appending message into: %s' % path)
     # message data
     data = utf8_decode(data=json_encode(msg.dictionary))
     data = data + '\n'
     return self.append_text(text=data, path=path)
Esempio n. 3
0
 def remove_message_batch(self, batch: dict, removed_count: int) -> bool:
     if removed_count <= 0:
         self.warning('message count to removed error: %d' % removed_count)
         return False
     # 0. get message file path
     path = batch.get('path')
     if path is None:
         receiver = batch.get('ID')
         filename = batch.get('filename')
         if receiver and filename:
             # message directory
             directory = self.__directory(receiver)
             # message file path
             path = os.path.join(directory, filename)
     if not self.exists(path):
         self.warning('message file not exists: %s' % path)
         return False
     # 1. remove all message(s)
     self.debug('remove message file: %s' % path)
     self.remove(path)
     # 2. store the rest messages back
     messages = batch.get('messages')
     if messages is None:
         return False
     total_count = len(messages)
     if removed_count < total_count:
         # remove message(s) partially
         messages = messages[removed_count:]
         for msg in messages:
             # message data
             if isinstance(msg, Message):
                 dictionary = msg.dictionary
             else:
                 dictionary = msg
             data = utf8_decode(data=json_encode(dictionary))
             data = data + '\n'
             self.append_text(text=data, path=path)
         self.debug('the rest messages(%d) write back into file: %s' % path)
     return True
Esempio n. 4
0
 def push_message(self, msg: ReliableMessage) -> bool:
     body = json_encode(msg.dictionary)
     return self.__push_data(body=body)