From 88ec6cdaaca6f58767df471614ee55e7b398d801 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sat, 30 Mar 2013 10:21:42 +0100 Subject: [PATCH] Code cleanup (flake8) --- __init__.py | 1 - diaspy/__init__.py | 1 - diaspy/client.py | 32 ++++++++++++++------------------ diaspy/conversations.py | 5 +++-- diaspy/models.py | 14 ++++++-------- docs/source/conf.py | 3 ++- setup.py | 2 +- tests.py | 4 ++-- 8 files changed, 28 insertions(+), 34 deletions(-) delete mode 100644 __init__.py diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 1c0c4a6..0000000 --- a/__init__.py +++ /dev/null @@ -1 +0,0 @@ -import diaspy diff --git a/diaspy/__init__.py b/diaspy/__init__.py index d6c47eb..25b3822 100644 --- a/diaspy/__init__.py +++ b/diaspy/__init__.py @@ -1,4 +1,3 @@ import diaspy.client import diaspy.models import diaspy.conversations - diff --git a/diaspy/client.py b/diaspy/client.py index 8c7eece..d88ae61 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -24,11 +24,11 @@ class Client: self._login() def _sessionget(self, string): - """This method gets data from session. - Performs additional checks if needed. + """This method gets data from session. + Performs additional checks if needed. Example: - To obtain 'foo' from pod one should call `_sessionget('foo')`. + To obtain 'foo' from pod one should call `_sessionget('foo')`. :param string: URL to get without the pod's URL and slash eg. 'stream'. :type string: str @@ -37,10 +37,10 @@ class Client: def _sessionpost(self, string, data, headers={}, params={}): """This method posts data to session. - Performs additional checks if needed. + Performs additional checks if needed. Example: - To post to 'foo' one should call `_sessionpost('foo', data={})`. + To post to 'foo' one should call `_sessionpost('foo', data={})`. :param string: URL to post without the pod's URL and slash eg. 'status_messages'. :type string: str @@ -56,7 +56,7 @@ class Client: elif not headers and params: r = self.session.post(string, data=data, params=params) else: r = self.session.post(string, data=data) return r - + def get_token(self): """This function gets a token needed for authentication in most cases @@ -67,14 +67,12 @@ class Client: return token def _setlogindata(self, username, password): - """This function is used to set data for login. - + """This function is used to set data for login. .. note:: It should be called before _login() function. """ self._username, self._password = username, password - self._login_data = { - 'user[username]': self._username, + self._login_data = {'user[username]': self._username, 'user[password]': self._password, 'authenticity_token': self.get_token(), } @@ -85,12 +83,10 @@ class Client: r = self.session.post('{0}/users/sign_in'.format(self.pod), data=self._login_data, headers={'accept': 'application/json'}) - if r.status_code != 201: raise Exception('{0}: Login failed.'.format(r.status_code)) def _setpostdata(self, text, aspect_id, photos): """This function prepares data for posting. - :param text: Text to post. :type text: str :param aspect_id: Aspect id to send post to. @@ -173,11 +169,11 @@ class Client: """ r = self._sessionget('stream.json') - if r.status_code != 200: + if r.status_code != 200: raise Exception('wrong status code: {0}'.format(r.status_code)) stream = r.json() - return [ diaspy.models.Post(str(post['id']), self) for post in stream ] + return [diaspy.models.Post(str(post['id']), self) for post in stream] def get_notifications(self): """This functions returns a list of notifications. @@ -186,7 +182,7 @@ class Client: """ r = self._sessionget('notifications.json') - if r.status_code != 200: + if r.status_code != 200: raise Exception('wrong status code: {0}'.format(r.status_code)) notifications = r.json() @@ -204,7 +200,7 @@ class Client: raise Exception('wrong status code: {0}'.format(r.status_code)) mentions = r.json() - return [ diaspy.models.Post(str(post['id']), self) for post in mentions ] + return [diaspy.models.Post(str(post['id']), self) for post in mentions] def get_tag(self, tag): """This functions returns a list of posts containing the tag. @@ -219,7 +215,7 @@ class Client: raise Exception('wrong status code: {0}'.format(r.status_code)) tagged_posts = r.json() - return [ diaspy.models.Post(str(post['id']), self) for post in tagged_posts ] + return [diaspy.models.Post(str(post['id']), self) for post in tagged_posts] def get_mailbox(self): """This functions returns a list of messages found in the conversation. @@ -232,7 +228,7 @@ class Client: raise Exception('wrong status code: {0}'.format(r.status_code)) mailbox = r.json() - return [ diaspy.conversations.Conversation(str(conversation['conversation']['id']), self) for conversation in mailbox ] + return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self) for conversation in mailbox] def add_user_to_aspect(self, user_id, aspect_id): """ this function adds a user to an aspect. diff --git a/diaspy/conversations.py b/diaspy/conversations.py index 22afe55..9417a8e 100644 --- a/diaspy/conversations.py +++ b/diaspy/conversations.py @@ -1,4 +1,5 @@ -import requests +#!/usr/bin/env python3 + class Conversation: """This class represents a conversation. @@ -25,7 +26,7 @@ class Conversation: def get_data(self): """ returns the plain json data representing conversation. """ - r = self._client.session.get('{0}/conversations/{1}.json'.format(self._client.pod, self.conv_id)) + r = self._client._sessionget('conversations/{1}.json'.format(self.conv_id)) if r.status_code == 200: return r.json()['conversation'] else: diff --git a/diaspy/models.py b/diaspy/models.py index f04328e..bccf351 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -1,4 +1,4 @@ -import requests +#!/usr/bin/env python3 class Post: @@ -22,18 +22,17 @@ class Post: def get_data(self): """This function retrieves data of the post. """ - r = self._client.session.get('{0}/posts/{1}.json'.format(self._client.pod, self.post_id)) - if r.status_code == 200: + r = self._client._sessionget('posts/{1}.json'.format(self.post_id)) + if r.status_code == 200: return r.json() - else: + else: raise Exception('wrong status code: {0}'.format(r.status_code)) def like(self): - """This function likes a post. + """This function likes a post. It abstracts the 'Like' functionality. :returns: dict -- json formatted like object. - """ data = {'authenticity_token': self._client.get_token()} @@ -48,7 +47,6 @@ class Post: def delete_like(self): """This function removes a like from a post - """ data = {'authenticity_token': self._client.get_token()} @@ -118,7 +116,7 @@ class Post: """ data = {'authenticity_token': self._client.get_token()} - r = self._client.session.delete('{0}/posts/{1}'.format(self._client.pod, self.post_id), data=data, headers={'accept': 'application/json'}) + if r.status_code != 204: raise Exception('{0}: Post could not be deleted'.format(r.status_code)) diff --git a/docs/source/conf.py b/docs/source/conf.py index fe1d785..e9d90fa 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -12,7 +12,8 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys, os +import sys +import os # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the diff --git a/setup.py b/setup.py index d2f5a3c..2fad8a6 100644 --- a/setup.py +++ b/setup.py @@ -3,6 +3,6 @@ setup(name='diaspy', version='0.0.1', author='Moritz Kiefer', author_email='moritz.kiefer@gmail.com', - packages = find_packages(), + packages=find_packages(), install_requires=['requests'] ) diff --git a/tests.py b/tests.py index 3b5a9a3..039cbfc 100644 --- a/tests.py +++ b/tests.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 import unittest -import getpass # failure to import any of the modules below indicates failed tests # modules used by diaspy -import requests, re +import requests +import re # actual diaspy code import diaspy -- 2.25.1