Skip to content

Commit

Permalink
fix(serialize): Ensure that indent is carried through to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey authored and Chris Mackey committed Mar 11, 2024
1 parent 903e5be commit c8548d9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions honeybee_grasshopper_core/json/HB_Dump_Objects.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.7.0",
"version": "1.7.1",
"nickname": "DumpObjects",
"outputs": [
[
Expand Down Expand Up @@ -57,7 +57,7 @@
}
],
"subcategory": "3 :: Serialize",
"code": "\nimport sys\nimport os\nimport json\n\ntry: # import the core honeybee dependencies\n from honeybee.model import Model\n from honeybee.room import Room\n from honeybee.face import Face\n from honeybee.aperture import Aperture\n from honeybee.door import Door\n from honeybee.shade import Shade\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 ladybug_{{cad}} dependencies\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, give_warning\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\ndef geo_object_warning(obj):\n \"\"\"Give a warning that individual geometry objects should be added to a Model.\"\"\"\n msg = 'An individual {} has been connected to the _hb_objs.\\n' \\\n 'The recommended practice is to add this object to a Model and\\n' \\\n 'serialize the Model instead of serializing individual objects.'.format(\n obj.__class__.__name__)\n print(msg)\n give_warning(ghenv.Component, msg)\n\n\nif all_required_inputs(ghenv.Component) and _dump:\n # set the component defaults\n name = _name_ if _name_ is not None else 'unnamed'\n file_name = '{}.json'.format(name) if len(_hb_objs) > 1 or not \\\n isinstance(_hb_objs[0], Model) else '{}.hbjson'.format(name)\n folder = _folder_ if _folder_ is not None else folders.default_simulation_folder\n hb_file = os.path.join(folder, file_name)\n abridged = bool(abridged_)\n\n # check to see if any objects are of the geometry type and give a warning\n geo_types = (Room, Face, Aperture, Door, Shade)\n for obj in _hb_objs:\n if isinstance(obj, geo_types):\n geo_object_warning(obj)\n\n # create the dictionary to be written to a JSON file\n if len(_hb_objs) == 1: # write a single object into a file if the length is 1\n try:\n obj_dict = _hb_objs[0].to_dict(abridged=abridged)\n except TypeError: # no abridged option\n obj_dict = _hb_objs[0].to_dict()\n else: # create a dictionary of the objects that are indexed by name\n obj_dict = {}\n for obj in _hb_objs:\n try:\n obj_dict[obj.identifier] = obj.to_dict(abridged=abridged)\n except TypeError: # no abridged option\n obj_dict[obj.identifier] = obj.to_dict()\n\n # write the dictionary into a file\n if not os.path.isdir(folder):\n os.makedirs(folder)\n if (sys.version_info < (3, 0)): # we need to manually encode it as UTF-8\n with open(hb_file, 'wb') as fp:\n obj_str = json.dumps(obj_dict, indent=4, ensure_ascii=False)\n fp.write(obj_str.encode('utf-8'))\n else:\n with open(hb_file, 'w', encoding='utf-8') as fp:\n obj_str = json.dump(obj_dict, fp, indent=4, ensure_ascii=False)\n",
"code": "\nimport sys\nimport os\nimport json\n\ntry: # import the core honeybee dependencies\n from honeybee.model import Model\n from honeybee.room import Room\n from honeybee.face import Face\n from honeybee.aperture import Aperture\n from honeybee.door import Door\n from honeybee.shade import Shade\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 ladybug_{{cad}} dependencies\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, give_warning\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\ndef geo_object_warning(obj):\n \"\"\"Give a warning that individual geometry objects should be added to a Model.\"\"\"\n msg = 'An individual {} has been connected to the _hb_objs.\\n' \\\n 'The recommended practice is to add this object to a Model and\\n' \\\n 'serialize the Model instead of serializing individual objects.'.format(\n obj.__class__.__name__)\n print(msg)\n give_warning(ghenv.Component, msg)\n\n\nif all_required_inputs(ghenv.Component) and _dump:\n # set the component defaults\n name = _name_ if _name_ is not None else 'unnamed'\n file_name = '{}.json'.format(name) if len(_hb_objs) > 1 or not \\\n isinstance(_hb_objs[0], Model) else '{}.hbjson'.format(name)\n folder = _folder_ if _folder_ is not None else folders.default_simulation_folder\n hb_file = os.path.join(folder, file_name)\n abridged = bool(abridged_)\n\n # check to see if any objects are of the geometry type and give a warning\n geo_types = (Room, Face, Aperture, Door, Shade)\n for obj in _hb_objs:\n if isinstance(obj, geo_types):\n geo_object_warning(obj)\n\n # create the dictionary to be written to a JSON file\n if len(_hb_objs) == 1: # write a single object into a file if the length is 1\n try:\n obj_dict = _hb_objs[0].to_dict(abridged=abridged)\n except TypeError: # no abridged option\n obj_dict = _hb_objs[0].to_dict()\n else: # create a dictionary of the objects that are indexed by name\n obj_dict = {}\n for obj in _hb_objs:\n try:\n obj_dict[obj.identifier] = obj.to_dict(abridged=abridged)\n except TypeError: # no abridged option\n obj_dict[obj.identifier] = obj.to_dict()\n\n # write the dictionary into a file\n if not os.path.isdir(folder):\n os.makedirs(folder)\n if (sys.version_info < (3, 0)): # we need to manually encode it as UTF-8\n with open(hb_file, 'wb') as fp:\n obj_str = json.dumps(obj_dict, indent=indent_, ensure_ascii=False)\n fp.write(obj_str.encode('utf-8'))\n else:\n with open(hb_file, 'w', encoding='utf-8') as fp:\n obj_str = json.dump(obj_dict, fp, indent=indent_, ensure_ascii=False)\n",
"category": "Honeybee",
"name": "HB Dump Objects",
"description": "Dump any honeybee object to a JSON file. You can use \"HB Load Objects\" component\nto load the objects from the file back into Grasshopper.\n-\nHoneybee objects include any honeybee energy Material, Construction,\nConstructionSet, Schedule, Load, ProgramType, or Simulation object.\n-"
Expand Down
6 changes: 3 additions & 3 deletions honeybee_grasshopper_core/src/HB Dump Objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

ghenv.Component.Name = 'HB Dump Objects'
ghenv.Component.NickName = 'DumpObjects'
ghenv.Component.Message = '1.7.0'
ghenv.Component.Message = '1.7.1'
ghenv.Component.Category = 'Honeybee'
ghenv.Component.SubCategory = '3 :: Serialize'
ghenv.Component.AdditionalHelpFromDocStrings = '2'
Expand Down Expand Up @@ -107,8 +107,8 @@ def geo_object_warning(obj):
os.makedirs(folder)
if (sys.version_info < (3, 0)): # we need to manually encode it as UTF-8
with open(hb_file, 'wb') as fp:
obj_str = json.dumps(obj_dict, indent=4, ensure_ascii=False)
obj_str = json.dumps(obj_dict, indent=indent_, ensure_ascii=False)
fp.write(obj_str.encode('utf-8'))
else:
with open(hb_file, 'w', encoding='utf-8') as fp:
obj_str = json.dump(obj_dict, fp, indent=4, ensure_ascii=False)
obj_str = json.dump(obj_dict, fp, indent=indent_, ensure_ascii=False)
Binary file modified honeybee_grasshopper_core/user_objects/HB Dump Objects.ghuser
Binary file not shown.

0 comments on commit c8548d9

Please sign in to comment.