Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serialize): Implement smarter loading so real exceptions are visible #353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified honeybee_grasshopper_core/icon/HB Load Objects.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions honeybee_grasshopper_core/json/HB_Load_Objects.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.8.1",
"version": "1.8.2",
"nickname": "LoadObjects",
"outputs": [
[
Expand Down Expand Up @@ -29,7 +29,7 @@
}
],
"subcategory": "3 :: Serialize",
"code": "\nimport io\n\ntry: # import the core honeybee dependencies\n import honeybee.dictutil as hb_dict_util\n from honeybee.model import Model\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry: # import the core honeybee_energy dependencies\n import honeybee_energy.dictutil as energy_dict_util\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_energy:\\n\\t{}'.format(e))\n\ntry: # import the core honeybee_radiance dependencies\n import honeybee_radiance.dictutil as radiance_dict_util\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_radiance:\\n\\t{}'.format(e))\n\ntry: # import the core ladybug_{{cad}} dependencies\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, give_warning\n from ladybug_{{cad}}.config import units_system, tolerance\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\nimport json\n\n\ndef model_units_tolerance_check(model):\n \"\"\"Convert a model to the current {{Cad}} units and check the tolerance.\n\n Args:\n model: A honeybee Model, which will have its units checked.\n \"\"\"\n # check the model units\n if model.units != units_system():\n print('Imported model units \"{}\" do not match that of the current {{Cad}} '\n 'model units \"{}\"\\nThe model is being automatically converted '\n 'to the {{Cad}} doc units.'.format(model.units, units_system()))\n model.convert_to_units(units_system())\n\n # check that the model tolerance is not too far from the {{Cad}} tolerance\n if model.tolerance / tolerance >= 100:\n msg = 'Imported Model tolerance \"{}\" is significantly coarser than the ' \\\n 'current {{Cad}} model tolerance \"{}\".\\nIt is recommended that the ' \\\n '{{Cad}} document tolerance be changed to be coarser and this ' \\\n 'component is re-run.'.format(model.tolerance, tolerance)\n print msg\n give_warning(ghenv.Component, msg)\n\n\ndef version_check(data):\n \"\"\"Check the version of the object if it was included in the dictionary.\n\n This is most useful in cases of importing entire Models to make sure\n the Model isn't newer than the currently installed Honeybee.\n\n Args:\n data: Dictionary of the object, which optionally has the \"version\" key.\n \"\"\"\n if 'version' in data and data['version'] is not None:\n model_ver = tuple(int(d) for d in data['version'].split('.'))\n hb_ver = folders.honeybee_schema_version\n if model_ver > hb_ver:\n msg = 'Imported Model schema version \"{}\" is newer than that with the ' \\\n 'currently installed Honeybee \"{}\".\\nThe Model may fail to import ' \\\n 'or (worse) some newer features of the Model might not be imported ' \\\n 'without detection.'.format(data['version'], folders.honeybee_schema_version_str)\n print msg\n give_warning(ghenv.Component, msg)\n elif model_ver != hb_ver:\n msg = 'Imported Model schema version \"{}\" is older than that with the ' \\\n 'currently installed Honeybee \"{}\".\\nThe Model will be upgraded upon ' \\\n 'import.'.format(data['version'], folders.honeybee_schema_version_str)\n print msg\n\n\nif all_required_inputs(ghenv.Component) and _load:\n with io.open(_hb_file, encoding='utf-8') as inf:\n first_char = inf.read(1)\n second_char = inf.read(1)\n with io.open(_hb_file, encoding='utf-8') as inf:\n if second_char == '{':\n inf.read(1)\n data = json.load(inf)\n\n version_check(data) # try to check the version\n try:\n hb_objs = hb_dict_util.dict_to_object(data, False) # re-serialize as a core object\n if hb_objs is None: # try to re-serialize it as an energy object\n hb_objs = energy_dict_util.dict_to_object(data, False)\n if hb_objs is None: # try to re-serialize it as a radiance object\n hb_objs = radiance_dict_util.dict_to_object(data, False)\n elif isinstance(hb_objs, Model):\n model_units_tolerance_check(hb_objs)\n except ValueError: # no 'type' key; assume that its a group of objects\n hb_objs = []\n for hb_dict in data.values():\n hb_obj = hb_dict_util.dict_to_object(hb_dict, False) # re-serialize as a core object\n if hb_obj is None: # try to re-serialize it as an energy object\n hb_obj = energy_dict_util.dict_to_object(hb_dict, False)\n if hb_obj is None: # try to re-serialize it as a radiance object\n hb_obj = radiance_dict_util.dict_to_object(hb_dict, False)\n hb_objs.append(hb_obj)",
"code": "\nimport io\n\ntry: # import the core honeybee dependencies\n import honeybee.dictutil as hb_dict_util\n from honeybee.model import Model\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry: # import the core honeybee_energy dependencies\n import honeybee_energy.dictutil as energy_dict_util\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_energy:\\n\\t{}'.format(e))\n\ntry: # import the core honeybee_radiance dependencies\n import honeybee_radiance.dictutil as radiance_dict_util\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_radiance:\\n\\t{}'.format(e))\n\ntry: # import the core ladybug_{{cad}} dependencies\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, give_warning\n from ladybug_{{cad}}.config import units_system, tolerance\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\nimport json\n\n\ndef model_units_tolerance_check(model):\n \"\"\"Convert a model to the current {{Cad}} units and check the tolerance.\n\n Args:\n model: A honeybee Model, which will have its units checked.\n \"\"\"\n # check the model units\n if model.units != units_system():\n print('Imported model units \"{}\" do not match that of the current {{Cad}} '\n 'model units \"{}\"\\nThe model is being automatically converted '\n 'to the {{Cad}} doc units.'.format(model.units, units_system()))\n model.convert_to_units(units_system())\n\n # check that the model tolerance is not too far from the {{Cad}} tolerance\n if model.tolerance / tolerance >= 100:\n msg = 'Imported Model tolerance \"{}\" is significantly coarser than the ' \\\n 'current {{Cad}} model tolerance \"{}\".\\nIt is recommended that the ' \\\n '{{Cad}} document tolerance be changed to be coarser and this ' \\\n 'component is re-run.'.format(model.tolerance, tolerance)\n print msg\n give_warning(ghenv.Component, msg)\n\n\ndef version_check(data):\n \"\"\"Check the version of the object if it was included in the dictionary.\n\n This is most useful in cases of importing entire Models to make sure\n the Model isn't newer than the currently installed Honeybee.\n\n Args:\n data: Dictionary of the object, which optionally has the \"version\" key.\n \"\"\"\n if 'version' in data and data['version'] is not None:\n model_ver = tuple(int(d) for d in data['version'].split('.'))\n hb_ver = folders.honeybee_schema_version\n if model_ver > hb_ver:\n msg = 'Imported Model schema version \"{}\" is newer than that with the ' \\\n 'currently installed Honeybee \"{}\".\\nThe Model may fail to import ' \\\n 'or (worse) some newer features of the Model might not be imported ' \\\n 'without detection.'.format(data['version'], folders.honeybee_schema_version_str)\n print msg\n give_warning(ghenv.Component, msg)\n elif model_ver != hb_ver:\n msg = 'Imported Model schema version \"{}\" is older than that with the ' \\\n 'currently installed Honeybee \"{}\".\\nThe Model will be upgraded upon ' \\\n 'import.'.format(data['version'], folders.honeybee_schema_version_str)\n print msg\n\n\nif all_required_inputs(ghenv.Component) and _load:\n with io.open(_hb_file, encoding='utf-8') as inf:\n first_char = inf.read(1)\n second_char = inf.read(1)\n with io.open(_hb_file, encoding='utf-8') as inf:\n if second_char == '{':\n inf.read(1)\n data = json.load(inf)\n\n version_check(data) # try to check the version\n if 'type' in data:\n hb_objs = hb_dict_util.dict_to_object(data, False) # re-serialize as a core object\n if hb_objs is None: # try to re-serialize it as an energy object\n hb_objs = energy_dict_util.dict_to_object(data, False)\n if hb_objs is None: # try to re-serialize it as a radiance object\n hb_objs = radiance_dict_util.dict_to_object(data, False)\n elif isinstance(hb_objs, Model):\n model_units_tolerance_check(hb_objs)\n else: # no 'type' key; assume that its a group of objects\n hb_objs = []\n for hb_dict in data.values():\n hb_obj = hb_dict_util.dict_to_object(hb_dict, False) # re-serialize as a core object\n if hb_obj is None: # try to re-serialize it as an energy object\n hb_obj = energy_dict_util.dict_to_object(hb_dict, False)\n if hb_obj is None: # try to re-serialize it as a radiance object\n hb_obj = radiance_dict_util.dict_to_object(hb_dict, False)\n hb_objs.append(hb_obj)",
"category": "Honeybee",
"name": "HB Load Objects",
"description": "Load any honeybee object from a honeybee JSON file\n-\nHoneybee objects include any Model, Room, Face, Aperture, Door, Shade, or\nboundary condition object.\n-\nIt also includes any honeybee energy Material, Construction, ConstructionSet,\nSchedule, Load, ProgramType, or Simulation object.\n-"
Expand Down
6 changes: 3 additions & 3 deletions honeybee_grasshopper_core/src/HB Load Objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

ghenv.Component.Name = 'HB Load Objects'
ghenv.Component.NickName = 'LoadObjects'
ghenv.Component.Message = '1.8.1'
ghenv.Component.Message = '1.8.2'
ghenv.Component.Category = 'Honeybee'
ghenv.Component.SubCategory = '3 :: Serialize'
ghenv.Component.AdditionalHelpFromDocStrings = '2'
Expand Down Expand Up @@ -124,15 +124,15 @@ def version_check(data):
data = json.load(inf)

version_check(data) # try to check the version
try:
if 'type' in data:
hb_objs = hb_dict_util.dict_to_object(data, False) # re-serialize as a core object
if hb_objs is None: # try to re-serialize it as an energy object
hb_objs = energy_dict_util.dict_to_object(data, False)
if hb_objs is None: # try to re-serialize it as a radiance object
hb_objs = radiance_dict_util.dict_to_object(data, False)
elif isinstance(hb_objs, Model):
model_units_tolerance_check(hb_objs)
except ValueError: # no 'type' key; assume that its a group of objects
else: # no 'type' key; assume that its a group of objects
hb_objs = []
for hb_dict in data.values():
hb_obj = hb_dict_util.dict_to_object(hb_dict, False) # re-serialize as a core object
Expand Down
Binary file modified honeybee_grasshopper_core/user_objects/HB Load Objects.ghuser
Binary file not shown.