def change_theme(theme_name): """ Hot-swap theme. :param theme_name: New theme name """ if theme_name == app.THEME_NAME: return False old_theme_name = app.THEME_NAME old_data_root = os.path.join(app.DATA_ROOT, old_theme_name) app.THEME_NAME = theme_name app.THEME_DATA_ROOT = os.path.join(app.DATA_ROOT, theme_name) static_file_handlers = app.instance.web_server.app.static_file_handlers log.info('Switching theme from "{old}" to "{new}"', {'old': old_theme_name, 'new': theme_name}) for rule in static_file_handlers.target.rules: if old_data_root not in rule.target_kwargs['path']: # Skip other static file handlers continue old_path = rule.target_kwargs['path'] new_path = old_path.replace(old_data_root, app.THEME_DATA_ROOT) rule.target_kwargs['path'] = new_path log.debug('Changed {old} to {new}', {'old': old_path, 'new': new_path}) # Reset cache StaticFileHandler.reset() return True
def get(self): request_log.info('ACCESS RELOAD (%s)', self.request.remote_ip) if self.request.remote_ip not in ['127.0.0.1', '::1']: return self.send_error(403) try: cfg = yaml.load(open('config.yaml', 'r', encoding='utf8')) if os.path.exists('downstream.yaml'): cfg_d = yaml.load(open('downstream.yaml', 'r', encoding='utf8')) cfg['downstream'] = cfg_d['downstream'] cfg['user'] = cfg_d['user'] cfg['domain'] = cfg_d['domain'] cfg['interface'] = cfg_d['interface'] self.application.config = cfg for downstream in sorted(cfg['downstream']): self.write(downstream + '\n') if os.path.exists('password.yaml'): password = yaml.load( open('password.yaml', 'r', encoding='utf8')) self.application.password = password self.finish() StaticFileHandler.reset() except Exception as e: request_log.exception('RELOAD FAIL') return self.finish('RELOAD FAIL %s' % repr(e))