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 an object as first argument: process(object).

object(object_)[source]

Processes an already loaded object.

Returns:the result of the processing step
Parameters:object – the object to be loaded
string(string)[source]

Load an object from a string and return the processed JSON content

Returns:the result of the processing step
Parameters:string (str) – the string to load the JSON from
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 a string 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.
string(string)[source]
Returns:the processed result of a string
Parameters:string (str) – the string to load the ocntent from
url(url, encoding='UTF-8')[source]

load and process the content behind a url

Returns:

the processed result of the url's content

Parameters:
  • url (str) – the url to retrieve the content from
  • encoding (str) – the encoding of the retrieved content. The default encoding is UTF-8.
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 a path as first argument: process(path).

__init__(process=<function identity>, chooses_path=<function true>)[source]

Create a PathLoader object.

Parameters:
  • processprocess(path) is called with the path to load. The result of process is returned to the caller. The default value is identity(), so the paths are returned when loaded.
  • chooses_pathchooses_path(path) is called before process and returns True or False depending on whether a specific path should be loaded and passed to 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 result

Parameters: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:
  • module (str) –

    can be

    • a path to a folder
    • a path to a file
    • a module name
  • folder (str) – the path of a folder relative to module
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:
  • module (str) –

    can be

    • a path to a folder
    • a path to a file
    • a module name
  • folder (str) – the path of a folder relative to module
Returns:

a list of the results of the processing

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. You can use choose_paths() to find out which paths are chosen to load.

knittingpattern.Loader.true(_)[source]
Returns:True
Parameters:_ – can be ignored
knittingpattern.Loader.identity(object_)[source]
Returns:the argument
Parameters:object_ – the object to be returned