From 11654863671bc34548a4018afee989d2b7811d7d Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 18 Mar 2015 19:57:54 +0800 Subject: [PATCH] pep8 --- trello/__init__.py | 91 +++++++++++++++++++++++++--------------------- trello/util.py | 23 +++++++----- 2 files changed, 63 insertions(+), 51 deletions(-) diff --git a/trello/__init__.py b/trello/__init__.py index d75c8b16..efe899f3 100644 --- a/trello/__init__.py +++ b/trello/__init__.py @@ -14,8 +14,7 @@ def __init__(self, msg, http_response): self._status = http_response.status_code def __str__(self): - return "%s (HTTP status: %s)" % ( - self._msg, self._status) + return "%s (HTTP status: %s)" % (self._msg, self._status) class Unauthorized(ResourceUnavailable): @@ -29,7 +28,8 @@ class TokenError(Exception): class TrelloClient(object): """ Base class for Trello API access """ - def __init__(self, api_key, api_secret=None, token=None, token_secret=None): + def __init__(self, api_key, api_secret=None, + token=None, token_secret=None): """ Constructor @@ -43,7 +43,8 @@ def __init__(self, api_key, api_secret=None, token=None, token_secret=None): # client key and secret for oauth1 session if api_key or token: self.oauth = OAuth1(client_key=api_key, client_secret=api_secret, - resource_owner_key=token, resource_owner_secret=token_secret) + resource_owner_key=token, + resource_owner_secret=token_secret) else: self.oauth = None @@ -67,7 +68,7 @@ def info_for_all_boards(self, actions): def logout(self): """Log out of Trello.""" - #TODO: This function. + # TODO: This function. raise NotImplementedError() @@ -79,8 +80,8 @@ def list_boards(self): Each board has the following noteworthy attributes: - id: the board's identifier - name: Name of the board - - desc: Description of the board (optional - may be missing from the - returned JSON) + - desc: Description of the board (optional - may be missing from + the returned JSON) - closed: Boolean representing whether this board is closed or not - url: URL to the board """ @@ -95,9 +96,10 @@ def list_organizations(self): Each organization has the following noteworthy attributes: - id: the organization's identifier - name: Name of the organization - - desc: Description of the organization (optional - may be missing from the - returned JSON) - - closed: Boolean representing whether this organization is closed or not + - desc: Description of the organization (optional - may be missing + from the returned JSON) + - closed: Boolean representing whether this organization is closed + or not - url: URL to the organization """ json_obj = self.fetch_json('members/me/organizations') @@ -168,14 +170,16 @@ def fetch_json( if response.status_code == 401: raise Unauthorized("%s at %s" % (response.text, url), response) if response.status_code != 200: - raise ResourceUnavailable("%s at %s" % (response.text, url), response) + raise ResourceUnavailable("%s at %s" % (response.text, url), + response) return response.json() def list_hooks(self, token=None): """ - Returns a list of all hooks associated with a specific token. If you don't pass in a token, - it tries to use the token associated with the TrelloClient object (if it exists) + Returns a list of all hooks associated with a specific token. + If you don't pass in a token, it tries to use the token associated with + the TrelloClient object (if it exists) """ token = token or self.resource_owner_key @@ -222,8 +226,8 @@ def create_hook(self, callback_url, id_model, desc=None, token=None): else: return False -class Organization(object): +class Organization(object): """ Class representing an organization """ @@ -240,10 +244,11 @@ def from_json(cls, trello_client, json_obj): :trello_client: the trello client :json_obj: the board json object """ - organization = Organization(trello_client, json_obj['id'], name=json_obj['name'].encode('utf-8')) + organization = Organization(trello_client, json_obj['id'], + name=json_obj['name'].encode('utf-8')) organization.description = json_obj.get('desc', '').encode('utf-8') # cannot close an organization - #organization.closed = json_obj['closed'] + # organization.closed = json_obj['closed'] organization.url = json_obj['url'] return organization @@ -273,15 +278,16 @@ def get_board(self, field_name): # error checking json_obj = self.client.fetch_json( '/organizations/' + self.id + '/boards', - query_params={'filter': 'open','fields':field_name}) + query_params={'filter': 'open', 'fields': field_name}) return [Board.from_json(organization=self, json_obj=obj) for obj in json_obj] def get_members(self): json_obj = self.client.fetch_json( - '/organizations/' + self.id + '/members', - query_params={'filter': 'all'}) + '/organizations/' + self.id + '/members', + query_params={'filter': 'all'}) return [Member.from_json(trello_client=self.client, json_obj=obj) for obj in json_obj] + class Board(object): """ Class representing a Trello board. Board attributes are stored as normal @@ -308,9 +314,8 @@ def __init__(self, client=None, board_id=None, organization=None, name=''): self.id = board_id self.name = name - @classmethod - def from_json(cls, trello_client=None, organization = None, json_obj=None): + def from_json(cls, trello_client=None, organization=None, json_obj=None): """ Deserialize the board json object to a Board object @@ -325,9 +330,11 @@ def from_json(cls, trello_client=None, organization = None, json_obj=None): :json_obj: the json board object """ if organization is None: - board = Board(client=trello_client, board_id=json_obj['id'], name=json_obj['name'].encode('utf-8')) + board = Board(client=trello_client, board_id=json_obj['id'], + name=json_obj['name'].encode('utf-8')) else: - board = Board(organization=organization, board_id=json_obj['id'], name=json_obj['name'].encode('utf-8')) + board = Board(organization=organization, board_id=json_obj['id'], + name=json_obj['name'].encode('utf-8')) board.description = json_obj.get('desc', '').encode('utf-8') board.closed = json_obj['closed'] @@ -567,6 +574,7 @@ def close(self): def cardsCnt(self): return len(self.list_cards()) + class Card(object): """ Class representing a Trello card. Card attributes are stored on @@ -654,7 +662,8 @@ def __repr__(self): def fetch(self, eager=True): """ Fetch all attributes for this card - :param eager: If eager is true comments and checklists will be fetched immediately, otherwise on demand + :param eager: If eager is true comments and checklists will be fetched + immediately, otherwise on demand """ json_obj = self.client.fetch_json( '/cards/' + self.id, @@ -692,10 +701,10 @@ def fetch_comments(self): def get_comments(self): comments = [] comments = self.client.fetch_json( - '/cards/' + self.id + '/actions', - query_params={'filter': 'commentCard'}) + '/cards/' + self.id + '/actions', + query_params={'filter': 'commentCard'}) return comments - + def fetch_checklists(self): checklists = [] json_obj = self.client.fetch_json( @@ -715,7 +724,6 @@ def fetch_actions(self, action_filter='createCard'): query_params={'filter': action_filter}) self.actions = json_obj - def attriExp(self, multiple): """ Provides the option to explore what comes from trello @@ -726,20 +734,20 @@ def attriExp(self, multiple): def listCardMove_date(self): """ - Will return the history of transitions of a card from one list to another - The lower the index the more resent the historical item + Will return the history of transitions of a card from one list to another. + The lower the index the more resent the historical item. It returns a list of lists. The sublists are triplates of starting list, ending list and when the transition occured. """ self.fetch_actions('updateCard:idList') - res =[] + res = [] for idx in self.actions: date_str = idx['date'][:-5] dateDate = datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S') strLst = idx['data']['listBefore']['name'] endLst = idx['data']['listAfter']['name'] - res.append([strLst,endLst,dateDate]) + res.append([strLst, endLst, dateDate]) return res @property @@ -885,7 +893,7 @@ def _post_remote_data(self, attribute, files=None, **kwargs): '/cards/' + self.id + '/' + attribute, http_method='POST', files=files, - post_args=kwargs ) + post_args=kwargs) class Member(object): @@ -898,7 +906,6 @@ def __init__(self, client, member_id, full_name=''): self.id = member_id self.full_name = full_name - def __repr__(self): return '' % self.id @@ -934,15 +941,15 @@ def from_json(cls, trello_client, json_obj): :json_obj: the member json object """ - member = Member(trello_client, json_obj['id'], full_name=json_obj['fullName'].encode('utf-8')) + member = Member(trello_client, json_obj['id'], + full_name=json_obj['fullName'].encode('utf-8')) member.username = json_obj.get('username', '').encode('utf-8') member.initials = json_obj.get('initials', '').encode('utf-8') # cannot close an organization - #organization.closed = json_obj['closed'] + # organization.closed = json_obj['closed'] return member - class Checklist(object): """ Class representing a Trello checklist. @@ -1029,11 +1036,11 @@ def rename_checklist_item(self, name, new_name): return json_obj = self.client.fetch_json( - '/cards/'+self.trello_card+\ - '/checklist/'+self.id+\ - '/checkItem/'+self.items[ix]['id'], - http_method = 'PUT', - post_args = {'name' : new_name}) + '/cards/' + self.trello_card + \ + '/checklist/' + self.id + \ + '/checkItem/' + self.items[ix]['id'], + http_method='PUT', + post_args={'name': new_name}) self.items[ix] = json_obj return json_obj diff --git a/trello/util.py b/trello/util.py index bf9e3586..bb70f541 100755 --- a/trello/util.py +++ b/trello/util.py @@ -4,6 +4,7 @@ from requests_oauthlib import OAuth1Session + def create_oauth_token(expiration=None, scope=None, key=None, secret=None): """ Script to obtain an OAuth token from Trello. @@ -25,12 +26,13 @@ def create_oauth_token(expiration=None, scope=None, key=None, secret=None): trello_secret = secret or os.environ['TRELLO_API_SECRET'] # Step 1: Get a request token. This is a temporary token that is used for - # having the user authorize an access token and to sign the request to obtain - # said access token. + # having the user authorize an access token and to sign the request to + # obtain said access token. session = OAuth1Session(client_key=trello_key, client_secret=trello_secret) response = session.fetch_request_token(request_token_url) - resource_owner_key, resource_owner_secret = response.get('oauth_token'), response.get('oauth_token_secret') + resource_owner_key = response.get('oauth_token') + resource_owner_secret = response.get('oauth_token_secret') print("Request Token:") print(" - oauth_token = %s" % resource_owner_key) @@ -64,13 +66,16 @@ def create_oauth_token(expiration=None, scope=None, key=None, secret=None): accepted = inputFunc('Have you authorized me? (y/n) ') oauth_verifier = inputFunc('What is the PIN? ') - # Step 3: Once the consumer has redirected the user back to the oauth_callback - # URL you can request the access token the user has approved. You use the - # request token to sign this request. After this is done you throw away the - # request token and use the access token returned. You should store this - # access token somewhere safe, like a database, for future use. + # Step 3: Once the consumer has redirected the user back to the + # oauth_callback URL you can request the access token the user has + # approved. You use the request token to sign this request. After this is + # done you throw away the request token and use the access token returned. + # You should store this access token somewhere safe, like a database, for + # future use. + session = OAuth1Session(client_key=trello_key, client_secret=trello_secret, - resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, + resource_owner_key=resource_owner_key, + resource_owner_secret=resource_owner_secret, verifier=oauth_verifier) access_token = session.fetch_access_token(access_token_url)