Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apiclient/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Retain apiclient as an alias for googleapiclient."""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 36-36 refactored with the following changes:


from six import iteritems

import googleapiclient
Expand Down Expand Up @@ -33,4 +34,4 @@
import sys

for module_name, module in iteritems(_SUBMODULES):
sys.modules["apiclient.%s" % module_name] = module
sys.modules[f"apiclient.{module_name}"] = module
37 changes: 18 additions & 19 deletions describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def add_param(pname, desc):
if pname is None:
return
if "(required)" not in desc:
pname = pname + "=None"
pname = f"{pname}=None"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function method_params.add_param refactored with the following changes:

parameters.append(pname)
else:
# required params should be put straight into sorted_parameters
Expand Down Expand Up @@ -291,7 +291,7 @@ def breadcrumbs(path, root_discovery):
display = p
if i == 0:
display = root_discovery.get("title", display)
crumbs.append('<a href="{}.html">{}</a>'.format(prefix + p, display))
crumbs.append(f'<a href="{prefix + p}.html">{display}</a>')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function breadcrumbs refactored with the following changes:

accumulated.append(p)

return " . ".join(crumbs)
Expand All @@ -314,10 +314,11 @@ def document_collection(resource, path, root_discovery, discovery, css=CSS):
html = [
"<html><body>",
css,
"<h1>%s</h1>" % breadcrumbs(path[:-1], root_discovery),
f"<h1>{breadcrumbs(path[:-1], root_discovery)}</h1>",
"<h2>Instance Methods</h2>",
]

Comment on lines -317 to 320
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function document_collection refactored with the following changes:


# Which methods are for collections.
for name in dir(resource):
if not name.startswith("_") and callable(getattr(resource, name)):
Expand Down Expand Up @@ -362,13 +363,11 @@ def document_collection_recursive(resource, path, root_discovery, discovery):

html = document_collection(resource, path, root_discovery, discovery)

f = open(os.path.join(FLAGS.dest, path + "html"), "w")
if sys.version_info.major < 3:
html = html.encode("utf-8")

f.write(html)
f.close()
with open(os.path.join(FLAGS.dest, f"{path}html"), "w") as f:
if sys.version_info.major < 3:
html = html.encode("utf-8")

f.write(html)
Comment on lines -365 to +370
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function document_collection_recursive refactored with the following changes:

for name in dir(resource):
if (
not name.startswith("_")
Expand Down Expand Up @@ -398,18 +397,18 @@ def document_api(name, version, uri):
service = build(name, version)
content = get_static_doc(name, version)
except UnknownApiNameOrVersion as e:
print("Warning: {} {} found but could not be built.".format(name, version))
print(f"Warning: {name} {version} found but could not be built.")
return
except HttpError as e:
print("Warning: {} {} returned {}.".format(name, version, e))
print(f"Warning: {name} {version} returned {e}.")
Comment on lines -401 to +403
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function document_api refactored with the following changes:

return

discovery = json.loads(content)

version = safe_version(version)

document_collection_recursive(
service, "{}_{}.".format(name, version), discovery, discovery
service, f"{name}_{version}.", discovery, discovery
)


Expand All @@ -429,7 +428,7 @@ def document_api_from_discovery_document(uri):
version = safe_version(discovery["version"])

document_collection_recursive(
service, "{}_{}.".format(name, version), discovery, discovery
service, f"{name}_{version}.", discovery, discovery
Comment on lines -432 to +431
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function document_api_from_discovery_document refactored with the following changes:

)


Expand Down Expand Up @@ -458,12 +457,12 @@ def document_api_from_discovery_document(uri):

markdown = []
for api, versions in api_directory.items():
markdown.append("## %s" % api)
for version in versions:
markdown.append(
"* [%s](http://googleapis.github.io/google-api-python-client/docs/dyn/%s_%s.html)"
% (version, api, safe_version(version))
)
markdown.append(f"## {api}")
markdown.extend(
f"* [{version}](http://googleapis.github.io/google-api-python-client/docs/dyn/{api}_{safe_version(version)}.html)"
for version in versions
)

Comment on lines -461 to +465
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 461-466 refactored with the following changes:

markdown.append("\n")

with open("docs/dyn/index.md", "w") as f:
Expand Down
5 changes: 3 additions & 2 deletions expandsymlinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
"""Copy files from source to dest expanding symlinks along the way.
"""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 28-32 refactored with the following changes:


from shutil import copytree

import argparse
import sys


# Ignore these files and directories when copying over files into the snapshot.
IGNORE = set(['.hg', 'httplib2', 'oauth2', 'simplejson', 'static'])
IGNORE = {'.hg', 'httplib2', 'oauth2', 'simplejson', 'static'}

# In addition to the above files also ignore these files and directories when
# copying over samples into the snapshot.
IGNORE_IN_SAMPLES = set(['googleapiclient', 'oauth2client', 'uritemplate'])
IGNORE_IN_SAMPLES = {'googleapiclient', 'oauth2client', 'uritemplate'}

parser = argparse.ArgumentParser(description=__doc__)

Expand Down
47 changes: 25 additions & 22 deletions googleapiclient/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ def with_scopes(credentials, scopes):
"""
if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials):
return google.auth.credentials.with_scopes_if_required(credentials, scopes)
else:
try:
if credentials.create_scoped_required():
return credentials.create_scoped(scopes)
else:
return credentials
except AttributeError:
return credentials
try:
return (
credentials.create_scoped(scopes)
if credentials.create_scoped_required()
else credentials
)

except AttributeError:
return credentials
Comment on lines -85 to +93
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function with_scopes refactored with the following changes:



def authorized_http(credentials):
Expand All @@ -106,17 +107,18 @@ def authorized_http(credentials):
"""
from googleapiclient.http import build_http

if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials):
if google_auth_httplib2 is None:
raise ValueError(
"Credentials from google.auth specified, but "
"google-api-python-client is unable to use these credentials "
"unless google-auth-httplib2 is installed. Please install "
"google-auth-httplib2."
)
return google_auth_httplib2.AuthorizedHttp(credentials, http=build_http())
else:
if not HAS_GOOGLE_AUTH or not isinstance(
credentials, google.auth.credentials.Credentials
):
return credentials.authorize(build_http())
if google_auth_httplib2 is None:
raise ValueError(
"Credentials from google.auth specified, but "
"google-api-python-client is unable to use these credentials "
"unless google-auth-httplib2 is installed. Please install "
"google-auth-httplib2."
)
return google_auth_httplib2.AuthorizedHttp(credentials, http=build_http())
Comment on lines -109 to +121
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function authorized_http refactored with the following changes:



def refresh_credentials(credentials):
Expand All @@ -125,11 +127,12 @@ def refresh_credentials(credentials):
# Http instance which would cause a weird recursive loop of refreshing
# and likely tear a hole in spacetime.
refresh_http = httplib2.Http()
if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials):
request = google_auth_httplib2.Request(refresh_http)
return credentials.refresh(request)
else:
if not HAS_GOOGLE_AUTH or not isinstance(
credentials, google.auth.credentials.Credentials
):
return credentials.refresh(refresh_http)
request = google_auth_httplib2.Request(refresh_http)
return credentials.refresh(request)
Comment on lines -128 to +135
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function refresh_credentials refactored with the following changes:



def apply_credentials(credentials, headers):
Expand Down
15 changes: 4 additions & 11 deletions googleapiclient/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ def positional_decorator(wrapped):
@functools.wraps(wrapped)
def positional_wrapper(*args, **kwargs):
if len(args) > max_positional_args:
plural_s = ""
if max_positional_args != 1:
plural_s = "s"
plural_s = "s" if max_positional_args != 1 else ""
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function positional.positional_decorator.positional_wrapper refactored with the following changes:

message = (
"{function}() takes at most {args_max} positional "
"argument{plural} ({args_given} given)".format(
Expand Down Expand Up @@ -158,10 +156,8 @@ def parse_unique_urlencoded(content):
params = {}
for key, value in six.iteritems(urlencoded_params):
if len(value) != 1:
msg = "URL-encoded content contains a repeated value:" "%s -> %s" % (
key,
", ".join(value),
)
msg = f'URL-encoded content contains a repeated value:{key} -> {", ".join(value)}'

Comment on lines -161 to +160
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_unique_urlencoded refactored with the following changes:

raise ValueError(msg)
params[key] = value[0]
return params
Expand Down Expand Up @@ -205,7 +201,4 @@ def _add_query_parameter(url, name, value):
Returns:
Updated query parameter. Does not update the url if value is None.
"""
if value is None:
return url
else:
return update_query_params(url, {name: value})
return url if value is None else update_query_params(url, {name: value})
Comment on lines -208 to +204
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _add_query_parameter refactored with the following changes:

23 changes: 9 additions & 14 deletions googleapiclient/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@


def _upper_header_keys(headers):
new_headers = {}
for k, v in six.iteritems(headers):
new_headers[k.upper()] = v
return new_headers
return {k.upper(): v for k, v in six.iteritems(headers)}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _upper_header_keys refactored with the following changes:



class Notification(object):
Expand Down Expand Up @@ -270,14 +267,14 @@ def notification_from_headers(channel, headers):
channel_id = headers[X_GOOG_CHANNEL_ID]
if channel.id != channel_id:
raise errors.InvalidNotificationError(
"Channel id mismatch: %s != %s" % (channel.id, channel_id)
f"Channel id mismatch: {channel.id} != {channel_id}"
)
else:
message_number = int(headers[X_GOOG_MESSAGE_NUMBER])
state = headers[X_GOOG_RESOURCE_STATE]
resource_uri = headers[X_GOOG_RESOURCE_URI]
resource_id = headers[X_GOOG_RESOURCE_ID]
return Notification(message_number, state, resource_uri, resource_id)

message_number = int(headers[X_GOOG_MESSAGE_NUMBER])
state = headers[X_GOOG_RESOURCE_STATE]
resource_uri = headers[X_GOOG_RESOURCE_URI]
resource_id = headers[X_GOOG_RESOURCE_ID]
return Notification(message_number, state, resource_uri, resource_id)
Comment on lines -273 to +277
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function notification_from_headers refactored with the following changes:



@util.positional(2)
Expand All @@ -304,9 +301,7 @@ def new_webhook_channel(url, token=None, expiration=None, params=None):
expiration_ms = (
delta.microseconds / 1000 + (delta.seconds + delta.days * 24 * 3600) * 1000
)
if expiration_ms < 0:
expiration_ms = 0

expiration_ms = max(expiration_ms, 0)
Comment on lines -307 to +304
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function new_webhook_channel refactored with the following changes:

return Channel(
"web_hook",
str(uuid.uuid4()),
Expand Down
Loading