<s> """<STR_LIT>""" <EOL> from os . path import join <EOL> import json <EOL> import unicodecsv <EOL> import urlparse <EOL> import requests <EOL> from openelex import PROJECT_ROOT <EOL> from openelex . base . datasource import BaseDatasource <EOL> from openelex . lib import build_github_url <EOL> class Datasource ( BaseDatasource ) : <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>' ] , self . _url_for_fetch ( item ) ) <EOL> for item in self . mappings ( year ) ] <EOL> def _build_metadata ( self , year , elections ) : <EOL> meta = [ ] <EOL> year_int = int ( year ) <EOL> for election in elections : <EOL> if election [ '<STR_LIT>' ] : <EOL> results = [ x for x in self . _url_paths ( ) if x [ '<STR_LIT:date>' ] == election [ '<STR_LIT>' ] and x [ '<STR_LIT>' ] == True ] <EOL> else : <EOL> results = [ x for x in self . _url_paths ( ) if x [ '<STR_LIT:date>' ] == election [ '<STR_LIT>' ] and x [ '<STR_LIT>' ] == False ] <EOL> for result in results : <EOL> if election [ '<STR_LIT>' ] : <EOL> raw_url = election [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> else : <EOL> raw_url = None <EOL> generated_filename = self . _generate_filename ( election [ '<STR_LIT>' ] , election [ '<STR_LIT>' ] , result ) <EOL> meta . append ( { <EOL> "<STR_LIT>" : generated_filename , <EOL> "<STR_LIT>" : raw_url , <EOL> "<STR_LIT>" : build_github_url ( self . state , generated_filename ) , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT:name>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : election [ '<STR_LIT>' ] <EOL> } ) <EOL> return meta <EOL> def _generate_filename ( self , start_date , election_type , result ) : <EOL> if result [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> office = result [ '<STR_LIT>' ] <EOL> else : <EOL> office = result [ '<STR_LIT>' ] + '<STR_LIT>' + result [ '<STR_LIT>' ] <EOL> if result [ '<STR_LIT>' ] : <EOL> election_type = '<STR_LIT>' + election_type <EOL> else : <EOL> election_type = election_type + '<STR_LIT>' <EOL> bits = [ <EOL> start_date . replace ( '<STR_LIT:->' , '<STR_LIT>' ) , <EOL> self . state . lower ( ) , <EOL> election_type , <EOL> office <EOL> ] <EOL> if office == '<STR_LIT>' : <EOL> bits . remove ( office ) <EOL> name = "<STR_LIT>" . join ( bits ) + '<STR_LIT>' <EOL> return name <EOL> def _jurisdictions ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . jurisdiction_mappings ( ) <EOL> def _url_for_fetch ( self , mapping ) : <EOL> if mapping [ '<STR_LIT>' ] : <EOL> return mapping [ '<STR_LIT>' ] <EOL> else : <EOL> return mapping [ '<STR_LIT>' ] </s>
