anyconfig.processors
¶
Abstract processor module.
-
New in version 0.9.5:
Add to abstract processors such like Parsers (loaders and dumpers).
-
anyconfig.processors.
load_plugins
(pgroup, safe=True)¶ Parameters: - pgroup – A string represents plugin type, e.g. anyconfig_backends
- safe – Do not raise ImportError during load if True
Raises: ImportError
-
anyconfig.processors.
find_with_pred
(predicate, prs)¶ Parameters: - predicate – any callable to filter results
- prs – A list of
anyconfig.models.processor.Processor
classes
Returns: Most appropriate processor class or None
>>> class A(anyconfig.models.processor.Processor): ... _type = "json" ... _extensions = ['json', 'js'] >>> class A2(A): ... _priority = 99 # Higher priority than A. >>> class B(anyconfig.models.processor.Processor): ... _type = "yaml" ... _extensions = ['yaml', 'yml'] >>> prs = [A, A2, B]
>>> find_with_pred(lambda p: 'js' in p.extensions(), prs) <class 'anyconfig.processors.A2'> >>> find_with_pred(lambda p: 'yml' in p.extensions(), prs) <class 'anyconfig.processors.B'> >>> x = find_with_pred(lambda p: 'xyz' in p.extensions(), prs) >>> assert x is None
>>> find_with_pred(lambda p: p.type() == "json", prs) <class 'anyconfig.processors.A2'> >>> find_with_pred(lambda p: p.type() == "yaml", prs) <class 'anyconfig.processors.B'> >>> x = find_with_pred(lambda p: p.type() == "x", prs) >>> assert x is None
-
anyconfig.processors.
find_by_type
(ptype, prs, cls=<class 'anyconfig.models.processor.Processor'>)¶ Parameters: - ptype – Type of the data to process or
anyconfig.models.processor.Processor
class object or its instance - prs – A list of
anyconfig.models.processor.Processor
classes - cls – A class object to compare with ptype
Returns: Most appropriate processor instance to process files of given data type ptype or None
Raises: UnknownProcessorTypeError
- ptype – Type of the data to process or
-
anyconfig.processors.
find_by_type_or_id
(type_or_id, prs, cls=<class 'anyconfig.models.processor.Processor'>)¶ Parameters: - type_or_id – Type of the data to process or ID of the processor class or
anyconfig.models.processor.Processor
class object or its instance - prs – A list of
anyconfig.models.processor.Processor
classes - cls – A class object to compare with type_or_id
Returns: Most appropriate processor instance to process files of given data type or processor type_or_id found by its ID or None
Raises: UnknownProcessorTypeError
- type_or_id – Type of the data to process or ID of the processor class or
-
anyconfig.processors.
find_by_fileext
(fileext, prs)¶ Parameters: - fileext – File extension
- prs – A list of
anyconfig.models.processor.Processor
classes
Returns: Most appropriate processor class to process files with given extentsions or None
Raises: UnknownFileTypeError
-
anyconfig.processors.
find_by_maybe_file
(obj, prs)¶ Parameters: - obj – a file path, file or file-like object, pathlib.Path object or ~anyconfig.globals.IOInfo (namedtuple) object
- cps_by_ext – A list of processor classes
Returns: An instance of most appropriate processor class to process given data
Raises: UnknownFileTypeError
-
anyconfig.processors.
find
(obj, prs, forced_type=None, cls=<class 'anyconfig.models.processor.Processor'>)¶ Parameters: - obj – a file path, file or file-like object, pathlib.Path object or ~anyconfig.globals.IOInfo (namedtuple) object
- prs – A list of
anyconfig.models.processor.Processor
classes - forced_type – 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
Returns: an instance of processor class to process obj data
Raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
-
class
anyconfig.processors.
Processors
(processors=None)¶ Bases:
object
An abstract class of which instance holding processors.
-
register
(*pclss)¶ Parameters: pclss – A list of Processor
or its children classes
-
load_plugins
()¶ Load and register pluggable processor classes internally.
-
list
(sort=True)¶ Returns: A list of Processor
or its children classes
-
find_by_type
(ptype)¶ Parameters: ptype – Processor’s type to find
-
find_by_type_or_id
(type_or_id)¶ Parameters: type_or_id – Processor’s type or ID to find
-
find
(obj, forced_type=None, cls=<class 'anyconfig.models.processor.Processor'>)¶ Parameters: - obj – a file path, file or file-like object, pathlib.Path object or ~anyconfig.globals.IOInfo (namedtuple) object
- forced_type – Forced processor type to find
- cls – A class object to compare with ptype
Returns: an instance of processor class to process ipath data later
Raises: ValueError, UnknownProcessorTypeError, UnknownFileTypeError
-