API¶
Config¶
-
class
walter.config.
Config
(author, name, sources=None, search_path=None)[source]¶ Creates a config object.
Parameters: - author (str) – Name of the person or company that created this program. Used on Windows to set the default search path.
- name (str) – Name of this program. Used on Windows to set the default search path.
- sources (iterable) –
An iterable of
Source
objects to pull configuration from. Defaults to the following: - search_path (iterable) – An iterable of directories to search for configuration files. Defaults to the current directory, followed by an appropriate user and site config directory depending on the operating system.
Sources¶
Built-In¶
Creating Your Own¶
-
class
walter.sources.
Source
[source]¶ Base class for configuration sources.
To implement a simple (non-file-based) configuration source, subclass this class and override
__getitem__
.__getitem__
should return a string, or raiseKeyError
if a key isn’t found in the configuration source.If you are implementing an ambient configuration source (e.g. one that reads from environment variables, command-line args, a single file in a well-known location, or something else that doesn’t depend on Walter’s search path), you can expose your
Source
subclass to users directly. If instead you are implementing a file-based source, see alsoFileSource
.
-
class
walter.sources.
FileSource
(filename=None, **kwargs)[source]¶ Base class for file-based configuration sources.
Because Walter implements searching for configuration files internally, and allows for a mix of different types of configuration files, a file-based configuration source consists of two classes.
One is the actual source itself. This is a subclass of
Source
— not this class — and behaves like a normal source, except it takes a file-like object as its first positional argument, and it is an implementation detail that is not exposed to your users.The other is the “meta-source”, which is a subclass of
FileSource
. It is responsible for two things: determining which filenames match the source, and creating new source objects from files. Users will create an instance of the meta-source and pass that to Walter, which will use it to create source instances.While it is possible to override
match_filename()
andcreate()
entirely, most meta-sources should be able to get by with simply setting two properties and adding a docstring:source_class
, your actual source class.pattern
, a default file pattern to match on, which can be either a shell glob or a compiled regular expression.
Unless you override
__init__
, your meta-source will accept afilename
arg that allows users to overridepattern
; any other keyword arguments given to the meta-source will be passed through to the source itself.