<s> """<STR_LIT>""" <EOL> import json <EOL> from xml . etree import ElementTree <EOL> from cloudcafe . extensions . rax_auth . v2_0 . tokens_api . models . base import BaseIdentityModel , BaseIdentityListModel <EOL> from cloudcafe . extensions . rax_auth . v2_0 . tokens_api . models . responses . role import Roles , Role <EOL> class Users ( BaseIdentityListModel ) : <EOL> ROOT_TAG = '<STR_LIT>' <EOL> def __init__ ( self , users = None ) : <EOL> super ( Users , self ) . __init__ ( ) <EOL> self . extend ( users ) <EOL> @ classmethod <EOL> def _json_to_obj ( cls , serialized_str ) : <EOL> ret = json . loads ( serialized_str ) <EOL> ret [ cls . ROOT_TAG ] = [ User . _dict_to_obj ( user ) <EOL> for user in ret . get ( cls . ROOT_TAG ) ] <EOL> return Users ( ** ret ) <EOL> @ classmethod <EOL> def _xml_to_obj ( cls , serialized_str ) : <EOL> element = ElementTree . fromstring ( serialized_str ) <EOL> cls . _remove_identity_xml_namespaces ( element ) <EOL> if element . tag != cls . ROOT_TAG : <EOL> return None <EOL> return cls . _xml_list_to_obj ( element . findall ( User . ROOT_TAG ) ) <EOL> @ classmethod <EOL> def _xml_list_to_obj ( cls , xml_list ) : <EOL> kwargs = { cls . ROOT_TAG : [ User . _xml_ele_to_obj ( ele ) <EOL> for ele in xml_list ] } <EOL> return Users ( ** kwargs ) <EOL> class User ( BaseIdentityModel ) : <EOL> ROOT_TAG = '<STR_LIT:user>' <EOL> def __init__ ( self , id = None , enabled = None , username = None , updated = None , <EOL> created = None , email = None , domainId = None , defaultRegion = None , <EOL> password = None , roles = None , name = None , display_name = None ) : <EOL> '''<STR_LIT>''' <EOL> super ( User , self ) . __init__ ( ) <EOL> self . id = id <EOL> self . enabled = enabled <EOL> self . username = username <EOL> self . updated = updated <EOL> self . created = created <EOL> self . email = email <EOL> self . domainId = domainId <EOL> self . defaultRegion = defaultRegion <EOL> self . password = password <EOL> self . roles = roles <EOL> self . name = name <EOL> self . display_name = display_name <EOL> def get_role ( self , id = None , name = None ) : <EOL> '''<STR_LIT>''' <EOL> for role in self . roles : <EOL> if id and not name : <EOL> if role . id == id : <EOL> return role <EOL> if name and not id : <EOL> if role . name == name : <EOL> return role <EOL> if name and id : <EOL> if ( role . name == name ) and ( role . id == id ) : <EOL> return role <EOL> @ classmethod <EOL> def _json_to_obj ( cls , serialized_str ) : <EOL> json_dict = json . loads ( serialized_str ) <EOL> user = cls . _dict_to_obj ( json_dict . get ( cls . ROOT_TAG ) ) <EOL> return user <EOL> @ classmethod <EOL> def _dict_to_obj ( cls , json_dict ) : <EOL> if '<STR_LIT>' in json_dict : <EOL> json_dict [ '<STR_LIT>' ] = json_dict [ '<STR_LIT>' ] <EOL> del json_dict [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in json_dict : <EOL> json_dict [ '<STR_LIT>' ] = json_dict [ '<STR_LIT>' ] <EOL> del json_dict [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in json_dict : <EOL> json_dict [ '<STR_LIT:password>' ] = json_dict [ '<STR_LIT>' ] <EOL> del json_dict [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in json_dict : <EOL> json_dict [ '<STR_LIT>' ] = json_dict [ '<STR_LIT>' ] <EOL> del json_dict [ '<STR_LIT>' ] <EOL> if Roles . ROOT_TAG in json_dict : <EOL> json_dict [ Roles . ROOT_TAG ] = Roles . _list_to_obj ( json_dict [ Roles . ROOT_TAG ] ) <EOL> return User ( ** json_dict ) <EOL> @ classmethod <EOL> def _xml_to_obj ( cls , serialized_str ) : <EOL> element = ElementTree . fromstring ( serialized_str ) <EOL> cls . _remove_identity_xml_namespaces ( element ) <EOL> if element . tag != cls . ROOT_TAG : <EOL> return None <EOL> return cls . _xml_ele_to_obj ( element ) <EOL> @ classmethod <EOL> def _xml_ele_to_obj ( cls , xml_ele ) : <EOL> kwargs = { '<STR_LIT:username>' : xml_ele . get ( '<STR_LIT:username>' ) , <EOL> '<STR_LIT>' : xml_ele . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : xml_ele . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:email>' : xml_ele . get ( '<STR_LIT:email>' ) , <EOL> '<STR_LIT>' : xml_ele . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : xml_ele . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:password>' : xml_ele . get ( '<STR_LIT:password>' ) , <EOL> '<STR_LIT:name>' : xml_ele . get ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT>' : xml_ele . get ( '<STR_LIT>' ) } <EOL> try : <EOL> kwargs [ '<STR_LIT:id>' ] = int ( xml_ele . get ( '<STR_LIT:id>' ) ) <EOL> except ( ValueError , TypeError ) : <EOL> kwargs [ '<STR_LIT:id>' ] = xml_ele . get ( '<STR_LIT:id>' ) <EOL> if xml_ele . get ( '<STR_LIT>' ) is not None : <EOL> kwargs [ '<STR_LIT>' ] = json . loads ( xml_ele . get ( '<STR_LIT>' ) . lower ( ) ) <EOL> roles = xml_ele . find ( Roles . ROOT_TAG ) <EOL> if roles is not None : <EOL> roles = roles . findall ( Role . ROOT_TAG ) <EOL> if roles is not None : <EOL> kwargs [ '<STR_LIT>' ] = Roles . _xml_list_to_obj ( roles ) <EOL> return User ( ** kwargs ) </s>
