Skip to main content
# confypy

Python **2.6**, **2.7**, **3.2** and **3.3** compatible

## Simple Configuration Loading and Management


## Usage

Once a config is configured with locations, it's values are accessed through
the `data` attribute. Key or Attribute access is valid:

```python
from confypy import Config
from confypy import Location

config = Config()
config.locations = [Location.from_env_keys(['FOO', 'BAR', 'BAZ'])]

print(config.data.FOO)
print(config.data.BAR)
print(config.data.BAZ)

# OR

print(config.data['FOO'])
print(config.data['BAR'])
print(config.data['BAZ'])

```

## Built-In Parsers
By *built-in*, we mean confypy will automatically check for common
config parsers of the following types:

- json (falls back to simplejson)
- yaml (pyyaml required)
- configparser (Python's own ConfigParser)
- python (load a module's __dict__)

It determines the parser to use by checking the file extension of
any file paths. However, you are also free to override this.

The parser can manually be set in the Location.* declaration.

For example, each of the following assumes JSON:

```python
Location.from_path('/data/foo.json')
```

So does this:
```python
Location.from_path('/data/foo', parser='json')
```

And so does this:

```python
import json
Location.from_path('/data/foo', parser=json.loads)
```

And this too:
*Note that the imported 'json' here is just a reference to the json.loads callable*

```python
import json
from confypy import Location
from confypy.loaders import load_file
from confypy.parsers import json

location = Location('data/foo', loader=load_file, parser=json)

```

Yes, the parser argument to Location can be a `string` OR a `callable`.
In fact, the various Location.* factories just handle passing the parser
arg for you.

Parsers are chosen from strings and file extensions with the following
rules: (see confypy.parsers.parser_for_ext)

```python
def parser_for_ext(ext):

mapping = {
'yml': yaml,
'yaml': yaml,
'json': json,
'ini': configparser,
'configparser': configparser
}

result = mapping.get(ext.lower(), None)

if result:
return result

raise ParserNotFound(ext)
```


## Defaults

Each config supports a set of defaults provided at creation time.

```python
from confypy import Config
from confypy import Location


defaults = {'foo':1, 'bar':2, 'baz':3}
config = Config(defaults=defaults)
```

If no locations are present, or if none of the locations, when loaded, return
any valid data, the defaults will be when looking up values.

When locations are chained, the defaults represent the last location for a
lookup before an error is raised.


## Examples


#### Stop loading after the 1st successful load.

In the example below, the `TEST_SETTINGS` environment variable will be
checked first. It's assumed that it's value will be a file path:
`from_env_path`. It's also possible to just load environment variables
as key/values which will follow in another example.

So `TEST_SETTINGS` will be checked first, if the file exists, the drama
stops there. If it doesn't exist, it moves on to the next Location
provided in the locations list.

Assuming our file contains this JSON:

```json
{
"name": "Lucy"
}
```

We can do this:

```python
from confypy import Config
from confypy import Location

config = Config()
config.locations = [
Location.from_env_path('TEST_SETTINGS'),
Location.from_path('/data/foo.json')
]

print(config.data.name)
```

#### Try to load everything and chain the results together.

The resultant config can be chained. Values will be looked up starting
from the last location provided in the locations list, falling **up**
to the first location provided in the location list.

In other words the lookup order is like this in a chain:

```python
from confypy import Config
from confypy import Location

config = Config(chain=True)
config.locations = [
Location.from_env_path('TEST_SETTINGS'), # 5
Location.from_path('/data/foo.json'), # 4 ^
Location.from_path('/data/foo.yaml'), # 3 ^
Location.from_env_keys(['FOO', 'BAR', 'BAZ']), # 2 ^
Location.from_python('path.to.my.module') # 1 ^
]
```

In order to get this chaining, the `Config` must be initialized with the
argument `chain=True`

Metadata

Release files for confypy 0.2.5

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

Source distribution (sdist)

Source distribution for confypy 0.2.5
File Size Uploaded
confypy-0.2.5.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for confypy 0.2.5
File Interpreter ABI Platform
confypy-0.2.5-py2-none-any.whl Python 2 none any Details

Total release size: 15.1 kB

Release files / confypy-0.2.5.tar.gz

Download URL confypy-0.2.5.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
98b93f84d83f73da462086897ca68f19ce4cf993d17a4fa24f7a5d25842e69e9
BLAKE2b-256 checksum
How to use checksums
5c90c25557d7e172e0f3e7e89328580fe7ca246b15a800b988860e8a2c6f1999
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / confypy-0.2.5-py2-none-any.whl

Download URL confypy-0.2.5-py2-none-any.whl
Size 9.1 kB
Tags Python 2
SHA-256 checksum
How to use checksums
0cee2e035fae9bf3b88552e0b7f95512b79f848c98e57731b08a0688a19d9c72
BLAKE2b-256 checksum
How to use checksums
519df19726db0c4ef83fa7def925181d46263351a91c5f0589a9fd1954575d0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.5 This release

2 release files

0.2.4

2 release files

0.2.3

3 release files

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1

1 release file

0.0.1

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