Skip to content

Commit 8a32a3a

Browse files
Swapped over to edq's logging.
1 parent 6bd4f4f commit 8a32a3a

File tree

8 files changed

+15
-80
lines changed

8 files changed

+15
-80
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ out*/
2121
/dist/
2222
*.egg-info
2323
/html/
24+
/.mypy_*/
2425

2526
# Project-Specific
2627
*.gif

pacai/capture/board.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def main() -> int:
487487
+ f" If not specified, a random number between {MIN_SIZE} and {MAX_RANDOM_SIZE} will be chosen."))
488488

489489
args = parser.parse_args()
490-
args = pacai.core.log.init_from_args(args)
490+
args = pacai.core.log.init_from_args(parser, args)
491491

492492
board = generate(args.seed, args.size)
493493
print(board)

pacai/core/log.py

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,30 @@
11
import argparse
2-
import logging
32
import typing
43

5-
DEFAULT_LOGGING_LEVEL: str = logging.getLevelName(logging.INFO)
6-
DEFAULT_LOGGING_FORMAT: str = '%(asctime)s [%(levelname)-8s] - %(filename)s:%(lineno)s -- %(message)s'
4+
import edq.core.log
75

8-
LEVELS: list[str] = [
9-
'TRACE',
10-
logging.getLevelName(logging.DEBUG),
11-
logging.getLevelName(logging.INFO),
12-
logging.getLevelName(logging.WARNING),
13-
logging.getLevelName(logging.ERROR),
14-
logging.getLevelName(logging.CRITICAL),
6+
WARN_LOGGERS: typing.List[str] = [
7+
"PIL",
158
]
16-
17-
def init(level: str = DEFAULT_LOGGING_LEVEL, log_format: str = DEFAULT_LOGGING_FORMAT, **kwargs: typing.Any) -> None:
18-
"""
19-
Initialize or re-initialize the logging infrastructure.
20-
"""
21-
22-
# Add trace.
23-
_add_logging_level('TRACE', logging.DEBUG - 5)
24-
25-
logging.basicConfig(level = level, format = log_format, force = True)
26-
27-
# Ignore logging from third-party libraries.
28-
logging.getLogger("PIL").setLevel(logging.WARNING)
9+
""" Loggers (usually third-party) to move up to warning on init. """
2910

3011
def set_cli_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
3112
"""
3213
Set common CLI arguments.
3314
This is a sibling to init_from_args(), as the arguments set here can be interpreted there.
3415
"""
3516

36-
parser.add_argument('--log-level', dest = 'log_level',
37-
action = 'store', type = str, default = logging.getLevelName(logging.INFO),
38-
choices = LEVELS,
39-
help = 'Set the logging level (default: %(default)s).')
40-
41-
parser.add_argument('--quiet', dest = 'quiet',
42-
action = 'store_true', default = False,
43-
help = 'Set the logging level to warning (overrides --log-level) (default: %(default)s).')
44-
45-
parser.add_argument('--debug', dest = 'debug',
46-
action = 'store_true', default = False,
47-
help = 'Set the logging level to debug (overrides --log-level and --quiet) (default: %(default)s).')
48-
17+
edq.core.log.set_cli_args(parser, {})
4918
return parser
5019

51-
def init_from_args(args: argparse.Namespace) -> argparse.Namespace:
20+
def init_from_args(parser: argparse.ArgumentParser, args: argparse.Namespace) -> argparse.Namespace:
5221
"""
5322
Take in args from a parser that was passed to set_cli_args(),
5423
and call init() with the appropriate arguments.
5524
"""
5625

57-
level = args.log_level
58-
59-
if (args.quiet):
60-
level = logging.getLevelName(logging.WARNING)
61-
62-
if (args.debug):
63-
level = logging.getLevelName(logging.DEBUG)
64-
65-
init(level)
66-
26+
edq.core.log.init_from_args(parser, args, {})
6727
return args
6828

69-
def _add_logging_level(level_name: str, level_number: int, method_name: str | None = None) -> None:
70-
"""
71-
Add a new logging level.
72-
73-
See https://stackoverflow.com/questions/2183233/how-to-add-a-custom-loglevel-to-pythons-logging-facility/35804945#35804945 .
74-
"""
75-
76-
if (method_name is None):
77-
method_name = level_name.lower()
78-
79-
# Level has already been defined.
80-
if hasattr(logging, level_name):
81-
return
82-
83-
def log_for_level(self: typing.Any, message: str, *args: typing.Any, **kwargs: typing.Any) -> None:
84-
if self.isEnabledFor(level_number):
85-
self._log(level_number, message, args, **kwargs)
86-
87-
def log_to_root(message: str, *args: typing.Any, **kwargs: typing.Any) -> None:
88-
logging.log(level_number, message, *args, **kwargs)
89-
90-
logging.addLevelName(level_number, level_name)
91-
setattr(logging, level_name, level_number)
92-
setattr(logging.getLoggerClass(), method_name, log_for_level)
93-
setattr(logging, method_name, log_to_root)
94-
9529
# Load the default logging when this module is loaded.
96-
init()
30+
edq.core.log.init(warn_loggers = WARN_LOGGERS)

pacai/eightpuzzle/bin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _parse_args(parser: argparse.ArgumentParser) -> argparse.Namespace:
7878
args = parser.parse_args()
7979

8080
# Parse logging arguments.
81-
args = pacai.core.log.init_from_args(args)
81+
args = pacai.core.log.init_from_args(parser, args)
8282

8383
# Parse specific options.
8484
args = init_from_args(args)

pacai/ui/web.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def _get_post_data(self) -> dict[str, typing.Any]:
236236
except Exception as ex:
237237
raise ValueError("Payload is not valid json.") from ex
238238

239-
return request
239+
return request # type: ignore[no-any-return]
240240

241241
class WebUI(pacai.core.ui.UI):
242242
"""

pacai/util/bin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def parse_args(
192192
args = parser.parse_args(args = argv)
193193

194194
# Parse logging arguments.
195-
args = pacai.core.log.init_from_args(args)
195+
args = pacai.core.log.init_from_args(parser, args)
196196

197197
# Parse custom options.
198198
base_agent_infos, remove_agent_indexes, board_options = custom_init_from_args(args)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Pillow>=8.3.2
2-
edq-utils>=0.0.3
2+
edq-utils>=0.0.4
33
json5>=0.9.14

scripts/check_strict_types.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function main() {
1616

1717
cd "${ROOT_DIR}"
1818

19-
mypy pacai --strict
19+
mypy pacai --strict --cache-dir "${ROOT_DIR}/.mypy_strict_cache"
2020
return $?
2121
}
2222

0 commit comments

Comments
 (0)