<s> """<STR_LIT>""" <EOL> import re <EOL> from openelex . base . datasource import BaseDatasource <EOL> class Datasource ( BaseDatasource ) : <EOL> base_url = "<STR_LIT>" <EOL> def mappings ( self , year = None ) : <EOL> """<STR_LIT>""" <EOL> mappings = [ ] <EOL> for yr , elecs in self . elections ( year ) . items ( ) : <EOL> mappings . extend ( self . _build_metadata ( yr , elecs ) ) <EOL> return mappings <EOL> def target_urls ( self , year = None ) : <EOL> "<STR_LIT>" <EOL> return [ item [ '<STR_LIT>' ] for item in self . mappings ( year ) ] <EOL> def filename_url_pairs ( self , year = None ) : <EOL> return [ ( item [ '<STR_LIT>' ] , item [ '<STR_LIT>' ] ) <EOL> for item in self . mappings ( year ) ] <EOL> def _races_by_type ( self , elections ) : <EOL> "<STR_LIT>" <EOL> races = { <EOL> '<STR_LIT>' : None , <EOL> } <EOL> for elec in elections : <EOL> rtype = self . _race_type ( elec ) <EOL> elec [ '<STR_LIT>' ] = self . _election_slug ( elec ) <EOL> races [ rtype ] = elec <EOL> return races [ '<STR_LIT>' ] , races [ '<STR_LIT>' ] , races [ '<STR_LIT>' ] <EOL> def _race_type ( self , election ) : <EOL> if election [ '<STR_LIT>' ] : <EOL> return '<STR_LIT>' <EOL> return election [ '<STR_LIT>' ] . lower ( ) <EOL> def _build_metadata ( self , year , elections ) : <EOL> year_int = int ( year ) <EOL> if year_int == <NUM_LIT> : <EOL> general , primary , special = self . _races_by_type ( elections ) <EOL> meta = [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" . join ( ( general [ '<STR_LIT>' ] . replace ( '<STR_LIT:->' , '<STR_LIT>' ) , self . state , "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : self . _get_2002_source_urls ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT:name>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : general [ '<STR_LIT>' ] <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" . join ( ( primary [ '<STR_LIT>' ] . replace ( '<STR_LIT:->' , '<STR_LIT>' ) , self . state , "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : self . _get_2002_source_urls ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT:name>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : primary [ '<STR_LIT>' ] <EOL> } <EOL> ] <EOL> else : <EOL> meta = self . _state_leg_meta ( year , elections ) + self . _county_meta ( year , elections ) <EOL> if year_int == <NUM_LIT> : <EOL> general , primary , special = self . _races_by_type ( elections ) <EOL> meta . append ( { <EOL> "<STR_LIT>" : "<STR_LIT>" . join ( ( primary [ '<STR_LIT>' ] . replace ( '<STR_LIT:->' , '<STR_LIT>' ) , self . state , "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT:name>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : primary [ '<STR_LIT>' ] <EOL> } ) <EOL> elif year_int == <NUM_LIT> : <EOL> meta . append ( self . _special_meta_2008 ( elections ) ) <EOL> return meta <EOL> def _state_leg_meta ( self , year , elections ) : <EOL> payload = [ ] <EOL> meta = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> } <EOL> general , primary , special = self . _races_by_type ( elections ) <EOL> general_url = self . _build_state_leg_url ( year ) <EOL> general_filename = self . _generate_state_leg_filename ( general_url , general [ '<STR_LIT>' ] ) <EOL> gen_meta = meta . copy ( ) <EOL> gen_meta . update ( { <EOL> '<STR_LIT>' : general_url , <EOL> '<STR_LIT>' : general_filename , <EOL> '<STR_LIT>' : general [ '<STR_LIT>' ] <EOL> } ) <EOL> payload . append ( gen_meta ) <EOL> if primary and int ( year ) > <NUM_LIT> : <EOL> for party in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> pri_meta = meta . copy ( ) <EOL> primary_url = self . _build_state_leg_url ( year , party ) <EOL> primary_filename = self . _generate_state_leg_filename ( primary_url , primary [ '<STR_LIT>' ] ) <EOL> pri_meta . update ( { <EOL> '<STR_LIT>' : primary_url , <EOL> '<STR_LIT>' : primary_filename , <EOL> '<STR_LIT>' : primary [ '<STR_LIT>' ] <EOL> } ) <EOL> payload . append ( pri_meta ) <EOL> return payload <EOL> def _build_state_leg_url ( self , year , party = "<STR_LIT>" ) : <EOL> tmplt = self . base_url + "<STR_LIT>" <EOL> kwargs = { '<STR_LIT>' : year } <EOL> year_int = int ( year ) <EOL> if party and year_int > <NUM_LIT> : <EOL> kwargs [ '<STR_LIT>' ] = party <EOL> if year_int == <NUM_LIT> : <EOL> tmplt += "<STR_LIT>" <EOL> else : <EOL> tmplt += "<STR_LIT>" <EOL> else : <EOL> if year_int in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> tmplt += "<STR_LIT>" <EOL> else : <EOL> tmplt += "<STR_LIT>" <EOL> tmplt += "<STR_LIT>" <EOL> return tmplt % kwargs <EOL> def _generate_state_leg_filename ( self , url , start_date ) : <EOL> bits = [ <EOL> start_date . replace ( '<STR_LIT:->' , '<STR_LIT>' ) , <EOL> self . state . lower ( ) , <EOL> ] <EOL> matches = self . _apply_party_racetype_regex ( url ) <EOL> if matches [ '<STR_LIT>' ] : <EOL> bits . append ( matches [ '<STR_LIT>' ] . lower ( ) ) <EOL> bits . extend ( [ <EOL> matches [ '<STR_LIT>' ] . lower ( ) , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> name = "<STR_LIT>" . join ( bits ) <EOL> return name <EOL> def _county_meta ( self , year , elections ) : <EOL> payload = [ ] <EOL> general , primary , special = self . _races_by_type ( elections ) <EOL> for jurisdiction in self . _jurisdictions ( ) : <EOL> meta = { <EOL> '<STR_LIT>' : jurisdiction [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : jurisdiction [ '<STR_LIT:name>' ] , <EOL> } <EOL> county = jurisdiction [ '<STR_LIT>' ] <EOL> for precinct_val in ( True , False ) : <EOL> general_url = self . _build_county_url ( year , county , precinct = precinct_val ) <EOL> general_filename = self . _generate_county_filename ( general_url , general [ '<STR_LIT>' ] , jurisdiction ) <EOL> gen_meta = meta . copy ( ) <EOL> gen_meta . update ( { <EOL> '<STR_LIT>' : general_url , <EOL> '<STR_LIT>' : general_filename , <EOL> '<STR_LIT>' : general [ '<STR_LIT>' ] <EOL> } ) <EOL> payload . append ( gen_meta ) <EOL> if primary and int ( year ) > <NUM_LIT> : <EOL> for party in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> for precinct_val in ( True , False ) : <EOL> pri_meta = meta . copy ( ) <EOL> primary_url = self . _build_county_url ( year , county , party , precinct_val ) <EOL> primary_filename = self . _generate_county_filename ( primary_url , primary [ '<STR_LIT>' ] , jurisdiction ) <EOL> pri_meta . update ( { <EOL> '<STR_LIT>' : primary_url , <EOL> '<STR_LIT>' : primary_filename , <EOL> '<STR_LIT>' : primary [ '<STR_LIT>' ] <EOL> } ) <EOL> payload . append ( pri_meta ) <EOL> return payload <EOL> def _build_county_url ( self , year , name , party = '<STR_LIT>' , precinct = False ) : <EOL> url_kwargs = { <EOL> '<STR_LIT>' : year , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> if name == "<STR_LIT>" and int ( year ) in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> name = "<STR_LIT>" <EOL> tmplt = self . base_url + name <EOL> if precinct : <EOL> tmplt += "<STR_LIT>" <EOL> else : <EOL> if int ( year ) not in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> tmplt += "<STR_LIT>" <EOL> if party : <EOL> url_kwargs [ '<STR_LIT>' ] = party <EOL> url_kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> tmplt += "<STR_LIT>" <EOL> if int ( year ) in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> tmplt += "<STR_LIT>" <EOL> else : <EOL> tmplt += "<STR_LIT>" <EOL> return tmplt % url_kwargs <EOL> def _generate_county_filename ( self , url , start_date , jurisdiction ) : <EOL> bits = [ <EOL> start_date . replace ( '<STR_LIT:->' , '<STR_LIT>' ) , <EOL> self . state , <EOL> ] <EOL> matches = self . _apply_party_racetype_regex ( url ) <EOL> if matches [ '<STR_LIT>' ] : <EOL> bits . append ( matches [ '<STR_LIT>' ] . lower ( ) ) <EOL> bits . extend ( [ <EOL> matches [ '<STR_LIT>' ] . lower ( ) , <EOL> jurisdiction [ '<STR_LIT>' ] . lower ( ) <EOL> ] ) <EOL> if '<STR_LIT>' in url . lower ( ) : <EOL> bits . append ( '<STR_LIT>' ) <EOL> filename = "<STR_LIT>" . join ( bits ) + '<STR_LIT>' <EOL> return filename <EOL> def _apply_party_racetype_regex ( self , url ) : <EOL> if re . search ( r'<STR_LIT>' , url ) : <EOL> pattern = re . compile ( r"""<STR_LIT>""" , re . IGNORECASE | re . VERBOSE ) <EOL> else : <EOL> pattern = re . compile ( r"""<STR_LIT>""" , re . IGNORECASE | re . VERBOSE ) <EOL> matches = re . search ( pattern , url ) . groupdict ( ) <EOL> return matches <EOL> def _get_2002_source_urls ( self , race_type = '<STR_LIT>' ) : <EOL> urls = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> if race_type : <EOL> return urls [ race_type ] <EOL> else : <EOL> return urls . values ( ) <EOL> def _generate_2002_filename ( self , url ) : <EOL> if url . endswith ( '<STR_LIT>' ) : <EOL> filename = "<STR_LIT>" <EOL> else : <EOL> filename = "<STR_LIT>" <EOL> return filename <EOL> def _jurisdictions ( self ) : <EOL> """<STR_LIT>""" <EOL> m = self . jurisdiction_mappings ( ) <EOL> mappings = [ x for x in m if x [ '<STR_LIT>' ] != "<STR_LIT>" ] <EOL> return mappings <EOL> def _special_meta_2008 ( self , elections ) : <EOL> """<STR_LIT>""" <EOL> general , primary , special = self . _races_by_type ( elections ) <EOL> filename_bits = [ <EOL> special [ '<STR_LIT>' ] . replace ( '<STR_LIT:->' , '<STR_LIT>' ) , <EOL> self . state , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> return { <EOL> "<STR_LIT>" : <EOL> "<STR_LIT>" . join ( filename_bits ) + "<STR_LIT>" , <EOL> "<STR_LIT>" : special [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT:name>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : special [ '<STR_LIT>' ] <EOL> } </s>
