Skip to content

Commit

Permalink
Use toGenericString and return a Pathlib.Path instead of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarrec committed Aug 14, 2024
1 parent c01601c commit 2c31f9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/EnergyPlus/api/datatransfer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,18 @@ void resetErrorFlag(EnergyPlusState state)
char *inputFilePath(EnergyPlusState state)
{
const auto *thisState = static_cast<EnergyPlus::EnergyPlusData *>(state);
char *p = new char[std::strlen(thisState->dataStrGlobals->inputFilePath.string().c_str()) + 1];
std::strcpy(p, thisState->dataStrGlobals->inputFilePath.string().c_str());
std::string const path_utf8 = EnergyPlus::FileSystem::toGenericString(thisState->dataStrGlobals->inputFilePath);
char *p = new char[std::strlen(path_utf8.c_str()) + 1];
std::strcpy(p, path_utf8.c_str());
return p;
}

char *epwFilePath(EnergyPlusState state)
{
const auto *thisState = static_cast<EnergyPlus::EnergyPlusData *>(state);
char *p = new char[std::strlen(thisState->files.inputWeatherFilePath.filePath.c_str()) + 1];
std::strcpy(p, thisState->files.inputWeatherFilePath.filePath.c_str());
std::string const path_utf8 = EnergyPlus::FileSystem::toGenericString(thisState->files.inputWeatherFilePath.filePath);
char *p = new char[std::strlen(path_utf8.c_str()) + 1];
std::strcpy(p, path_utf8.c_str());
return p;
}

Expand Down
19 changes: 12 additions & 7 deletions src/EnergyPlus/api/datatransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
from ctypes import cdll, c_int, c_char_p, c_void_p, POINTER, Structure, byref
from pyenergyplus.common import RealEP, EnergyPlusException, is_number
from typing import List, Union

from pathlib import Path

class DataExchange:
"""
Expand Down Expand Up @@ -363,27 +363,32 @@ def reset_api_error_flag(self, state: c_void_p) -> None:
"""
self.api.resetErrorFlag(state)

def get_input_file_path(self, state: c_void_p) -> bytes:
def get_input_file_path(self, state: c_void_p) -> Path:
"""
Provides the input file path back to the client. In most circumstances the client will know the path to the
input file, but there are some cases where code is generalized in unexpected workflows. Users have requested
a way to get the input file path back from the running instance.
:param state: An active EnergyPlus "state" that is returned from a call to `api.state_manager.new_state()`.
:return: Returns a raw bytes representation of the input file path
:return: A pathlib.Path of the input file path
"""
return self.api.inputFilePath(state) # TODO: Need to call free for the underlying char *?
c_string = self.api.inputFilePath(state)
res = Path(c_string.decode('utf-8'))
return res

def get_weather_file_path(self, state: c_void_p) -> bytes:
def get_weather_file_path(self, state: c_void_p) -> Path:
"""
Provides the weather file path back to the client. In most circumstances the client will know the path to the
weather file, but there are some cases where code is generalized in unexpected workflows. Users have requested
a way to get the weather file path back from the running instance.
:param state: An active EnergyPlus "state" that is returned from a call to `api.state_manager.new_state()`.
:return: Returns a raw bytes representation of the weather file path
:return: A pathlib.Path of the weather file
"""
return self.api.epwFilePath(state)
c_string = self.api.epwFilePath(state)
res = Path(c_string.decode('utf-8'))
return res


def get_object_names(self, state: c_void_p, object_type_name: Union[str, bytes]) -> List[str]:
"""
Expand Down

5 comments on commit 2c31f9f

@nrel-bot-2b
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (jmarrec) - x86_64-Linux-Ubuntu-22.04-gcc-11.4: OK (3698 of 3698 tests passed, 0 test warnings)

Build Badge Test Badge

@nrel-bot-3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (jmarrec) - x86_64-MacOS-10.18-clang-15.0.0: OK (3657 of 3657 tests passed, 0 test warnings)

Build Badge Test Badge

@nrel-bot-2c
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (jmarrec) - x86_64-Linux-Ubuntu-22.04-gcc-11.4-IntegrationCoverage-Debug: OK (744 of 796 tests passed, 0 test warnings)

Failures:\n

integration Test Summary

  • Passed: 744
  • Failed: 52

Build Badge Test Badge Coverage Badge

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (jmarrec) - x86_64-Linux-Ubuntu-22.04-gcc-11.4-UnitTestsCoverage-Debug: OK (2051 of 2071 tests passed, 0 test warnings)

Failures:\n

EnergyPlusFixture Test Summary

  • Passed: 1563
  • Subprocess aborted: 15

ZoneIdealLoadsTest Test Summary

  • Passed: 4
  • Subprocess aborted: 4

Build Badge Test Badge Coverage Badge

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (jmarrec) - Win64-Windows-10-VisualStudio-16: OK (2864 of 2864 tests passed, 0 test warnings)

Build Badge Test Badge

Please sign in to comment.