# MIT License## Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2023## Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit# persons to whom the Software is furnished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in all copies or substantial portions of the# Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE# SOFTWARE."""This module loads and provides configuration parameters for HEART"""importjsonimportloggingimportosimportnumpyasnplogger:logging.Logger=logging.getLogger(__name__)# ------------------------------------------------------------------------------------------------- CONSTANTS AND TYPESHEART_NUMPY_DTYPE=np.float32HEART_DATA_PATH:str# --------------------------------------------------------------------------------------------- DEFAULT PACKAGE CONFIGS_folder=os.path.expanduser("~")ifnotos.access(_folder,os.W_OK):# pragma: no cover_folder="/tmp"# noqa S108_folder=os.path.join(_folder,".heart")
[docs]defset_data_path(path:str)->None:"""Set the path for HEART's data directory (HEART_DATA_PATH). Args: path (str): data path. Raises: OSError: if path cannot be read from. """expanded_path=os.path.abspath(os.path.expanduser(path))os.makedirs(expanded_path,exist_ok=True)ifnotos.access(expanded_path,os.R_OK):# pragma: no coverraiseOSError(f"path {expanded_path} cannot be read from")ifnotos.access(expanded_path,os.W_OK):# pragma: no coverlogger.warning("path %s is read only",expanded_path)globalHEART_DATA_PATHHEART_DATA_PATH=expanded_pathlogger.info("set HEART_DATA_PATH to %s",expanded_path)
# Load data from configuration file if it exists. Otherwise create one._config:dict={}_config_path=os.path.expanduser(os.path.join(_folder,"config.json"))ifos.path.exists(_config_path):try:withopen(_config_path,encoding="utf8")as_f:_config=json.load(_f)# Since renaming this variable we must update existing config filesif"DATA_PATH"in_config:# pragma: no cover_config["HEART_DATA_PATH"]=_config.pop("DATA_PATH")try:withopen(_config_path,"w",encoding="utf8")as_f:_f.write(json.dumps(_config,indent=4))exceptOSError:logger.warning("Unable to update configuration file",exc_info=True)exceptValueError:# pragma: no cover_config={}ifnotos.path.exists(_folder):try:os.makedirs(_folder)exceptOSError:# pragma: no coverlogger.warning("Unable to create folder for configuration file.",exc_info=True)ifnotos.path.exists(_config_path):# Generate default config_config={"HEART_DATA_PATH":os.path.join(_folder,"data")}try:withopen(_config_path,"w",encoding="utf8")as_f:_f.write(json.dumps(_config,indent=4))exceptOSError:# pragma: no coverlogger.warning("Unable to create configuration file",exc_info=True)if"HEART_DATA_PATH"in_config:# pragma: no coverset_data_path(_config["HEART_DATA_PATH"])