def load(self,template,vars): dp=os.path.dirname(template) if dp != '': self.domain=dp filename=os.path.basename(template) try: d=Domain(self.domain, quoting=None, errors=4) # fixme: what do we really want for quoting? t=Domain(self.domain, errors=4).get_template(filename, quoting="str") # fixme: why create two Domains? except Exception as e: print "(domain is %s)" % self.domain raise e evars=evoque_dict() # special dict that returns missing keys as "${key}" evars.update(vars) yaml_txt=t.evoque(evars) # fix eval errors: # fixed_yaml=re.sub('\[EvalError\(([a-z_]+)\)]', "${\g<1>}", yaml_txt) # ooh, magic! great. # fixed_yaml=re.sub('\[NameError: name ' ([a-z_]+)\)]', "${\g<1>}", yaml_txt) # ooh, magic! great. # fixed_yaml=re.sub('<built-in function ([a-z_]+)>', "${\g<1>}", fixed_yaml) # christ, more magic # warn("syml.load: fixed_yaml is %s" % fixed_yaml) d=yaml.load(yaml_txt) # "fix" config; ie, resolve referenced values d=self._fix_hash(d,d) return d
def runTest(self): evd=evoque_dict(this='that', these='those') self.assertEqual(evd['fred'],'${fred}') self.assertEqual(evd['this'],'that') self.assertEqual(evd['these'],'those') self.assertEqual(evd.this, 'that') self.assertEqual(evd.these, 'those')
def load(self, **args): assert hasattr(self,'name') # can't do "assert self.name" because that throws Attribute error assert hasattr(self,'type') # before assert even gets to it # get the template and call evoque() on it. This should yield a yaml string tf=self.template_file() try: domain=Domain(os.path.dirname(tf), errors=4, quoting=str) # errors=4 means raise errors as an exception except ValueError as ve: raise ConfigError("Error in setting template directory: "+str(ve)) try: tf=self.template_file() template=domain.get_template(tf) except ValueError as ve: raise UserError("%s '%s': missing template file %s" % (self.type, self.name, self.template_file())) vars=args['vars'] if args.has_key('vars') else {} # consider default of self instead of {}? Or is that stupid? vars['config']=RnaseqGlobals.config #print "%s.%s: about to evoque: vars are:\n%s" % (self.name, self.type, yaml.dump(vars)) ev=evoque_dict() if 'vars' in args and args['vars']==None: raise ProgrammerGoof("vars is None") ev.update(vars) #print "templated: ev is %s\nvars is %s" % (ev,vars) try: yaml_str=template.evoque(ev) # why we want to keep this: evoque_dicts protect us against simple Key errors, but not # errors of the type ${readset['missing_key']} except KeyError as ke: print "ke is %s (%s)" % (ke, type(ke)) raise ConfigError("%s '%s': %s" % (self.type, self.name, ke)) except AttributeError as ae: raise ConfigError("%s '%s': %s" % (self.type, self.name, ae)) except TypeError as ae: raise ProgrammerGoof("%s '%s': %s" % (self.type, self.name, ae)) # Check if all keys are needed: if 'final' in args and args['final']: # might be present but false; perl rules! if len(ev.missing_keys)>0: raise ConfigError("%s %s: missing keys in final load: %s" %(self.type, self.name, ", ".join(str(i) for i in (set(ev.missing_keys))))) # call yaml.load on the string produced above, then call self.update() on the resulting dict object # print "yaml_str:\n%s" % yaml_str d=yaml.load(yaml_str) # fixme: what if template isn't yaml??? try: self.update(d) except ProgrammerGoof as oopsie: if (re.search('not a dict or dict_like', str(oopsie))): pass else: raise oopsie return self
def runTest(self): evd=evoque_dict(this='that', these='those') d={'honda':'red', 'yamaha':'blue'} evd.update(d) try: print "1. evd.honda=%s (yay)" % evd.honda self.fail() except AttributeError as ae: self.assertTrue(re.search("object has no attribute 'honda'", str(ae))) try: self.assertEqual(evd['honda'],'red') except IndexError as ie: self.fail() self.assertEqual(evd['honda'], 'red') self.assertEqual(evd['yamaha'],'blue')
def eval_tmpl(self,**args): assert hasattr(self,'name') # can't do "assert self.name" because that throws Attribute error assert hasattr(self,'type') # before assert even gets to it domain=Domain(self.template_dir, errors=4) tf=self.template_file() #print "templated: tf is %s" % tf template=domain.get_template(tf) vars=args['vars'] if args.has_key('vars') else {} # consider default of self instead of {}? Or is that stupid? try: ev=evoque_dict() ev.update(vars) output=template.evoque(ev) except (KeyError, AttributeError, TypeError) as any_e: raise ConfigError("%s '%s': %s" % (self.type, self.name, any_e)) # Check if all keys are needed: if 'final' in args and args['final']: if len(ev.missing_keys)>0: raise ConfigError("%s %s: missing keys in final load: %s" %(self.type, self.name, ", ".join(ev.missing_keys))) return output