Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
corepost
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
nexlab
corepost
Commits
0d42ce09
Commit
0d42ce09
authored
Aug 23, 2011
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
request router work
parent
4a135a33
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
2 deletions
+29
-2
server.py
src/corepost/server.py
+28
-1
server_test.py
src/corepost/test/server_test.py
+1
-1
No files found.
src/corepost/server.py
View file @
0d42ce09
...
...
@@ -3,6 +3,7 @@ Created on 2011-08-23
@author: jacekf
'''
import
re
from
twisted.internet
import
reactor
from
twisted.web.resource
import
Resource
from
twisted.web.server
import
Site
...
...
@@ -31,14 +32,39 @@ class MediaType:
TEXT_HTML
=
"text/html"
class
RequestRouter
:
__urlMatcher
=
re
.
compile
(
r"<(int|float|):?([a-zA-Z0-9]+)>"
)
__urlRegexReplace
=
{
""
:
r"(.+)"
,
"int"
:
r"(d+)"
,
"float"
:
r"(d+\.d+)"
}
""" Common class for containing info related to routing a request to a function """
def
__init__
(
self
,
url
,
method
,
accepts
,
produces
):
def
__init__
(
self
,
f
,
url
,
method
,
accepts
,
produces
):
self
.
__url
=
url
self
.
__method
=
method
self
.
__accepts
=
accepts
self
.
__produces
=
produces
self
.
__f
=
f
self
.
__args
=
[]
# dict of arg names -> group index
#parse URL into regex used for matching
m
=
RequestRouter
.
__urlMatcher
.
findall
(
url
)
self
.
__matchUrl
=
url
for
match
in
m
:
self
.
__args
.
append
(
match
[
1
])
if
len
(
match
[
0
])
==
0
:
# string
self
.
__matchUrl
=
self
.
__matchUrl
.
replace
(
"<
%
s>"
%
match
[
1
],
RequestRouter
.
__urlRegexReplace
[
match
[
0
]])
else
:
# non string
self
.
__matchUrl
=
self
.
__matchUrl
.
replace
(
"<
%
s:
%
s>"
%
match
,
RequestRouter
.
__urlRegexReplace
[
match
[
0
]])
self
.
__matcher
=
re
.
compile
(
self
.
__matchUrl
)
def
getMatch
(
self
,
url
):
return
self
.
__matcher
.
findall
(
url
)
def
call
(
self
,
**
kwargs
):
self
.
__f
(
**
kwargs
)
class
CorePost
(
Resource
):
'''
...
...
@@ -61,6 +87,7 @@ class CorePost(Resource):
for
method
in
methods
:
self
.
__urls
[
method
][
url
]
=
f
rq
=
RequestRouter
(
f
,
url
,
method
,
accepts
,
produces
)
self
.
__methods
[
url
]
=
f
...
...
src/corepost/test/server_test.py
View file @
0d42ce09
...
...
@@ -12,7 +12,7 @@ app = CorePost()
def
test
():
return
"test"
@
app
.
route
(
"/"
,(
Http
.
POST
,
Http
.
PUT
))
@
app
.
route
(
"/
test/<int:jacek>/test/<stringid>/float/<float:floater>/test2
"
,(
Http
.
POST
,
Http
.
PUT
))
def
test_post
():
return
"test POST/PUT"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment