<s> import os <EOL> import os . path <EOL> import urllib <EOL> import urlparse <EOL> from zipfile import ZipFile <EOL> from openelex . base . fetch import BaseFetcher <EOL> from openelex . us . ri . datasource import Datasource <EOL> class FetchResults ( BaseFetcher ) : <EOL> def __init__ ( self ) : <EOL> super ( FetchResults , self ) . __init__ ( ) <EOL> self . _datasource = Datasource ( ) <EOL> self . _fetched = set ( ) <EOL> def fetch ( self , url , fname = None , overwrite = False ) : <EOL> if url in self . _fetched : <EOL> return <EOL> if url . endswith ( '<STR_LIT>' ) and url != '<STR_LIT>' : <EOL> zip_fname = self . _local_zip_file_name ( url ) <EOL> super ( FetchResults , self ) . fetch ( url , zip_fname , overwrite ) <EOL> self . _extract_zip ( url , zip_fname , overwrite ) <EOL> else : <EOL> super ( FetchResults , self ) . fetch ( url , fname , overwrite ) <EOL> self . _fetched . add ( url ) <EOL> def _local_zip_file_name ( self , url ) : <EOL> """<STR_LIT>""" <EOL> parsed = urlparse . urlsplit ( url ) <EOL> fname = parsed . path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> return os . path . join ( self . cache . abspath , fname ) <EOL> def _extract_zip ( self , url , zip_fname = None , overwrite = False , remove = True ) : <EOL> if zip_fname is None : <EOL> zip_fname = self . _local_zip_file_name ( url ) <EOL> with ZipFile ( zip_fname , '<STR_LIT:r>' ) as zipf : <EOL> for mapping in self . _datasource . mappings_for_url ( url ) : <EOL> local_file_name = os . path . join ( self . cache . abspath , <EOL> mapping [ '<STR_LIT>' ] ) <EOL> if not os . path . exists ( local_file_name ) : <EOL> zipf . extract ( mapping [ '<STR_LIT>' ] , <EOL> self . cache . abspath ) <EOL> extracted_file_name = os . path . join ( self . cache . abspath , <EOL> mapping [ '<STR_LIT>' ] ) <EOL> os . rename ( extracted_file_name , local_file_name ) <EOL> print "<STR_LIT>" % local_file_name <EOL> else : <EOL> print "<STR_LIT>" % local_file_name <EOL> if remove : <EOL> os . remove ( zip_fname ) </s>
