Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
pylibs
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
domotika
pylibs
Commits
4ebc01ec
Commit
4ebc01ec
authored
Jul 19, 2020
by
root
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CorePost to python3
parent
496d4f42
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
19 additions
and
19 deletions
+19
-19
convert.py
CorePost/corepost/convert.py
+5
-5
zmq.py
CorePost/corepost/ext/multicore/zmq.py
+1
-1
routing.py
CorePost/corepost/routing.py
+9
-9
web.py
CorePost/corepost/web.py
+4
-4
No files found.
CorePost/corepost/convert.py
View file @
4ebc01ec
...
...
@@ -11,7 +11,7 @@ import logging
import
json
from
twisted.python
import
log
primitives
=
(
int
,
long
,
float
,
bool
,
str
,
unicode
)
primitives
=
(
int
,
float
,
bool
,
str
,
str
)
def
convertForSerialization
(
obj
):
"""Converts anything (clas,tuples,list) to the safe serializable equivalent"""
...
...
@@ -39,7 +39,7 @@ def convertForSerialization(obj):
def
convertClassToDict
(
clazz
):
"""Converts a class to a dictionary"""
properties
=
{}
for
prop
,
val
in
clazz
.
__dict__
.
ite
rite
ms
():
for
prop
,
val
in
clazz
.
__dict__
.
items
():
#omit private fields
if
not
prop
.
startswith
(
"_"
):
properties
[
prop
]
=
val
...
...
@@ -50,7 +50,7 @@ def traverseDict(dictObject):
"""Traverses a dict recursively to convertForSerialization any nested classes"""
newDict
=
{}
for
prop
,
val
in
dictObject
.
ite
rite
ms
():
for
prop
,
val
in
dictObject
.
items
():
newDict
[
prop
]
=
convertForSerialization
(
val
)
return
newDict
...
...
@@ -94,14 +94,14 @@ def getXML(obj, objname=None):
list
:
getXML_list
,
tuple
:
getXML_list
,
}
if
adapt
.
has_key
(
obj
.
__class__
)
:
if
obj
.
__class__
in
adapt
:
return
adapt
[
obj
.
__class__
](
obj
,
objname
)
else
:
return
"<
%(n)
s>
%(o)
s</
%(n)
s>"
%
{
'n'
:
objname
,
'o'
:
str
(
obj
)}
def
getXML_dict
(
indict
,
objname
=
None
):
h
=
"<
%
s>"
%
objname
for
k
,
v
in
indict
.
items
(
):
for
k
,
v
in
list
(
indict
.
items
()
):
h
+=
getXML
(
v
,
k
)
h
+=
"</
%
s>"
%
objname
return
h
...
...
CorePost/corepost/ext/multicore/zmq.py
View file @
4ebc01ec
...
...
@@ -3,7 +3,7 @@ __author__ = 'jacekf'
try
:
import
txZMQ
except
ImportError
as
ex
:
print
"You must have ZeroMQ and txZMQ installed"
print
(
"You must have ZeroMQ and txZMQ installed"
)
raise
ex
from
corepost
import
Response
,
IRESTResource
...
...
CorePost/corepost/routing.py
View file @
4ebc01ec
...
...
@@ -11,7 +11,7 @@ from corepost.utils import getMandatoryArgumentNames, safeDictUpdate
from
corepost.convert
import
convertForSerialization
,
generateXml
,
convertToJson
from
corepost.filters
import
IRequestFilter
,
IResponseFilter
from
enums
import
MediaType
from
.
enums
import
MediaType
from
twisted.internet
import
defer
from
twisted.web.http
import
parse_qs
from
twisted.python
import
log
...
...
@@ -89,7 +89,7 @@ class UrlRouter:
args
=
g
.
groupdict
()
# convert to expected datatypes
if
len
(
args
)
>
0
:
for
name
in
args
.
keys
(
):
for
name
in
list
(
args
.
keys
()
):
converter
=
self
.
__argConverters
[
name
]
if
converter
!=
None
:
args
[
name
]
=
converter
(
args
[
name
])
...
...
@@ -228,7 +228,7 @@ class RequestRouter:
# go through all the URLs, pick up the ones matching by content type
# and then validate which ones match by path/argument to a particular UrlRouterInstance
for
contentTypeInstances
in
self
.
__urls
[
request
.
method
]
.
values
(
):
for
contentTypeInstances
in
list
(
self
.
__urls
[
request
.
method
]
.
values
()
):
if
contentType
in
contentTypeInstances
:
# there is an exact function for this incoming content type
...
...
@@ -368,7 +368,7 @@ class RequestRouter:
def
__parseRequestData
(
self
,
request
):
'''Automatically parses JSON,XML,YAML if present'''
if
request
.
method
in
(
Http
.
POST
,
Http
.
PUT
)
and
HttpHeader
.
CONTENT_TYPE
in
request
.
received_headers
.
keys
(
):
if
request
.
method
in
(
Http
.
POST
,
Http
.
PUT
)
and
HttpHeader
.
CONTENT_TYPE
in
list
(
request
.
received_headers
.
keys
()
):
contentType
=
request
.
received_headers
[
"content-type"
]
request
.
data
=
request
.
content
.
read
()
...
...
@@ -394,23 +394,23 @@ class RequestRouter:
# see: http://twistedmatrix.com/pipermail/twisted-web/2007-March/003338.html
requestargs
=
request
.
args
if
request
.
method
==
Http
.
PUT
and
HttpHeader
.
CONTENT_TYPE
in
request
.
received_headers
.
keys
(
)
\
if
request
.
method
==
Http
.
PUT
and
HttpHeader
.
CONTENT_TYPE
in
list
(
request
.
received_headers
.
keys
()
)
\
and
request
.
received_headers
[
HttpHeader
.
CONTENT_TYPE
]
==
MediaType
.
APPLICATION_FORM_URLENCODED
:
# request.data is populated in __parseRequestData
requestargs
=
parse_qs
(
request
.
data
,
1
)
#merge form args
if
len
(
requestargs
.
keys
(
))
>
0
:
for
arg
in
requestargs
.
keys
(
):
if
len
(
list
(
requestargs
.
keys
()
))
>
0
:
for
arg
in
list
(
requestargs
.
keys
()
):
# maintain first instance of an argument always
safeDictUpdate
(
allargs
,
arg
,
requestargs
[
arg
][
0
])
elif
hasattr
(
request
,
'json'
):
# if YAML parse root elements instead of form elements
for
key
in
request
.
json
.
keys
(
):
for
key
in
list
(
request
.
json
.
keys
()
):
safeDictUpdate
(
allargs
,
key
,
request
.
json
[
key
])
elif
hasattr
(
request
,
'yaml'
):
# if YAML parse root elements instead of form elements
for
key
in
request
.
yaml
.
keys
(
):
for
key
in
list
(
request
.
yaml
.
keys
()
):
safeDictUpdate
(
allargs
,
key
,
request
.
yaml
[
key
])
elif
hasattr
(
request
,
'xml'
):
# if XML, parse attributes first, then root nodes
...
...
CorePost/corepost/web.py
View file @
4ebc01ec
...
...
@@ -6,7 +6,7 @@ Main server classes
from
corepost
import
Response
,
IRESTResource
from
corepost.enums
import
Http
from
corepost.routing
import
UrlRouter
,
RequestRouter
from
enums
import
MediaType
from
.
enums
import
MediaType
from
formencode
import
FancyValidator
,
Invalid
from
twisted.internet
import
reactor
from
twisted.internet.defer
import
Deferred
...
...
@@ -77,7 +77,7 @@ class RESTResource(Resource):
def
__applyResponse
(
self
,
request
,
code
,
headers
=
{
"content-type"
:
MediaType
.
TEXT_PLAIN
}):
request
.
setResponseCode
(
code
)
if
headers
!=
None
:
for
header
,
value
in
headers
.
ite
rite
ms
():
for
header
,
value
in
headers
.
items
():
request
.
setHeader
(
header
,
value
)
def
run
(
self
,
port
=
8080
):
...
...
@@ -117,11 +117,11 @@ def validate(schema=None,**vKwargs):
try
:
schema
.
to_python
(
kwargs
)
except
Invalid
as
ex
:
for
arg
,
error
in
ex
.
error_dict
.
items
(
):
for
arg
,
error
in
list
(
ex
.
error_dict
.
items
()
):
errors
.
append
(
"
%
s:
%
s ('
%
s')"
%
(
arg
,
error
.
msg
,
error
.
value
))
# custom validators
for
arg
in
vKwargs
.
keys
(
):
for
arg
in
list
(
vKwargs
.
keys
()
):
validator
=
vKwargs
[
arg
]
if
arg
in
kwargs
:
val
=
kwargs
[
arg
]
...
...
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