-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
config.py
94 lines (77 loc) · 3.81 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# MIT License
#
# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2018
#
# 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 ART.
"""
import json
import logging
import os
import numpy as np
logger = logging.getLogger(__name__)
# ------------------------------------------------------------------------------------------------- CONSTANTS AND TYPES
ART_NUMPY_DTYPE = np.float32 # pylint: disable=C0103
ART_DATA_PATH: str
# --------------------------------------------------------------------------------------------- DEFAULT PACKAGE CONFIGS
_folder = os.path.expanduser("~")
if not os.access(_folder, os.W_OK): # pragma: no cover
_folder = "/tmp" # pylint: disable=C0103
_folder = os.path.join(_folder, ".art")
def set_data_path(path):
"""
Set the path for ART's data directory (ART_DATA_PATH).
"""
expanded_path = os.path.abspath(os.path.expanduser(path))
os.makedirs(expanded_path, exist_ok=True)
if not os.access(expanded_path, os.R_OK): # pragma: no cover
raise OSError(f"path {expanded_path} cannot be read from")
if not os.access(expanded_path, os.W_OK): # pragma: no cover
logger.warning("path %s is read only", expanded_path)
global ART_DATA_PATH # pylint: disable=W0603
ART_DATA_PATH = expanded_path
logger.info("set ART_DATA_PATH to %s", expanded_path)
# Load data from configuration file if it exists. Otherwise create one.
_config_path = os.path.expanduser(os.path.join(_folder, "config.json"))
if os.path.exists(_config_path):
try:
with open(_config_path, encoding="utf8") as f:
_config = json.load(f)
# Since renaming this variable we must update existing config files
if "DATA_PATH" in _config: # pragma: no cover
_config["ART_DATA_PATH"] = _config.pop("DATA_PATH")
try:
with open(_config_path, "w", encoding="utf8") as f:
f.write(json.dumps(_config, indent=4))
except IOError:
logger.warning("Unable to update configuration file", exc_info=True)
except ValueError: # pragma: no cover
_config = {}
if not os.path.exists(_folder):
try:
os.makedirs(_folder)
except OSError: # pragma: no cover
logger.warning("Unable to create folder for configuration file.", exc_info=True)
if not os.path.exists(_config_path):
# Generate default config
_config = {"ART_DATA_PATH": os.path.join(_folder, "data")}
try:
with open(_config_path, "w", encoding="utf8") as f:
f.write(json.dumps(_config, indent=4))
except IOError: # pragma: no cover
logger.warning("Unable to create configuration file", exc_info=True)
if "ART_DATA_PATH" in _config: # pragma: no cover
set_data_path(_config["ART_DATA_PATH"])