Updating Credentials.new_from_json.
New _from_bytes behavior warrants update to docstring. Also updated 1-letter or other non-descriptive variable names.
This commit is contained in:
@@ -270,31 +270,32 @@ class Credentials(object):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def new_from_json(cls, s):
|
def new_from_json(cls, s):
|
||||||
"""Utility class method to instantiate a Credentials subclass from a JSON
|
"""Utility class method to instantiate a Credentials subclass from JSON.
|
||||||
representation produced by to_json().
|
|
||||||
|
Expects the JSON string to have been produced by to_json().
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
s: string, JSON from to_json().
|
s: string or bytes, JSON from to_json().
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
An instance of the subclass of Credentials that was serialized with
|
An instance of the subclass of Credentials that was serialized with
|
||||||
to_json().
|
to_json().
|
||||||
"""
|
"""
|
||||||
s = _from_bytes(s)
|
json_string_as_unicode = _from_bytes(s)
|
||||||
data = json.loads(s)
|
data = json.loads(json_string_as_unicode)
|
||||||
# Find and call the right classmethod from_json() to restore the object.
|
# Find and call the right classmethod from_json() to restore the object.
|
||||||
module = data['_module']
|
module_name = data['_module']
|
||||||
try:
|
try:
|
||||||
m = __import__(module)
|
module_obj = __import__(module_name)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# In case there's an object from the old package structure, update it
|
# In case there's an object from the old package structure, update it
|
||||||
module = module.replace('.googleapiclient', '')
|
module_name = module_name.replace('.googleapiclient', '')
|
||||||
m = __import__(module)
|
module_obj = __import__(module_name)
|
||||||
|
|
||||||
m = __import__(module, fromlist=module.split('.')[:-1])
|
module_obj = __import__(module_name, fromlist=module_name.split('.')[:-1])
|
||||||
kls = getattr(m, data['_class'])
|
kls = getattr(module_obj, data['_class'])
|
||||||
from_json = getattr(kls, 'from_json')
|
from_json = getattr(kls, 'from_json')
|
||||||
return from_json(s)
|
return from_json(json_string_as_unicode)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, unused_data):
|
def from_json(cls, unused_data):
|
||||||
|
|||||||
Reference in New Issue
Block a user