Skip to content

francois-le-ko4la/python-config-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-config-file

Description:

This package loads the configuration values defined in external JSON or YAML file, not the built-in data structures.

Setup:

$ git clone https://github.com/francois-le-ko4la/python-config-file.git
$ cd python-config-file
$ make install

Test:

This module has been tested and validated on Ubuntu.

$ make test

Use:

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))

Project Structure

.
├── 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

Todo:

  • 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

License

This package is distributed under the GPLv3 license

Runtime


python-3.6.x


UML Diagram

alt text

Objects

PytConfigFile()
PytConfigFile.isjson()
PytConfigFile.isyaml()
PytConfigFile.keys()
PytConfigFile.items()
PytConfigError()
PytConfigFileNotFound()
PytConfigLoadError()
PytFile()
@Property PytFile.filename
PytFile.exists()
PytFile.read()

PytConfigFile()

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
PytConfigFile.isjson()
@staticmethod
def PytConfigFile.isjson():

use it to define the file type

PytConfigFile.isyaml()
@staticmethod
def PytConfigFile.isyaml():

use it to define the file type

PytConfigFile.keys()
def PytConfigFile.keys(self):

None

PytConfigFile.items()
def PytConfigFile.items(self):

None

PytConfigError()

class PytConfigError(Exception):
Generic exception for pytconfig

PytConfigFileNotFound()

class PytConfigFileNotFound(PytConfigError):
Config file is not found

PytConfigLoadError()

class PytConfigLoadError(PytConfigError):
Content cannot be loaded.

PytFile()

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 PytFile.filename
@property
def PytFile.filename(self):

@Property filename (str): /path/to/the/file

PytFile.exists()
def PytFile.exists(self):

exists (bool): True if the file exists.

PytFile.read()
def PytFile.read(self):

Read the content