Skip to content

Commit 783a756

Browse files
committed
Address Mitar's comments
1 parent 7896b33 commit 783a756

4 files changed

Lines changed: 30 additions & 22 deletions

File tree

‎openml/config.py‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import os
1010
from pathlib import Path
1111
from typing import Tuple, cast
12-
import warnings
1312

1413
from io import StringIO
1514
import configparser
@@ -21,7 +20,7 @@
2120
file_handler = None
2221

2322

24-
def _create_log_handlers():
23+
def _create_log_handlers(create_file_handler=True):
2524
""" Creates but does not attach the log handlers. """
2625
global console_handler, file_handler
2726
if console_handler is not None or file_handler is not None:
@@ -34,12 +33,13 @@ def _create_log_handlers():
3433
console_handler = logging.StreamHandler()
3534
console_handler.setFormatter(output_formatter)
3635

37-
one_mb = 2 ** 20
38-
log_path = os.path.join(cache_directory, "openml_python.log")
39-
file_handler = logging.handlers.RotatingFileHandler(
40-
log_path, maxBytes=one_mb, backupCount=1, delay=True
41-
)
42-
file_handler.setFormatter(output_formatter)
36+
if create_file_handler:
37+
one_mb = 2 ** 20
38+
log_path = os.path.join(cache_directory, "openml_python.log")
39+
file_handler = logging.handlers.RotatingFileHandler(
40+
log_path, maxBytes=one_mb, backupCount=1, delay=True
41+
)
42+
file_handler.setFormatter(output_formatter)
4343

4444

4545
def _convert_log_levels(log_level: int) -> Tuple[int, int]:
@@ -194,12 +194,20 @@ def _setup():
194194
if not os.path.exists(expanded_openml_dir):
195195
try:
196196
os.mkdir(expanded_openml_dir)
197+
cache_exists = True
197198
except PermissionError:
198-
warnings.warn(
199-
"No permission to create openml directory at %s! This can result in OpenML-Python "
200-
"not working properly." % expanded_openml_dir
201-
)
202-
pass
199+
cache_exists = False
200+
else:
201+
cache_exists = True
202+
203+
if cache_exists:
204+
_create_log_handlers()
205+
else:
206+
_create_log_handlers(create_file_handler=False)
207+
openml_logger.warning(
208+
"No permission to create openml directory at %s! This can result in OpenML-Python "
209+
"not working properly." % expanded_openml_dir
210+
)
203211

204212
config = _parse_config()
205213
apikey = config.get("FAKE_SECTION", "apikey")
@@ -213,14 +221,13 @@ def _setup():
213221
try:
214222
os.mkdir(cache_directory)
215223
except PermissionError:
216-
warnings.warn(
224+
openml_logger.warning(
217225
"No permission to create openml cache directory at %s! This can result in "
218226
"OpenML-Python not working properly." % cache_directory
219227
)
220-
pass
221228

222229
avoid_duplicate_runs = config.getboolean("FAKE_SECTION", "avoid_duplicate_runs")
223-
connection_n_retries = config.get("FAKE_SECTION", "connection_n_retries")
230+
connection_n_retries = int(config.get("FAKE_SECTION", "connection_n_retries"))
224231
if connection_n_retries > 20:
225232
raise ValueError(
226233
"A higher number of retries than 20 is not allowed to keep the "
@@ -296,4 +303,3 @@ def set_cache_directory(cachedir):
296303
]
297304

298305
_setup()
299-
_create_log_handlers()

‎openml/utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def _create_cache_directory(key):
279279
os.makedirs(cache_dir, exist_ok=True)
280280
except Exception as e:
281281
raise openml.exceptions.OpenMLCacheException(
282-
f"Cannot create cache directory {cache_dir} due to exception {e}"
282+
f"Cannot create cache directory {cache_dir}."
283283
) from e
284284
return cache_dir
285285

‎tests/test_openml/test_config.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
class TestConfig(openml.testing.TestBase):
1212
@unittest.mock.patch("os.path.expanduser")
13-
@unittest.mock.patch("warnings.warn")
14-
def test_non_writable_home(self, warnings_mock, expanduser_mock):
13+
@unittest.mock.patch("openml.config.openml_logger.warning")
14+
@unittest.mock.patch("openml.config._create_log_handlers")
15+
def test_non_writable_home(self, log_handler_mock, warnings_mock, expanduser_mock):
1516
with tempfile.TemporaryDirectory(dir=self.workdir) as td:
1617
expanduser_mock.side_effect = (
1718
os.path.join(td, "openmldir"),
@@ -21,6 +22,8 @@ def test_non_writable_home(self, warnings_mock, expanduser_mock):
2122
openml.config._setup()
2223

2324
self.assertEqual(warnings_mock.call_count, 2)
25+
self.assertEqual(log_handler_mock.call_count, 1)
26+
self.assertFalse(log_handler_mock.call_args_list[0][1]["create_file_handler"])
2427

2528

2629
class TestConfigurationForExamples(openml.testing.TestBase):

‎tests/test_utils/test_utils.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def test__create_cache_directory(self, config_mock):
9797
os.chmod(subdir, 0o444)
9898
config_mock.return_value = subdir
9999
with self.assertRaisesRegex(
100-
openml.exceptions.OpenMLCacheException,
101-
r"due to exception \[Errno 13\] Permission denied",
100+
openml.exceptions.OpenMLCacheException, r"Cannot create cache directory",
102101
):
103102
openml.utils._create_cache_directory("ghi")

0 commit comments

Comments
 (0)