Skip to content

Commit f07e684

Browse files
committed
Add PathLike support to more libtcodpy functions.
Remove long deprecated bytes to Unicode conversion.
1 parent 26f2406 commit f07e684

File tree

3 files changed

+99
-43
lines changed

3 files changed

+99
-43
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Changes relevant to the users of python-tcod are documented here.
44
This project adheres to [Semantic Versioning](https://semver.org/) since version `2.0.0`.
55

66
## [Unreleased]
7+
### Added
8+
- Added PathLike support to more libtcodpy functions.
9+
10+
### Removed
11+
- `tcod.console_set_custom_font` can no longer take bytes.
712

813
## [15.0.3] - 2023-05-25
914
### Deprecated

tcod/image.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,16 @@ def blit_2x(
288288
img_height,
289289
)
290290

291-
def save_as(self, filename: str) -> None:
291+
def save_as(self, filename: str | PathLike[str]) -> None:
292292
"""Save the Image to a 32-bit .bmp or .png file.
293293
294294
Args:
295295
filename (Text): File path to same this Image.
296+
297+
.. versionchanged:: Unreleased
298+
Added PathLike support.
296299
"""
297-
lib.TCOD_image_save(self.image_c, filename.encode("utf-8"))
300+
lib.TCOD_image_save(self.image_c, bytes(Path(filename)))
298301

299302
@property
300303
def __array_interface__(self) -> dict[str, Any]:

tcod/libtcodpy.py

Lines changed: 89 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import sys
66
import threading
77
import warnings
8+
from os import PathLike
89
from pathlib import Path
9-
from typing import Any, AnyStr, Callable, Hashable, Iterable, Iterator, Sequence
10+
from typing import Any, Callable, Hashable, Iterable, Iterator, Sequence
1011

1112
import numpy as np
1213
from numpy.typing import NDArray
@@ -954,7 +955,7 @@ def console_init_root(
954955
https://python-tcod.readthedocs.io/en/latest/tcod/getting-started.html"""
955956
)
956957
def console_set_custom_font(
957-
fontFile: AnyStr,
958+
fontFile: str | PathLike[str],
958959
flags: int = FONT_LAYOUT_ASCII_INCOL,
959960
nb_char_horiz: int = 0,
960961
nb_char_vertic: int = 0,
@@ -983,9 +984,12 @@ def console_set_custom_font(
983984
.. deprecated:: 11.13
984985
Load fonts using :any:`tcod.tileset.load_tilesheet` instead.
985986
See :ref:`getting-started` for more info.
987+
988+
.. versionchanged:: Unreleased
989+
Added PathLike support. `fontFile` no longer takes bytes.
986990
"""
987-
path = Path(_unicode(fontFile)).resolve(strict=True)
988-
_check(lib.TCOD_console_set_custom_font(path, flags, nb_char_horiz, nb_char_vertic))
991+
fontFile = Path(fontFile).resolve(strict=True)
992+
_check(lib.TCOD_console_set_custom_font(bytes(fontFile), flags, nb_char_horiz, nb_char_vertic))
989993

990994

991995
@deprecate("Check `con.width` instead.")
@@ -1780,7 +1784,7 @@ def console_new(w: int, h: int) -> tcod.console.Console:
17801784

17811785

17821786
@deprecate("This loading method is no longer supported, use tcod.console_load_xp instead.")
1783-
def console_from_file(filename: str) -> tcod.console.Console:
1787+
def console_from_file(filename: str | PathLike[str]) -> tcod.console.Console:
17841788
"""Return a new console object from a filename.
17851789
17861790
The file format is automatically determined. This can load REXPaint `.xp`,
@@ -1795,9 +1799,12 @@ def console_from_file(filename: str) -> tcod.console.Console:
17951799
Use :any:`tcod.console_load_xp` to load REXPaint consoles.
17961800
17971801
Other formats are not actively supported.
1802+
1803+
.. versionchanged:: Unreleased
1804+
Added PathLike support.
17981805
"""
1799-
path = Path(filename).resolve(strict=True)
1800-
return tcod.console.Console._from_cdata(_check_p(lib.TCOD_console_from_file(bytes(path))))
1806+
filename = Path(filename).resolve(strict=True)
1807+
return tcod.console.Console._from_cdata(_check_p(lib.TCOD_console_from_file(bytes(filename))))
18011808

18021809

18031810
@deprecate("Call the `Console.blit` method instead.")
@@ -1966,76 +1973,106 @@ def console_fill_char(con: tcod.console.Console, arr: Sequence[int]) -> None:
19661973

19671974

19681975
@deprecate("This format is not actively supported")
1969-
def console_load_asc(con: tcod.console.Console, filename: str) -> bool:
1976+
def console_load_asc(con: tcod.console.Console, filename: str | PathLike[str]) -> bool:
19701977
"""Update a console from a non-delimited ASCII `.asc` file.
19711978
19721979
.. deprecated:: 12.7
19731980
This format is no longer supported.
1981+
1982+
.. versionchanged:: Unreleased
1983+
Added PathLike support.
19741984
"""
1975-
return bool(lib.TCOD_console_load_asc(_console(con), filename.encode("utf-8")))
1985+
filename = Path(filename).resolve(strict=True)
1986+
return bool(lib.TCOD_console_load_asc(_console(con), bytes(filename)))
19761987

19771988

19781989
@deprecate("This format is not actively supported")
1979-
def console_save_asc(con: tcod.console.Console, filename: str) -> bool:
1990+
def console_save_asc(con: tcod.console.Console, filename: str | PathLike[str]) -> bool:
19801991
"""Save a console to a non-delimited ASCII `.asc` file.
19811992
19821993
.. deprecated:: 12.7
19831994
This format is no longer supported.
1995+
1996+
.. versionchanged:: Unreleased
1997+
Added PathLike support.
19841998
"""
1985-
return bool(lib.TCOD_console_save_asc(_console(con), filename.encode("utf-8")))
1999+
return bool(lib.TCOD_console_save_asc(_console(con), bytes(Path(filename))))
19862000

19872001

19882002
@deprecate("This format is not actively supported")
1989-
def console_load_apf(con: tcod.console.Console, filename: str) -> bool:
2003+
def console_load_apf(con: tcod.console.Console, filename: str | PathLike[str]) -> bool:
19902004
"""Update a console from an ASCII Paint `.apf` file.
19912005
19922006
.. deprecated:: 12.7
19932007
This format is no longer supported.
2008+
2009+
.. versionchanged:: Unreleased
2010+
Added PathLike support.
19942011
"""
1995-
return bool(lib.TCOD_console_load_apf(_console(con), filename.encode("utf-8")))
2012+
filename = Path(filename).resolve(strict=True)
2013+
return bool(lib.TCOD_console_load_apf(_console(con), bytes(filename)))
19962014

19972015

19982016
@deprecate("This format is not actively supported")
1999-
def console_save_apf(con: tcod.console.Console, filename: str) -> bool:
2017+
def console_save_apf(con: tcod.console.Console, filename: str | PathLike[str]) -> bool:
20002018
"""Save a console to an ASCII Paint `.apf` file.
20012019
20022020
.. deprecated:: 12.7
20032021
This format is no longer supported.
2022+
2023+
.. versionchanged:: Unreleased
2024+
Added PathLike support.
20042025
"""
2005-
return bool(lib.TCOD_console_save_apf(_console(con), filename.encode("utf-8")))
2026+
return bool(lib.TCOD_console_save_apf(_console(con), bytes(Path(filename))))
20062027

20072028

20082029
@deprecate("Use tcod.console.load_xp to load this file.")
2009-
def console_load_xp(con: tcod.console.Console, filename: str) -> bool:
2030+
def console_load_xp(con: tcod.console.Console, filename: str | PathLike[str]) -> bool:
20102031
"""Update a console from a REXPaint `.xp` file.
20112032
20122033
.. deprecated:: 11.18
20132034
Functions modifying console objects in-place are deprecated.
20142035
Use :any:`tcod.console_from_xp` to load a Console from a file.
2036+
2037+
.. versionchanged:: Unreleased
2038+
Added PathLike support.
20152039
"""
2016-
return bool(lib.TCOD_console_load_xp(_console(con), filename.encode("utf-8")))
2040+
filename = Path(filename).resolve(strict=True)
2041+
return bool(lib.TCOD_console_load_xp(_console(con), bytes(filename)))
20172042

20182043

20192044
@deprecate("Use tcod.console.save_xp to save this console.")
2020-
def console_save_xp(con: tcod.console.Console, filename: str, compress_level: int = 9) -> bool:
2021-
"""Save a console to a REXPaint `.xp` file."""
2022-
return bool(lib.TCOD_console_save_xp(_console(con), filename.encode("utf-8"), compress_level))
2045+
def console_save_xp(con: tcod.console.Console, filename: str | PathLike[str], compress_level: int = 9) -> bool:
2046+
"""Save a console to a REXPaint `.xp` file.
2047+
2048+
.. versionchanged:: Unreleased
2049+
Added PathLike support.
2050+
"""
2051+
return bool(lib.TCOD_console_save_xp(_console(con), bytes(Path(filename)), compress_level))
20232052

20242053

20252054
@deprecate("Use tcod.console.load_xp to load this file.")
2026-
def console_from_xp(filename: str) -> tcod.console.Console:
2027-
"""Return a single console from a REXPaint `.xp` file."""
2028-
path = Path(filename).resolve(strict=True)
2029-
return tcod.console.Console._from_cdata(_check_p(lib.TCOD_console_from_xp(bytes(path))))
2055+
def console_from_xp(filename: str | PathLike[str]) -> tcod.console.Console:
2056+
"""Return a single console from a REXPaint `.xp` file.
2057+
2058+
.. versionchanged:: Unreleased
2059+
Added PathLike support.
2060+
"""
2061+
filename = Path(filename).resolve(strict=True)
2062+
return tcod.console.Console._from_cdata(_check_p(lib.TCOD_console_from_xp(bytes(filename))))
20302063

20312064

20322065
@deprecate("Use tcod.console.load_xp to load this file.")
20332066
def console_list_load_xp(
2034-
filename: str,
2067+
filename: str | PathLike[str],
20352068
) -> list[tcod.console.Console] | None:
2036-
"""Return a list of consoles from a REXPaint `.xp` file."""
2037-
path = Path(filename).resolve(strict=True)
2038-
tcod_list = lib.TCOD_console_list_from_xp(bytes(path))
2069+
"""Return a list of consoles from a REXPaint `.xp` file.
2070+
2071+
.. versionchanged:: Unreleased
2072+
Added PathLike support.
2073+
"""
2074+
filename = Path(filename).resolve(strict=True)
2075+
tcod_list = lib.TCOD_console_list_from_xp(bytes(filename))
20392076
if tcod_list == ffi.NULL:
20402077
return None
20412078
try:
@@ -2051,15 +2088,19 @@ def console_list_load_xp(
20512088
@deprecate("Use tcod.console.save_xp to save these consoles.")
20522089
def console_list_save_xp(
20532090
console_list: Sequence[tcod.console.Console],
2054-
filename: str,
2091+
filename: str | PathLike[str],
20552092
compress_level: int = 9,
20562093
) -> bool:
2057-
"""Save a list of consoles to a REXPaint `.xp` file."""
2094+
"""Save a list of consoles to a REXPaint `.xp` file.
2095+
2096+
.. versionchanged:: Unreleased
2097+
Added PathLike support.
2098+
"""
20582099
tcod_list = lib.TCOD_list_new()
20592100
try:
20602101
for console in console_list:
20612102
lib.TCOD_list_push(tcod_list, _console(console))
2062-
return bool(lib.TCOD_console_list_save_xp(tcod_list, filename.encode("utf-8"), compress_level))
2103+
return bool(lib.TCOD_console_list_save_xp(tcod_list, bytes(Path(filename)), compress_level))
20632104
finally:
20642105
lib.TCOD_list_delete(tcod_list)
20652106

@@ -2992,13 +3033,17 @@ def image_is_pixel_transparent(image: tcod.image.Image, x: int, y: int) -> bool:
29923033
"This function may be removed in the future."
29933034
" It's recommended to load images with a more complete image library such as python-Pillow or python-imageio."
29943035
)
2995-
def image_load(filename: str) -> tcod.image.Image:
3036+
def image_load(filename: str | PathLike[str]) -> tcod.image.Image:
29963037
"""Load an image file into an Image instance and return it.
29973038
29983039
Args:
2999-
filename (AnyStr): Path to a .bmp or .png image file.
3040+
filename: Path to a .bmp or .png image file.
3041+
3042+
.. versionchanged:: Unreleased
3043+
Added PathLike support.
30003044
"""
3001-
return tcod.image.Image._from_cdata(ffi.gc(lib.TCOD_image_load(_bytes(filename)), lib.TCOD_image_delete))
3045+
filename = Path(filename).resolve(strict=True)
3046+
return tcod.image.Image._from_cdata(ffi.gc(lib.TCOD_image_load(bytes(filename)), lib.TCOD_image_delete))
30023047

30033048

30043049
@pending_deprecate()
@@ -3085,7 +3130,7 @@ def image_blit_2x(
30853130

30863131

30873132
@pending_deprecate()
3088-
def image_save(image: tcod.image.Image, filename: str) -> None:
3133+
def image_save(image: tcod.image.Image, filename: str | PathLike[str]) -> None:
30893134
image.save_as(filename)
30903135

30913136

@@ -3382,8 +3427,8 @@ def mouse_get_status() -> Mouse:
33823427

33833428

33843429
@pending_deprecate()
3385-
def namegen_parse(filename: str, random: tcod.random.Random | None = None) -> None:
3386-
lib.TCOD_namegen_parse(_bytes(filename), random or ffi.NULL)
3430+
def namegen_parse(filename: str | PathLike[str], random: tcod.random.Random | None = None) -> None:
3431+
lib.TCOD_namegen_parse(bytes(Path(filename)), random or ffi.NULL)
33873432

33883433

33893434
@pending_deprecate()
@@ -3583,10 +3628,10 @@ def _pycall_parser_error(msg: Any) -> None:
35833628

35843629

35853630
@deprecate("Parser functions have been deprecated.")
3586-
def parser_run(parser: Any, filename: str, listener: Any = None) -> None:
3631+
def parser_run(parser: Any, filename: str | PathLike[str], listener: Any = None) -> None:
35873632
global _parser_listener
35883633
if not listener:
3589-
lib.TCOD_parser_run(parser, _bytes(filename), ffi.NULL)
3634+
lib.TCOD_parser_run(parser, bytes(Path(filename)), ffi.NULL)
35903635
return
35913636

35923637
propagate_manager = _PropagateException()
@@ -3605,7 +3650,7 @@ def parser_run(parser: Any, filename: str, listener: Any = None) -> None:
36053650
with _parser_callback_lock:
36063651
_parser_listener = listener
36073652
with propagate_manager:
3608-
lib.TCOD_parser_run(parser, _bytes(filename), c_listener)
3653+
lib.TCOD_parser_run(parser, bytes(Path(filename)), c_listener)
36093654

36103655

36113656
@deprecate("libtcod objects are deleted automatically.")
@@ -4007,7 +4052,7 @@ def sys_get_renderer() -> int:
40074052

40084053
# easy screenshots
40094054
@deprecate("This function is not supported if contexts are being used.")
4010-
def sys_save_screenshot(name: str | None = None) -> None:
4055+
def sys_save_screenshot(name: str | PathLike[str] | None = None) -> None:
40114056
"""Save a screenshot to a file.
40124057
40134058
By default this will automatically save screenshots in the working
@@ -4022,6 +4067,9 @@ def sys_save_screenshot(name: str | None = None) -> None:
40224067
.. deprecated:: 11.13
40234068
This function is not supported by contexts.
40244069
Use :any:`Context.save_screenshot` instead.
4070+
4071+
.. versionchanged:: Unreleased
4072+
Added PathLike support.
40254073
"""
40264074
lib.TCOD_sys_save_screenshot(bytes(Path(name)) if name is not None else ffi.NULL)
40274075

0 commit comments

Comments
 (0)