Skip to main content

Latest PyPI Version License Wheel Status Downloads

Fileconfig turns config file sections into instances of your class. Create a class referring to an INI file collecting the arguments for the different instances to be created. Calling the class with the section name as parameter will return the instance with the parameters specified in the given section.

Installation

This package runs under Python 2.7 and 3.3+, use pip to install:

$ pip install fileconfig

Usage

Create as subclass of fileconfig.Config and set its filename attribute to the path of your INI file.

If the filename is relative, it is resolved relative to the path of the module where your class is defined (i.e. not relative to the current working directory if its file not happens do be there).

>>> import fileconfig

>>> class Cfg(fileconfig.Config):
...     filename = 'docs/pet-shop.ini'
...     def __init__(self, key, **kwargs):
...         self.key = key
...         self.__dict__.update(kwargs)

On instance creation, the __init__ method will be called with the section name (key) and the keyword parameters from the given section of the specified file.

Suppose your INI file begins like this:

[parrot]
species = Norwegian blue
can_talk = yes
quantity = 0
characteristics = beautiful plumage, pining for the fjords

To retrieve this instance, call the class with its section name.

>>> c = Cfg('parrot')

>>> print(c)
{
  'can_talk': 'yes',
  'characteristics': 'beautiful plumage, pining for the fjords',
  'key': 'parrot',
  'quantity': '0',
  'species': 'Norwegian blue'
}

Singleton

Only one instance will be created, cached and returned for each config file section (a.k.a. the singleton pattern):

>>> Cfg('parrot') is c
True

The constructor is also idempotent:

>>> Cfg(c) is c
True

The default __repr__ of instances allows round-trips:

>>> c
__main__.Cfg('parrot')

Aliasing

You can specify a space-delimited list of aliases for each section:

[slug]
aliases = snail special_offer
species = slug
can_talk = no
quantity = 1

For changing the delimiter, see below.

Aliases map to the same instance:

>>> s = Cfg('special_offer')

>>> s
__main__.Cfg('slug')

>>> s is Cfg('snail') is Cfg('slug')
True

Inspect instance names (key + aliases):

>>> s.key
'slug'

>>> s.aliases
['snail', 'special_offer']

>>> s.names
['slug', 'snail', 'special_offer']

Inheritance

Config file sections can inherit from another section:

[Polly]
inherits = parrot
can_talk = no
characteristics = dead, totally stiff, ceased to exist

Specified keys override inherited ones:

>>> print(Cfg('Polly'))
{
  'can_talk': 'no',
  'characteristics': 'dead, totally stiff, ceased to exist',
  'inherits': 'parrot',
  'key': 'Polly',
  'quantity': '0',
  'species': 'Norwegian blue'
}

Sections can inherit from a single section. Multiple or transitive inheritance is not supported.

Introspection

Use the class to iterate over the instances from all section:

>>> list(Cfg)
[__main__.Cfg('parrot'), __main__.Cfg('slug'), __main__.Cfg('Polly')]

Print the string representation of all instances:

>>> Cfg.pprint_all()  # doctest: +ELLIPSIS
{
  'can_talk': 'yes',
  'characteristics': 'beautiful plumage, pining for the fjords',
  'key': 'parrot',
...

Hints

Apart from the key, aliases, and inherits parameters, your __init__ method receives the unprocessed strings from the config file parser.

Use the __init__ method to process the other parameters to fit your needs.

>>> class Pet(Cfg):
...     def __init__(self, can_talk, quantity, characteristics=None, **kwargs):
...         self.can_talk = {'yes':True, 'no': False}[can_talk]
...         self.quantity = int(quantity)
...         if characteristics is not None and characteristics.split():
...             self.characteristics = [c.strip() for c in characteristics.split(',')]
...         super(Pet, self).__init__(**kwargs)

>>> print(Pet('Polly'))
{
  'can_talk': False,
  'characteristics': ['dead', 'totally stiff', 'ceased to exist'],
  'inherits': 'parrot',
  'key': 'Polly',
  'quantity': 0,
  'species': 'Norwegian blue'
}

This way, the __init__ method also defines parameters as required or optional, set their defaults, etc.

Overlay

Sometimes one wants to combine multiple config files, e.g. have a default file included in the package directory, overridden by a user-supplied file in a different location.

To support this, subclass fileconfig.Stacked and set the filename to the location of the default config.

>>> class Settings(fileconfig.Stacked):
...     filename = 'docs/pet-shop.ini'

Use the add method to load an overriding config file on top of that:

>>> Settings.add('docs/lumberjack.ini')

If the filename is relative, it is resolved relative to the path of the module where the add method has been called.

You can access the sections from all files:

>>> print(Settings('Bevis'))
{
  'can_talk': 'yes',
  'characteristics': "sleeps all night, works all day, puts on women's clothing",
  'key': 'Bevis',
  'species': 'human'
}

As long as they have different names:

>>> print(Settings('Polly'))
{
  'can_talk': 'no',
  'characteristics': 'dead, totally stiff, ceased to exist',
  'inherits': 'parrot',
  'key': 'Polly',
  'quantity': '0',
  'species': 'Norwegian blue'
}

Config files added to the top of the stack mask sections with the same names from previous files:

>>> print(Settings('parrot'))
{
  'characteristics': 'unsolved problem',
  'key': 'parrot'
}

Customization

To use a different delimiter for aliases override the _split_aliases method on your class. Make it a staticmethod or classmethod that takes a string argument and returns the splitted list.

By default, fileconfig will use ConfigParser.SafeConfigParser from the standard library to parse the config file. To use a different parser, override the _parser attribute in your fileconfig.Config subclass.

To specify the encoding from which the config file should be decoded by the config parser, override the _encoding attribute on your subclass.

Fileconfig raises an error, if the config file is not found. If you want this error to pass silently instead, set the _pass_notfound attribute on your subclass to True.

Potential issues

This package uses sys._getframe (which is almost the same as inspect.currentframe, see docs). Under IronPython this might require enabling the FullFrames option of the interpreter.

License

Fileconfig is distributed under the MIT license.

Metadata

Release files for fileconfig 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fileconfig 0.5.1
File Size Uploaded
fileconfig-0.5.1.zip 20.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fileconfig 0.5.1
File Interpreter ABI Platform
fileconfig-0.5.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 32.4 kB

Release files / fileconfig-0.5.1.zip

Download URL fileconfig-0.5.1.zip
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7636e0dd396fe782e5ce678ef8047c8c402debdfea57d831311a5cd85bb3045e
BLAKE2b-256 checksum
How to use checksums
54beb208f1b2d1bc314a38c5e0e54e6d2be7cd8af7cbae93dd053583e18f567d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / fileconfig-0.5.1-py2.py3-none-any.whl

Download URL fileconfig-0.5.1-py2.py3-none-any.whl
Size 12.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3b4ff32a1e8bad0719fe8ef3d608415a6ffb4248648cd00a102a9f63e66b4746
BLAKE2b-256 checksum
How to use checksums
011d3e128a82224dc3f99fffa71fb7242b3c2501b0f716469eadfcfd877b6382
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.7

2 release files

0.6.1

2 release files

0.6

2 release files

0.5.10

2 release files

0.5.9

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

This release

0.5.1 This release

2 release files

0.5

1 release file

0.4.1

1 release file

0.4

1 release file

0.3.1

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page