summaryrefslogtreecommitdiff
path: root/foundry/test_router.py
blob: 15e97fdbed463e9c963ed02d5a6a8f57fd61d189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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()