Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
Initial test implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
acdha committed Apr 6, 2011
1 parent 25a10d0 commit 1c8c820
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion remoteobjects/dataobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,11 @@ def to_dict(self):
return data

@classmethod
def from_dict(cls, data):
def from_dict(cls, data, client=None):
"""Decodes a dictionary into a new `DataObject` instance."""
self = cls()
if client:
self._http = client
self.update_from_dict(data)
return self

Expand Down
20 changes: 10 additions & 10 deletions remoteobjects/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __get__(self, obj, cls):
else:
value = self.default
else:
value = self.decode(value)
value = self.decode(value, client=getattr(obj, "_http", None))
# Store the value so we need decode it only once.
obj.__dict__[self.attrname] = value

Expand All @@ -167,7 +167,7 @@ def __delete__(self, obj):
except KeyError:
pass

def decode(self, value):
def decode(self, value, client=None):
"""Decodes a dictionary value into a `DataObject` attribute value.
This implementation returns the `value` parameter unchanged. This is
Expand Down Expand Up @@ -239,7 +239,7 @@ def __set__(self, obj, value):
raise ValueError('Value %r is not expected value %r'
% (value, self.value))

def decode(self, value):
def decode(self, value, client=None):
if value != self.value:
raise ValueError('Value %r is not expected value %r'
% (value, self.value))
Expand Down Expand Up @@ -276,10 +276,10 @@ def install(self, attrname, cls):
# Make sure our content field knows its owner too.
self.fld.install(attrname, cls)

def decode(self, value):
def decode(self, value, client=None):
"""Decodes the dictionary value (a list of dictionary values) into a
`DataObject` attribute (a list of `DataObject` attribute values)."""
return [self.fld.decode(v) for v in value]
return [self.fld.decode(v, client=client) for v in value]

def encode(self, value):
"""Encodes a `DataObject` attribute (a list of `DataObject` attribute
Expand All @@ -296,11 +296,11 @@ class Dict(List):
"""

def decode(self, value):
def decode(self, value, client=None):
"""Decodes the dictionary value (a dictionary with dictionary values
for values) into a `DataObject` attribute (a dictionary with
`DataObject` attributes for values)."""
return dict((k, self.fld.decode(v)) for k, v in value.iteritems())
return dict((k, self.fld.decode(v, client=client)) for k, v in value.iteritems())

def encode(self, value):
"""Encodes a `DataObject` attribute (a dictionary with decoded
Expand Down Expand Up @@ -341,14 +341,14 @@ def set_cls(self, cls):

cls = property(get_cls, set_cls)

def decode(self, value):
def decode(self, value, client=None):
"""Decodes the dictionary value into an instance of the `DataObject`
class the field references."""
if value is None:
if callable(self.default):
return self.default()
return self.default
return self.cls.from_dict(value)
return self.cls.from_dict(value, client=client)

def encode(self, value):
"""Encodes an instance of the field's DataObject class into its
Expand All @@ -367,7 +367,7 @@ def __init__(self, dateformat=None, **kwargs):
if dateformat is not None:
self.dateformat = dateformat

def decode(self, value):
def decode(self, value, client=None):
"""Decodes a timestamp string into a `DataObject` attribute (a Python
`datetime` instance).
Expand Down

0 comments on commit 1c8c820

Please sign in to comment.