From 27a28aaf291bb214e17183d9d7782c9a0703b2de Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Thu, 16 May 2013 22:43:34 +0200 Subject: [PATCH] Refactoring in Connection() and streams.*, changes in streams.Aspects() - read docstring, new tests --- diaspy/connection.py | 7 ++-- diaspy/streams.py | 89 ++++++++++++++++++++++---------------------- tests.py | 15 ++++++++ 3 files changed, 63 insertions(+), 48 deletions(-) diff --git a/diaspy/connection.py b/diaspy/connection.py index a93c920..1ea41b6 100644 --- a/diaspy/connection.py +++ b/diaspy/connection.py @@ -13,6 +13,10 @@ class Connection(): """Object representing connection with the server. It is pushed around internally and is considered private. """ + _token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') + _userinfo_regex = re.compile(r'window.current_user_attributes = ({.*})') + login_data = {} + def __init__(self, pod, username='', password=''): """ :param pod: The complete url of the diaspora pod to use. @@ -24,9 +28,6 @@ class Connection(): """ self.pod = pod self.session = requests.Session() - self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') - self._userinfo_regex = re.compile(r'window.current_user_attributes = ({.*})') - self.login_data = {} self._setlogin(username, password) def get(self, string): diff --git a/diaspy/streams.py b/diaspy/streams.py index 43db6cb..dcfe906 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -1,4 +1,5 @@ import json +import re from diaspy.models import Post """Docstrings for this module are taken from: @@ -13,17 +14,18 @@ class Generic: """Object representing generic stream. Used in Tag(), Stream(), Activity() etc. """ + _location = 'stream.json' + _stream = [] + def __init__(self, connection, location=''): """ :param connection: Connection() object :type connection: diaspy.connection.Connection - :param location: location of json + :param location: location of json (optional) :type location: str """ self._connection = connection - self._setlocation() if location: self._location = location - self._stream = [] self.fill() def __contains__(self, post): @@ -48,26 +50,6 @@ class Generic: """ return len(self._stream) - def _setlocation(self): - """Sets location of the stream. - Location defaults to 'stream.json' - - NOTICE: inheriting objects should override this method - and set their own value to the `location`. - However, it is possible to override default location by - passing the desired one to the constructor. - For example: - - def _setlocation(self): - self._location = 'foo.json' - - :param location: url of the stream - :type location: str - :returns: str - """ - self._location = 'stream.json' - return self._location - def _obtain(self): """Obtains stream from pod. """ @@ -134,8 +116,7 @@ class Stream(Generic): followed users and tags and the community spotlights posts if the user enabled those. """ - def _setlocation(self): - self._location = 'stream.json' + location = 'stream.json' def post(self, text='', aspect_ids='public', photos=None, photo=''): """This function sends a post to an aspect. @@ -202,8 +183,7 @@ class Stream(Generic): class Activity(Generic): """Stream representing user's activity. """ - def _setlocation(self): - self._location = 'activity.json' + _location = 'activity.json' def _delid(self, id): """Deletes post with given id. @@ -227,7 +207,7 @@ class Activity(Generic): if type(post) == str: self._delid(post) elif type(post) == Post: post.delete() else: - raise TypeError('this method accepts only int, str or Post: {0} given') + raise TypeError('this method accepts str or Post types: {0} given') self.fill() @@ -239,59 +219,78 @@ class Aspects(Generic): If the parameter is ommitted all aspects are assumed. An example call would be `aspects.json?aspect_ids=23,5,42` """ - def _setlocation(self): - self._location = 'aspects.json' + _location = 'aspects.json' + _id_regexp = re.compile(r'