Last active
August 29, 2015 14:22
-
-
Save abravalheri/bc84ab6c20220a6ed505 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/abravalheri/restclient.py/ brainstorm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
class JSONMapper(Mapper): | |
def string_to_intermediate(self, string): | |
return json.loads(string) | |
def intermediate_to_string(self, intermediate): | |
return json.dumps(intermediate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mapper: | |
def __init__(self, cls): | |
self.cls = cls | |
def object_to_intermediate(self, obj): | |
return obj.to_dict() | |
def intermediate_to_object(self, intermediate): | |
return self.cls(intermediate) | |
def intermediate_to_string(self, intermediate): | |
raise NotImplemented | |
def string_to_intermediate(self, string): | |
raise NotImplemented | |
def encode(self, obj): | |
return self.intermediate_to_string(self.object_to_intermediate(obj)) | |
def decode(self, string): | |
return self.intermediate_to_object(self.string_to_intermediate(string)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from lxml import etree, Element as elem | |
class XMLMapper(Mapper): | |
def object_to_intermediate(self, obj): | |
internal = obj.to_dict() | |
root = elem(obj.__class__.__name__) | |
for (key, value) in internal.items(): | |
if hasattr(value, 'mapper'): | |
value = value.mapper.object_to_intermediate(value) | |
else: | |
value = str(value) | |
root.append(elem(key, value)) | |
def intermediate_to_object(self, intermediate): | |
fields = self.cls.meta['fields'] | |
field_dict = {} | |
root = intermediate | |
for (key, desc) in fields.items(): | |
node = root.xpath("/[local-name() = '" + key + "']")[0] | |
if hasattr(desc, 'mapper'): | |
value = desc.mapper.intermediate_to_object(node) | |
else: | |
value = desc.text | |
field_dict[key] = value | |
return cls(field_dict) | |
def string_to_intermediate(self, string): | |
return etree.fromstring(string) | |
def intermediate_to_string(self, intermediate): | |
return etree.tostring(intermediate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment