<s> import datetime <EOL> import json <EOL> from os . path import join <EOL> import re <EOL> import unicodecsv <EOL> from openelex . models import RawResult <EOL> from . state import StateBase <EOL> class BaseLoader ( StateBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( BaseLoader , self ) . __init__ ( ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> def run ( self , mapping ) : <EOL> """<STR_LIT>""" <EOL> self . mapping = mapping <EOL> self . source = mapping [ '<STR_LIT>' ] <EOL> self . timestamp = datetime . datetime . now ( ) <EOL> self . election_id = mapping [ '<STR_LIT>' ] <EOL> self . delete_previously_loaded ( ) <EOL> self . load ( ) <EOL> def delete_previously_loaded ( self ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" % self . source ) <EOL> result_count = RawResult . objects . filter ( source = self . source ) . count ( ) <EOL> if result_count > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % result_count ) <EOL> RawResult . objects . filter ( source = self . source ) . delete ( ) <EOL> def load ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def jurisdiction_mappings ( self , headers ) : <EOL> """<STR_LIT>""" <EOL> filename = join ( self . mappings_dir , self . state + '<STR_LIT>' ) <EOL> with open ( filename , '<STR_LIT>' ) as csvfile : <EOL> reader = unicodecsv . DictReader ( csvfile , fieldnames = headers ) <EOL> mappings = json . dumps ( [ row for row in reader ] ) <EOL> return json . loads ( mappings ) <EOL> @ property <EOL> def _file_handle ( self ) : <EOL> return open ( join ( self . cache . abspath , self . source ) , '<STR_LIT>' ) <EOL> @ property <EOL> def _xls_file_path ( self ) : <EOL> return join ( self . cache . abspath , self . source ) <EOL> def _build_common_election_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> year = int ( re . search ( r'<STR_LIT>' , self . election_id ) . group ( ) ) <EOL> elecs = self . datasource . elections ( year ) [ year ] <EOL> elec_meta = [ e for e in elecs if e [ '<STR_LIT>' ] == self . election_id ] [ <NUM_LIT:0> ] <EOL> kwargs = { <EOL> '<STR_LIT>' : self . timestamp , <EOL> '<STR_LIT>' : self . timestamp , <EOL> '<STR_LIT:source>' : self . source , <EOL> '<STR_LIT>' : self . election_id , <EOL> '<STR_LIT:state>' : self . state . upper ( ) , <EOL> '<STR_LIT>' : datetime . datetime . strptime ( elec_meta [ '<STR_LIT>' ] , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : datetime . datetime . strptime ( elec_meta [ '<STR_LIT>' ] , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : elec_meta [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : elec_meta [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : elec_meta [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : elec_meta [ '<STR_LIT>' ] , <EOL> } <EOL> return kwargs </s>
