Skip to content

Commit

Permalink
Issue #332 Restore BatchJob._string_to_log_level, was removed but was…
Browse files Browse the repository at this point in the history
… needed after all
  • Loading branch information
JohanKJSchreurs committed Feb 1, 2023
1 parent bce5b34 commit 149119f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
41 changes: 28 additions & 13 deletions openeo/rest/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,20 @@ def logs(
return VisualList('logs', data=entries)

@classmethod
def _normalize_log_level(cls, log_level):
def _normalize_log_level(cls, log_level: Optional[Union[int, str]]) -> int:
"""Convert log level from inputs that can be str, int or None to the integer
constants uses in logging for internal use in BatchJob.
:param log_level: the input to be converted.
:raises TypeError: when log_level it is neither a str, an int or None.
:return: one of the following log level constants from the standard module ``logging``:
logging.ERROR, logging.WARNING, logging.INFO, or logging.DEBUG
"""
if log_level is None:
return logging.ERROR

if isinstance(log_level, str):
log_level = log_level.upper()

if log_level in ["CRITICAL", "ERROR"]:
return logging.ERROR
elif log_level == "WARNING":
return logging.WARNING
elif log_level == "INFO":
return logging.INFO
elif log_level == "DEBUG":
return logging.DEBUG

return logging.ERROR
return cls._string_to_log_level(log_level)

# Now is should be an int
if not isinstance(log_level, int):
Expand All @@ -199,6 +196,24 @@ def _normalize_log_level(cls, log_level):

return log_level

@staticmethod
def _string_to_log_level(log_level: str) -> int:
"""Simpler conversion when you know log_level is always a string."""
if not log_level:
return logging.ERROR

log_level = log_level.upper()
if log_level in ["CRITICAL", "ERROR"]:
return logging.ERROR
elif log_level == "WARNING":
return logging.WARNING
elif log_level == "INFO":
return logging.INFO
elif log_level == "DEBUG":
return logging.DEBUG

return logging.ERROR

def run_synchronous(
self, outputfile: Union[str, Path, None] = None,
print=print, max_poll_interval=60, connection_retry_interval=30
Expand Down
6 changes: 1 addition & 5 deletions tests/rest/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,8 @@ def test_normalize_log_level(log_level_in, expected_log_level):
assert BatchJob._normalize_log_level(log_level_in) == expected_log_level



@pytest.mark.parametrize(
"log_level", [1.0, b"not a string"]
)
@pytest.mark.parametrize("log_level", [1.0, b"not a string"])
def test_normalize_log_level_raises_type_error(log_level):

with pytest.raises(TypeError):
assert BatchJob._normalize_log_level(log_level)

Expand Down

0 comments on commit 149119f

Please sign in to comment.