Skip to content

Commit

Permalink
Merge pull request #2357 from SEED-platform/refactor/update_with_buil…
Browse files Browse the repository at this point in the history
…dingsync

refactor(building_sync): handle custom exception
  • Loading branch information
adrian-lara committed Aug 5, 2020
2 parents 4a0464c + 1549c7f commit fb2ae06
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
24 changes: 14 additions & 10 deletions seed/building_sync/building_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
etree.register_namespace('auc', BUILDINGSYNC_URI)


class ParsingError(Exception):
pass


class BuildingSync(object):
BUILDINGSYNC_V2_0 = '2.0'
VERSION_MAPPINGS_DICT = {
Expand All @@ -57,7 +61,7 @@ def import_file(self, source):
# save element tree
if isinstance(source, str):
if not os.path.isfile(source):
raise Exception("File not found: {}".format(source))
raise ParsingError("File not found: {}".format(source))
with open(source) as f:
self.element_tree = etree.parse(f)
else:
Expand Down Expand Up @@ -109,7 +113,7 @@ def init_tree(self, version=BUILDINGSYNC_V2_0):
:param version: string, should be one of the valid BuildingSync versions
"""
if version not in self.VERSION_MAPPINGS_DICT:
raise Exception(f'Invalid version "{version}"')
raise ParsingError(f'Invalid version "{version}"')

xml_string = '''<?xml version="1.0"?>
<auc:BuildingSync xmlns:auc="http://buildingsync.net/schemas/bedes-auc/2019" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://buildingsync.net/schemas/bedes-auc/2019 https://raw.githubusercontent.com/BuildingSync/schema/v{}/BuildingSync.xsd">
Expand Down Expand Up @@ -177,7 +181,7 @@ def get_schema(cls, version):
if version == cls.BUILDINGSYNC_V2_0:
schema_path = os.path.join(schema_dir, 'BuildingSync_v2_0.xsd')
else:
raise Exception(f'Unknown file version "{version}"')
raise ParsingError(f'Unknown file version "{version}"')

return xmlschema.XMLSchema(schema_path)

Expand Down Expand Up @@ -300,7 +304,7 @@ def process(self, table_mappings=None):
# prcess_struct = new_use_case (from Building Selection Tool)
base_mapping = self.VERSION_MAPPINGS_DICT.get(self.version)
if base_mapping is None:
raise Exception(f'Version of BuildingSync object is not supported: "{self.version}"')
raise ParsingError(f'Version of BuildingSync object is not supported: "{self.version}"')

# convert the table_mappings into a buildingsync mapping
custom_mapping = None
Expand All @@ -319,7 +323,7 @@ def process_property_xpaths(self, table_mappings=None):
"""
base_mapping = self.VERSION_MAPPINGS_DICT.get(self.version)
if base_mapping is None:
raise Exception(f'Version of BuildingSync object is not supported: "{self.version}"')
raise ParsingError(f'Version of BuildingSync object is not supported: "{self.version}"')

# convert the table_mappings into a buildingsync mapping
custom_mapping = None
Expand Down Expand Up @@ -353,7 +357,7 @@ def _parse_version(self):
:return: string, schema version (raises Exception when not found or invalid)
"""
if self.element_tree is None:
raise Exception('A file must first be imported with import method')
raise ParsingError('A file must first be imported with import method')

# first check if it's a file form Audit Template Tool and infer the version
# Currently ATT doesn't include a schemaLocation so this is necessary
Expand All @@ -362,7 +366,7 @@ def _parse_version(self):

bsync_element = self.element_tree.getroot()
if not bsync_element.tag.endswith('BuildingSync'):
raise Exception('Expected BuildingSync element as root element in xml')
raise ParsingError('Expected BuildingSync element as root element in xml')

# attempt to parse the version from the xsi:schemaLocation
schemas = bsync_element.get('{http://www.w3.org/2001/XMLSchema-instance}schemaLocation', '').split()
Expand All @@ -375,9 +379,9 @@ def _parse_version(self):
if parsed_version in self.VERSION_MAPPINGS_DICT:
return parsed_version

raise Exception(f'Unsupported BuildingSync schema version "{parsed_version}". Supported versions: {list(self.VERSION_MAPPINGS_DICT.keys())}')
raise ParsingError(f'Unsupported BuildingSync schema version "{parsed_version}". Supported versions: {list(self.VERSION_MAPPINGS_DICT.keys())}')

raise Exception('Invalid or missing schema specification. Expected a valid BuildingSync schemaLocation in the BuildingSync element. For example: https://raw.githubusercontent.com/BuildingSync/schema/v<schema version here>/BuildingSync.xsd')
raise ParsingError('Invalid or missing schema specification. Expected a valid BuildingSync schemaLocation in the BuildingSync element. For example: https://raw.githubusercontent.com/BuildingSync/schema/v<schema version here>/BuildingSync.xsd')

def _is_from_audit_template_tool(self):
"""Determines if the source file is from audit template tool
Expand All @@ -397,5 +401,5 @@ def _is_from_audit_template_tool(self):

def get_base_mapping(self):
if not self.version:
raise Exception('You must call import_file to determine the version first')
raise ParsingError('You must call import_file to determine the version first')
return copy.deepcopy(self.VERSION_MAPPINGS_DICT[self.version])
15 changes: 9 additions & 6 deletions seed/models/building_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from django.db import models

from seed.building_sync.building_sync import BuildingSync
from seed.building_sync.building_sync import BuildingSync, ParsingError
from seed.hpxml.hpxml import HPXML as HPXMLParser
from seed.lib.merging.merging import merge_state
from seed.models import (
Expand Down Expand Up @@ -142,11 +142,14 @@ def process(self, organization_id, cycle, property_view=None):
return False, None, None, "File format was not one of: {}".format(acceptable_file_types)

parser = Parser()
parser.import_file(self.file.path)
parser_args = []
parser_kwargs = {}
# TODO: use table_mappings for BuildingSync process method
data, messages = parser.process(*parser_args, **parser_kwargs)
try:
parser.import_file(self.file.path)
parser_args = []
parser_kwargs = {}
# TODO: use table_mappings for BuildingSync process method
data, messages = parser.process(*parser_args, **parser_kwargs)
except ParsingError as e:
return False, None, None, [str(e)]

if len(messages['errors']) > 0 or not data:
return False, None, None, messages
Expand Down

0 comments on commit fb2ae06

Please sign in to comment.