Skip to content

Commit 3e34000

Browse files
committed
Add pathlib support to tcod.tileset and tcod.image.
1 parent f487667 commit 3e34000

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ v2.0.0
88

99
Unreleased
1010
------------------
11+
Added
12+
- *tcod.image* and *tcod.tileset* now support *pathlib*.
1113

1214
12.6.2 - 2021-06-15
1315
-------------------

tcod/console.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
See :ref:`getting-started` for info on how to set those up.
55
"""
66
import os
7-
import pathlib
87
import warnings
8+
from pathlib import Path
99
from typing import Any, Iterable, Optional, Sequence, Tuple, Union
1010

1111
import numpy as np
@@ -1266,7 +1266,7 @@ def recommended_size() -> Tuple[int, int]:
12661266
return w, h
12671267

12681268

1269-
def load_xp(path: Union[str, pathlib.Path], order: Literal["C", "F"] = "C") -> Tuple[Console, ...]:
1269+
def load_xp(path: Union[str, Path], order: Literal["C", "F"] = "C") -> Tuple[Console, ...]:
12701270
"""Load a REXPaint file as a tuple of consoles.
12711271
12721272
`path` is the name of the REXPaint file to load.
@@ -1308,7 +1308,7 @@ def load_xp(path: Union[str, pathlib.Path], order: Literal["C", "F"] = "C") -> T
13081308

13091309

13101310
def save_xp(
1311-
path: Union[str, pathlib.Path],
1311+
path: Union[str, Path],
13121312
consoles: Iterable[Console],
13131313
compress_level: int = 9,
13141314
) -> None:

tcod/image.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
`an alternative library for graphics rendering
66
<https://wiki.python.org/moin/PythonGameLibraries>`_.
77
"""
8-
9-
10-
from typing import Any, Dict, Tuple
8+
from pathlib import Path
9+
from typing import Any, Dict, Tuple, Union
1110

1211
import numpy as np
1312

@@ -334,7 +333,7 @@ def _get_format_name(format: int) -> str:
334333
" It's recommended to load images with a more complete image library such as python-Pillow or python-imageio.",
335334
category=PendingDeprecationWarning,
336335
)
337-
def load(filename: str) -> np.ndarray:
336+
def load(filename: Union[str, Path]) -> np.ndarray:
338337
"""Load a PNG file as an RGBA array.
339338
340339
`filename` is the name of the file to load.
@@ -343,7 +342,7 @@ def load(filename: str) -> np.ndarray:
343342
344343
.. versionadded:: 11.4
345344
"""
346-
image = Image._from_cdata(ffi.gc(lib.TCOD_image_load(filename.encode()), lib.TCOD_image_delete))
345+
image = Image._from_cdata(ffi.gc(lib.TCOD_image_load(str(filename).encode()), lib.TCOD_image_delete))
347346
array = np.asarray(image, dtype=np.uint8)
348347
height, width, depth = array.shape
349348
if depth == 3:

tcod/tileset.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"""
1313
import itertools
1414
import os
15-
from typing import Any, Iterable, Optional, Tuple
15+
from pathlib import Path
16+
from typing import Any, Iterable, Optional, Tuple, Union
1617

1718
import numpy as np
1819

@@ -200,7 +201,7 @@ def set_default(tileset: Tileset) -> None:
200201
lib.TCOD_set_default_tileset(tileset._tileset_p)
201202

202203

203-
def load_truetype_font(path: str, tile_width: int, tile_height: int) -> Tileset:
204+
def load_truetype_font(path: Union[str, Path], tile_width: int, tile_height: int) -> Tileset:
204205
"""Return a new Tileset from a `.ttf` or `.otf` file.
205206
206207
Same as :any:`set_truetype_font`, but returns a :any:`Tileset` instead.
@@ -210,14 +211,14 @@ def load_truetype_font(path: str, tile_width: int, tile_height: int) -> Tileset:
210211
"""
211212
if not os.path.exists(path):
212213
raise RuntimeError("File not found:\n\t%s" % (os.path.realpath(path),))
213-
cdata = lib.TCOD_load_truetype_font_(path.encode(), tile_width, tile_height)
214+
cdata = lib.TCOD_load_truetype_font_(str(path).encode(), tile_width, tile_height)
214215
if not cdata:
215216
raise RuntimeError(ffi.string(lib.TCOD_get_error()))
216217
return Tileset._claim(cdata)
217218

218219

219220
@deprecate("Accessing the default tileset is deprecated.")
220-
def set_truetype_font(path: str, tile_width: int, tile_height: int) -> None:
221+
def set_truetype_font(path: Union[str, Path], tile_width: int, tile_height: int) -> None:
221222
"""Set the default tileset from a `.ttf` or `.otf` file.
222223
223224
`path` is the file path for the font file.
@@ -239,11 +240,11 @@ def set_truetype_font(path: str, tile_width: int, tile_height: int) -> None:
239240
"""
240241
if not os.path.exists(path):
241242
raise RuntimeError("File not found:\n\t%s" % (os.path.realpath(path),))
242-
if lib.TCOD_tileset_load_truetype_(path.encode(), tile_width, tile_height):
243+
if lib.TCOD_tileset_load_truetype_(str(path).encode(), tile_width, tile_height):
243244
raise RuntimeError(ffi.string(lib.TCOD_get_error()))
244245

245246

246-
def load_bdf(path: str) -> Tileset:
247+
def load_bdf(path: Union[str, Path]) -> Tileset:
247248
"""Return a new Tileset from a `.bdf` file.
248249
249250
For the best results the font should be monospace, cell-based, and
@@ -258,13 +259,13 @@ def load_bdf(path: str) -> Tileset:
258259
""" # noqa: E501
259260
if not os.path.exists(path):
260261
raise RuntimeError("File not found:\n\t%s" % (os.path.realpath(path),))
261-
cdata = lib.TCOD_load_bdf(path.encode())
262+
cdata = lib.TCOD_load_bdf(str(path).encode())
262263
if not cdata:
263264
raise RuntimeError(ffi.string(lib.TCOD_get_error()).decode())
264265
return Tileset._claim(cdata)
265266

266267

267-
def load_tilesheet(path: str, columns: int, rows: int, charmap: Optional[Iterable[int]]) -> Tileset:
268+
def load_tilesheet(path: Union[str, Path], columns: int, rows: int, charmap: Optional[Iterable[int]]) -> Tileset:
268269
"""Return a new Tileset from a simple tilesheet image.
269270
270271
`path` is the file path to a PNG file with the tileset.
@@ -289,7 +290,7 @@ def load_tilesheet(path: str, columns: int, rows: int, charmap: Optional[Iterabl
289290
mapping = []
290291
if charmap is not None:
291292
mapping = list(itertools.islice(charmap, columns * rows))
292-
cdata = lib.TCOD_tileset_load(path.encode(), columns, rows, len(mapping), mapping)
293+
cdata = lib.TCOD_tileset_load(str(path).encode(), columns, rows, len(mapping), mapping)
293294
if not cdata:
294295
_raise_tcod_error()
295296
return Tileset._claim(cdata)

0 commit comments

Comments
 (0)