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

View file

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

View file

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

View file

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

View file

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

View file

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