def compile(self): logger.debug('pre compiling client') try: path = self._get_path() template = self.jinja2_env.get_template('lona.js') template_context = { 'protocol': dumps(self.get_enums()), 'settings': dumps(self.get_settings()), } file_content = template.render(**template_context, ) with open(path, 'w+') as f: f.write(file_content) f.close() except Exception: logger.error( 'exception raised while pre compiling js client', exc_info=True, )
def encode_view_stop(window_id, view_runtime_id): return PROTOCOL.MESSAGE_PREFIX.value + dumps( [window_id, view_runtime_id, METHOD.VIEW_STOP, None], )
def encode_data(window_id, view_runtime_id, title, data): return PROTOCOL.MESSAGE_PREFIX.value + dumps( [window_id, view_runtime_id, METHOD.DATA, [title, data]], )
def encode_http_redirect(window_id, view_runtime_id, target_url): return PROTOCOL.MESSAGE_PREFIX.value + dumps( [window_id, view_runtime_id, METHOD.HTTP_REDIRECT, target_url], )
def encode_input_event_ack(window_id, view_runtime_id, input_event_id): return PROTOCOL.MESSAGE_PREFIX.value + dumps( [window_id, view_runtime_id, METHOD.INPUT_EVENT_ACK, input_event_id], )
def update_state(self): self.server_state.set_text(dumps(self.data))
def render_response_dict(self, raw_response_dict, view_name): response_dict = { 'status': 200, 'content_type': 'text/html', 'text': '', 'file': '', 'redirect': '', 'http_redirect': '', } key_words = { 'text', 'redirect', 'http_redirect', 'template', 'template_string', 'file', 'json', } # Node response if isinstance(raw_response_dict, AbstractNode): response_dict['text'] = str(raw_response_dict) return response_dict # string response if isinstance(raw_response_dict, str): logger.debug("'%s' is a string based view", view_name) response_dict['text'] = raw_response_dict return response_dict # response dict elif isinstance(raw_response_dict, dict): # check keys if len(set(raw_response_dict) & key_words) != 1: raise RuntimeError( 'response dicts have to contain exactly one keyword of:{}'. format( # NOQA ', '.join(key_words), )) # find keys for key in response_dict.keys(): if key in raw_response_dict: value = raw_response_dict[key] response_dict[key] = value logger.debug("'%s' sets '%s' to %s", view_name, key, repr(value)) # redirects if 'redirect' in raw_response_dict: response_dict['redirect'] = raw_response_dict['redirect'] # http redirect elif 'http_redirect' in raw_response_dict: response_dict['http_redirect'] = raw_response_dict['http_redirect'] # template response elif ('template' in raw_response_dict or 'template_string' in raw_response_dict): logger.debug("'%s' is a template view", view_name) template_context = raw_response_dict if 'context' in template_context: template_context = template_context['context'] # template file if 'template' in raw_response_dict: response_dict['text'] = \ self.server.templating_engine.render_template( raw_response_dict['template'], template_context, ) # template string else: response_dict['text'] = \ self.server.templating_engine.render_string( raw_response_dict['template_string'], template_context, ) # json response elif 'json' in raw_response_dict: logger.debug("'%s' is a json view", view_name) response_dict['text'] = dumps(raw_response_dict['json']) response_dict['content_type'] = 'application/json' return response_dict