ExtensionIO

class upsilonconf.io.ExtensionIO(io: ConfigIO, *more_ios: ConfigIO, default_ext: str | None = None)[source]

IO for selecting IOs based on file extensions.

This IO is a mapping from file extensions to other IO objects. Whenever a file is to be read/written, the correct IO is chosen based on the file extension.

Added in version 0.7.0.

Parameters:
ioConfigIO

One of the IO objects to be included. Extensions of this IO will be extracted automatically.

*more_iosConfigIO

Other IO objects to be included. Extensions of these IOs will be extracted automatically.

default_extstr, optional

The extension to use when the file-extension is unknown. E.g. when reading from a file-like object. If not specified, the default extension of io is used.

property extensions

Collection of extensions that are supported by this IO.

property default_ext

Default extension for this IO.

read_from(stream)[source]

Read configuration from a file-like object.

Parameters:
streamTextIO

Readable character stream (file-like object).

Returns:
configdict

A dictionary representing the configuration in the stream.

Raises:
TypeError

If the implementation does not support reading from a stream.

parse_value(val)[source]

Parse string representing a value for a configuration without key.

Added in version 0.8.0.

Parameters:
valstr

The string representing the value.

Returns:
objany basic type

The value object represented by val.

read(path, encoding='utf-8')[source]

Read configuration from a file.

Parameters:
pathPath

Path to a readable text file.

encodingstr, optional

The character encoding to use for the given file.

Returns:
configdict

A dictionary representing the configuration in the file.

write_to(stream, config)[source]

Write configuration to a file-like object.

Parameters:
streamTextIO

Writeable character stream (file-like object).

confMapping

A dictionary representing the configuration to be written.

Raises:
TypeError

If the implementation does not support writing to a stream.

write(config, path, encoding='utf-8')[source]

Write configuration to a file.

Parameters:
confMapping

A dictionary representing the configuration to be written.

pathPath or str

Path to a writeable text file.

encodingstr, optional

The character encoding to use for the given file.