Exemplo n.º 1
0
	def _resolve(self, raw_path):
		# Tokenize path
		path = tokenize_path(raw_path)
		node = control.root_controller()
		cls = node
		
		log.debug('resolving %s (%r) on tree %r', raw_path, path, node)
		
		# Check root
		if node is None:
			return wrap_exc_in_callable(http.ControllerNotFound('No root controller exists'))
		
		# Special case: empty path == root.__call__
		if not path:
			try:
				node = node().__call__
				log.debug('found leaf: %s', node)
				return node
			except AttributeError:
				return wrap_exc_in_callable(http.MethodNotFound('/'))
		
		# Traverse tree
		for part in path:
			log.debug('looking at part %r', part)
			found = None
			
			# 1. Search subclasses first
			log.debug('matching %r to subclasses of %r', part, node)
			try:
				subclasses = node.__subclasses__()
			except AttributeError:
				log.debug('node %r does not have subclasses -- returning MethodNotFound')
				return wrap_exc_in_callable(http.MethodNotFound(raw_path))
			for subclass in node.__subclasses__():
				if _node_name(subclass, subclass.controller_name()) == part:
					if getattr(subclass, 'hidden', False):
						continue
					found = subclass
					break
			if found is not None:
				node = found
				cls = node
				continue
			
			# 2. Search methods
			log.debug('matching %r to methods of %r', part, node)
			# Aquire instance
			if type(node) is type:
				node = node()
			for k,v in node.__dict__.items():
				if _node_name(v, k.lower()) == part:
					# If the leaf is hidden, we skip it
					if getattr(v, 'hidden', False):
						continue
					# If the leaf is not defined directly on parent node node, and
					# node.delegate evaluates to False, we bail out
					if not control.leaf_is_visible(v, cls):
						node = None
					else:
						found = v
					break
			
			# Check found node
			if found is not None:
				node = found
				node_type = type(node)
				# The following two lines enables accepting prefix routes:
				#if node_type is MethodType or node_type is FunctionType:
				#	break
			else:
				# Not found
				return wrap_exc_in_callable(http.MethodNotFound(raw_path))
		
		# Did we hit a class/type at the end? If so, get its instance.
		if type(node) is type:
			try:
				cls = node
				node = cls().__call__
				if not control.leaf_is_visible(node, cls):
					node = None
			except AttributeError:
				# Uncallable leaf
				node = None
		
		# Not callable?
		if node is None or not callable(node):
			return wrap_exc_in_callable(http.MethodNotFound(raw_path))
		
		log.debug('found leaf: %s', node)
		return node
Exemplo n.º 2
0
def _get_template(node):
    tpl = getattr(node, "template", None)
    if tpl is not None:
        if not isinstance(tpl, list):
            tpl = tokenize_path(str(tpl))
        return tpl