<s> try : <EOL> from urllib . request import HTTPError <EOL> from urllib . parse import urlparse , parse_qs <EOL> except ImportError : <EOL> from urllib2 import HTTPError <EOL> from urlparse import urlparse , parse_qs <EOL> from lxml import etree <EOL> from lxml . builder import E <EOL> from . xml import namespaces , OUT , SP , SEARCH , SQ <EOL> USER_PATH = '<STR_LIT>' <EOL> PEOPLE_WEBSERVICE = '<STR_LIT>' <EOL> SEARCH_WEBSERVICE = '<STR_LIT>' <EOL> class SharePointUsers ( object ) : <EOL> def __init__ ( self , opener ) : <EOL> self . opener = opener <EOL> self . _users = { } <EOL> self . _user_searches = { } <EOL> self . _resolved_principals = { } <EOL> def __getitem__ ( self , key ) : <EOL> key = int ( key ) <EOL> if key not in self . _users : <EOL> url = self . opener . base_url + USER_PATH . format ( key ) <EOL> try : <EOL> data = self . opener . open ( url ) <EOL> except HTTPError as e : <EOL> if e . code == <NUM_LIT> : <EOL> self . _users [ key ] = None <EOL> else : <EOL> raise <EOL> else : <EOL> props = etree . parse ( data ) . xpath ( '<STR_LIT>' , <EOL> namespaces = namespaces ) <EOL> self . _users [ key ] = SharePointUser ( key , props ) <EOL> if self . _users [ key ] is None : <EOL> raise KeyError ( key ) <EOL> return self . _users [ key ] <EOL> def resolve_principal ( self , principal ) : <EOL> return self . resolve_principals ( [ principal ] ) [ <NUM_LIT:0> ] <EOL> def resolve_principals ( self , principals ) : <EOL> principals_to_resolve = set ( p for p in principals if p not in self . _resolved_principals ) <EOL> if principals_to_resolve : <EOL> xml = SP . ResolvePrincipals ( SP . principalKeys ( * ( SP . string ( p ) for p in principals_to_resolve ) ) ) <EOL> xml . append ( SP . principalType ( '<STR_LIT>' ) ) <EOL> result = self . opener . post_soap ( PEOPLE_WEBSERVICE , xml ) <EOL> for principal_info in result . xpath ( '<STR_LIT>' , namespaces = namespaces ) : <EOL> user_id = int ( principal_info . find ( '<STR_LIT>' , namespaces = namespaces ) . text ) <EOL> account_name = principal_info . find ( '<STR_LIT>' , namespaces = namespaces ) . text <EOL> display_name = principal_info . find ( '<STR_LIT>' , namespaces = namespaces ) <EOL> display_name = display_name . text if display_name is not None else '<STR_LIT>' <EOL> if user_id == - <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" . format ( account_name , <EOL> display_name ) ) <EOL> self . _resolved_principals [ account_name ] = self [ user_id ] <EOL> return [ self . _resolved_principals . get ( p ) for p in principals ] <EOL> def search ( self , name , max_results = None ) : <EOL> if name in self . _user_searches : <EOL> return self . _user_searches [ name ] <EOL> query = SQ . QueryPacket ( <EOL> E . Query ( <EOL> E . Context ( <EOL> E . QueryText ( <EOL> '<STR_LIT>' + name <EOL> , type = '<STR_LIT>' ) , <EOL> ) <EOL> ) , <EOL> ) <EOL> xml = SEARCH . Query ( SEARCH . queryXml ( etree . tostring ( query ) ) ) <EOL> results = self . opener . post_soap ( SEARCH_WEBSERVICE , xml , soapaction = '<STR_LIT>' ) <EOL> results = etree . fromstring ( results . find ( '<STR_LIT>' , namespaces = namespaces ) . text ) <EOL> account_names = [ ] <EOL> for result in results . xpath ( '<STR_LIT>' , namespaces = namespaces ) : <EOL> link = result . xpath ( '<STR_LIT>' , namespaces = namespaces ) [ <NUM_LIT:0> ] . text <EOL> account_name = parse_qs ( urlparse ( link ) . query ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> account_names . append ( account_name ) <EOL> users = self . resolve_principals ( account_names ) <EOL> self . _user_searches [ name ] = users <EOL> return users <EOL> def as_xml ( self , user_ids , ** kwargs ) : <EOL> xml = OUT . users ( ) <EOL> for user_id in user_ids : <EOL> xml . append ( self [ user_id ] . as_xml ( ) ) <EOL> return xml <EOL> class SharePointUser ( object ) : <EOL> def __init__ ( self , id , props ) : <EOL> self . _props = props <EOL> self . _data = { } <EOL> self . id = id <EOL> prefix = '<STR_LIT:{>' + namespaces [ '<STR_LIT:d>' ] + '<STR_LIT:}>' <EOL> for prop in props : <EOL> tag = prop . tag <EOL> ns , local = tag [ <NUM_LIT:1> : ] . split ( '<STR_LIT:}>' , <NUM_LIT:1> ) <EOL> name = tag . split ( '<STR_LIT:}>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> if prop . attrib . get ( '<STR_LIT>' . format ( namespaces [ '<STR_LIT:m>' ] ) ) == '<STR_LIT:true>' : <EOL> value = None <EOL> else : <EOL> value = prop . text <EOL> self . _data [ ( ns , local ) ] = value <EOL> if not tag . startswith ( prefix ) : <EOL> continue <EOL> setattr ( self , name , value ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . _data [ key ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . Name ) <EOL> def __unicode__ ( self ) : <EOL> return self . Name <EOL> def as_xml ( self , ** kwargs ) : <EOL> return OUT . user ( * self . _props , id = unicode ( self . id ) ) </s>
