<s> import datetime <EOL> import copy <EOL> import unicode_helpers <EOL> import json <EOL> import logging <EOL> from util import cached_property <EOL> from util import dict_from_dir <EOL> from totalimpactwebapp import db <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def clean_id ( nid ) : <EOL> try : <EOL> nid = nid . strip ( '<STR_LIT>' ) . strip ( ) <EOL> nid = unicode_helpers . remove_nonprinting_characters ( nid ) <EOL> except ( TypeError , AttributeError ) : <EOL> pass <EOL> return ( nid ) <EOL> def normalize_alias_tuple ( ns , nid ) : <EOL> ns = clean_id ( ns ) <EOL> ns = ns . lower ( ) <EOL> if ns == "<STR_LIT>" : <EOL> return ( ns , nid ) <EOL> nid = clean_id ( nid ) <EOL> from totalimpact . providers import crossref <EOL> from totalimpact . providers import pubmed <EOL> from totalimpact . providers import arxiv <EOL> from totalimpact . providers import webpage <EOL> from totalimpact import importer <EOL> clean_nid = None <EOL> if ns == "<STR_LIT>" or importer . is_doi ( nid ) : <EOL> ns = "<STR_LIT>" <EOL> clean_nid = crossref . clean_doi ( nid ) <EOL> elif ns == "<STR_LIT>" or importer . is_pmid ( nid ) : <EOL> ns = "<STR_LIT>" <EOL> clean_nid = pubmed . clean_pmid ( nid ) <EOL> elif ns == "<STR_LIT>" or importer . is_arxiv ( nid ) : <EOL> ns = "<STR_LIT>" <EOL> clean_nid = arxiv . clean_arxiv_id ( nid ) <EOL> elif ns == "<STR_LIT:url>" or importer . is_url ( nid ) : <EOL> ns = "<STR_LIT:url>" <EOL> clean_nid = webpage . clean_url ( nid ) <EOL> elif ns not in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:url>" ] : <EOL> clean_nid = nid <EOL> if not clean_nid : <EOL> return None <EOL> return ( ns , clean_nid ) <EOL> def clean_alias_tuple_for_comparing ( ns , nid ) : <EOL> alias_tuple = normalize_alias_tuple ( ns , nid ) <EOL> if not alias_tuple : <EOL> return None <EOL> try : <EOL> ( ns , nid ) = alias_tuple <EOL> cleaned_alias = ( ns . lower ( ) , nid . lower ( ) ) <EOL> except AttributeError : <EOL> logger . debug ( u"<STR_LIT>" . format ( <EOL> ns = ns , nid = nid ) ) <EOL> cleaned_alias = ( ns , nid ) <EOL> return cleaned_alias <EOL> def alias_tuples_from_dict ( aliases_dict ) : <EOL> """<STR_LIT>""" <EOL> alias_tuples = [ ] <EOL> for ns , ids in aliases_dict . iteritems ( ) : <EOL> if isinstance ( ids , basestring ) : <EOL> alias_tuples . append ( ( ns , ids ) ) <EOL> else : <EOL> for id in ids : <EOL> alias_tuples . append ( ( ns , id ) ) <EOL> return alias_tuples <EOL> def alias_dict_from_tuples ( aliases_tuples ) : <EOL> alias_dict = { } <EOL> for ( ns , ids ) in aliases_tuples : <EOL> if ns in alias_dict : <EOL> alias_dict [ ns ] += [ ids ] <EOL> else : <EOL> alias_dict [ ns ] = [ ids ] <EOL> return alias_dict <EOL> def canonical_aliases ( orig_aliases_dict ) : <EOL> lowercase_aliases_dict = { } <EOL> for orig_namespace in orig_aliases_dict : <EOL> lowercase_namespace = clean_id ( orig_namespace . lower ( ) ) <EOL> if lowercase_namespace == "<STR_LIT>" : <EOL> lowercase_aliases_dict [ lowercase_namespace ] = [ clean_id ( doi . lower ( ) ) for doi in orig_aliases_dict [ orig_namespace ] ] <EOL> else : <EOL> lowercase_aliases_dict [ lowercase_namespace ] = [ clean_id ( nid ) for nid in orig_aliases_dict [ orig_namespace ] ] <EOL> return lowercase_aliases_dict <EOL> def merge_alias_dicts ( aliases1 , aliases2 ) : <EOL> merged_aliases = copy . deepcopy ( aliases1 ) <EOL> for ns , nid_list in aliases2 . iteritems ( ) : <EOL> for nid in nid_list : <EOL> try : <EOL> if not nid in merged_aliases [ ns ] : <EOL> merged_aliases [ ns ] . append ( nid ) <EOL> except KeyError : <EOL> merged_aliases [ ns ] = [ nid ] <EOL> return merged_aliases <EOL> def matches_alias ( product1 , product2 , exclude = [ ] ) : <EOL> alias_tuple_list1 = [ alias_row . my_alias_tuple_for_comparing for alias_row in product1 . alias_rows ] <EOL> alias_tuple_list2 = [ alias_row . my_alias_tuple_for_comparing for alias_row in product2 . alias_rows ] <EOL> has_matches = False <EOL> for alias_tuple1 in alias_tuple_list1 : <EOL> if alias_tuple1 : <EOL> ( ns , nid ) = alias_tuple1 <EOL> if alias_tuple1 in alias_tuple_list2 and ns not in exclude : <EOL> has_matches = True <EOL> return has_matches <EOL> class AliasRow ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> tiid = db . Column ( db . Text , db . ForeignKey ( '<STR_LIT>' ) , primary_key = True ) <EOL> namespace = db . Column ( db . Text , primary_key = True ) <EOL> nid = db . Column ( db . Text , primary_key = True ) <EOL> collected_date = db . Column ( db . DateTime ( ) ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> if "<STR_LIT>" not in kwargs : <EOL> self . collected_date = datetime . datetime . utcnow ( ) <EOL> super ( AliasRow , self ) . __init__ ( ** kwargs ) <EOL> @ cached_property <EOL> def alias_tuple ( self ) : <EOL> return ( self . namespace , self . nid ) <EOL> @ cached_property <EOL> def my_alias_tuple_for_comparing ( self ) : <EOL> return clean_alias_tuple_for_comparing ( self . namespace , self . nid ) <EOL> def is_equivalent_alias ( self , given_namespace , given_nid ) : <EOL> if not given_nid : <EOL> return False <EOL> given_clean_alias = clean_alias_tuple_for_comparing ( given_namespace , given_nid ) <EOL> if not given_clean_alias : <EOL> return False <EOL> return given_clean_alias == self . my_alias_tuple_for_comparing <EOL> class Aliases ( object ) : <EOL> def __init__ ( self , alias_rows ) : <EOL> ignore_namepaces = [ "<STR_LIT>" ] <EOL> self . tiid = None <EOL> for alias_row in alias_rows : <EOL> if alias_row . namespace not in ignore_namepaces : <EOL> self . tiid = alias_row . tiid <EOL> try : <EOL> getattr ( self , alias_row . namespace ) . append ( alias_row . nid ) <EOL> except AttributeError : <EOL> setattr ( self , alias_row . namespace , [ alias_row . nid ] ) <EOL> @ cached_property <EOL> def best_url ( self ) : <EOL> if self . display_doi : <EOL> return u"<STR_LIT>" + self . display_doi <EOL> if self . display_pmid : <EOL> return u"<STR_LIT>" + self . display_pmid <EOL> if self . display_pmc : <EOL> return u"<STR_LIT>" + self . display_pmc <EOL> if self . resolved_url : <EOL> return self . resolved_url <EOL> try : <EOL> return self . url [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> return None <EOL> @ cached_property <EOL> def display_best_url ( self ) : <EOL> return self . best_url <EOL> @ cached_property <EOL> def display_pmid ( self ) : <EOL> try : <EOL> return self . pmid [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> return None <EOL> @ cached_property <EOL> def display_pmc ( self ) : <EOL> try : <EOL> return self . pmc [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> return None <EOL> @ cached_property <EOL> def display_doi ( self ) : <EOL> try : <EOL> return self . doi [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> return None <EOL> @ cached_property <EOL> def display_arxiv ( self ) : <EOL> try : <EOL> return self . arxiv [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> return None <EOL> @ cached_property <EOL> def has_formal_alias ( self ) : <EOL> if self . display_arxiv or self . display_doi or self . display_pmid or self . display_pmc : <EOL> return True <EOL> else : <EOL> return False <EOL> @ cached_property <EOL> def resolved_url ( self ) : <EOL> try : <EOL> for url in self . url : <EOL> if "<STR_LIT>" in url : <EOL> continue <EOL> elif "<STR_LIT>" in url : <EOL> continue <EOL> elif "<STR_LIT>" in url : <EOL> continue <EOL> elif "<STR_LIT>" in url : <EOL> continue <EOL> elif "<STR_LIT>" in url : <EOL> continue <EOL> else : <EOL> return url <EOL> return self . url [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> return None <EOL> def get_genre ( self ) : <EOL> return self . _guess_genre_and_host_from_aliases ( ) [ <NUM_LIT:0> ] <EOL> def get_host ( self ) : <EOL> return self . _guess_genre_and_host_from_aliases ( ) [ <NUM_LIT:1> ] <EOL> def _guess_genre_and_host_from_aliases ( self ) : <EOL> """<STR_LIT>""" <EOL> genre = "<STR_LIT>" <EOL> host = "<STR_LIT>" <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> joined_doi_string = "<STR_LIT>" . join ( self . doi ) . lower ( ) <EOL> if "<STR_LIT>" in joined_doi_string : <EOL> genre = "<STR_LIT>" <EOL> host = "<STR_LIT>" <EOL> elif "<STR_LIT>" in joined_doi_string : <EOL> host = "<STR_LIT>" <EOL> genre = "<STR_LIT>" <EOL> else : <EOL> genre = "<STR_LIT>" <EOL> elif hasattr ( self , "<STR_LIT>" ) : <EOL> genre = "<STR_LIT>" <EOL> elif hasattr ( self , "<STR_LIT>" ) : <EOL> genre = "<STR_LIT>" <EOL> host = "<STR_LIT>" <EOL> elif hasattr ( self , "<STR_LIT>" ) : <EOL> genre = "<STR_LIT>" <EOL> host = "<STR_LIT>" <EOL> elif hasattr ( self , "<STR_LIT>" ) : <EOL> genre = "<STR_LIT>" <EOL> host = "<STR_LIT>" <EOL> elif hasattr ( self , "<STR_LIT:url>" ) : <EOL> joined_url_string = "<STR_LIT>" . join ( self . url ) . lower ( ) <EOL> if "<STR_LIT>" in joined_url_string : <EOL> genre = "<STR_LIT>" <EOL> host = "<STR_LIT>" <EOL> elif "<STR_LIT>" in joined_url_string : <EOL> genre = "<STR_LIT>" <EOL> host = "<STR_LIT>" <EOL> elif ( "<STR_LIT>" in joined_url_string ) or ( "<STR_LIT>" in joined_url_string ) : <EOL> genre = "<STR_LIT>" <EOL> host = "<STR_LIT>" <EOL> elif "<STR_LIT>" in joined_url_string : <EOL> genre = "<STR_LIT>" <EOL> host = "<STR_LIT>" <EOL> else : <EOL> genre = "<STR_LIT>" <EOL> return genre , host <EOL> def to_dict ( self ) : <EOL> ret = dict_from_dir ( self ) <EOL> return ret </s>
