anyconfig.processors.utils

Utility functions for anyconfig.processors.

anyconfig.processors.utils.sort_by_prio(prs)

Sort an iterable of processor classes by each priority.

Parameters:

prs (Iterable[TypeVar(ProcT, bound= Processor)]) – A list of anyconfig.models.processor.Processor classes

Return type:

list[TypeVar(ProcT, bound= Processor)]

Returns:

Sambe as above but sorted by priority

anyconfig.processors.utils.select_by_key(items, sort_fn=<built-in function sorted>)

Select items from items by key.

Parameters:

items (Iterable[tuple[tuple[str, ...], Any]]) – A list of tuples of keys and values, [([key], val)]

Return type:

list[tuple[str, list[Any]]]

Returns:

A list of tuples of key and values, [(key, [val])]

>>> select_by_key([(["a", "aaa"], 1), (["b", "bb"], 2), (["a"], 3)])
[('a', [1, 3]), ('aaa', [1]), ('b', [2]), ('bb', [2])]
anyconfig.processors.utils.list_by_x(prs, key)

List items by the factor ‘x’.

Parameters:

key (str) – Grouping key, ‘type’ or ‘extensions’

Return type:

list[tuple[str, list[TypeVar(ProcT, bound= Processor)]]]

Returns:

A list of Processor or its children classes grouped by given ‘item’, [(cid, [Processor)]] by default

anyconfig.processors.utils.findall_with_pred(predicate, prs)

Find all of the items match with given predicates.

Parameters:
Return type:

list[TypeVar(ProcT, bound= Processor)]

Returns:

A list of appropriate processor classes or []

anyconfig.processors.utils.maybe_processor(type_or_id, cls=<class 'anyconfig.models.processor.Processor'>)

Try to get the processor.

Parameters:
  • type_or_id (Union[TypeVar(ProcT, bound= Processor), type[TypeVar(ProcT, bound= Processor)]]) – Type of the data to process or ID of the processor class or anyconfig.models.processor.Processor class object or its instance

  • cls (type[TypeVar(ProcT, bound= Processor)]) – A class object to compare with ‘type_or_id’

Return type:

Optional[TypeVar(ProcT, bound= Processor)]

Returns:

Processor instance or None

anyconfig.processors.utils.find_by_type_or_id(type_or_id, prs)

Find the processor by types or IDs.

Parameters:
Return type:

list[TypeVar(ProcT, bound= Processor)]

Returns:

A list of processor classes to process files of given data type or processor ‘type_or_id’ found by its ID

Raises:

anyconfig.common.UnknownProcessorTypeError

anyconfig.processors.utils.find_by_fileext(fileext, prs)

Find the processor by file extensions.

Parameters:
Return type:

list[TypeVar(ProcT, bound= Processor)]

Returns:

A list of processor class to processor files with given extension

Raises:

common.UnknownFileTypeError

anyconfig.processors.utils.find_by_maybe_file(obj, prs)

Find the processor appropariate for the given file obj.

Parameters:
  • obj (Union[str, Path, IO, IOInfo]) – a file path, file or file-like object, pathlib.Path object or an ‘anyconfig.ioinfo.IOInfo’ (namedtuple) object

  • cps_by_ext – A list of processor classes

Return type:

list[TypeVar(ProcT, bound= Processor)]

Returns:

A list of processor classes to process given (maybe) file

Raises:

common.UnknownFileTypeError

anyconfig.processors.utils.findall(obj, prs, forced_type=None)

Find all of the processors match with the conditions.

Parameters:
  • obj (Union[str, Path, IO, IOInfo, None]) – a file path, file, file-like object, pathlib.Path object or an ‘anyconfig.ioinfo.IOInfo` (namedtuple) object

  • prs (list[TypeVar(ProcT, bound= Processor)]) – A list of anyconfig.models.processor.Processor classes

  • forced_type (Optional[str]) – Forced processor type of the data to process or ID of the processor class or None

Return type:

list[TypeVar(ProcT, bound= Processor)]

Returns:

A list of instances of processor classes to process ‘obj’ data

Raises:

ValueError, common.UnknownProcessorTypeError, common.UnknownFileTypeError

anyconfig.processors.utils.find(obj, prs, forced_type=None)

Find the processors best match with the conditions.

Parameters:
  • obj (Union[str, Path, IO, IOInfo, None]) – a file path, file, file-like object, pathlib.Path object or an ‘anyconfig.ioinfo.IOInfo’ (namedtuple) object

  • prs (list[TypeVar(ProcT, bound= Processor)]) – A list of anyconfig.models.processor.Processor classes

  • forced_type (Union[str, TypeVar(ProcT, bound= Processor), type[TypeVar(ProcT, bound= Processor)], None]) – Forced processor type of the data to process or ID of the processor class or anyconfig.models.processor.Processor class object or its instance itself

  • cls – A class object to compare with ‘forced_type’ later

Return type:

TypeVar(ProcT, bound= Processor)

Returns:

an instance of processor class to process ‘obj’ data

Raises:

ValueError, common.UnknownProcessorTypeError, common.UnknownFileTypeError

anyconfig.processors.utils.load_plugins(pgroup)

Load processor plugins.

A generator function to yield a class object of anyconfig.models.processor.Processor.

Parameters:

pgroup (str) – A string represents plugin type, e.g. anyconfig_backends

Return type:

Iterator[type[TypeVar(ProcT, bound= Processor)]]