from router import Router, Resource from router import JSONRenderer, HTMLRenderer, XMLRenderer, AtomRenderer router = Router() # Add Renderers router.add_renderer(JSONRenderer, default=True) router.add_renderer(HTMLRenderer) router.add_renderer(AtomRenderer) router.add_auth_source(DbAuthenticator('users.db')) router.add_authenz_source(DbAuthorizor('users.db')) revision_spec = FragmentSpec(required=False, default='tip', regex='(tip|[0-9a-zA-Z]+)') # Add Resource Mappings Route('/{project_name}', ProjectSummaryResource, [ Route('/summary', ProjectSummaryResource), Route('/shortlog/{revision}', ShortLogResource, uri_spec={ 'revision': revision_spec }), Route('/graph/{revision}', GraphResource, uri_spec={ 'revision': revision_spec }), Route('/raw-rev/{revision}', RawRevisionResource, uri_spec={ 'revision': revision_spec }), Route('/tags', TagsResource), Route('/annotate/{revision}/{filename}', AnnotateResource, uri_spec={ 'revision': revision_spec, 'filename': FragmentSpec(required=False) }), Route(('/diff/{revision}/{filename}', '/filediff/{revision}/{filename}'), DiffResource, uri_spec={ 'revision': revision_spec, 'filename': FragmentSpec(required=False) }), Route('/raw-file/{revision}/{filename}', RawFileResource, uri_spec={ 'revision': revision_spec, 'filename': FragmentSpec(required=False) }), Route('/branches', BranchesResource), Route('/archive/{revision}.tar.{format}', ArchiveResource, uri_spec={ 'revision': FragmentSpec(regex='(tip|[0-9a-zA-Z]+)'), 'format': FragmentSpec(value_list=('gz', 'bz2') }), Route(('/rev/{revision}','/changeset/{revision}'), RevisionResource, uri_spec={ 'revision': revision_spec }), Route(('/log/{revision}', '/changelog/{revision}', '/filelog/{revision}'), LogResource, uri_spec={ 'revision': revision_spec }), Route('/file/{revision}/{filename}', FileResource, uri_spec={ 'revision': revision_spec, 'filename': FragmentSpec(required=False) }), ]) class constant(object): """ Constant descriptor to provide a level of protection against changes to constants in class instances. Does not prevent changes directly to constants in non-instances. """ def __init__(self, value): self.value = value def __get__(self, instance, owner): return self.value def __set__(self, instance, value): raise ValueError('Can not assign to constant.') def __delete__(self, instance): raise ValueError('Can not delete constant.') class Route(object): # ---------------------------------------------------------------- # Constants # ---------------------------------------------------------------- REDIRECT = constant('redirect') FAILURE = constant('failure') # ---------------------------------------------------------------- # Required Parameters # ---------------------------------------------------------------- #: Part of the URI that will map to this route uri_part = None #: Resource existing at this route resource = None #: Resource requires that the user is using an HTTPS connection #: can be True, False, REDIRECT or FAILRE. If True and no HTTPS #: the route will fail to match. If REDIRECT and no HTTPS the #: client will be redirect to the secure version of the resouce. #: If FAILURE a 403 (Forbidden) error will be returned to the #: client. https = False #: Route requires authentication requires_authentication = True # ---------------------------------------------------------------- # Optional, if unspecified these will not be used # ---------------------------------------------------------------- #: Dictionary of uri fragment names to FragmentSpec objects used #: to validate the individual uri fragments uri_spec = None #: Callable or list of callables to which the matching resouce #: and context will be passed, the first failure will cause the #: route to not match. conditions = None #: Type of permission required. The router doesn't care what #: this object is as long as the security system understands it permissions_required = None # ---------------------------------------------------------------- # Optional, if unspecified these will use the router defaults # ---------------------------------------------------------------- #: List of content-types supported by this route content_types = None #: Source for authentication auth_source = None #: Source for authorization authz_source = None class FragmentSpec(object): #: Indicates if the framgent is required required = True #: Regex used to check if the fragment is valid if specified #: value_list may not be specified. regex = r'.*' #: Optional list of acceptable values. May not be specified #: with regex value_list = None #: Default value if the user provides no value default = None class NotSupported(object): pass class Resource(object): GET = NotSupported() POST = NotSupported() HEAD = NotSupported() PUT = NotSupported() DELETE = NotSupported()