def reset(): global _list , _plugins_key , _cache_key _work_plugins = [] _config = {} _list = {} for plugin in models.Plugins.select().order_by(models.Plugins.id.desc()): _work_plugins.append(plugin.name) _config[plugin.name] = Json.decode(plugin.config) # 注册插件路由 for handler in Json.decode(plugin.handlers,[]): import_object(handler) binds = Json.decode(plugin.bind,{}) for event in binds: _list.setdefault(event,[]) for v in binds[event]: v['handler'] = import_object(str(plugin.name)) v['name'] = plugin.name _list[event].append(v) _plugins_key = '|'.join(_work_plugins) cache.client.set(_cache_key,_plugins_key) cache.client.set('xcat.plugins.work_plugins',_work_plugins) cache.client.set('xcat.plugins.config',_config)
def __call__(self, request): if self._sync_id != cache.client.get(self._sync_key, 0): # 重新加载 app handlers app_handlers = self.settings['app_path'].split( os.path.sep).pop() + '.handlers' handlers = import_object(app_handlers) Route._routes = {} for name in handlers.__all__: handler_module = import_object(app_handlers + '.' + name) reload(handler_module) for v in dir(handler_module): o = getattr(handler_module, v) if type(o) is types.ModuleType: reload(o) # 重新加载 plugins from xcat.models import Plugins for plugin in Plugins.select( Plugins.handlers).where(Plugins.handlers != '[]'): for v in Json.decode(plugin.handlers): plugin_module = v.split('.handlers.')[0] + '.handlers' reload(import_object(str(plugin_module))) self.handlers = [] self.named_handlers = {} Route.routes(self) # 标记已同步 self._sync_id = cache.client.get(self._sync_key) return super(Application, self).__call__(request)
def __call__(self, request): if self._sync_id != cache.client.get(self._sync_key,0): # 重新加载 app handlers app_handlers = self.settings['app_path'].split(os.path.sep).pop() + '.handlers' handlers = import_object(app_handlers) Route._routes = {} for name in handlers.__all__: handler_module = import_object(app_handlers + '.' + name) reload(handler_module) for v in dir(handler_module): o = getattr(handler_module,v) if type(o) is types.ModuleType: reload(o) # 重新加载 plugins from xcat.models import Plugins for plugin in Plugins.select(Plugins.handlers).where(Plugins.handlers != '[]'): for v in Json.decode(plugin.handlers): plugin_module = v.split('.handlers.')[0] + '.handlers' reload(import_object(str(plugin_module))) self.handlers = [] self.named_handlers = {} Route.routes(self) # 标记已同步 self._sync_id = cache.client.get(self._sync_key) return super(Application,self).__call__(request)
def set_config(plugin_name,config): _config = cache.client.get('xcat.plugins.config',{}) pl_ar = models.Plugins.get(models.Plugins.name == plugin_name) pl_ar.config = Json.encode(config) pl_ar.save() _config[plugin_name] = config cache.client.set('xcat.plugins.config',_config)
def sync(): global is_sync if is_sync: return is_sync = True if False == (yield gen.Task(MemcacheModel.table_exists)): yield gen.Task(MemcacheModel.create_table) else: # 从数据表中读取数据,并写入缓存 data = yield gen.Task(MemcacheModel.select().execute) for v in data: self.set(v.key, Json.decode(v.value), 0)
def uninstall(plugin_name): register = import_object(plugin_name.strip() + '.register') name = register._handler.__module__ + \ '.' + register._handler.__name__ if models.Plugins.filter(models.Plugins.name == name).count() == 1 : plugin = import_object(name)() plugin.uninstall() plugin_ar = models.Plugins.get(models.Plugins.name == name) for v in Json.decode(plugin_ar.ui_modules): ui_name = str(v.split('.').pop()) if ui_name in _application.ui_modules: del _application.ui_modules[ui_name] # 卸载路由 routes = [] for v in Json.decode(plugin_ar.handlers): ctr = import_object(v) if hasattr(ctr,'_menu'): from xcat.web import menu menu.remove(getattr(ctr,'_menu')) routes.append(ctr._route) if len(routes) > 0: # 重新注册路由 from xcat.web import Application cache.client.set(Application._sync_key,Date.time()) models.Plugins.delete().where(models.Plugins.name==name).execute() reset()
def _on_check_user(self, response): # 检查用户信息 user_info = Json.decode(response.body) response.rethrow() user_oauth = UserOAuth.select()\ .where(UserOAuth.source == 'dropbox')\ .where(UserOAuth.oauth_id == self.uid) # 判断用户是否已经注册 if 0 == user_oauth.count(): return self.reg_usr(user_info) # 更新 access_token user_oauth = user_oauth.get() user_oauth.set_token(self.access_token) user_oauth.save() user = user_oauth.user # 为用户进行登陆 user.login(self) # 执行一次手动更新 ar = wiki.Metadata.select()\ .where(wiki.Metadata.user == user)\ .where(wiki.Metadata.is_dir == 1)\ .where(wiki.Metadata.root_id == 0) # 墙的存在,可能初始化不成功,再执行 if ar.count() == 0: self.add_task( route.url_for('task.SyncPath', user.id, 0), 99 ) else: self.add_task( route.url_for('task.SyncPath', user.id, ar.get().id), 1 ) self.redirect(route.url_for('wiki.Index'))
def set(self, key, val, left_time=0, callback=None): is_persistence = False if -1 == left_time: is_persistence = True left_time = 0 ret = yield gen.Task(self._conn.set, str(key), val, left_time) if callback: callback(ret) # 需要持久化,写入数据表 if is_persistence: model = MemcacheModel.select().where(MemcacheModel.key == key) if 1 == (yield gen.Task(model.count)): ar = yield gen.Task(model.get) else: ar = MemcacheModel() ar.key = key ar.value = Json.encode(val) yield gen.Task(ar.save)
def _on_check_user(self, response): # 检查用户信息 user_info = Json.decode(response.body) response.rethrow() user_oauth = UserOAuth.select()\ .where(UserOAuth.source == 'dropbox')\ .where(UserOAuth.oauth_id == self.uid) # 判断用户是否已经注册 if 0 == user_oauth.count(): return self.reg_usr(user_info) # 更新 access_token user_oauth = user_oauth.get() user_oauth.set_token(self.access_token) user_oauth.save() user = user_oauth.user # 为用户进行登陆 user.login(self) # 执行一次手动更新 ar = wiki.Metadata.select()\ .where(wiki.Metadata.user == user)\ .where(wiki.Metadata.is_dir == 1)\ .where(wiki.Metadata.root_id == 0) # 墙的存在,可能初始化不成功,再执行 if ar.count() == 0: self.add_task(route.url_for('task.SyncPath', user.id, 0), 99) else: self.add_task(route.url_for('task.SyncPath', user.id, ar.get().id), 1) self.redirect(route.url_for('wiki.Index'))
def callback(self, response): if response.error: self.write('Init User(%s) Metadata Error : %s' % (self.user.id, response.error)) self.finish() return json = Json.decode(response.body) if len(json.get('path', '')) > 255: self.write('Path Too long.') self.finish() return hash_key = json.get('hash', False) or json.get('rev', '') if '' == hash_key: # 中文目录,先删除处理 self.metadata.remove() self.finish() return if self.metadata: metadata = self.metadata # 没有变更 # if hash_key == str(metadata.hash_key): # self.finish() # return else: # 初始化根目录 metadata = wiki.Metadata() metadata.user = self.user metadata.path = json['path'] metadata.bytes = json['bytes'] metadata.is_dir = json['is_dir'] and 1 or 0 if json.get('modified', False): metadata.modified = Date.str_to_time( json['modified'].split('+')[0], '%a, %d %b %Y %H:%M:%S ') metadata.hash_key = hash_key metadata.save() task_affair = wiki.TaskAffairs.add(metadata, self.affair_parent) # 取目录下文件 path 列表, 与新数据比对 # 存在的 path 删除, 列表中留下的path , # 就是需要删除的 文件 / 目录 path_list = [] for v in wiki.Metadata.select(wiki.Metadata.path)\ .where(wiki.Metadata.root_id == metadata.id): path_list.append(v.path) for v in json.get('contents', []): if len(json['path']) < 255: if v['path'] in path_list: path_list.remove(v['path']) is_dir = v['is_dir'] and 1 or 0 hash_key = v.get('rev', False) or v['hash'] if 0 == is_dir: uri, ext = os.path.splitext(v['path']) # 不支持的文件类型, 跳过 if ext.lower() not in self.settings['support_ext']: continue # 判断是否存在 ar = wiki.Metadata.select()\ .where(wiki.Metadata.root_id == metadata.id)\ .where(wiki.Metadata.path == v['path'])\ .where(wiki.Metadata.is_dir == is_dir) if ar.count() == 0: ar = wiki.Metadata() ar.user = self.user ar.root_id = metadata.id ar.path = v['path'] ar.is_dir = is_dir else: ar = ar.get() # 没有更改, 跳过 if 0 == is_dir and hash_key == ar.hash_key: continue ar.bytes = v['bytes'] ar.hash_key = hash_key if v.get('modified', False): ar.modified = Date.str_to_time(v['modified'].split('+')[0], '%a, %d %b %Y %H:%M:%S ') ar.save() # 子事务 child_task_affair = wiki.TaskAffairs.add(ar, task_affair.id) if v['is_dir']: self.add_task( route.url_for('task.SyncPath', self.user.id, ar.id) + '?parent=%s' % child_task_affair.id, 1) else: self.add_task( route.url_for('task.SyncFile', ar.id) + '?affair=%s' % child_task_affair.id, 2) # 删除不存在的文件/目录 for v in path_list: ar = wiki.Metadata.select()\ .where(wiki.Metadata.root_id == metadata.id)\ .where(wiki.Metadata.path == v) if ar.count() != 0: ar.get().remove() self.finish()
handlers = [] for v in plugin._handlers: handlers.append( v.__module__ + '.' + v.__name__ ) ui_modules = [] for v in plugin._ui_modules: _application.ui_modules[v.__name__] = v ui_modules.append( v.__module__ + '.' + v.__name__ ) pl = models.Plugins() pl.name = name pl.bind = Json.encode(register._targets) pl.handlers = Json.encode(handlers) pl.ui_modules = Json.encode(ui_modules) if plugin.get_form() : pl.config = Json.encode(plugin.get_form().get_default_values()) pl.save() # 安装路由 if len(plugin._handlers) > 0 : from xcat.web import Application cache.client.set(Application._sync_key,Date.time()) reset() class Register(object):
def get_token(self): return Json.decode(self.access_token,None)
def set_token(self, access_token): self.access_token = Json.encode(access_token)
def callback(self, response): if response.error: self.write('Init User(%s) Metadata Error : %s' % (self.user.id, response.error) ) self.finish() return json = Json.decode(response.body) if len(json.get('path','')) > 255: self.write('Path Too long.') self.finish() return hash_key = json.get('hash', False) or json.get('rev','') if '' == hash_key: # 中文目录,先删除处理 self.metadata.remove() self.finish() return if self.metadata: metadata = self.metadata # 没有变更 # if hash_key == str(metadata.hash_key): # self.finish() # return else: # 初始化根目录 metadata = wiki.Metadata() metadata.user = self.user metadata.path = json['path'] metadata.bytes = json['bytes'] metadata.is_dir = json['is_dir'] and 1 or 0 if json.get('modified', False): metadata.modified = Date.str_to_time(json['modified'].split('+')[0], '%a, %d %b %Y %H:%M:%S ') metadata.hash_key = hash_key metadata.save() task_affair = wiki.TaskAffairs.add( metadata, self.affair_parent ) # 取目录下文件 path 列表, 与新数据比对 # 存在的 path 删除, 列表中留下的path , # 就是需要删除的 文件 / 目录 path_list = [] for v in wiki.Metadata.select(wiki.Metadata.path)\ .where(wiki.Metadata.root_id == metadata.id): path_list.append(v.path) for v in json.get('contents', []): if len(json['path']) < 255: if v['path'] in path_list: path_list.remove(v['path']) is_dir = v['is_dir'] and 1 or 0 hash_key = v.get('rev', False) or v['hash'] if 0 == is_dir: uri, ext = os.path.splitext(v['path']) # 不支持的文件类型, 跳过 if ext.lower() not in self.settings['support_ext']: continue # 判断是否存在 ar = wiki.Metadata.select()\ .where(wiki.Metadata.root_id == metadata.id)\ .where(wiki.Metadata.path == v['path'])\ .where(wiki.Metadata.is_dir == is_dir) if ar.count() == 0: ar = wiki.Metadata() ar.user = self.user ar.root_id = metadata.id ar.path = v['path'] ar.is_dir = is_dir else: ar = ar.get() # 没有更改, 跳过 if 0 == is_dir and hash_key == ar.hash_key: continue ar.bytes = v['bytes'] ar.hash_key = hash_key if v.get('modified', False): ar.modified = Date.str_to_time(v['modified'].split('+')[0], '%a, %d %b %Y %H:%M:%S ') ar.save() # 子事务 child_task_affair = wiki.TaskAffairs.add( ar, task_affair.id ) if v['is_dir']: self.add_task( route.url_for('task.SyncPath', self.user.id, ar.id) + '?parent=%s' % child_task_affair.id, 1 ) else: self.add_task( route.url_for('task.SyncFile', ar.id) + '?affair=%s' % child_task_affair.id, 2 ) # 删除不存在的文件/目录 for v in path_list: ar = wiki.Metadata.select()\ .where(wiki.Metadata.root_id == metadata.id)\ .where(wiki.Metadata.path == v) if ar.count() != 0: ar.get().remove() self.finish()