def test_findTemplateTargetsFor(self): cwd = os.getcwd() templates = findTemplateTargetsFor(cwd+'/resource','MyService', 'm1') self.assertEquals(len(templates), 3) targets = [i[0] for i in templates] self.assertTrue('xml' in targets) self.assertTrue('html' in targets) self.assertTrue('rss' in targets) templates = findTemplateTargetsFor(cwd+'/resource', 'MyService', 'm2') targets = [i[0] for i in templates] self.assertEquals(len(templates), 2) self.assertTrue('xml' in targets) self.assertTrue('html' in targets)
def loadConfig(self, servicePath, runconfig=None): """ Load service configuration file and then augment with details of all the methods in this service classed as 'public'. In doing this the attributes assigned by any decorators on the public methods are checked out, especially the transform chain. Defaults are assigned to, for example, HTML and XML output keys if none has been specified. Standard templates are sought out on the filesystem and attached where found and the @template keyword is substituted for in the transform chain. runconfig is the name of a run-time configuration file specified at the time of launching (either relative to the service config dir or an absolute path). This can specify many things including, for example, the name under which to publish this service and the location of the resource folder. Developers should not use the logger here: loadConfig should be useable prior to initSupportServices having been called.""" servicePath, self.settings = locateService(self.name, servicePath, runconfig=runconfig) if self.settings.has_key('profile'): self.profile = self.settings.profile else: self.profile = PelotonSettings() self.profile['_sysRunConfig'] = runconfig if not self.profile.has_key('publishedName'): self.profile['publishedName'] = self.name publicMethods = [m for m in dir(self) if m.startswith('public_') and callable(getattr(self, m))] if not self.profile.has_key('methods'): self.profile['methods'] = PelotonSettings() methods = self.profile['methods'] for nme in publicMethods: mthd = getattr(self, nme) shortname = nme[7:] templateTargets = findTemplateTargetsFor(self.profile['resourceRoot'], self.name, shortname) if hasattr(mthd, "_PELOTON_METHOD_PROPS"): properties = mthd._PELOTON_METHOD_PROPS else: properties = PelotonSettings() # step one, find all template files and insert # into transforms for target, templateFile in templateTargets: key = "transform.%s" % target if properties.has_key(key): # look for @template and substitute if "@template" in properties[key]: properties[key][properties[key].index("@template")] \ = "template('%s')" % templateFile else: # insert an entry properties['transform.%s'%target] = \ ["template('%s')" % templateFile] # step two insert defaults for any empty transforms that # need a little something defaultTransforms = {'xml' : 'defaultXMLTransform', 'html' : 'defaultHTMLTransform', 'json' : 'jsonTransform' } for target in ['xml', 'html', 'json']: key = "transform.%s" % target if not properties.has_key(key) \ or properties[key] == []: properties[key]=[defaultTransforms[target]] # step three, look for all transforms which still have # @transform in the chain and replace with defaults for k, v in properties.items(): if not k.startswith('transform.'): continue target = k[10:] if "@template" in v: try: v[v.index('@template')] = defaultTransforms[target] except: v[v.index('@template')] = '' if not methods.has_key(shortname): methods[shortname] = PelotonSettings() record = methods[shortname] record['doc'] = mthd.__doc__ record['properties'] = str(properties) self.version = self.profile['version']