Skip to content

Commit

Permalink
Removed mutable default values from arguments capless#32
Browse files Browse the repository at this point in the history
  • Loading branch information
armicron committed May 30, 2017
1 parent 856df67 commit e4b2d3c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions warrant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from .aws_srp import AWSSRP
from .exceptions import TokenVerificationException

def cognito_to_dict(attr_list,attr_map=dict()):
def cognito_to_dict(attr_list, attr_map=None):
if attr_map is None:
attr_map = {}
attr_dict = dict()
for a in attr_list:
name = a.get('Name')
Expand All @@ -20,11 +22,13 @@ def cognito_to_dict(attr_list,attr_map=dict()):
attr_dict[name] = value
return attr_dict

def dict_to_cognito(attributes,attr_map=dict()):
def dict_to_cognito(attributes, attr_map=None):
"""
:param attributes: Dictionary of User Pool attribute names/values
:return: list of User Pool attribute formatted dicts: {'Name': <attr_name>, 'Value': <attr_value>}
"""
if attr_map is None:
attr_map = {}
for k,v in attr_map.items():
if v in attributes.keys():
attributes[k] = attributes.pop(v)
Expand All @@ -34,7 +38,7 @@ def dict_to_cognito(attributes,attr_map=dict()):

class UserObj(object):

def __init__(self, username, attribute_list, cognito_obj, metadata=dict(),attr_map=dict()):
def __init__(self, username, attribute_list, cognito_obj, metadata=None, attr_map=None):
"""
:param username:
:param attribute_list:
Expand All @@ -43,12 +47,12 @@ def __init__(self, username, attribute_list, cognito_obj, metadata=dict(),attr_m
self.username = username
self.pk = username
self._cognito = cognito_obj
self._attr_map = attr_map
self._attr_map = {} if attr_map is None else attr_map
self._data = cognito_to_dict(attribute_list,self._attr_map)
self.sub = self._data.pop('sub',None)
self.email_verified = self._data.pop('email_verified',None)
self.phone_number_verified = self._data.pop('phone_number_verified',None)
self._metadata = metadata
self._metadata = {} if metadata is None else metadata

def __repr__(self):
return '<{class_name}: {uni} >'.format(
Expand Down Expand Up @@ -162,7 +166,8 @@ def verify_token(self,token,id_name,token_use):
setattr(self,id_name,token)
return verified

def get_user_obj(self,username=None,attribute_list=[],metadata={},attr_map=dict()):
def get_user_obj(self, username=None, attribute_list=None, metadata=None,
attr_map=None):
"""
Returns the specified
:param username: Username of the user
Expand Down Expand Up @@ -202,7 +207,7 @@ def check_token(self):
return True
return False

def register(self, username, password,attr_map=dict(),**kwargs):
def register(self, username, password, attr_map=None, **kwargs):
"""
Register the user. Other base attributes from AWS Cognito User Pools
are address, birthdate, email, family_name (last name), gender,
Expand Down Expand Up @@ -322,15 +327,15 @@ def logout(self):
self.access_token = None
self.token_type = None

def admin_update_profile(self, attrs, attr_map=dict()):
def admin_update_profile(self, attrs, attr_map=None):
user_attrs = dict_to_cognito(attrs, attr_map)
self.client.admin_update_user_attributes(
UserPoolId = self.user_pool_id,
Username = self.username,
UserAttributes = user_attrs
)

def update_profile(self, attrs,attr_map=dict()):
def update_profile(self, attrs, attr_map=None):
"""
Updates User attributes
:param attrs: Dictionary of attribute name, values
Expand All @@ -343,7 +348,7 @@ def update_profile(self, attrs,attr_map=dict()):
AccessToken=self.access_token
)

def get_user(self,attr_map=dict()):
def get_user(self, attr_map=None):
"""
Returns a UserObj (or whatever the self.user_class is) by using the
user's access token.
Expand All @@ -365,7 +370,7 @@ def get_user(self,attr_map=dict()):
attribute_list=user.get('UserAttributes'),
metadata=user_metadata,attr_map=attr_map)

def get_users(self,attr_map=dict()):
def get_users(self, attr_map=None):
"""
Returns all users for a user pool. Returns instances of the
self.user_class.
Expand All @@ -381,7 +386,7 @@ def get_users(self,attr_map=dict()):
attr_map=attr_map)
for user in response.get('Users')]

def admin_get_user(self,attr_map=dict()):
def admin_get_user(self, attr_map=None):
"""
Get the user's details using admin super privileges.
:param attr_map: Dictionary map from Cognito attributes to attribute
Expand All @@ -402,7 +407,7 @@ def admin_get_user(self,attr_map=dict()):
attribute_list=user.get('UserAttributes'),
metadata=user_metadata,attr_map=attr_map)

def admin_create_user(self, username, temporary_password='', attr_map=dict(), **kwargs):
def admin_create_user(self, username, temporary_password='', attr_map=None, **kwargs):
"""
Create a user using admin super privileges.
:param username: User Pool username
Expand Down

0 comments on commit e4b2d3c

Please sign in to comment.