anyconfig.ioinfo

Functions for value objects represent inputs and outputs.

New in version 0.9.5.

  • Add functions to make and process input and output object holding some attributes like input and output type (path, stream or pathlib.Path object), path, opener, etc.
anyconfig.ioinfo.guess_io_type(obj)

Guess input or output type of obj.

Parameters:obj – a path string, a pathlib.Path or a file / file-like object
Returns:IOInfo type defined in anyconfig.globals.IOI_TYPES
>>> apath = "/path/to/a_conf.ext"
>>> assert guess_io_type(apath) == IOI_PATH_STR
>>> from anyconfig.compat import pathlib
>>> if pathlib is not None:
...     assert guess_io_type(pathlib.Path(apath)) == IOI_PATH_OBJ
>>> assert guess_io_type(open(__file__)) == IOI_STREAM
anyconfig.ioinfo.inspect_io_obj(obj)
Parameters:obj – a path string, a pathlib.Path or a file / file-like object
Returns:A tuple of (objtype, objpath, objopener)
Raises:UnknownFileTypeError
anyconfig.ioinfo.find_by_fileext(fileext, cps_by_ext)
Parameters:
  • fileext – File extension
  • cps_by_ext – A list of pairs (file_extension, [processor_class])
Returns:

Most appropriate processor class to process given file

>>> from anyconfig.backends import _PARSERS_BY_EXT as cps
>>> find_by_fileext("json", cps)
<class 'anyconfig.backend.json.Parser'>
>>> find_by_fileext("ext_should_not_be_found", cps) is None
True
anyconfig.ioinfo.find_by_filepath(filepath, cps_by_ext)
Parameters:
  • filepath – Path to the file to find out processor to process it
  • cps_by_ext – A list of pairs (file_extension, [processor_class])
Returns:

Most appropriate processor class to process given file

>>> from anyconfig.backends import _PARSERS_BY_EXT as cps
>>> find_by_filepath("/a/b/c/x.json", cps)
<class 'anyconfig.backend.json.Parser'>
>>> find_by_filepath("/path/to/a.ext_should_not_be_found", cps) is None
True
anyconfig.ioinfo.find_by_type(cptype, cps_by_type)
Parameters:
  • cptype – Config file’s type
  • cps_by_type – A list of pairs, (processor_type, [processor_class])
Returns:

Most appropriate processor class to process given type or None

>>> from anyconfig.backends import _PARSERS_BY_TYPE as cps
>>> find_by_type("json", cps)
<class 'anyconfig.backend.json.Parser'>
>>> find_by_type("missing_type", cps) is None
True
anyconfig.ioinfo.find_processor(ipath, cps_by_ext, cps_by_type, forced_type=None)
Parameters:
  • ipath – file path
  • cps_by_ext – A list of pairs (file_extension, [processor_cls])
  • cps_by_type – A list of pairs (processor_type, [processor_cls])
  • forced_type – Forced processor type or processor object
Returns:

Instance of processor class appropriate for the input ipath

Raises:

ValueError, UnknownParserTypeError, UnknownFileTypeError

anyconfig.ioinfo.make(obj, cps_by_ext, cps_by_type, forced_type=None)
Parameters:
  • obj – a path string, a pathlib.Path or a file / file-like object
  • cps_by_ext – A list of pairs (file_extension, [processor_cls])
  • cps_by_type – A list of pairs (processor_type, [processor_cls])
  • forced_type – Forced configuration processor type
Returns:

Namedtuple object represents a kind of input object such as a file / file-like object, path string or pathlib.Path object

Raises:

ValueError, UnknownParserTypeError, UnknownFileTypeError