<s> import api <EOL> import dbutils <EOL> class User ( object ) : <EOL> def __init__ ( self , user_id , name , fullname , status , email ) : <EOL> self . id = user_id <EOL> self . name = name <EOL> self . fullname = fullname <EOL> self . status = status <EOL> self . email = email <EOL> self . __internal = None <EOL> def isAnonymous ( self ) : <EOL> return self . id is None <EOL> def getInternal ( self , critic ) : <EOL> if not self . __internal : <EOL> if self . isAnonymous ( ) : <EOL> self . __internal = dbutils . User . makeAnonymous ( ) <EOL> else : <EOL> self . __internal = dbutils . User . fromId ( critic . database , self . id ) <EOL> return self . __internal <EOL> def getPrimaryEmails ( self , critic ) : <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( self . id , ) ) <EOL> return [ api . user . User . PrimaryEmail ( address , bool ( selected ) , <EOL> dbutils . boolean ( verified ) ) <EOL> for address , selected , verified in cursor ] <EOL> def getGitEmails ( self , critic ) : <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( self . id , ) ) <EOL> return set ( email for ( email , ) in cursor ) <EOL> def getRepositoryFilters ( self , critic ) : <EOL> all_repositories = { } <EOL> filters = { } <EOL> def processRepository ( repository_id ) : <EOL> if repository_id not in all_repositories : <EOL> all_repositories [ repository_id ] = api . repository . fetch ( <EOL> critic , repository_id = repository_id ) <EOL> return all_repositories [ repository_id ] <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( self . id , ) ) <EOL> for subject_id , filter_type , path , filter_id , repository_id , delegate_string in cursor : <EOL> repository = processRepository ( repository_id ) <EOL> filters . setdefault ( repository , [ ] ) . append ( <EOL> api . filters . RepositoryFilter ( <EOL> critic , <EOL> api . impl . filters . RepositoryFilter ( <EOL> subject_id , filter_type , path , filter_id , repository_id , <EOL> delegate_string , repository = repository ) ) ) <EOL> return filters <EOL> def hasRole ( self , critic , role ) : <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( self . id , role ) ) <EOL> row = cursor . fetchone ( ) <EOL> if row : <EOL> return row [ <NUM_LIT:0> ] is not None <EOL> raise api . user . InvalidRole ( role ) <EOL> def getPreference ( self , critic , item , user , repository ) : <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , ( item , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if not row : <EOL> raise api . preference . InvalidPreferenceItem ( item ) <EOL> preference_type = row [ <NUM_LIT:0> ] <EOL> arguments = [ item ] <EOL> where = [ "<STR_LIT>" ] <EOL> if preference_type in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> column = "<STR_LIT>" <EOL> else : <EOL> column = "<STR_LIT:string>" <EOL> if not self . isAnonymous ( ) : <EOL> arguments . append ( user . id ) <EOL> where . append ( "<STR_LIT>" ) <EOL> else : <EOL> where . append ( "<STR_LIT>" ) <EOL> if repository is not None : <EOL> arguments . append ( repository . id ) <EOL> where . append ( "<STR_LIT>" ) <EOL> else : <EOL> where . append ( "<STR_LIT>" ) <EOL> where = "<STR_LIT>" . join ( "<STR_LIT>" % condition for condition in where ) <EOL> query = ( """<STR_LIT>""" <EOL> % { "<STR_LIT>" : column , "<STR_LIT>" : where } ) <EOL> cursor . execute ( query , arguments ) <EOL> row = sorted ( cursor , key = lambda row : row [ <NUM_LIT:1> : ] ) [ - <NUM_LIT:1> ] <EOL> value , user_id , repository_id = row <EOL> if preference_type == "<STR_LIT>" : <EOL> value = bool ( value ) <EOL> if user_id is None : <EOL> user = None <EOL> if repository_id is None : <EOL> repository = None <EOL> return api . preference . Preference ( item , value , user , repository ) <EOL> def wrap ( self , critic ) : <EOL> return api . user . User ( critic , self ) <EOL> def make ( critic , args ) : <EOL> for user_id , name , fullname , status , email in args : <EOL> def callback ( ) : <EOL> return User ( user_id , name , fullname , status , email ) . wrap ( critic ) <EOL> yield critic . _impl . cached ( api . user . User , user_id , callback ) <EOL> def fetch ( critic , user_id , name ) : <EOL> try : <EOL> return fetchMany ( critic , <EOL> user_ids = None if user_id is None else [ user_id ] , <EOL> names = None if name is None else [ name ] ) [ <NUM_LIT:0> ] <EOL> except api . user . InvalidUserIds as error : <EOL> raise api . user . InvalidUserId ( error . values [ <NUM_LIT:0> ] ) <EOL> except api . user . InvalidUserNames as error : <EOL> raise api . user . InvalidUserName ( error . values [ <NUM_LIT:0> ] ) <EOL> def fetchMany ( critic , user_ids , names ) : <EOL> return_type = list <EOL> if user_ids is not None : <EOL> if isinstance ( user_ids , set ) : <EOL> return_type = set <EOL> where_column = "<STR_LIT>" <EOL> values = [ int ( user_id ) for user_id in user_ids ] <EOL> column_index = <NUM_LIT:0> <EOL> else : <EOL> if isinstance ( names , set ) : <EOL> return_type = set <EOL> where_column = "<STR_LIT:name>" <EOL> values = [ str ( name ) for name in names ] <EOL> column_index = <NUM_LIT:1> <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" + where_column + """<STR_LIT>""" , <EOL> ( values , ) ) <EOL> rows = cursor . fetchall ( ) <EOL> if len ( rows ) < len ( values ) : <EOL> found = set ( row [ column_index ] for row in rows ) <EOL> if user_ids is not None : <EOL> exception_type = api . user . InvalidUserIds <EOL> else : <EOL> exception_type = api . user . InvalidUserNames <EOL> values = [ value for value in values if value not in found ] <EOL> raise exception_type ( values ) <EOL> rows = dict ( ( row [ column_index ] , row ) for row in rows ) <EOL> return return_type ( make ( critic , ( rows [ key ] for key in values ) ) ) <EOL> def fetchAll ( critic , status ) : <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> if status is None : <EOL> condition = "<STR_LIT>" <EOL> values = ( ) <EOL> else : <EOL> condition = "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT:%s>" ] * len ( status ) ) <EOL> values = tuple ( status ) <EOL> cursor . execute ( """<STR_LIT>""" + condition + """<STR_LIT>""" , <EOL> values ) <EOL> return list ( make ( critic , cursor ) ) <EOL> def anonymous ( critic ) : <EOL> def callback ( ) : <EOL> return User ( None , None , None , "<STR_LIT>" , None ) . wrap ( critic ) <EOL> return critic . _impl . cached ( api . user . User , None , callback ) </s>
