Ejemplo n.º 1
0
 def build_resource(self, target, host=None):
     resource = Resource()
             
     for k,v in get_rest_dict(target).items():
         if k in resource.__dict__ and not resource.__dict__.get(k) and k != 'path':
             resource.__dict__[k] = v
     
     #build path
     resource.path='/'
      
     self._path_chain.append(get_rest_dict(target))              #add tail
     if self._path_chain is not []:
         for rest_dict in self._path_chain:
             if rest_dict.path:
                 resource.path = (resource.path.rstrip('/') +    #remove the tailing letter '/' 
                              '/' +
                              rest_dict.path.strip('/'))
     self._path_chain.remove(get_rest_dict(target))              #remove tail
         
     resource.target = target
             
     params = dict(('{{{0}}}'.format(p),
                    '(?P<{0}>.+)'.format(p)
                   ) for p in re.findall(r'''\{
                                         (.+?)    #groups
                                         \}''', resource.path, re.VERBOSE))
     
     pattern = resource.path
     for k,v in params.items():
         pattern = re.sub(k, v, pattern)
                 
     resource.pattern = re.compile(r'^{0}$'.format(pattern.strip('/'))) #match exactly the path
     
     if host:
         resource.host = host
         
     resource._fullargspec = getfullargspec(resource.target)
     if resource._fullargspec.defaults:
         tmp = resource._fullargspec
         resource._args_defs = dict(zip(tmp.args[-(len(tmp.defaults)):], tmp.defaults))
     
     return resource
Ejemplo n.º 2
0
 def build_resources(self):
     resources = []
     if has_rest_dict(self.buildobj):
         self._path_chain.append(get_rest_dict(self.buildobj))# search chain
         
     for v in vars(self.buildobj).values():
         if isinstance(v, type):
             resources.extend(ResourceBuilder(v,
                                 self._path_chain).build_resources())
         elif isinstance(v, FunctionType):
             if has_rest_dict(v):
                 resources.append(self.build_resource(target=v,
                                                      host=self.buildobj))
     return resources