forked from libtcod/python-tcod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
82 lines (56 loc) · 2.7 KB
/
Copy path__init__.py
File metadata and controls
82 lines (56 loc) · 2.7 KB
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
from __future__ import annotations
import logging
from pkgutil import extend_path
from typing import Any, Callable, TypeVar
from tcod.loader import ffi, lib
__path__ = extend_path(__path__, __name__)
T = TypeVar("T")
logger = logging.getLogger(__name__)
_LOG_PRIORITY = {
1: logging.DEBUG, # SDL_LOG_PRIORITY_VERBOSE
2: logging.DEBUG, # SDL_LOG_PRIORITY_DEBUG
3: logging.INFO, # SDL_LOG_PRIORITY_INFO
4: logging.WARNING, # SDL_LOG_PRIORITY_WARN
5: logging.ERROR, # SDL_LOG_PRIORITY_ERROR
6: logging.CRITICAL, # SDL_LOG_PRIORITY_CRITICAL
}
@ffi.def_extern() # type: ignore
def _sdl_log_output_function(_userdata: Any, category: int, priority: int, message: Any) -> None:
"""Pass logs sent by SDL to Python's logging system."""
logger.log(_LOG_PRIORITY.get(priority, 0), "%i:%s", category, ffi.string(message).decode("utf-8"))
def _get_error() -> str:
"""Return a message from SDL_GetError as a Unicode string."""
return str(ffi.string(lib.SDL_GetError()), encoding="utf-8")
def _check(result: int) -> int:
"""Check if an SDL function returned without errors, and raise an exception if it did."""
if result < 0:
raise RuntimeError(_get_error())
return result
def _check_p(result: Any) -> Any:
"""Check if an SDL function returned NULL, and raise an exception if it did."""
if not result:
raise RuntimeError(_get_error())
return result
if lib._sdl_log_output_function:
lib.SDL_LogSetOutputFunction(lib._sdl_log_output_function, ffi.NULL)
def _compiled_version() -> tuple[int, int, int]:
return int(lib.SDL_MAJOR_VERSION), int(lib.SDL_MINOR_VERSION), int(lib.SDL_PATCHLEVEL)
def _linked_version() -> tuple[int, int, int]:
sdl_version = ffi.new("SDL_version*")
lib.SDL_GetVersion(sdl_version)
return int(sdl_version.major), int(sdl_version.minor), int(sdl_version.patch)
def _version_at_least(required: tuple[int, int, int]) -> None:
"""Raise an error if the compiled version is less than required. Used to guard recently defined SDL functions."""
if required <= _compiled_version():
return
msg = f"This feature requires SDL version {required}, but tcod was compiled with version {_compiled_version()}"
raise RuntimeError(msg)
def _required_version(required: tuple[int, int, int]) -> Callable[[T], T]:
if not lib: # Read the docs mock object.
return lambda x: x
if required <= _compiled_version():
return lambda x: x
def replacement(*_args: Any, **_kwargs: Any) -> Any:
msg = f"This feature requires SDL version {required}, but tcod was compiled with version {_compiled_version()}"
raise RuntimeError(msg)
return lambda x: replacement # type: ignore[return-value]