Loader
Module¶
One can load objects from different locations. This module provides functionality to load objects from different locations while preserving a simple interface to the cosumer.
-
class
knittingpattern.Loader.
JSONLoader
(process=<function identity>, chooses_path=<function true>)[source]¶ Bases:
knittingpattern.Loader.ContentLoader
Load an process JSON from various locations.
The
process
is called with anobject
as first argument:process(object)
.
-
class
knittingpattern.Loader.
ContentLoader
(process=<function identity>, chooses_path=<function true>)[source]¶ Bases:
knittingpattern.Loader.PathLoader
Load contents of files and ressources.
The
process
is called with astring
as first argument:process(string)
.-
file
(file)[source]¶ Returns: the processed result of the content of a file-like object. Parameters: file¶ – the file-like object to load the content from. It should support the read
method.
-
path
(path)[source]¶ Returns: the processed result of a path's
content.Parameters: path¶ (str) – the path where to load the content from. It should exist on the local file system.
-
-
class
knittingpattern.Loader.
PathLoader
(process=<function identity>, chooses_path=<function true>)[source]¶ Bases:
object
Load paths and folders from the local file system.
The
process
is called with apath
as first argument:process(path)
.-
__init__
(process=<function identity>, chooses_path=<function true>)[source]¶ Create a PathLoader object.
Parameters: - process¶ –
process(path)
is called with the path to load. The result ofprocess
is returned to the caller. The default value isidentity()
, so the paths are returned when loaded. - chooses_path¶ –
chooses_path(path)
is called beforeprocess
and returnsTrue
orFalse
depending on whether a specific path should be loaded and passed toprocess
.
- process¶ –
-
__weakref__
¶ list of weak references to the object (if defined)
-
choose_paths
(paths)[source]¶ Returns: the paths that are chosen by chooses_path()
Return type: list
-
chooses_path
(path)[source]¶ Returns: whether the path should be loaded Return type: bool Parameters: path¶ (str) – the path to the file to be tested
-
example
(relative_path)[source]¶ Load an example from the knitting pattern examples.
Parameters: relative_path¶ (str) – the path to load Returns: the result of the processing You can use
knittingpattern.Loader.PathLoader.examples()
to find out the paths of all examples.
-
examples
()[source]¶ Load all examples form the examples folder of this packge.
Returns: a list of processed examples Return type: list Depending on
chooses_path()
some paths may not be loaded. Every loaded path is processed and returned part of the returned list.
-
folder
(folder)[source]¶ Load all files from a folder recursively.
Depending on
chooses_path()
some paths may not be loaded. Every loaded path is processed and returned part of the returned list.Parameters: folder¶ (str) – the folder to load the files from Return type: list Returns: a list of the results of the processing steps of the loaded files
-
path
(path)[source]¶ load a
path
and return the processed resultParameters: path¶ (str) – the path to the file to be processed Returns: the result of processing step
-
relative_file
(module, file)[source]¶ Load a file relative to a module.
Parameters: Returns: the result of the processing
-
relative_folder
(module, folder)[source]¶ Load a folder located relative to a module and return the processed result.
Parameters: Returns: a list of the results of the processing
Return type: Depending on
chooses_path()
some paths may not be loaded. Every loaded path is processed and returned part of the returned list. You can usechoose_paths()
to find out which paths are chosen to load.
-