Renamed <object>._trovebox to <object>._client, to match API classes.

Changed _type to a class attribute.
This commit is contained in:
sneakypete81 2013-09-09 18:08:11 +01:00
parent 3cfc090dd0
commit 7a7b43afc7
6 changed files with 66 additions and 67 deletions

View file

@ -7,11 +7,12 @@ from .photo import Photo
class Action(TroveboxObject):
""" Representation of an Action object """
def __init__(self, trovebox, json_dict):
_type = "action"
def __init__(self, client, json_dict):
self.target = None
self.target_type = None
TroveboxObject.__init__(self, trovebox, json_dict)
self._type = "action"
TroveboxObject.__init__(self, client, json_dict)
self._update_fields_with_objects()
def _update_fields_with_objects(self):
@ -19,7 +20,7 @@ class Action(TroveboxObject):
# Update the photo target with photo objects
if self.target is not None:
if self.target_type == "photo":
self.target = Photo(self._trovebox, self.target)
self.target = Photo(self._client, self.target)
else:
raise NotImplementedError("Actions can only be assigned to "
"Photos")
@ -32,7 +33,7 @@ class Action(TroveboxObject):
Returns True if successful.
Raises a TroveboxError if not.
"""
result = self._trovebox.post("/action/%s/delete.json" %
result = self._client.post("/action/%s/delete.json" %
self.id, **kwds)["result"]
if not result:
raise TroveboxError("Delete response returned False")
@ -46,7 +47,7 @@ class Action(TroveboxObject):
Requests the full contents of the action.
Updates the action's fields with the response.
"""
result = self._trovebox.get("/action/%s/view.json" %
result = self._client.get("/action/%s/view.json" %
self.id, **kwds)["result"]
self._replace_fields(result)
self._update_fields_with_objects()

View file

@ -8,11 +8,12 @@ from .photo import Photo
class Activity(TroveboxObject):
""" Representation of an Activity object """
def __init__(self, trovebox, json_dict):
_type = "activity"
def __init__(self, client, json_dict):
self.data = None
self.type = None
TroveboxObject.__init__(self, trovebox, json_dict)
self._type = "activity"
TroveboxObject.__init__(self, client, json_dict)
self._update_fields_with_objects()
def _update_fields_with_objects(self):
@ -20,7 +21,7 @@ class Activity(TroveboxObject):
# Update the data with photo objects
if self.type is not None:
if self.type.startswith("photo"):
self.data = Photo(self._trovebox, self.data)
self.data = Photo(self._client, self.data)
else:
raise NotImplementedError("Unrecognised activity type: %s"
% self.type)
@ -32,7 +33,7 @@ class Activity(TroveboxObject):
Requests the full contents of the activity.
Updates the activity's fields with the response.
"""
result = self._trovebox.get("/activity/%s/view.json" %
result = self._client.get("/activity/%s/view.json" %
self.id, **kwds)["result"]
# TBD: Why is the result enclosed/encoded like this?

View file

@ -7,11 +7,12 @@ from .photo import Photo
class Album(TroveboxObject):
""" Representation of an Album object """
def __init__(self, trovebox, json_dict):
_type = "album"
def __init__(self, client, json_dict):
self.photos = None
self.cover = None
TroveboxObject.__init__(self, trovebox, json_dict)
self._type = "album"
TroveboxObject.__init__(self, client, json_dict)
self._update_fields_with_objects()
def _update_fields_with_objects(self):
@ -19,7 +20,7 @@ class Album(TroveboxObject):
# Update the cover with a photo object
try:
if isinstance(self.cover, dict):
self.cover = Photo(self._trovebox, self.cover)
self.cover = Photo(self._client, self.cover)
except AttributeError:
pass # No cover
@ -27,7 +28,7 @@ class Album(TroveboxObject):
try:
for i, photo in enumerate(self.photos):
if isinstance(photo, dict):
self.photos[i] = Photo(self._trovebox, photo)
self.photos[i] = Photo(self._client, photo)
except (AttributeError, TypeError):
pass # No photos, or not a list
@ -38,16 +39,16 @@ class Album(TroveboxObject):
Update the cover photo of this album.
"""
if not isinstance(photo, Photo):
photo = Photo(self._trovebox, {"id": photo})
photo = Photo(self._client, {"id": photo})
result = self._trovebox.post("/album/%s/cover/%s/update.json" %
result = self._client.post("/album/%s/cover/%s/update.json" %
(self.id, photo.id),
**kwds)["result"]
# API currently doesn't return the updated album
# (frontend issue #1369)
if isinstance(result, bool): # pragma: no cover
result = self._trovebox.get("/album/%s/view.json" %
result = self._client.get("/album/%s/view.json" %
self.id)["result"]
self._replace_fields(result)
self._update_fields_with_objects()
@ -60,7 +61,7 @@ class Album(TroveboxObject):
Returns True if successful.
Raises a TroveboxError if not.
"""
result = self._trovebox.post("/album/%s/delete.json" %
result = self._client.post("/album/%s/delete.json" %
self.id, **kwds)["result"]
if not result:
raise TroveboxError("Delete response returned False")
@ -77,12 +78,12 @@ class Album(TroveboxObject):
automatically.
Updates the album's fields with the response.
"""
result = self._trovebox.album.add(self, objects, object_type, **kwds)
result = self._client.album.add(self, objects, object_type, **kwds)
# API currently doesn't return the updated album
# (frontend issue #1369)
if isinstance(result, bool): # pragma: no cover
result = self._trovebox.get("/album/%s/view.json" %
result = self._client.get("/album/%s/view.json" %
self.id)["result"]
self._replace_fields(result)
self._update_fields_with_objects()
@ -97,12 +98,12 @@ class Album(TroveboxObject):
automatically.
Updates the album's fields with the response.
"""
result = self._trovebox.album.remove(self, objects, object_type,
result = self._client.album.remove(self, objects, object_type,
**kwds)
# API currently doesn't return the updated album
# (frontend issue #1369)
if isinstance(result, bool): # pragma: no cover
result = self._trovebox.get("/album/%s/view.json" %
result = self._client.get("/album/%s/view.json" %
self.id)["result"]
self._replace_fields(result)
self._update_fields_with_objects()
@ -113,12 +114,12 @@ class Album(TroveboxObject):
Updates this album with the specified parameters.
"""
result = self._trovebox.post("/album/%s/update.json" %
result = self._client.post("/album/%s/update.json" %
self.id, **kwds)["result"]
# APIv1 doesn't return the updated album (frontend issue #937)
if isinstance(result, bool): # pragma: no cover
result = self._trovebox.get("/album/%s/view.json" %
result = self._client.get("/album/%s/view.json" %
self.id)["result"]
self._replace_fields(result)
@ -131,7 +132,7 @@ class Album(TroveboxObject):
Requests all properties of an album.
Updates the album's fields with the response.
"""
result = self._trovebox.get("/album/%s/view.json" %
result = self._client.get("/album/%s/view.json" %
self.id, **kwds)["result"]
self._replace_fields(result)
self._update_fields_with_objects()

View file

@ -6,9 +6,7 @@ from .trovebox_object import TroveboxObject
class Photo(TroveboxObject):
""" Representation of a Photo object """
def __init__(self, trovebox, json_dict):
TroveboxObject.__init__(self, trovebox, json_dict)
self._type = "photo"
_type = "photo"
def delete(self, **kwds):
"""
@ -18,7 +16,7 @@ class Photo(TroveboxObject):
Returns True if successful.
Raises a TroveboxError if not.
"""
result = self._trovebox.post("/photo/%s/delete.json" %
result = self._client.post("/photo/%s/delete.json" %
self.id, **kwds)["result"]
if not result:
raise TroveboxError("Delete response returned False")
@ -41,7 +39,7 @@ class Photo(TroveboxObject):
Updates this photo with the specified parameters.
"""
result = self._trovebox.post("/photo/%s/update.json" %
result = self._client.post("/photo/%s/update.json" %
self.id, **kwds)["result"]
self._replace_fields(result)
@ -55,7 +53,7 @@ class Photo(TroveboxObject):
by using the "returnSizes" parameter.
Updates the photo's fields with the response.
"""
result = self._trovebox.get("/photo/%s/view.json" %
result = self._client.get("/photo/%s/view.json" %
self.id, **kwds)["result"]
self._replace_fields(result)
@ -71,7 +69,7 @@ class Photo(TroveboxObject):
Returns a dict containing the next and previous photo lists
(there may be more than one next/previous photo returned).
"""
result = self._trovebox.get("/photo/%s/nextprevious.json" %
result = self._client.get("/photo/%s/nextprevious.json" %
self.id, **kwds)["result"]
value = {}
if "next" in result:
@ -81,7 +79,7 @@ class Photo(TroveboxObject):
value["next"] = []
for photo in result["next"]:
value["next"].append(Photo(self._trovebox, photo))
value["next"].append(Photo(self._client, photo))
if "previous" in result:
# Workaround for APIv1
@ -90,7 +88,7 @@ class Photo(TroveboxObject):
value["previous"] = []
for photo in result["previous"]:
value["previous"].append(Photo(self._trovebox, photo))
value["previous"].append(Photo(self._client, photo))
return value
@ -102,12 +100,12 @@ class Photo(TroveboxObject):
eg. transform(photo, rotate=90)
Updates the photo's fields with the response.
"""
result = self._trovebox.post("/photo/%s/transform.json" %
result = self._client.post("/photo/%s/transform.json" %
self.id, **kwds)["result"]
# APIv1 doesn't return the transformed photo (frontend issue #955)
if isinstance(result, bool): # pragma: no cover
result = self._trovebox.get("/photo/%s/view.json" %
result = self._client.get("/photo/%s/view.json" %
self.id)["result"]
self._replace_fields(result)

View file

@ -11,9 +11,7 @@ from .trovebox_object import TroveboxObject
class Tag(TroveboxObject):
""" Representation of a Tag object """
def __init__(self, trovebox, json_dict):
TroveboxObject.__init__(self, trovebox, json_dict)
self._type = "tag"
_type = "tag"
def delete(self, **kwds):
"""
@ -23,7 +21,7 @@ class Tag(TroveboxObject):
Returns True if successful.
Raises a TroveboxError if not.
"""
result = self._trovebox.post("/tag/%s/delete.json" %
result = self._client.post("/tag/%s/delete.json" %
quote(self.id), **kwds)["result"]
if not result:
raise TroveboxError("Delete response returned False")
@ -37,7 +35,7 @@ class Tag(TroveboxObject):
Updates this tag with the specified parameters.
Returns the updated tag object.
"""
result = self._trovebox.post("/tag/%s/update.json" % quote(self.id),
result = self._client.post("/tag/%s/update.json" % quote(self.id),
**kwds)["result"]
self._replace_fields(result)

View file

@ -3,11 +3,11 @@ Base object supporting the storage of custom fields as attributes
"""
class TroveboxObject(object):
""" Base object supporting the storage of custom fields as attributes """
def __init__(self, trovebox, json_dict):
self._type = "None"
_type = "None"
def __init__(self, client, json_dict):
self.id = None
self.name = None
self._trovebox = trovebox
self._client = client
self._json_dict = json_dict
self._set_fields(json_dict)