This package loads the configuration values defined in external JSON or YAML file, not the built-in data structures.
$ git clone https://github.com/francois-le-ko4la/python-config-file.git
$ cd python-config-file
$ make install
This module has been tested and validated on Ubuntu.
$ make test
from pytconfig import PytConfigFile
conf = "/path/to/the/file"
# PytConfigFile(path (str), PytConfigFile.{isjson|isyaml})
config = PytConfigFile(conf, PytConfigFile.isjson)
print(config['mykey'])
print(config.keys())
print(len(config))
.
├── last_check.log
├── LICENSE
├── Makefile
├── pictures
│ ├── classes_pytconfig.png
│ └── packages_pytconfig.png
├── pytconfig
│ ├── __about__.py
│ ├── config.py
│ ├── file.py
│ └── __init__.py
├── README.md
├── requirements.txt
├── runtime.txt
├── setup.cfg
├── setup.py
└── tests
├── facebook.json
├── facebook.yaml
├── test_doctest.py
└── test_pycodestyle.py
- Create the project
- Write code and tests
- Test installation and requirements (setup.py and/or Makefile)
- Test code
- Validate features
- Write Doc/stringdoc
- Run PEP8 validation
- Clean & last check
- Fix global header
- Fix tests
- Fix doc
- Release : 0.1.7
- change (un)install process
- remove MANIFEST.in
- manage global var: __ version __...
- improve the doc
- remove old tests
- Release : 0.1.9
- improve Makefile
- Release : 0.1.10
- Fix setup.py
- Fix setup.cfg
- Release : 1.1.0
This package is distributed under the GPLv3 license
python-3.6.x
PytConfigFile()
PytConfigFile.isjson()
PytConfigFile.isyaml()
PytConfigFile.keys()
PytConfigFile.items()
PytConfigError()
PytConfigFileNotFound()
PytConfigLoadError()
PytFile()
@Property PytFile.filename
PytFile.exists()
PytFile.read()
class PytConfigFile(dict):
This Class provides a dict from a JSON File or YAML file.
You can use it to avoid a lot of CONST in your scripts.
Use:
>>> # pathlib to run the test everywhere
>>> import pathlib
>>> path = str(pathlib.Path(__file__).resolve().parent) + "/"
>>> cur_file = '/etc/fst'
>>> config = PytConfigFile(cur_file, PytConfigFile.isjson)
Traceback (most recent call last):
...
pytconfig.exceptions.PytConfigFileNotFound: File "/etc/fst" not found!
>>> cur_file = path + '../LICENSE'
>>> config = PytConfigFile(cur_file, PytConfigFile.isjson)
Traceback (most recent call last):
...
pytconfig.exceptions.PytConfigLoadError: Can't load the configuration...
>>> cur_file = path + '../tests/facebook.json'
>>> config = PytConfigFile(cur_file, PytConfigFile.isjson)
>>> print(config['debug'])
True
>>> print(config.keys())
dict_keys(['description', 'debug', 'data'])
>>> print(len(config))
3
>>> cur_file = path + '../tests/facebook.yaml'
>>> configyaml = PytConfigFile(cur_file, PytConfigFile.isyaml)
>>> print(configyaml['debug'])
True
>>> print(configyaml.keys())
dict_keys(['description', 'debug', 'data'])
>>> print(len(configyaml))
3
@staticmethod
def PytConfigFile.isjson():
use it to define the file type
@staticmethod
def PytConfigFile.isyaml():
use it to define the file type
def PytConfigFile.keys(self):
None
def PytConfigFile.items(self):
None
class PytConfigError(Exception):
Generic exception for pytconfig
class PytConfigFileNotFound(PytConfigError):
Config file is not found
class PytConfigLoadError(PytConfigError):
Content cannot be loaded.
class PytFile(object):
>>> data_file = PytFile("lorem")
Traceback (most recent call last):
...
pytconfig.exceptions.PytConfigFileNotFound: File "lorem" not found!
>>> data_file = PytFile(None)
Traceback (most recent call last):
...
pytconfig.exceptions.PytConfigFileNotFound: File "None" not found!
>>> fstab = PytFile("/etc/fstab")
>>> fstab.filename.stem
'fstab'
>>> fstab
/etc/fstab
>>> # pathlib to run the test everywhere
>>> import pathlib
>>> path = str(pathlib.Path(__file__).resolve().parent) + "/"
>>> license = PytFile(path + "../LICENSE")
>>> license.filename.stem
'LICENSE'
>>> license.exists()
True
>>> result = license.read()
>>> result = result.split("\n")
>>> result[0]
' GNU GENERAL PUBLIC LICENSE'
@property
def PytFile.filename(self):
@Property filename (str): /path/to/the/file
def PytFile.exists(self):
exists (bool): True if the file exists.
def PytFile.read(self):
Read the content