class Handler(): def __init__(self, name, *args, **kwargs): self.args = args self.kwargs = kwargs if name not in [ "io", "redis", "mysql", "ram", "http", "connect", 'rule', 'alarm' ]: # 如果是自定义的,自己处理 self.server = None else: self.server = DBServer('everhomes_{0}'.format(name), 'tasks') @abc.abstractmethod def invoke(self, data=None, **conditions): raise NotImplementedError("The method is not Implemented") def close(self): self.server.close() @staticmethod def create_handler(name, dic, arr): if len(name.split('.')) > 2: # if is extension handler, do yourself module, class_form = name.split('.')[:-1], name.split('.')[-1] module = __import__('.'.join(module), fromlist=[class_form]) return getattr(module, class_form)('', *arr, **dic) module = __import__(Handler.__module__, fromlist=[name]) return getattr(module, name)(arr[0]) @staticmethod def unknown_handler(method, name, params): return ExtensionHandler(method, name, params)
class Handler(): def __init__(self, name, *args, **kwargs): self.args = args self.kwargs = kwargs if name not in ["io", "redis", "mysql", "ram", "http", "connect", 'rule', 'alarm']: # 如果是自定义的,自己处理 self.server = None else: self.server = DBServer('everhomes_{0}'.format(name), 'tasks') @abc.abstractmethod def invoke(self, data=None, **conditions): raise NotImplementedError("The method is not Implemented") def close(self): self.server.close() @staticmethod def create_handler(name, dic, arr): if len(name.split('.')) > 2: # if is extension handler, do yourself module, class_form = name.split('.')[:-1], name.split('.')[-1] module = __import__('.'.join(module), fromlist=[class_form]) return getattr(module, class_form)('', *arr, **dic) module = __import__(Handler.__module__, fromlist=[name]) return getattr(module, name)(arr[0]) @staticmethod def unknown_handler(method, name, params): return ExtensionHandler(method, name, params)
def validate(key, condition): server = DBServer(key, 'tasks') # should add datetime support? ret = server.query(pattern=condition) server.close() if ret is None or len(ret) <= 0: return False return True
def __init__(self, name, *args, **kwargs): self.args = args self.kwargs = kwargs if name not in [ "io", "redis", "mysql", "ram", "http", "connect", 'rule', 'alarm' ]: # 如果是自定义的,自己处理 self.server = None else: self.server = DBServer('everhomes_{0}'.format(name), 'tasks')
def __init__(self, name, *args, **kwargs): self.args = args self.kwargs = kwargs if name not in ["io", "redis", "mysql", "ram", "http", "connect", 'rule', 'alarm']: # 如果是自定义的,自己处理 self.server = None else: self.server = DBServer('everhomes_{0}'.format(name), 'tasks')
class Rule(): cls = None def __init__(self): self.server = DBServer('everhomes_rule', 'tasks') def _find(self, pattern=None, **conditions): return self.server.query(pattern=pattern, **conditions) def _find_all(self): return self.server.find_all() @staticmethod @log def validate(key, condition): server = DBServer(key, 'tasks') # should add datetime support? ret = server.query(pattern=condition) server.close() if ret is None or len(ret) <= 0: return False return True # send alarm @staticmethod def send_alarm(data): server = DBServer('everhomes_alarm', 'tasks') server.insert(data) server.close() @classmethod def find(cls, pattern=None, **conditions): return cls.instance()._find(pattern=pattern, **conditions) @classmethod def find_all(cls): return cls.instance()._find_all() @classmethod def instance(cls): if cls.cls is None: cls.cls = cls() return cls.cls
def __init__(self): self.server = DBServer('everhomes_rule', 'tasks')
def send_alarm(data): server = DBServer('everhomes_alarm', 'tasks') server.insert(data) server.close()
def __init__(self): """ db client DBServer('collection','table') """ self.db = DBServer('everhomes_rule', 'tasks')
class RuleManager(): def __init__(self): """ db client DBServer('collection','table') """ self.db = DBServer('everhomes_rule', 'tasks') def register(self, rule, **condition): """ :param rule: unique name :param condition: :return: """ if self.db.find_one(**{'_name': rule}): logger.error("The rule name is exist.", "Rule name=", rule) raise ValueError("Duplicate key error") self.db.insert(condition) def deregister(self, rule): """ :param rule: rule name :return: """ self.db.delete(pattern={'_name': rule}) def list_rules(self): return self.db.find_all() def query(self, **condition): return self.db.query(**condition) def close(self): self.db.close() def __del__(self): self.db.close()