def jsonrpc_delete(self, cascade=False): try: b = Backend() resp = yield b.delete([self.path], cascade) defer.returnValue(resp) except: raise
def _init(config, mode='normal'): from almar.global_config import GlobalConfig, MODE_PROXY, MODE_WORKER g = GlobalConfig.create_instance(config) # configure web service from almar.service import worker_root, proxy_root from twisted.web import server if mode == 'proxy': g.server_mode = MODE_PROXY if not g.proxy or not g.searcher: fatal_out('proxy configuration is invalid') # configure reactor reactor.suggestThreadPoolSize(int(g.proxy.max_threads)) return int(g.proxy.port), server.Site(proxy_root) else: if not g.server or not g.model or not g.database: fatal_out('server configuration is invalid') # configure reactor reactor.suggestThreadPoolSize(int(g.server.max_threads)) g.server_mode = MODE_WORKER # configure database from txpostgres import txpostgres txpostgres.ConnectionPool.min = int(g.database.min_connections) txpostgres.ConnectionPool.max = int(g.database.max_connections) from almar.backend.postgresql import PostgreSQLBackend as Backend Backend.create_instance(g.database) return int(g.server.port), server.Site(worker_root)
def async_GET(self, request): b = Backend() result = yield b.get([self.path], self.method) if self.method == 'descendant': retval = RESTResult(code=200, content=result) else: retval = RESTResult(code=200, content=result[0][1]) defer.returnValue(retval)
def jsonrpc_get(self): try: b = Backend() resp = yield b.get([self.path], None) if resp: defer.returnValue(resp[0][1]) else: defer.returnValue(dict()) except: raise
def jsonrpc_upsert(self, lst): try: b = Backend() result = yield b.upsert(lst) defer.returnValue(result) except exception.ModelNotExistError as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.MissingFieldError as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.KeyNotDefinedError as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.ConstraintViolationError as e: defer.returnValue(Fault(exception.CONSTRAINT_VIOLATION, str(e))) except Exception as e: raise
def jsonrpc_push(self, lst): try: b = Backend() resp = yield b.push(self.path, lst) defer.returnValue(resp) except exception.MissingFieldError as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.ModelNotExistError as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.KeyNotDefinedError as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.MalformedIncomingData as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.ConstraintViolationError as e: defer.returnValue(Fault(exception.CONSTRAINT_VIOLATION, str(e))) except: raise
def makeService(self, options): from almar import _init as almar_init # get site and default port port_no, site = almar_init(options["config"], mode=("normal", "proxy")[options["proxy"]]) # override port use user specified option if "port" in options and options["port"]: port_no = int(options["port"]) # start database connection if options["proxy"] == "normal": from almar.backend.postgresql import PostgreSQLBackend as Backend b = Backend() b.start() return internet.TCPServer(port_no, site)
def jsonrpc_update(self, item): try: b = Backend() if isinstance(item, dict): item['path'] = self.path resp = yield b.upsert([item]) defer.returnValue(resp) else: defer.returnValue(Fault(exception.INVALID_INPUT, "content must be json dict")) except exception.MissingFieldError as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.ModelNotExistError as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.KeyNotDefinedError as e: defer.returnValue(Fault(exception.INVALID_INPUT, str(e))) except exception.ConstraintViolationError as e: defer.returnValue(Fault(exception.CONSTRAINT_VIOLATION, str(e))) except: raise
def async_DELETE(self, request): b = Backend() result = yield b.delete([self.path], False) defer.returnValue(result)
def async_POST(self, request): b = Backend() self.postdata['path'] = self.path result = yield b.upsert([self.postdata]) defer.returnValue(RESTResult(code=200, content=result))
def jsonrpc_search(self, query): b = Backend() result = yield b.search(query) defer.returnValue(result)
def jsonrpc_touch(self, paths): b = Backend() return b.touch(paths)
def jsonrpc_delete(self, paths, cascade=False): print paths, cascade b = Backend() return b.delete(paths, cascade)
def jsonrpc_get(self, paths, method='self'): b = Backend() return b.get(paths, method)