From f50cbea31f915755413c7b2b8f30def77489ed7f Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Wed, 11 Sep 2013 20:39:05 +0200 Subject: [PATCH] Style fixes and small adjustments in regular expressions used in diaspy/settings.py --- Changelog.markdown | 6 +++++ diaspy/__init__.py | 4 +++- diaspy/conversations.py | 2 +- diaspy/errors.py | 6 +++++ diaspy/models.py | 49 ++++++++++++++++++----------------------- diaspy/people.py | 33 +++++++++++++-------------- diaspy/settings.py | 31 +++++++++++++++----------- testconf.markdown | 1 + tests.py | 3 +++ 9 files changed, 75 insertions(+), 60 deletions(-) diff --git a/Changelog.markdown b/Changelog.markdown index e36467a..e581132 100644 --- a/Changelog.markdown +++ b/Changelog.markdown @@ -41,6 +41,12 @@ pods running on older versions. And the test suite was updated. Yay! +* __new__: `diaspy.errors.SettingsError`, + + +* __upd__: `diaspy.settings.Account.setEmail()` can now raise `SettingsError` when request fails, + + **`0.4.1-rc.3` (2013-09-08):** * __new__: `diaspy.settings.Profile.load()` method for loading profile information, diff --git a/diaspy/__init__.py b/diaspy/__init__.py index 4621ccc..97a2143 100644 --- a/diaspy/__init__.py +++ b/diaspy/__init__.py @@ -1,3 +1,5 @@ +# flake8: noqa + import diaspy.connection as connection import diaspy.models as models import diaspy.streams as streams @@ -8,4 +10,4 @@ import diaspy.notifications as notifications import diaspy.settings as settings -__version__ = '0.4.1-rc.3' +__version__ = '0.4.1' diff --git a/diaspy/conversations.py b/diaspy/conversations.py index a81afca..cd141ec 100644 --- a/diaspy/conversations.py +++ b/diaspy/conversations.py @@ -27,6 +27,6 @@ class Mailbox(): request = self._connection.get('conversations.json') if request.status_code != 200: - raise errors.DiaspyError('wrong status code: {0}'.format(r.status_code)) + raise errors.DiaspyError('wrong status code: {0}'.format(request.status_code)) mailbox = request.json() self._mailbox = [models.Conversation(self._connection, c['conversation']['id']) for c in mailbox] diff --git a/diaspy/errors.py b/diaspy/errors.py index bbb0350..1b2a5b1 100644 --- a/diaspy/errors.py +++ b/diaspy/errors.py @@ -62,6 +62,12 @@ class StreamError(DiaspyError): pass +class SettingsError(DiaspyError): + """Exception raised when something related to settings goes wrong. + """ + pass + + def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError): """This method tries to decides how to react to a response code passed to it. If it's an diff --git a/diaspy/models.py b/diaspy/models.py index 75db85d..9760a9c 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -253,7 +253,7 @@ class Conversation(): headers={'accept': 'application/json'}) if request.status_code != 200: raise errors.ConversationError('{0}: Answer could not be posted.' - .format(request.status_code)) + .format(request.status_code)) return request.json() def delete(self): @@ -263,13 +263,13 @@ class Conversation(): data = {'authenticity_token': repr(self._connection)} request = self._connection.delete('conversations/{0}/visibility/' - .format(self.id), - data=data, - headers={'accept': 'application/json'}) + .format(self.id), + data=data, + headers={'accept': 'application/json'}) if request.status_code != 404: raise errors.ConversationError('{0}: Conversation could not be deleted.' - .format(request.status_code)) + .format(request.status_code)) def get_subject(self): """Returns the subject of this conversation @@ -279,7 +279,7 @@ class Conversation(): class Comment(): """Represents comment on post. - + Does not require Connection() object. Note that you should not manually create `Comment()` objects -- they are designed to be created automatically by `Post()` objects. @@ -398,12 +398,12 @@ class Post(): data = {'authenticity_token': repr(self._connection)} request = self._connection.post('posts/{0}/likes'.format(self.id), - data=data, - headers={'accept': 'application/json'}) + data=data, + headers={'accept': 'application/json'}) if request.status_code != 201: raise errors.PostError('{0}: Post could not be liked.' - .format(request.status_code)) + .format(request.status_code)) return request.json() def reshare(self): @@ -413,8 +413,8 @@ class Post(): 'authenticity_token': repr(self._connection)} request = self._connection.post('reshares', - data=data, - headers={'accept': 'application/json'}) + data=data, + headers={'accept': 'application/json'}) if request.status_code != 201: raise Exception('{0}: Post could not be reshared'.format(request.status_code)) return request.json() @@ -428,8 +428,8 @@ class Post(): data = {'text': text, 'authenticity_token': repr(self._connection)} request = self._connection.post('posts/{0}/comments'.format(self.id), - data=data, - headers={'accept': 'application/json'}) + data=data, + headers={'accept': 'application/json'}) if request.status_code != 201: raise Exception('{0}: Comment could not be posted.' @@ -441,8 +441,8 @@ class Post(): """ data = {'authenticity_token': repr(self._connection)} request = self._connection.delete('posts/{0}'.format(self.id), - data=data, - headers={'accept': 'application/json'}) + data=data, + headers={'accept': 'application/json'}) if request.status_code != 204: raise errors.PostError('{0}: Post could not be deleted'.format(request.status_code)) @@ -454,28 +454,23 @@ class Post(): """ data = {'authenticity_token': repr(self._connection)} request = self._connection.delete('posts/{0}/comments/{1}' - .format(self.id, - comment_id), - data=data, - headers={'accept': 'application/json'}) + .format(self.id, comment_id), + data=data, + headers={'accept': 'application/json'}) if request.status_code != 204: raise errors.PostError('{0}: Comment could not be deleted' - .format(request.status_code)) + .format(request.status_code)) def delete_like(self): """This function removes a like from a post """ data = {'authenticity_token': self._connection.get_token()} - - request = self._connection.delete('posts/{0}/likes/{1}' - .format(self.id, - self.data['interactions'] - ['likes'][0]['id']), - data=data) + url = 'posts/{0}/likes/{1}'.format(self.id, self.data['interactions']['likes'][0]['id']) + request = self._connection.delete(url, data=data) if request.status_code != 204: raise errors.PostError('{0}: Like could not be removed.' - .format(request.status_code)) + .format(request.status_code)) def author(self, key='name'): """Returns author of the post. diff --git a/diaspy/people.py b/diaspy/people.py index c20283f..13aa747 100644 --- a/diaspy/people.py +++ b/diaspy/people.py @@ -40,8 +40,6 @@ class User(): def __init__(self, connection, guid='', handle='', fetch='posts', id=0): self._connection = connection self.stream = [] - self.handle = handle - self.guid = guid self.data = { 'guid': guid, 'handle': handle, @@ -62,21 +60,21 @@ class User(): """Fetch user posts or data. """ if fetch == 'posts': - if self.handle and not self.guid: self.fetchhandle() + if self['handle'] and not self['guid']: self.fetchhandle() else: self.fetchguid() - elif fetch == 'data' and self.handle: + elif fetch == 'data' and self['handle']: self.fetchprofile() def _finalize_data(self, data): """Adjustments are needed to have similar results returned - by search feature and fetchguid/handle(). + by search feature and fetchguid()/fetchhandle(). """ - names = [ ('id', 'id'), - ('guid', 'guid'), - ('name', 'name'), - ('avatar', 'avatar'), - ('handle', 'diaspora_id'), - ] + names = [('id', 'id'), + ('guid', 'guid'), + ('name', 'name'), + ('avatar', 'avatar'), + ('handle', 'diaspora_id'), + ] final = {} for f, d in names: final[f] = data[d] @@ -90,7 +88,7 @@ class User(): :type request: request """ if request.status_code != 200: raise Exception('wrong error code: {0}'.format(request.status_code)) - else: request = request.json() + request = request.json() if not len(request): raise errors.UserError('cannot extract user data: no posts to analyze') self.data = self._finalize_data(request[0]['author']) self.stream = Outer(self._connection, location='people/{0}.json'.format(self['guid'])) @@ -98,15 +96,15 @@ class User(): def fetchhandle(self, protocol='https'): """Fetch user data and posts using Diaspora handle. """ - pod, user = sephandle(self.handle) + pod, user = sephandle(self['handle']) request = self._connection.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user), direct=True) self._postproc(request) def fetchguid(self): """Fetch user data and posts using guid. """ - if self.guid: - request = self._connection.get('people/{0}.json'.format(self.guid)) + if self['guid']: + request = self._connection.get('people/{0}.json'.format(self['guid'])) self._postproc(request) else: raise errors.UserError('GUID not set') @@ -114,7 +112,7 @@ class User(): def fetchprofile(self): """Fetches user data. """ - data = search.Search(self._connection).user(self.handle)[0] + data = search.Search(self._connection).user(self['handle'])[0] self.data = data @@ -167,5 +165,4 @@ class Contacts(): request = self._connection.get('contacts.json', params=params) if request.status_code != 200: raise Exception('status code {0}: cannot get contacts'.format(request.status_code)) - contacts = [User(self._connection, guid=user['guid'], handle=user['handle'], fetch=None, id=user['id']) for user in request.json()] - return contacts + return [User(self._connection, guid=user['guid'], handle=user['handle'], fetch=None) for user in request.json()] diff --git a/diaspy/settings.py b/diaspy/settings.py index 98aa0ce..da6f183 100644 --- a/diaspy/settings.py +++ b/diaspy/settings.py @@ -14,6 +14,8 @@ from diaspy import errors, streams class Account(): """Provides interface to account settings. """ + email_regexp = re.compile('') - lastname_regexp = re.compile('') + firstname_regexp = re.compile('id="profile_first_name" name="profile\[first_name\]" type="text" value="(.*?)" />') + lastname_regexp = re.compile('id="profile_last_name" name="profile\[last_name\]" type="text" value="(.*?)" />') bio_regexp = re.compile('') - location_regexp = re.compile('') - gender_regexp = re.compile('') - birth_year_regexp = re.compile('') - birth_month_regexp = re.compile('') - birth_day_regexp = re.compile('') - is_searchable_regexp = re.compile('') - is_nsfw_regexp = re.compile('') + location_regexp = re.compile('id="profile_location" name="profile\[location\]" placeholder="Fill me out" type="text" value="(.*?)" />') + gender_regexp = re.compile('id="profile_gender" name="profile\[gender\]" placeholder="Fill me out" type="text" value="(.*?)" />') + birth_year_regexp = re.compile('selected="selected" value="([0-9]{4,4})">[0-9]{4,4}') + birth_month_regexp = re.compile('selected="selected" value="([0-9]{1,2})">(.*?)') + birth_day_regexp = re.compile('selected="selected" value="([0-9]{1,2})">[0-9]{1,2}') + is_searchable_regexp = re.compile('checked="checked" id="profile_searchable" name="profile\[searchable\]" type="checkbox" value="(.*?)" />') + is_nsfw_regexp = re.compile('checked="checked" id="profile_nsfw" name="profile\[nsfw\]" type="checkbox" value="(.*?)" />') def __init__(self, connection): self._connection = connection diff --git a/testconf.markdown b/testconf.markdown index 5a51bd8..8936401 100644 --- a/testconf.markdown +++ b/testconf.markdown @@ -30,6 +30,7 @@ Template file: # Details of your account diaspora_name = 'Foo Bar' user_names_tuple = ('Foo', 'Bar') + user_email = 'email@example.com' user_location_string = 'Nowhere' user_gender_string = 'Gender' user_date_of_birth = (2013, 9, 7) diff --git a/tests.py b/tests.py index 1dbec1a..60ddd5e 100644 --- a/tests.py +++ b/tests.py @@ -251,6 +251,9 @@ class SettingsTests(unittest.TestCase): def testGettingLanguages(self): self.assertIn(('English', 'en'), self.account.getLanguages()) + def testGettingEmail(self): + self.assertEqual(testconf.user_email, self.account.getEmail()) + if __name__ == '__main__': print('Hello World!') -- 2.25.1