Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Makes all code PEP8 compliant. #276

Merged
merged 9 commits into from
Aug 23, 2015
42 changes: 21 additions & 21 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,33 @@
# been installed run `pip install -r docs/requirements.txt`.
import django
if django.VERSION[1] < 7:
sys.path.insert(0, '.')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_settings'
sys.path.insert(0, '.')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_settings'

# -- Options for HTML output ----------------------------------------------

# We want to set the RTD theme, but not if we're on RTD.
if os.environ.get('READTHEDOCS', None) == 'True':
# Download the GAE SDK if we are building on READTHEDOCS.
docs_dir = os.path.dirname(os.path.abspath(__file__))
root_dir = os.path.abspath(os.path.join(docs_dir, '..'))
gae_dir = os.path.join(root_dir, 'google_appengine')
if not os.path.isdir(gae_dir):
scripts_dir = os.path.join(root_dir, 'scripts')
sys.path.append(scripts_dir)
import fetch_gae_sdk
# The first argument is the script name and the second is
# the destination dir (where google_appengine is downloaded).
result = fetch_gae_sdk.main([None, root_dir])
if result not in (0, None):
sys.stderr.write('Result failed %d\n' % (result,))
sys.exit(result)
# Allow imports from the GAE directory as well.
sys.path.append(gae_dir)
# Download the GAE SDK if we are building on READTHEDOCS.
docs_dir = os.path.dirname(os.path.abspath(__file__))
root_dir = os.path.abspath(os.path.join(docs_dir, '..'))
gae_dir = os.path.join(root_dir, 'google_appengine')
if not os.path.isdir(gae_dir):
scripts_dir = os.path.join(root_dir, 'scripts')
sys.path.append(scripts_dir)
import fetch_gae_sdk
# The first argument is the script name and the second is
# the destination dir (where google_appengine is downloaded).
result = fetch_gae_sdk.main([None, root_dir])
if result not in (0, None):
sys.stderr.write('Result failed %d\n' % (result,))
sys.exit(result)
# Allow imports from the GAE directory as well.
sys.path.append(gae_dir)
else:
import sphinx_rtd_theme
html_theme = 'sphinx_rtd_theme'
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
import sphinx_rtd_theme
html_theme = 'sphinx_rtd_theme'
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
Expand Down
120 changes: 61 additions & 59 deletions oauth2client/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,83 +19,85 @@


def _parse_pem_key(raw_key_input):
"""Identify and extract PEM keys.
"""Identify and extract PEM keys.

Determines whether the given key is in the format of PEM key, and extracts
the relevant part of the key if it is.
Determines whether the given key is in the format of PEM key, and extracts
the relevant part of the key if it is.

Args:
raw_key_input: The contents of a private key file (either PEM or PKCS12).
Args:
raw_key_input: The contents of a private key file (either PEM or
PKCS12).

Returns:
string, The actual key if the contents are from a PEM file, or else None.
"""
offset = raw_key_input.find(b'-----BEGIN ')
if offset != -1:
return raw_key_input[offset:]
Returns:
string, The actual key if the contents are from a PEM file, or
else None.
"""
offset = raw_key_input.find(b'-----BEGIN ')
if offset != -1:
return raw_key_input[offset:]


def _json_encode(data):
return json.dumps(data, separators=(',', ':'))
return json.dumps(data, separators=(',', ':'))


def _to_bytes(value, encoding='ascii'):
"""Converts a string value to bytes, if necessary.

Unfortunately, ``six.b`` is insufficient for this task since in
Python2 it does not modify ``unicode`` objects.

Args:
value: The string/bytes value to be converted.
encoding: The encoding to use to convert unicode to bytes. Defaults
to "ascii", which will not allow any characters from ordinals
larger than 127. Other useful values are "latin-1", which
which will only allows byte ordinals (up to 255) and "utf-8",
which will encode any unicode that needs to be.

Returns:
The original value converted to bytes (if unicode) or as passed in
if it started out as bytes.

Raises:
ValueError if the value could not be converted to bytes.
"""
result = (value.encode(encoding)
if isinstance(value, six.text_type) else value)
if isinstance(result, six.binary_type):
return result
else:
raise ValueError('%r could not be converted to bytes' % (value,))
"""Converts a string value to bytes, if necessary.

Unfortunately, ``six.b`` is insufficient for this task since in
Python2 it does not modify ``unicode`` objects.

Args:
value: The string/bytes value to be converted.
encoding: The encoding to use to convert unicode to bytes. Defaults
to "ascii", which will not allow any characters from ordinals
larger than 127. Other useful values are "latin-1", which
which will only allows byte ordinals (up to 255) and "utf-8",
which will encode any unicode that needs to be.

Returns:
The original value converted to bytes (if unicode) or as passed in
if it started out as bytes.

Raises:
ValueError if the value could not be converted to bytes.
"""
result = (value.encode(encoding)
if isinstance(value, six.text_type) else value)
if isinstance(result, six.binary_type):
return result
else:
raise ValueError('%r could not be converted to bytes' % (value,))


def _from_bytes(value):
"""Converts bytes to a string value, if necessary.
"""Converts bytes to a string value, if necessary.

Args:
value: The string/bytes value to be converted.
Args:
value: The string/bytes value to be converted.

Returns:
The original value converted to unicode (if bytes) or as passed in
if it started out as unicode.
Returns:
The original value converted to unicode (if bytes) or as passed in
if it started out as unicode.

Raises:
ValueError if the value could not be converted to unicode.
"""
result = (value.decode('utf-8')
if isinstance(value, six.binary_type) else value)
if isinstance(result, six.text_type):
return result
else:
raise ValueError('%r could not be converted to unicode' % (value,))
Raises:
ValueError if the value could not be converted to unicode.
"""
result = (value.decode('utf-8')
if isinstance(value, six.binary_type) else value)
if isinstance(result, six.text_type):
return result
else:
raise ValueError('%r could not be converted to unicode' % (value,))


def _urlsafe_b64encode(raw_bytes):
raw_bytes = _to_bytes(raw_bytes, encoding='utf-8')
return base64.urlsafe_b64encode(raw_bytes).rstrip(b'=')
raw_bytes = _to_bytes(raw_bytes, encoding='utf-8')
return base64.urlsafe_b64encode(raw_bytes).rstrip(b'=')


def _urlsafe_b64decode(b64string):
# Guard against unicode strings, which base64 can't handle.
b64string = _to_bytes(b64string)
padded = b64string + b'=' * (4 - len(b64string) % 4)
return base64.urlsafe_b64decode(padded)
# Guard against unicode strings, which base64 can't handle.
b64string = _to_bytes(b64string)
padded = b64string + b'=' * (4 - len(b64string) % 4)
return base64.urlsafe_b64decode(padded)
Loading