def idffile2epjfile(idfpath, epjpath=None, schemapath=None, epjext=None): """convert an IDF file on disk to an EPJ file on disk reads the idf file at idfpath. Converts it to epj file Writes the epj file to epjpath Parameters ---------- idfpath: str, pathlib.Path idf file name or path to the idf file epjpath: str, pathlib.Path epj file name or path to the epj file schemapath: str, pathlib.Path path to Energy+.schema.epJSON usuallty found in the install location of EnergyPlus. If schemapah=None, the function will attempt to find the schema path in the standard install locations Returns ------- pathlib.Path the path to the saved EPJ file """ if not epjext: epjext = "epJSON" idfpath = pathlib.Path(idfpath) if not schemapath: with open(idfpath, "r") as idfhandle: version = getidfversion(idfhandle) schemapath = installlocation.schemapath(version) if not epjpath: epjpath = idfpath.with_suffix(f".{epjext}") schemahandle = open(schemapath, "r") idfhandle = open(idfpath, "r") epjtxt = idf2json(idfhandle, schemahandle) with open(epjpath, "w") as epjhandle: epjhandle.write(epjtxt) return epjpath
def epjfile2idffile(epjpath, idfpath=None, schemapath=None): """convert an EPJ file on disk to an IDF file on disk""" epjpath = pathlib.Path(epjpath) if not schemapath: epj = EPJ(epjpath) version = epj.epobjects["Version"][0].version_identifier schemapath = installlocation.schemapath(version) if not idfpath: idfpath = epjpath.with_suffix(".idf") schemahandle = open(schemapath, "r") epjhandle = open(epjpath, "r") idftxt = json2idf(epjhandle, schemahandle) with open(idfpath, "w") as idfhandle: idfhandle.write(idftxt)
def test_epjfile2idffile(tmp_path, idftxt, epjname, idfname, schemapath, expected): """py.test for epjfile2idffile""" # 1. convert idf to epj # 2. write epj to epjfile # 3. convert epjfile to idffile -> the function we are testing # 4. read the spj file and comvert it to idf # 5. read idffile and compare to idf in 4. # - # 1. convert idf to epj idfhandle = StringIO(idftxt) if schemapath: new_epschemapath = schemapath else: version = idfjsonconverter.getidfversion(idfhandle) new_epschemapath = installlocation.schemapath(version) idfhandle = StringIO(idftxt) # rest handle with open(new_epschemapath, "r") as schemahandle: epjstr = idfjsonconverter.idf2json(idfhandle, schemahandle) # 2. write epj to epjfile tmpdir = tmp_path tmpepjfile = tmpdir / epjname tmpepjfile.write_text(epjstr) epjfilepath = tmpepjfile.resolve() # - if idfname: tmpidffile = tmpdir / idfname idffilepath = tmpidffile.resolve() else: idffilepath = None newidfname = f'{epjname.split(".")[0]}.idf' tmpidffile = tmpdir / newidfname newidffilepath = tmpidffile.resolve() # 3. convert epjfile to idffile -> the function we are testing idfjsonconverter.epjfile2idffile(epjfilepath, idffilepath, schemapath) # 4. read the spj file and comvert it to idf with open(epjfilepath, "r") as epjhandle: expected = idfjsonconverter.json2idf(epjhandle, open(new_epschemapath, "r")) # 5. read idffile and compare to idf in 4. if idffilepath: result = open(idffilepath, "r").read() else: result = open(newidffilepath, "r").read() assert result == expected
def openidf(idfpath: Union[str, PathLike[str]], epjpath: Union[str, PathLike[str]] = None, schemapath: Union[str, bytes, PathLike] = None, epjext: str = None) -> EPJ: """open and idf file as a epj file""" if not epjext: epjext = "epJSON" idfpath = pathlib.Path(idfpath) if not schemapath: with open(idfpath, "r") as idfhandle: version = getidfversion(idfhandle) schemapath = installlocation.schemapath(version) if not epjpath: epjpath = idfpath.with_suffix(f".{epjext}") schemahandle = open(schemapath, "r") idfhandle = open(idfpath, "r") epjtxt = idf2json(idfhandle, schemahandle) epj = EPJ(StringIO(epjtxt)) epj.epjname = epjpath return epj
def test_idffile2epjfile( tmp_path, idftxt, idffilename, epjfilename, epschemapath, expected ): """py.test for idffile2epjfile""" # 1. write idf to idffile # 2. convert idffile to epjfile -> the function we are testing # 3. convert idf to epj (expected) # 4. read epjfile and compare to epj in 3. # - # 1. write idf to idffile tmpdir = tmp_path tmpidffile = tmpdir / idffilename tmpidffile.write_text(idftxt) idffilepath = tmpidffile.resolve() # - if epjfilename: tmpepjfile = tmpdir / epjfilename epjfilepath = tmpepjfile.resolve() else: epjfilepath = None newepjname = f'{idffilename.split(".")[0]}.epj' tmpepjfile = tmpdir / newepjname newepjfilepath = tmpepjfile.resolve() # 2. convert idffile to epjfile -> the function we are testing savedepjpath = idfjsonconverter.idffile2epjfile( idffilepath, epjfilepath, epschemapath ) # 3. convert idf to epj (expected) if epschemapath: new_epschemapath = epschemapath else: with open(idffilepath, "r") as idfhandle: version = idfjsonconverter.getidfversion(idfhandle) new_epschemapath = installlocation.schemapath(version) idfhandle = StringIO(idftxt) with open(new_epschemapath, "r") as epschemahandle: expected = idfjsonconverter.idf2json(idfhandle, epschemahandle) # 4. read epjfile and compare to epj in 3. result = open(savedepjpath, "r").read() assert result == expected
def test_idffolder2epjfolder( tmp_path, idftxt, startfolder, endfolder, idfext, epjext, schemapath ): """py.test for idffolder2epjfolder""" # 1. set up a start folder # 2. use idftxt and put 2 idftxtfiles in that folder # 3. set up an end folder # 4. run conversion function idf to epj (startfolder to endfolder) # 5. do a in memory conversion for idfxtx to epj and back to idf # 6. compare contents of endfolder to idf in 5. # alternate methods # conversions # idffolder -> epjfolder -> idf1folder -> epj1folder -> idf2folder # idf1folder and idf2 folder should be identical # set endfolder if not endfolder: endfolder4test = startfolder else: endfolder4test = endfolder # setup extennsion values for use in the test if not idfext: idfext4test = "idf" else: idfext4test = idfext if not epjext: epjext4test = "epJSON" else: epjext4test = epjext # setup schemapath for use in test if schemapath: schemapath4test = schemapath else: version = idfjsonconverter.getidfversion(StringIO(idftxt)) schemapath4test = installlocation.schemapath(version) # 1. set up a start folder tmpdir = tmp_path startfolder = tmpdir / startfolder startfolder.mkdir() # 2. use idftxt and put 2 idftxtfiles in that folder fnames = ["file1.idf", "file2.idf"] fnames = [f"file1.{idfext4test}", f"file2.{idfext4test}"] for fname in fnames: fpath = startfolder / fname fpath.write_text(idftxt) # 3. set up an end folder endfolder4test = tmpdir / endfolder4test try: endfolder4test.mkdir() passendfolder = endfolder4test except FileExistsError as e: passendfolder = endfolder # we are using the start folder as endfolder # 4. run conversion function idf to epj (startfolder to endfolder) idfjsonconverter.idffolder2epjfolder( startfolder, epjfolder=passendfolder, idfext=idfext, epjext=epjext, schemapath=schemapath, ) # 5. do a in memory conversion for idftxt to epj expected = idfjsonconverter.idf2json(StringIO(idftxt), open(schemapath4test, "r")) # 6. compare contents of endfolder to idf in 5. for fname in fnames: fpath = endfolder4test / fname epjpath = fpath.with_suffix(f".{epjext4test}") result = open(epjpath, "r").read() assert result == expected
def test_schemapath(version, platform_system, expected): """py.test for schemapath""" result = installlocation.schemapath(version, platform_system) assert result == expected