def parseAction(self,config): s=[] fields={} pars=config["params"] for elem in config["content"]: if type(elem) is tuple: if elem[0]=="where": pars["where"]=makeTree("".join(elem[2])) elif elem[0]=="field": fields[elem[1]["name"]]=bool(str2obj(elem[1]["show"])) else: pars[elem[0]]=(elem[1],elem[2]) elif type(elem) is str: s.append(elem.strip()) try: show=pars["show"].split(",") pars["fields"]=dict(map(lambda x: (x.strip(), 1), show)) except KeyError: pass try: hide=pars["hide"].split(",") pars["fields"]=dict(map(lambda x: (x.strip(), 0), hide)) except KeyError: pass try: coll=pars["coll"].split(".") if len(coll) is 1: coll=coll[0] pars["coll"]=coll except KeyError: raise Error("no coll parameter specified") try: sort=pars["sort"].split(",") directions=pars.get("direction",self.DIRECTION).split(",") directions=map(lambda x: pymongo.__dict__.get(x.upper()),directions) if len(directions)>=len(sort): pars["sort"]=zip(sort,directions) else: import itertools pars["sort"]=list(itertools.izip_longest(sort,directions,fillvalue=directions[-1])) except: pass # if fields: # pars["fields"]=show or hide return { "command":config["command"], "content":makeTree("".join(s)), "params":pars }
def __init__(self, name, app): #All "static" computations should be here. Don't do it inside handle! #setting immutable because of setter self.immutable=False self.name=".".join(name) self.app=app self.path=os.path.join(app.viewsPath,*name)+".xml" #try: self.timestamp=os.stat(self.path).st_mtime tree=xml2tree(self.path, True) #TODO support more exception classes; #except Exception,e: # self.immutable=True # raise ViewNotFound("view '%s' not found"%(name)) ns={} attrs=tree[1] if not attrs: return #parses "xmlns:" attributes and extracts namespaces for i in attrs: if i.startswith("xmlns:"): key=i.split(":",1)[1] value=attrs[i].rsplit("/",1)[1].lower() ns[key]=value self.namespaces=ns #checks whether view inherits from another view # raise Exception(filter(lambda x: len(x) and not str.isspace(x), attrs["inherits"].split("/"))) try: self.parent=app.getView(["api"]+filter(lambda x: len(x) and not str.isspace(x), attrs["inherits"].split("/")),True)[0] except KeyError: self.parent=None except IOError: raise Error("ParentViewNotFound","View '%s' not found."%attrs["inherits"]) # except Exception,e: # raise Error("ParentViewError",str(e)) inputSchemas=[] conditions=[] actions=[] postSchemas=[] output=[] for node in tree[2]: if type(node) is str: continue name=node[0] if name=="param": inputSchemas.append(node) elif name=="condition": conditions.append(node) elif name=="output": output.append(node) elif name=="post": postSchemas=filter(lambda x: type(x) is not str, node[2]) elif name in ACTIONS: actions.append(node) self.output={} try: self.output["format"]=output[0][1]["format"] except: pass try: self.output["XSLTFile"]=str2obj(output[0][1]["XSLTFile"]) except: pass try: self.output["XSLTForceReload"]=str2obj(output[0][1]["XSLTForceReload"]) except: pass try: self.outputConfig=output[0][1]["config"] except: outputConfig="config" self.conditions=self.parseConditions(conditions) self.actions=self.parseActions(actions) self.inputSchemas=parseInputs(inputSchemas) or [] if self.parent and self.parent.inputSchemas: self.inputSchemas=self.parent.inputSchemas+self.inputSchemas self.postSchemas, self.postCount=parsePosts(postSchemas) # TODO check if it is correct ## output inheritance #if self.parent and self.parent.outputFormat: # self.outputFormat=self.parent.outputFormat # self.outputConfig=self.parent.outputConfig ## posts inheritance #if self.parent and self.parent.posts: # self.posts=self.parent.posts #if not self.actions: # self.immutable=True # return #past here this object MUST be immutable self.immutable=True
def validate(self,value,config=None): if type(str2obj(value)) in [str,unicode]: raise Error("NotNumber", "Should be number, but is %s",value) return True
def _prepareValue(self,value): return str2obj(value)
db_overwritten = config.get("/mongo[@db]") if db_overwritten: self.DB_NAME = db_overwritten connopts = {} host = config.get("/mongo[@host]") if host: connopts["host"] = host if str2obj(config.get("/deployment[@enable]", "f")): self.deploymentMode = True try: self.storage = pymongo.Connection(**connopts)[self.DB_NAME] except Exception, e: raise Error("MongoError", str(e)) if config.get("/debug"): self.dbg = { "enabled": str2obj(config.get("/debug[@enable]")), "level": config.get("/debug[@level]", "error"), } if config.get("/debug[@cutAfter]"): self.dbg["cutAfter"] = config.get("/debug[@cutAfter]") if config.get("/profiler"): self.profiler = {"enabled": str2obj(config.get("/profiler[@enable]")), "dbtimer": 0, "dbcounter": 0} self.output = { "XSLTFile": config.get("/output[@XSLTFile]", "index.xsl"), "format": config.get("/output[@format]", "application/xml"), } forceReload = config.get("/output[@XSLTForceReload]") if forceReload: self.output["XSLTForceReload"] = str2obj(forceReload) self.prefix = (config.get("/prefix") or "ACR") + "_"
db_overwritten=config.get("/mongo[@db]") if db_overwritten: self.DB_NAME=db_overwritten connopts={} host=config.get("/mongo[@host]") if host: connopts["host"]=host if str2obj(config.get("/deployment[@enable]","f")): self.deploymentMode=True try: self.storage=pymongo.Connection(**connopts)[self.DB_NAME] except Exception,e: raise Error("MongoError",str(e)) if acconfig.debug or config.get("/debug"): self.dbg={ "enabled": acconfig.debug or str2obj(config.get("/debug[@enable]")), "level": config.get("/debug[@level]","error") } if config.get("/debug[@cutAfter]"): self.dbg["cutAfter"]=config.get("/debug[@cutAfter]") if config.get("/profiler"): self.profiler={ "enabled":str2obj(config.get("/profiler[@enable]")), "dbtimer":0, "dbcounter":0 } self.output={ "XSLTFile":config.get("/output[@XSLTFile]","index.xsl"), "format":config.get("/output[@format]","application/xml") }