wmwpy.utils package

Subpackages

Submodules

wmwpy.utils.XMLTools module

wmwpy.utils.XMLTools.findTag(root: ElementBase, tag: str) ElementBase[source]

Find an element in an etree element.

Parameters:
  • root (etree.ElementBase) – The root element to search.

  • tag (str) – The element to find.

Returns:

etree Element

Return type:

etree.ElementBase

wmwpy.utils.XMLTools.strbool(value: str) bool[source]

Convert a string to a bool

Parameters:

value (str) – String. Can be ‘true’, ‘false’, ‘t’, ‘f’, ‘0’, or ‘1’. The string is case insensative.

Returns:

Python bool object.

Return type:

bool

wmwpy.utils.color module

wmwpy.utils.color.hls_to_rgb(hls_array: ndarray) ndarray[source]

Expects an array of shape (X, 3), each row being HLS colours. Returns an array of same size, each row being RGB colours. Like colorsys python module, all values are between 0 and 1. NOTE: like colorsys, this uses HLS rather than the more usual HSL

wmwpy.utils.color.hsv_to_rgb(hsv_array: ndarray) ndarray[source]

Expects an array of shape (X, 3), each row being HSV colours. Returns an array of same size, each row being RGB colours. Like colorsys python module, all values are between 0 and 1.

wmwpy.utils.color.rgb_to_hls(rgb_array: ndarray) ndarray[source]

Expects an array of shape (X, 3), each row being RGB colours. Returns an array of same size, each row being HLS colours. Like colorsys python module, all values are between 0 and 1. NOTE: like colorsys, this uses HLS rather than the more usual HSL

wmwpy.utils.color.rgb_to_hsv(rgb_array: ndarray) ndarray[source]

Expects an array of shape (X, 3), each row being RGB colours. Returns an array of same size, each row being HSV colours. Like colorsys python module, all values are between 0 and 1.

wmwpy.utils.filesystem module

class wmwpy.utils.filesystem.File(parent, path: str, data: bytes | str | BytesIO)[source]

Bases: FileBase

add(path: str, file: bytes, replace=False)[source]

Add file to folder.

Parameters:
  • path (str) – New file path.

  • content (bytes) – Content of file in bytes.

  • replace (bool, optional) – Whether to replace any conflicting file.. Defaults to False.

Raises:
  • NotADirectoryError – Path to file contains file, not folder.

  • FileExistsError – File already exists.

Returns:

Newly added File.

Return type:

File

exists(path: str)[source]

Tests whether path exists.

Parameters:

path (str) – Path to check.

Returns:

Does path exist?

Return type:

bool

property extension: str

The file extension

Returns:

File extension

Return type:

str

listdir(recursive=False, search='*')[source]

Returns a list of files and subfolders in path.

Parameters:
  • path (str, optional) – Path to folder to list. Defaults to ‘/’.

  • recursive (bool, optional) – Whether to include subfolders. Defaults to False.

Returns:

List of files and subfolders.

Return type:

list

property rawdata: BytesIO

Returns the raw data of the file as a BytesIO object.

Returns:

Raw data of file.

Return type:

io.BytesIO

read(mime=None, extension=None, **kwargs)[source]

Read file.

Returns:

Object for file.

Return type:

Any

setReader(extension=None, mime=None, **kwargs)[source]
testFile()[source]

Tests what type of file this is.

write(data: bytes, extension=None, mime=None) int[source]

Write data to file.

Parameters:

data (bytes) – New data.

Returns:

bytes written.

Return type:

int

class wmwpy.utils.filesystem.FileBase(parent, path: str)[source]

Bases: object

detatch()[source]

Detatch this file or folder from it’s parent.

property filesystem: Filesystem

Returns the Filesystem or root Folder for this File or Folder.

Returns:

Root Filesystem or Folder

Return type:

Filesystem | Folder

get(path: str)[source]

Get file in filesystem.

Parameters:

path (str) – Path to file or folder.

Returns:

File or Folder object.

Return type:

File or Folder

is_dir() bool[source]

Check whether this is a folder.

Returns:

bool

name = ''
property path: str

Path to this file or folder from the root.

Returns:

Path this file or folder.

Return type:

str

remove(path: str = '.')[source]

Remove file or folder.

Parameters:

path (str, optional) – Path to file or folder. If ‘.’, it removes itself. Defaults to ‘.’.

property root: Folder

Root Folder for this File or Folder.

Returns:

Root Folder for this File or Folder

Return type:

Folder

class wmwpy.utils.filesystem.Filesystem(gamepath: str, assets: str, baseassets: str = '/')[source]

Bases: object

add(path: str, file: str | bytes, replace=False)[source]

Adds file with path to Folder.

Parameters:
  • path (str) – Path to destenation of file. Must be str.

  • file (str | bytes) – File. This can be a path to the file to use, raw bytes of the file, or file-like object.

  • replace (bool, optional) – Replace any conflicting files? Defaults to False.

Raises:

TypeError – If file is not str, bytes or file-like object.

Returns:

File object of the added file.

Return type:

File

dump(folder: str | None = None, callback: Callable[[int, str, int], Any] | None = None)[source]

Dump the contents of the filesystem to the specified directory

Parameters:
  • folder (str, optional) – Path to output directory. Defaults to original path.

  • callback (Callable[[int, str, int], Any], optional) – A callback function to be ran while dumping the filesystem. Defaults to None.

exists(fp: str) bool[source]

Test if file path exists.

Parameters:

fp (str) – File path.

Returns:

Whether the path exists.

Return type:

bool

get(path: str)[source]

Get file in filesystem.

Parameters:

path (str) – Path to file or folder.

Returns:

File or Folder object.

Return type:

File or Folder

getAssets(extract_zip=False, split_imagelist=False, load_callback: Callable[[int, str, int], Any] | None = None)[source]

Scans the assets directory and loads all files into the filesystem. This is so wmwpy can modify files without modifying the actual files.

Parameters:
  • extract_zip (bool, optional) – Extrack zip files? Defaults to False.

  • split_imagelist (bool, optional) – Split imagelist files? Defaults to False.

  • hook (Callable[[int, str, int], Any], optional) – Hook for loading assets, useful for guis. The function gets called with the paramaters (progress : int, current : str, max : int). Defaults to None.

Raises:

FileNotFoundError – Assets folder does not exist.

Returns:

Current Filesystem object.

Return type:

this

listdir(path='/', recursive=False, search='*') list[source]

Returns a list of files and subfolders in path.

Parameters:
  • path (str, optional) – Path to folder to list. Defaults to ‘/’.

  • recursive (bool, optional) – Whether to include subfolders. Defaults to False.

Returns:

List of files and subfolders.

Return type:

list

remove(path: str)[source]

Remove a file or folder

Parameters:

path (str) – Path to file or folder to remove.

class wmwpy.utils.filesystem.Folder(parent=None, path: str | None = None)[source]

Bases: FileBase

add(path: str, content: bytes, replace=False) File[source]

Add file to folder.

Parameters:
  • path (str) – New file path.

  • content (bytes) – Content of file in bytes.

  • replace (bool, optional) – Whether to replace any conflicting file.. Defaults to False.

Raises:
  • NotADirectoryError – Path to file contains file, not folder.

  • FileExistsError – File already exists.

Returns:

Newly added File.

Return type:

File

exists(path: str) bool[source]

Tests whether path exists.

Parameters:

path (str) – Path to check.

Returns:

Does path exist?

Return type:

bool

get(path: str) File[source]

Get file in filesystem.

Parameters:

path (str) – Path to file or folder.

Returns:

File or Folder object.

Return type:

File or Folder

listdir(recursive=False, search='*')[source]

Returns a list of files and subfolders in path.

Parameters:
  • path (str, optional) – Path to folder to list. Defaults to ‘/’.

  • recursive (bool, optional) – Whether to include subfolders. Defaults to False.

Returns:

List of files and subfolders.

Return type:

list

wmwpy.utils.gif module

class wmwpy.utils.gif.TransparentAnimatedGifConverter(img_rgba: Image, alpha_threshold: int = 0)[source]

Bases: object

process() Image[source]

Return the processed mode P Image.

wmwpy.utils.gif.save_transparent_gif(images: List[Image], durations: int | List[int], save_file) Image[source]

Creates a transparent GIF, adjusting to avoid transparency issues that are present in the PIL library

Note that this does NOT work for partial alpha. The partial alpha gets discarded and replaced by solid colors.

Parameters:
  • images – a list of PIL Image objects that compose the GIF frames

  • durations – an int or List[int] that describes the animation durations for the frames of this GIF

  • save_file – A filename (string), pathlib.Path object or file object. (This parameter corresponds

and is passed to the PIL.Image.save() method.)

Returns:

Image - The PIL Image object (after first saving the image to the specified target)

wmwpy.utils.imageprocessing module

wmwpy.utils.imageprocessing.recolor_image(image: Image, colorRGBA: tuple[int, int, int, int]) Image[source]

Replace color in image

Parameters:
  • image (PIL.Image.Image) – Input image

  • color (tuple[int,int,int,int]) – The color to recolor the image with

Returns:

resulting image

Return type:

PIL.Image.Image

wmwpy.utils.logging_utils module

wmwpy.utils.path module

wmwpy.utils.path.joinPath(*args: str) str[source]

Join a list of paths to create a new path, while ignoring any forward or backward slashes (/ \)

Parameters:

*args

(str): str path

Returns:

New path

Return type:

str

wmwpy.utils.path.makeRelativePath(path: str) str[source]

Remove forward and backward slashes (/ \) from the beginning of a path.

Parameters:

path (str) – Path to check

Returns:

New path without a forward or backward slash.

Return type:

str

wmwpy.utils.rotate module

wmwpy.utils.rotate.rotate(point: tuple[float, float], degrees: float = 0)[source]

Rotate a point around (0,0)

Parameters:
  • point (tuple[float,float]) – Point to rotate.

  • degrees (float, optional) – Amount of degrees to rotate. Defaults to 0.

Returns:

New point (x,y)

Return type:

tuple[float,float]

wmwpy.utils.textures module

class wmwpy.utils.textures.HDFile(file: str | File, HD: bool = True, TabHD: bool = False, filesystem: Filesystem | Folder | None = None, gamepath: str | None = None, assets: str = '/assets', baseassets: str = '/')[source]

Bases: GameObject

property filename: str
wmwpy.utils.textures.getHDFile(file: str, HD=True, TabHD=False, filesystem: Filesystem | None = None, gamepath: str | None = None, assets: str = '/assets', baseassets: str = '/') str[source]

Get HD filename.

Parameters:
  • file (str) – Filename. Must be a string.

  • HD (bool, optional) –

Returns:

HD filename.

Return type:

str

wmwpy.utils.textures.getTexture(path: str, textureSettings: dict, size: tuple, cache=True) Image[source]

Get image.

Parameters:
  • path (str) – Path to Image.

  • textureSettings (dict) – Texture settings.

  • size (tuple[width,height]) – Size of image.

  • cache (bool, optional) – Whether to cache waltex images. Defaults to True.

Returns:

PIL Image.

Return type:

Image.Image

wmwpy.utils.waltex module

wmwpy.utils.waltex.GenerateBinaryMask(numOnes)[source]
class wmwpy.utils.waltex.Waltex(file: str | bytes, byte_order: str = 'little')[source]

Bases: object

format = 'waltex'
format_description = 'Walaber image file'
read(byte_order: str | None = None) Image[source]

Read the waltex image.

Parameters:

byte_order (str, optional) – The byte order. Can be ‘little’ or ‘big’. Defaults to None.

Returns:

PIL Image object

Return type:

PIL.Image.Image

property size

Return size of image

Returns:

tuple(width, height)

Return type:

tuple[int,int]

wmwpy.utils.waltex.WaltexImage(path: str | bytes | BytesIO, premultiplyAlpha: bool = False, dePremultiplyAlpha: bool = False, endian: str = 'little') Image[source]

Depracted

Use Image.load() instead.

I am only keeping this just in case you need to load RGB565 (01) or RGBA5551 (02) waltex format.

Get image from waltex file

Data on image can be found in coorisponding imagelist or in Data/TextureSettings.xml.

param path:

Path to waltex image

type path:

str

param premultiplyAlpha:

Defaults to False.

type premultiplyAlpha:

bool, optional

param dePremultiplyAlpha:

Defaults to False.

type dePremultiplyAlpha:

bool, optional

param endian:

Endian mode. Set to ‘big’ to use big endian. Defaults to ‘little’.

type endian:

str, optional

returns:

Pillow image.

rtype:

PIL.Image.Image

Thanks to @campbellsonic for the WrapRawData() function.

wmwpy.utils.waltex.WrapRawData(rawData: bytes, width: int, height: int, bytesPerPixel: int, redBits: int, greenBits: int, blueBits: int, alphaBits: int, colorOrder: str, premultiplyAlpha: bool = False, dePremultiplyAlpha: bool = False, offset: int = 0, endian='little')[source]

Module contents