def write(self, chunk): if isinstance(chunk, dict): chunk = json.dumps(chunk).replace("</", "<\\/") self.set_header("Content-Type", "application/json; charset=UTF-8") else: return super(ModelAPIHandler, self).write() self._write_buffer.append(chunk)
def func(*args, **kwargs): call = { 'cmd': item, 'args': args, 'kwargs': kwargs, } self.wfile.write((my_json.dumps(call) + '\n').encode()) self.wfile.flush() return my_json.loads(self.rfile.readline().decode())
def create_label_buttons(self, tmp, cmd, active): sync_if_necessary(tmp) label_buttons = [ { 'text': '{} (on)'.format(label['name']) if label['id'] in active else label['name'], 'callback_data': my_json.dumps({ 'cmd': cmd, 'label': label['id'], }), } for label in tmp['api']['labels']] label_buttons.append({ 'text': 'Finish.', 'callback_data': my_json.dumps({ 'cmd': cmd, 'label': -1, }), }) return self.buttons_in_rows(label_buttons, 2)
def create_project_buttons(self, tmp, cmd): sync_if_necessary(tmp) project_buttons = [ { 'text': project['name'], 'callback_data': my_json.dumps({ 'cmd': cmd, 'project': project['id'], }), } for project in tmp['api']['projects']] return self.buttons_in_rows(project_buttons, 2)
def cmd_template(self, message): with self.config_manager.get(self.chat_to_user[message['chat']['id']]) as (cfg, tmp): if 'templates' not in cfg or not cfg['templates']['enabled']: return self.reply(message, 'Templates are not enabled.') my_templates = templates.get_templates(tmp['api'], tmp['timezone'], cfg['templates'], tmp.setdefault('templates', {})) template_buttons = [{ 'text': template['name'], 'callback_data': my_json.dumps({ 'cmd': 'template', 'template': template['id'], }) } for template in my_templates] self.reply_keyboard(message, 'Choose template:', self.buttons_in_rows(template_buttons, 2))
def handle(self): for line in self.rfile: if not line: continue try: data = my_json.loads(line.strip()) except ValueError as e: logger.warning('Invalid Request', e) continue if data['cmd'] in server_handlers.handlers: res = server_handlers.handlers[data['cmd']]( *data.get('args', []), **data.get('kwargs', {}), mgr=config_manager) self.wfile.write((my_json.dumps(res) + '\n').encode())