Skip to content

Commit

Permalink
Merge pull request #290 from eumiro/fstrings
Browse files Browse the repository at this point in the history
refactor: use f-strings
  • Loading branch information
halcy committed Dec 2, 2022
2 parents c796cf3 + 325cc91 commit f5d4fbc
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 194 deletions.
59 changes: 24 additions & 35 deletions mastodon/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ def create_account(self, username, password, email, agreement=False, reason=None
response = self.__api_request('POST', '/oauth/token', oauth_params, do_ratelimiting=False)
temp_access_token = response['access_token']
except Exception as e:
raise MastodonIllegalArgumentError('Invalid request during oauth phase: %s' % e)
raise MastodonIllegalArgumentError(f'Invalid request during oauth phase: {e}')

# Step 2: Use that to create a user
try:
response = self.__api_request('POST', '/api/v1/accounts', params, do_ratelimiting=False, access_token_override=temp_access_token, skip_error_check=True)
if "error" in response:
if return_detailed_error:
return None, response
raise MastodonIllegalArgumentError('Invalid request: %s' % e)
raise MastodonIllegalArgumentError(f'Invalid request: {e}')
self.access_token = response['access_token']
self.__set_refresh_token(response.get('refresh_token'))
self.__set_token_expired(int(response.get('expires_in', 0)))
Expand All @@ -88,7 +88,10 @@ def create_account(self, username, password, email, agreement=False, reason=None
received_scopes += _SCOPE_SETS[scope_set]

if not set(scopes) <= set(received_scopes):
raise MastodonAPIError('Granted scopes "' + " ".join(received_scopes) + '" do not contain all of the requested scopes "' + " ".join(scopes) + '".')
raise MastodonAPIError(
f'Granted scopes "{" ".join(received_scopes)}" '
f'do not contain all of the requested scopes "{" ".join(scopes)}".'
)

if to_file is not None:
with open(to_file, 'w') as token_file:
Expand Down Expand Up @@ -124,8 +127,7 @@ def account(self, id):
Returns a :ref:`account dict <account dict>`.
"""
id = self.__unpack_id(id)
url = '/api/v1/accounts/{0}'.format(str(id))
return self.__api_request('GET', url)
return self.__api_request('GET', f'/api/v1/accounts/{id}')

@api_version("1.0.0", "2.1.0", _DICT_VERSION_ACCOUNT)
def account_verify_credentials(self):
Expand Down Expand Up @@ -185,8 +187,7 @@ def account_statuses(self, id, only_media=False, pinned=False, exclude_replies=F
if not exclude_reblogs:
del params["exclude_reblogs"]

url = '/api/v1/accounts/{0}/statuses'.format(str(id))
return self.__api_request('GET', url, params)
return self.__api_request('GET', f'/api/v1/accounts/{id}/statuses', params)

@api_version("1.0.0", "2.6.0", _DICT_VERSION_ACCOUNT)
def account_following(self, id, max_id=None, min_id=None, since_id=None, limit=None):
Expand All @@ -206,8 +207,7 @@ def account_following(self, id, max_id=None, min_id=None, since_id=None, limit=N
since_id = self.__unpack_id(since_id, dateconv=True)

params = self.__generate_params(locals(), ['id'])
url = '/api/v1/accounts/{0}/following'.format(str(id))
return self.__api_request('GET', url, params)
return self.__api_request('GET', f'/api/v1/accounts/{id}/following', params)

@api_version("1.0.0", "2.6.0", _DICT_VERSION_ACCOUNT)
def account_followers(self, id, max_id=None, min_id=None, since_id=None, limit=None):
Expand All @@ -227,8 +227,7 @@ def account_followers(self, id, max_id=None, min_id=None, since_id=None, limit=N
since_id = self.__unpack_id(since_id, dateconv=True)

params = self.__generate_params(locals(), ['id'])
url = '/api/v1/accounts/{0}/followers'.format(str(id))
return self.__api_request('GET', url, params)
return self.__api_request('GET', f'/api/v1/accounts/{id}/followers', params)

@api_version("1.0.0", "1.4.0", _DICT_VERSION_RELATIONSHIP)
def account_relationships(self, id):
Expand Down Expand Up @@ -269,8 +268,7 @@ def account_lists(self, id):
"""
id = self.__unpack_id(id)
params = self.__generate_params(locals(), ['id'])
url = '/api/v1/accounts/{0}/lists'.format(str(id))
return self.__api_request('GET', url, params)
return self.__api_request('GET', f'/api/v1/accounts/{id}/lists', params)

@api_version("3.4.0", "3.4.0", _DICT_VERSION_ACCOUNT)
def account_lookup(self, acct):
Expand Down Expand Up @@ -317,8 +315,7 @@ def account_follow(self, id, reblogs=True, notify=False):
if params["reblogs"] is None:
del params["reblogs"]

url = '/api/v1/accounts/{0}/follow'.format(str(id))
return self.__api_request('POST', url, params)
return self.__api_request('POST', f'/api/v1/accounts/{id}/follow', params)

@api_version("1.0.0", "2.1.0", _DICT_VERSION_ACCOUNT)
def follows(self, uri):
Expand All @@ -338,7 +335,7 @@ def account_unfollow(self, id):
Returns a :ref:`relationship dict <relationship dict>` containing the updated relationship to the user.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/accounts/{0}/unfollow'.format(str(id)))
return self.__api_request('POST', f'/api/v1/accounts/{id}/unfollow')

@api_version("3.5.0", "3.5.0", _DICT_VERSION_RELATIONSHIP)
def account_remove_from_followers(self, id):
Expand All @@ -349,7 +346,7 @@ def account_remove_from_followers(self, id):
Returns a :ref:`relationship dict <relationship dict>` reflecting the updated following status.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/accounts/{0}/remove_from_followers'.format(str(id)))
return self.__api_request('POST', f'/api/v1/accounts/{id}/remove_from_followers')


@api_version("1.0.0", "1.4.0", _DICT_VERSION_RELATIONSHIP)
Expand All @@ -360,8 +357,7 @@ def account_block(self, id):
Returns a :ref:`relationship dict <relationship dict>` containing the updated relationship to the user.
"""
id = self.__unpack_id(id)
url = '/api/v1/accounts/{0}/block'.format(str(id))
return self.__api_request('POST', url)
return self.__api_request('POST', f'/api/v1/accounts/{id}/block')

@api_version("1.0.0", "1.4.0", _DICT_VERSION_RELATIONSHIP)
def account_unblock(self, id):
Expand All @@ -371,8 +367,7 @@ def account_unblock(self, id):
Returns a :ref:`relationship dict <relationship dict>` containing the updated relationship to the user.
"""
id = self.__unpack_id(id)
url = '/api/v1/accounts/{0}/unblock'.format(str(id))
return self.__api_request('POST', url)
return self.__api_request('POST', f'/api/v1/accounts/{id}/unblock')

@api_version("1.1.0", "2.4.3", _DICT_VERSION_RELATIONSHIP)
def account_mute(self, id, notifications=True, duration=None):
Expand All @@ -387,8 +382,7 @@ def account_mute(self, id, notifications=True, duration=None):
"""
id = self.__unpack_id(id)
params = self.__generate_params(locals(), ['id'])
url = '/api/v1/accounts/{0}/mute'.format(str(id))
return self.__api_request('POST', url, params)
return self.__api_request('POST', f'/api/v1/accounts/{id}/mute', params)

@api_version("1.1.0", "1.4.0", _DICT_VERSION_RELATIONSHIP)
def account_unmute(self, id):
Expand All @@ -398,8 +392,7 @@ def account_unmute(self, id):
Returns a :ref:`relationship dict <relationship dict>` containing the updated relationship to the user.
"""
id = self.__unpack_id(id)
url = '/api/v1/accounts/{0}/unmute'.format(str(id))
return self.__api_request('POST', url)
return self.__api_request('POST', f'/api/v1/accounts/{id}/unmute', params)

@api_version("1.1.1", "3.1.0", _DICT_VERSION_ACCOUNT)
def account_update_credentials(self, display_name=None, note=None,
Expand Down Expand Up @@ -436,10 +429,8 @@ def account_update_credentials(self, display_name=None, note=None,

fields_attributes = []
for idx, (field_name, field_value) in enumerate(fields):
params_initial['fields_attributes[' +
str(idx) + '][name]'] = field_name
params_initial['fields_attributes[' +
str(idx) + '][value]'] = field_value
params_initial[f'fields_attributes[{idx}][name]'] = field_name
params_initial[f'fields_attributes[{idx}][value]'] = field_value

# Clean up params
for param in ["avatar", "avatar_mime_type", "header", "header_mime_type", "fields"]:
Expand All @@ -464,8 +455,7 @@ def account_pin(self, id):
Returns a :ref:`relationship dict <relationship dict>` containing the updated relationship to the user.
"""
id = self.__unpack_id(id)
url = '/api/v1/accounts/{0}/pin'.format(str(id))
return self.__api_request('POST', url)
return self.__api_request('POST', f'/api/v1/accounts/{id}/pin')

@api_version("2.5.0", "2.5.0", _DICT_VERSION_RELATIONSHIP)
def account_unpin(self, id):
Expand All @@ -475,8 +465,7 @@ def account_unpin(self, id):
Returns a :ref:`relationship dict <relationship dict>` containing the updated relationship to the user.
"""
id = self.__unpack_id(id)
url = '/api/v1/accounts/{0}/unpin'.format(str(id))
return self.__api_request('POST', url)
return self.__api_request('POST', f'/api/v1/accounts/{id}/unpin')

@api_version("3.2.0", "3.2.0", _DICT_VERSION_RELATIONSHIP)
def account_note_set(self, id, comment):
Expand All @@ -487,7 +476,7 @@ def account_note_set(self, id, comment):
"""
id = self.__unpack_id(id)
params = self.__generate_params(locals(), ["id"])
return self.__api_request('POST', '/api/v1/accounts/{0}/note'.format(str(id)), params)
return self.__api_request('POST', f'/api/v1/accounts/{id}/note', params)

@api_version("3.3.0", "3.3.0", _DICT_VERSION_HASHTAG)
def account_featured_tags(self, id):
Expand All @@ -497,4 +486,4 @@ def account_featured_tags(self, id):
Returns a list of :ref:`hashtag dicts <hashtag dicts>` (NOT `featured tag dicts`_).
"""
id = self.__unpack_id(id)
return self.__api_request('GET', '/api/v1/accounts/{0}/featured_tags'.format(str(id)))
return self.__api_request('GET', f'/api/v1/accounts/{id}/featured_tags')
35 changes: 17 additions & 18 deletions mastodon/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def admin_account(self, id):
Returns that dict.
"""
id = self.__unpack_id(id)
return self.__api_request('GET', '/api/v1/admin/accounts/{0}'.format(id))
return self.__api_request('GET', f'/api/v1/admin/accounts/{id}')

@api_version("2.9.1", "2.9.1", _DICT_VERSION_ADMIN_ACCOUNT)
def admin_account_enable(self, id):
Expand All @@ -152,7 +152,7 @@ def admin_account_enable(self, id):
Returns the updated :ref:`admin account dict <admin account dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/accounts/{0}/enable'.format(id))
return self.__api_request('POST', f'/api/v1/admin/accounts/{id}/enable')

@api_version("2.9.1", "2.9.1", _DICT_VERSION_ADMIN_ACCOUNT)
def admin_account_approve(self, id):
Expand All @@ -162,7 +162,7 @@ def admin_account_approve(self, id):
Returns the updated :ref:`admin account dict <admin account dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/accounts/{0}/approve'.format(id))
return self.__api_request('POST', f'/api/v1/admin/accounts/{id}/approve')

@api_version("2.9.1", "2.9.1", _DICT_VERSION_ADMIN_ACCOUNT)
def admin_account_reject(self, id):
Expand All @@ -172,7 +172,7 @@ def admin_account_reject(self, id):
Returns the updated :ref:`admin account dict <admin account dict>` for the account that is now gone.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/accounts/{0}/reject'.format(id))
return self.__api_request('POST', f'/api/v1/admin/accounts/{id}/reject')

@api_version("2.9.1", "2.9.1", _DICT_VERSION_ADMIN_ACCOUNT)
def admin_account_unsilence(self, id):
Expand All @@ -182,7 +182,7 @@ def admin_account_unsilence(self, id):
Returns the updated :ref:`admin account dict <admin account dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/accounts/{0}/unsilence'.format(id))
return self.__api_request('POST', f'/api/v1/admin/accounts/{id}/unsilence')

@api_version("2.9.1", "2.9.1", _DICT_VERSION_ADMIN_ACCOUNT)
def admin_account_unsuspend(self, id):
Expand All @@ -192,7 +192,7 @@ def admin_account_unsuspend(self, id):
Returns the updated :ref:`admin account dict <admin account dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/accounts/{0}/unsuspend'.format(id))
return self.__api_request('POST', f'/api/v1/admin/accounts/{id}/unsuspend')

@api_version("3.3.0", "3.3.0", _DICT_VERSION_ADMIN_ACCOUNT)
def admin_account_delete(self, id):
Expand All @@ -202,7 +202,7 @@ def admin_account_delete(self, id):
The deleted accounts :ref:`admin account dict <admin account dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('DELETE', '/api/v1/admin/accounts/{0}'.format(id))
return self.__api_request('DELETE', f'/api/v1/admin/accounts/{id}')

@api_version("3.3.0", "3.3.0", _DICT_VERSION_ADMIN_ACCOUNT)
def admin_account_unsensitive(self, id):
Expand All @@ -212,7 +212,7 @@ def admin_account_unsensitive(self, id):
Returns the updated :ref:`admin account dict <admin account dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/accounts/{0}/unsensitive'.format(id))
return self.__api_request('POST', f'/api/v1/admin/accounts/{f}/unsensitive')

@api_version("2.9.1", "2.9.1", "2.9.1")
def admin_account_moderate(self, id, action=None, report_id=None, warning_preset_id=None, text=None, send_email_notification=True):
Expand Down Expand Up @@ -248,8 +248,7 @@ def admin_account_moderate(self, id, action=None, report_id=None, warning_preset

params["type"] = action

self.__api_request(
'POST', '/api/v1/admin/accounts/{0}/action'.format(id), params)
self.__api_request('POST', f'/api/v1/admin/accounts/{id}/action', params)

@api_version("2.9.1", "2.9.1", _DICT_VERSION_REPORT)
def admin_reports(self, resolved=False, account_id=None, target_account_id=None, max_id=None, min_id=None, since_id=None, limit=None):
Expand Down Expand Up @@ -290,7 +289,7 @@ def admin_report(self, id):
Returns a :ref:`report dict <report dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('GET', '/api/v1/admin/reports/{0}'.format(id))
return self.__api_request('GET', f'/api/v1/admin/reports/{id}')

@api_version("2.9.1", "2.9.1", _DICT_VERSION_REPORT)
def admin_report_assign(self, id):
Expand All @@ -300,7 +299,7 @@ def admin_report_assign(self, id):
Returns the updated :ref:`report dict <report dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/reports/{0}/assign_to_self'.format(id))
return self.__api_request('POST', f'/api/v1/admin/reports/{id}/assign_to_self')

@api_version("2.9.1", "2.9.1", _DICT_VERSION_REPORT)
def admin_report_unassign(self, id):
Expand All @@ -310,7 +309,7 @@ def admin_report_unassign(self, id):
Returns the updated :ref:`report dict <report dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/reports/{0}/unassign'.format(id))
return self.__api_request('POST', f'/api/v1/admin/reports/{id}/unassign')

@api_version("2.9.1", "2.9.1", _DICT_VERSION_REPORT)
def admin_report_reopen(self, id):
Expand All @@ -320,7 +319,7 @@ def admin_report_reopen(self, id):
Returns the updated :ref:`report dict <report dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/reports/{0}/reopen'.format(id))
return self.__api_request('POST', f'/api/v1/admin/reports/{id}/reopen')

@api_version("2.9.1", "2.9.1", _DICT_VERSION_REPORT)
def admin_report_resolve(self, id):
Expand All @@ -330,7 +329,7 @@ def admin_report_resolve(self, id):
Returns the updated :ref:`report dict <report dict>`.
"""
id = self.__unpack_id(id)
return self.__api_request('POST', '/api/v1/admin/reports/{0}/resolve'.format(id))
return self.__api_request('POST', f'/api/v1/admin/reports/{id}/resolve')

@api_version("3.5.0", "3.5.0", _DICT_VERSION_HASHTAG)
def admin_trending_tags(self, limit=None):
Expand Down Expand Up @@ -376,7 +375,7 @@ def admin_domain_blocks(self, id=None, limit:int=None):
"""
if id is not None:
id = self.__unpack_id(id)
return self.__api_request('GET', '/api/v1/admin/domain_blocks/{0}'.format(id))
return self.__api_request('GET', f'/api/v1/admin/domain_blocks/{id}')
else:
params = self.__generate_params(locals(),['limit'])
return self.__api_request('GET', '/api/v1/admin/domain_blocks/', params)
Expand Down Expand Up @@ -430,7 +429,7 @@ def admin_update_domain_block(self, id, severity:str=None, reject_media:bool=Non
raise AttributeError("Must provide an id to modify the existing moderation actions on a given domain.")
id = self.__unpack_id(id)
params = self.__generate_params(locals(), ["id"])
return self.__api_request('PUT', '/api/v1/admin/domain_blocks/{0}'.format(id), params)
return self.__api_request('PUT', f'/api/v1/admin/domain_blocks/{id}', params)

@api_version("4.0.0", "4.0.0", _DICT_VERSION_ADMIN_DOMAIN_BLOCK)
def admin_delete_domain_block(self, id=None):
Expand All @@ -443,7 +442,7 @@ def admin_delete_domain_block(self, id=None):
"""
if id is not None:
id = self.__unpack_id(id)
self.__api_request('DELETE', '/api/v1/admin/domain_blocks/{0}'.format(id))
self.__api_request('DELETE', f'/api/v1/admin/domain_blocks/{id}')
else:
raise AttributeError("You must provide an id of an existing domain block to remove it.")

Expand Down

0 comments on commit f5d4fbc

Please sign in to comment.