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
4ecf20e7
Commit
4ecf20e7
authored
Jun 27, 2012
by
Jacek Furmankiewicz
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://github.com/kaosat-dev/corepost
parents
b2b41a31
4204008e
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
109 additions
and
61 deletions
+109
-61
convert.py
corepost/convert.py
+41
-14
enums.py
corepost/enums.py
+7
-1
routing.py
corepost/routing.py
+58
-37
utils.py
corepost/utils.py
+3
-9
No files found.
corepost/convert.py
View file @
4ecf20e7
...
...
@@ -7,27 +7,41 @@ for JSON/XML/YAML output
'''
import
collections
import
logging
import
json
from
UserDict
import
DictMixin
from
twisted.python
import
log
advanced_json
=
False
try
:
import
jsonpickle
advanced_json
=
True
except
ImportError
:
pass
primitives
=
(
int
,
long
,
float
,
bool
,
str
,
unicode
)
def
convertForSerialization
(
obj
):
"""Converts anything (clas,tuples,list) to the safe serializable equivalent"""
if
type
(
obj
)
in
primitives
:
try
:
if
type
(
obj
)
in
primitives
:
# no conversion
return
obj
elif
isinstance
(
obj
,
dict
)
or
isinstance
(
obj
,
DictMixin
):
return
traverseDict
(
obj
)
elif
isClassInstance
(
obj
):
return
convertClassToDict
(
obj
)
elif
isinstance
(
obj
,
collections
.
Iterable
)
and
not
isinstance
(
obj
,
str
):
# iterable
values
=
[]
for
val
in
obj
:
values
.
append
(
convertForSerialization
(
val
))
return
values
else
:
# return as-is
return
obj
elif
isinstance
(
obj
,
dict
)
or
isinstance
(
obj
,
DictMixin
):
return
traverseDict
(
obj
)
elif
isClassInstance
(
obj
):
return
convertClassToDict
(
obj
)
elif
isinstance
(
obj
,
collections
.
Iterable
)
and
not
isinstance
(
obj
,
str
):
# iterable
values
=
[]
for
val
in
obj
:
values
.
append
(
convertForSerialization
(
val
))
return
values
else
:
# return as-is
return
obj
except
AttributeError
as
ex
:
log
.
msg
(
ex
,
logLevel
=
logging
.
WARN
)
return
obj
def
convertClassToDict
(
clazz
):
...
...
@@ -48,6 +62,19 @@ def traverseDict(dictObject):
newDict
[
prop
]
=
convertForSerialization
(
val
)
return
newDict
def
convertToJson
(
obj
):
"""Converts to JSON, including Python classes that are not JSON serializable by default"""
if
advanced_json
:
try
:
return
jsonpickle
.
encode
(
obj
,
unpicklable
=
False
)
except
Exception
as
ex
:
raise
RuntimeError
(
str
(
ex
))
try
:
return
json
.
dumps
(
obj
)
except
Exception
as
ex
:
raise
RuntimeError
(
str
(
ex
))
def
generateXml
(
obj
):
"""Generates basic XML from an object that has already been converted for serialization"""
...
...
corepost/enums.py
View file @
4ecf20e7
...
...
@@ -4,20 +4,26 @@ Common enums
@author: jacekf
'''
class
Http
:
"""Enumerates HTTP methods"""
GET
=
"GET"
POST
=
"POST"
PUT
=
"PUT"
DELETE
=
"DELETE"
OPTIONS
=
"OPTIONS"
HEAD
=
"HEAD"
PATCH
=
"PATCH"
class
HttpHeader
:
"""Enumerates common HTTP headers"""
CONTENT_TYPE
=
"content-type"
ACCEPT
=
"accept"
class
MediaType
:
"""Enumerates media types"""
"""Enumerates media types"""
WILDCARD
=
"*/*"
APPLICATION_XML
=
"application/xml"
APPLICATION_ATOM_XML
=
"application/atom+xml"
...
...
corepost/routing.py
View file @
4ecf20e7
This diff is collapsed.
Click to expand it.
corepost/utils.py
View file @
4ecf20e7
...
...
@@ -2,7 +2,7 @@
Various CorePost utilities
'''
from
inspect
import
getargspec
import
json
def
getMandatoryArgumentNames
(
f
):
'''Returns a tuple of the mandatory arguments required in a function'''
...
...
@@ -11,17 +11,12 @@ def getMandatoryArgumentNames(f):
return
args
else
:
return
args
[
0
:
len
(
args
)
-
len
(
defaults
)]
def
getRouterKey
(
method
,
url
):
'''Returns the common key used to represent a function that a request can be routed to'''
return
"
%
s
%
s"
%
(
method
,
url
)
def
convertToJson
(
obj
):
"""Converts to JSON, including Python classes that are not JSON serializable by default"""
try
:
return
json
.
dumps
(
obj
)
except
Exception
as
ex
:
raise
RuntimeError
(
str
(
ex
))
def
checkExpectedInterfaces
(
objects
,
expectedInterface
):
"""Verifies that all the objects implement the expected interface"""
...
...
@@ -33,4 +28,3 @@ def safeDictUpdate(dictObject,key,value):
"""Only adds a key to a dictionary. If key exists, it leaves it untouched"""
if
key
not
in
dictObject
:
dictObject
[
key
]
=
value
\ No newline at end of file
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