Skip to content

Commit

Permalink
fix(writer): Ensure Unix systems write Windows-compatible line breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey authored and Chris Mackey committed May 17, 2024
1 parent dcfca45 commit 6901399
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
8 changes: 7 additions & 1 deletion honeybee_doe2/writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8
"""Methods to write to inp."""
from __future__ import division
import os
import math

from ladybug_geometry.geometry2d import Vector2D, Point2D
Expand Down Expand Up @@ -918,7 +919,12 @@ def model_to_inp(
for report in report_types:
model_str.append(header_comment_minor(report))
model_str.append('END ..\nCOMPUTE ..\nSTOP ..\n')
return '\n'.join(model_str)

# create the final string and ensure that it is windows-compatible
inp_str = '\n'.join(model_str)
if os.name != 'nt': # we are on a unix-based system
inp_str = inp_str.replace('\n', '\r\n')
return inp_str


def room_doe2_conditioning_type(room):
Expand Down
29 changes: 19 additions & 10 deletions tests/writer_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test the translators for geometry to INP."""
import os

from ladybug_geometry.geometry3d import Point3D, Vector3D, Mesh3D

from honeybee.model import Model
Expand All @@ -15,6 +17,13 @@
import honeybee_energy.lib.scheduletypelimits as schedule_types
from honeybee_energy.lib.programtypes import office_program, program_type_by_identifier

if os.name =='nt':
START_TEXT = 'INPUT ..\n\n'
END_TEXT = 'END ..\nCOMPUTE ..\nSTOP ..\n'
else:
START_TEXT = 'INPUT ..\r\n\r\n'
END_TEXT = 'END ..\r\nCOMPUTE ..\r\nSTOP ..\r\n'


def test_shade_writer():
"""Test the basic functionality of the Shade inp writer."""
Expand Down Expand Up @@ -515,8 +524,8 @@ def test_model_writer():
model = Model('Tiny_House', [room], shade_meshes=[awning_1])

inp_str = model.to.inp(model)
assert inp_str.startswith('INPUT ..\n\n')
assert inp_str.endswith('END ..\nCOMPUTE ..\nSTOP ..\n')
assert inp_str.startswith(START_TEXT)
assert inp_str.endswith(END_TEXT)


def test_model_writer_from_standard_hbjson():
Expand All @@ -525,8 +534,8 @@ def test_model_writer_from_standard_hbjson():
hb_model = Model.from_file(standard_test)

inp_str = hb_model.to.inp(hb_model, hvac_mapping='Model')
assert inp_str.startswith('INPUT ..\n\n')
assert inp_str.endswith('END ..\nCOMPUTE ..\nSTOP ..\n')
assert inp_str.startswith(START_TEXT)
assert inp_str.endswith(END_TEXT)


def test_model_writer_from_hvac_hbjson():
Expand All @@ -535,8 +544,8 @@ def test_model_writer_from_hvac_hbjson():
hb_model = Model.from_file(hvac_test)

inp_str = hb_model.to.inp(hb_model, hvac_mapping='AssignedHVAC')
assert inp_str.startswith('INPUT ..\n\n')
assert inp_str.endswith('END ..\nCOMPUTE ..\nSTOP ..\n')
assert inp_str.startswith(START_TEXT)
assert inp_str.endswith(END_TEXT)


def test_model_writer_from_air_wall_hbjson():
Expand All @@ -545,8 +554,8 @@ def test_model_writer_from_air_wall_hbjson():
hb_model = Model.from_file(air_wall_test)

inp_str = hb_model.to.inp(hb_model, hvac_mapping='Room')
assert inp_str.startswith('INPUT ..\n\n')
assert inp_str.endswith('END ..\nCOMPUTE ..\nSTOP ..\n')
assert inp_str.startswith(START_TEXT)
assert inp_str.endswith(END_TEXT)


def test_model_writer_from_ceil_adj_hbjson():
Expand All @@ -555,7 +564,7 @@ def test_model_writer_from_ceil_adj_hbjson():
hb_model = Model.from_file(ceiling_adj_test)

inp_str = hb_model.to.inp(hb_model, hvac_mapping='AssignedHVAC')
assert inp_str.startswith('INPUT ..\n\n')
assert inp_str.endswith('END ..\nCOMPUTE ..\nSTOP ..\n')
assert inp_str.startswith(START_TEXT)
assert inp_str.endswith(END_TEXT)


0 comments on commit 6901399

Please sign in to comment.