<s> import xmltodict <EOL> from collections import Iterable <EOL> from data_importer . models import ROW_DELIMITER <EOL> from seed . models import ( <EOL> BuildingSnapshot , <EOL> Meter , <EOL> TimeSeries , <EOL> CanonicalBuilding , <EOL> set_initial_sources , <EOL> GREEN_BUTTON_BS , <EOL> ) <EOL> import seed . models <EOL> from audit_logs . models import AuditLog <EOL> from datetime import datetime <EOL> def energy_type ( service_category ) : <EOL> """<STR_LIT>""" <EOL> service_category = int ( service_category ) <EOL> category_mapping = { <EOL> <NUM_LIT:0> : seed . models . ELECTRICITY , <EOL> <NUM_LIT:1> : seed . models . NATURAL_GAS , <EOL> } <EOL> if service_category in category_mapping : <EOL> return category_mapping [ service_category ] <EOL> else : <EOL> return None <EOL> def energy_units ( uom ) : <EOL> """<STR_LIT>""" <EOL> uom = int ( uom ) <EOL> unit_mapping = { <EOL> <NUM_LIT> : seed . models . WATT_HOURS , <EOL> <NUM_LIT> : seed . models . THERMS <EOL> } <EOL> if uom in unit_mapping : <EOL> return unit_mapping [ uom ] <EOL> else : <EOL> return None <EOL> def as_collection ( val ) : <EOL> """<STR_LIT>""" <EOL> is_atomic = ( <EOL> isinstance ( val , basestring ) or <EOL> isinstance ( val , dict ) or <EOL> ( not isinstance ( val , Iterable ) ) <EOL> ) <EOL> if val is None : <EOL> return None <EOL> elif is_atomic : <EOL> return [ val ] <EOL> else : <EOL> return val <EOL> def interval_data ( reading_xml_data ) : <EOL> """<STR_LIT>""" <EOL> cost = reading_xml_data [ '<STR_LIT>' ] <EOL> value = reading_xml_data [ '<STR_LIT:value>' ] <EOL> time_period = reading_xml_data [ '<STR_LIT>' ] <EOL> start_time = time_period [ '<STR_LIT:start>' ] <EOL> duration = time_period [ '<STR_LIT>' ] <EOL> result = { <EOL> '<STR_LIT>' : cost , <EOL> '<STR_LIT:value>' : value , <EOL> '<STR_LIT>' : start_time , <EOL> '<STR_LIT>' : duration <EOL> } <EOL> return result <EOL> def meter_data ( raw_meter_meta ) : <EOL> """<STR_LIT>""" <EOL> params_data = raw_meter_meta [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] <EOL> currency = params_data [ '<STR_LIT>' ] <EOL> power_of_ten_multiplier = params_data [ '<STR_LIT>' ] <EOL> uom = params_data [ '<STR_LIT>' ] <EOL> result = { <EOL> '<STR_LIT>' : currency , <EOL> '<STR_LIT>' : power_of_ten_multiplier , <EOL> '<STR_LIT>' : uom <EOL> } <EOL> return result <EOL> def interval_block_data ( ib_xml_data ) : <EOL> """<STR_LIT>""" <EOL> interval = ib_xml_data [ '<STR_LIT>' ] <EOL> raw_readings_data = as_collection ( ib_xml_data [ '<STR_LIT>' ] ) <EOL> readings_data = [ interval_data ( data ) for data in raw_readings_data ] <EOL> block_data = { <EOL> '<STR_LIT>' : interval [ '<STR_LIT:start>' ] , <EOL> '<STR_LIT>' : interval [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : readings_data <EOL> } <EOL> return block_data <EOL> def building_data ( xml_data ) : <EOL> """<STR_LIT>""" <EOL> entries = xml_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> info_entry = next ( entry for entry in entries ) <EOL> meta_data , meter_reading_decl , meter_params , reading_intervals = entries <EOL> address = meta_data [ '<STR_LIT:title>' ] [ '<STR_LIT>' ] <EOL> s_category = info_entry [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> raw_interval_block = reading_intervals [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] <EOL> block_data = interval_block_data ( raw_interval_block ) <EOL> m_data = meter_data ( meter_params ) <EOL> result = { <EOL> '<STR_LIT:address>' : address , <EOL> '<STR_LIT>' : s_category , <EOL> '<STR_LIT>' : m_data , <EOL> '<STR_LIT>' : block_data <EOL> } <EOL> return result <EOL> def create_models ( data , import_file ) : <EOL> """<STR_LIT>""" <EOL> import_file . cached_first_row = ROW_DELIMITER . join ( [ "<STR_LIT:address>" ] ) <EOL> import_file . cached_second_to_fifth_row = ROW_DELIMITER . join ( <EOL> [ data [ '<STR_LIT:address>' ] ] <EOL> ) <EOL> import_file . save ( ) <EOL> raw_bs = BuildingSnapshot ( ) <EOL> raw_bs . import_file = import_file <EOL> raw_bs . save ( ) <EOL> super_org = import_file . import_record . super_organization <EOL> raw_bs . super_organization = super_org <EOL> set_initial_sources ( raw_bs ) <EOL> raw_bs . address_line_1 = data [ '<STR_LIT:address>' ] <EOL> raw_bs . source_type = GREEN_BUTTON_BS <EOL> raw_bs . save ( ) <EOL> cb = CanonicalBuilding . objects . create ( canonical_snapshot = raw_bs ) <EOL> raw_bs . canonical_building = cb <EOL> raw_bs . save ( ) <EOL> AuditLog . objects . create ( <EOL> organization = import_file . import_record . super_organization , <EOL> user = import_file . import_record . owner , <EOL> content_object = cb , <EOL> action = "<STR_LIT>" , <EOL> action_note = "<STR_LIT>" , <EOL> ) <EOL> e_type = energy_type ( data [ '<STR_LIT>' ] ) <EOL> e_type_string = next ( <EOL> pair [ <NUM_LIT:1> ] for pair in seed . models . ENERGY_TYPES if pair [ <NUM_LIT:0> ] == e_type <EOL> ) <EOL> m_name = "<STR_LIT>" . format ( str ( raw_bs . id ) , e_type_string ) <EOL> m_energy_units = energy_units ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> meter = Meter . objects . create ( <EOL> name = m_name , energy_type = e_type , energy_units = m_energy_units <EOL> ) <EOL> meter . building_snapshot . add ( raw_bs ) <EOL> meter . save ( ) <EOL> for reading in data [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> start_time = int ( reading [ '<STR_LIT>' ] ) <EOL> duration = int ( reading [ '<STR_LIT>' ] ) <EOL> begin_time = datetime . fromtimestamp ( start_time ) <EOL> end_time = datetime . fromtimestamp ( start_time + duration ) <EOL> value = reading [ '<STR_LIT:value>' ] <EOL> cost = reading [ '<STR_LIT>' ] <EOL> new_ts = TimeSeries . objects . create ( <EOL> begin_time = begin_time , <EOL> end_time = end_time , <EOL> reading = value , <EOL> cost = cost <EOL> ) <EOL> new_ts . meter = meter <EOL> new_ts . save ( ) <EOL> return cb <EOL> def import_xml ( import_file ) : <EOL> """<STR_LIT>""" <EOL> xml_file = import_file . local_file <EOL> xml_string = xml_file . read ( ) <EOL> raw_data = xmltodict . parse ( xml_string ) <EOL> data = building_data ( raw_data ) <EOL> return create_models ( data , import_file ) </s>
