bear_hug.resources module

Loaders for the various ASCII-art formats.

class bear_hug.resources.ASCIILoader

Bases: object

A base class for all the resource loaders. It knows how to return its chars and colors (or a fragment thereof), but expects the children to do their loading by themselves.

get_image()

Return the entire chars and colors of this loader

Returns:chars, colors (two 2-nested lists of equal size)
get_image_region(x, y, xsize, ysize)

Return some rectangular region of this loader’s chars and colors.

Parameters:
  • x – X coordinate of the leftmost column of required region.
  • y – Y coordinate of the topmost row of required region.
  • xsize – width of the required region.
  • ysize – height of the required region.
Returns:

chars, colors (two 2-nested lists of equal size).

class bear_hug.resources.TxtLoader(filename, default_color='white', load_file=False)

Bases: bear_hug.resources.ASCIILoader

A loader that reads a plaintext file.

Accepts a filename (anything acceptable by open()) as a single position argument. Since plaintext files don’t store colour data, all loaded chars will be the same colour.

Parameters:
  • default_color – the color of chars.
  • load_file – if True, the file is parsed immediately. Otherwise, only its existence is checked on loader creation, but the file is not parsed until something is required from this loader.
get_image()

Return the entire chars and colors of this loader :returns: chars, colors (two 2-nested lists of equal size)

get_image_region(x, y, xsize, ysize)

Return some rectangular region of this loader’s chars and colors.

Parameters:
  • x – X coordinate of the leftmost column of required region.
  • y – Y coordinate of the topmost row of required region.
  • xsize – width of the required region.
  • ysize – height of the required region.
Returns:

chars, colors (two 2-nested lists of equal size).

class bear_hug.resources.XpLoader(filename, default_color='white')

Bases: bear_hug.resources.ASCIILoader

A loader that reads REXPaint *.xp files. The file is never parsed until one of the get_* methods gets called. Its existence, though, is checked on Loader creation.

As the bear_hug widget API does not allow multi-layered widgets, get_image and get_image_region return the image with the only the character and color from the highest non-empty layer. For getting data from layers separately, use get_layer and get_layer_region.

Background colors are ignored altogether.

Most of the XP parsing in this class code is taken from MIT-licensed XPLoaderPy3, copyright Sean Hagar, Erwan Castioni and Gawein Le Goff.

get_image()

Return chars and colors for the entire image. For each cell only the values from the topmost layer are used. Background colors are ignored altogether.

Returns:chars, colors (2 2-nested lists)
get_image_region(x, y, xsize, ysize)

Return chars and colors for the image region.

For each cell only the values from the topmost layer are used. Background colors are ignored altogether.

Parameters:
  • x – leftmost column of the required region.
  • y – topmost row of the required region.
  • xsize – width of the required region
  • ysize – height of the required region
Returns:

chars, colors (2 2-nested lists)

get_layer(layer)

Get chars and (foreground) colors for the entire image layer.

This method does not check layer size and returns whatever is available. By default, the REXPaint creates layers the size of the entire image, so this shouldn’t be much of an issue.

Parameters:layer – layer number
Returns:chars, colors (2 2-nested lists)
get_layer_region(layer, x, y, xsize, ysize)

Return a rectangular region from a certain layer.

Parameters:
  • layer – layer number
  • x – leftmost column of the required region.
  • y – topmost row of the required region.
  • xsize – width of the required region
  • ysize – height of the required region
Returns:

chars, colors (2 2-nested lists)

class bear_hug.resources.Atlas(loader, json_file)

Bases: object

An image atlas.

An instance of this class accepts a Loader instance and a path to the JSON file. The latter is parsed immediately and should contain a list of objects, each of which has five keys: “name”, “x”, “y”, “xsize” and “ysize”. Other keys, if any, are ignored. The purpose of this is, basically, to be able to address image regions by a human-readable name, so coordinates and sizes should describe valid regions in the loader. A single region may be called by multiple names, but not the other way around. Other values for the elements, if any, are ignored.

Parameters:
  • loader – a Loader instance.
  • json_file – path to a JSON file (str).
get_element(name)

Return an element with a given name.

If nothing with this name was described in JSON file, raises KeyError

Parameters:name – A sub-image ID.
Returns:A region (chars, colors)
class bear_hug.resources.Multiatlas(atlases)

Bases: object

A class that contains multiple atlases and fetches elements from whichever has the element you need.

This class supports the get_element method similar to Atlas, but does not inherit from it. Consequently, Multiatlas can be created empty and its atlas list can be extended with valid atlases later.

Parameters:atlases – an iterable of Atlas instances
add_atlas(atlas)

Add an atlas to the multiatlas

Checks whether an atlas is valid and has any names shared with other atlases. If so, throws an exception :param atlas: :return: