confini

Parse and merge multiple ini files in python3
git clone git://git.defalsify.org/python-confini.git
Log | Files | Refs | README | LICENSE

commit d74cf1bf38a77c70f01b0332974ab59870b567e1
parent e67501d22daddd9cd9ba11ec471ff408447d0e80
Author: nolash <dev@holbrook.no>
Date:   Thu, 27 Aug 2020 21:08:41 +0200

Add gnupg support

Diffstat:
ACHANGELOG | 2++
Mconfini/config.py | 28++++++++++++++++++++++++++--
Mscripts/parse.py | 7++++++-
Msetup.py | 6+++---
4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -0,0 +1,2 @@ +1.0.0 + - Add gnupg support diff --git a/confini/config.py b/confini/config.py @@ -6,11 +6,18 @@ import os import tempfile import configparser import re +import gnupg logg = logging.getLogger() current_config = None +gpg = gnupg.GPG( + verbose=False, + use_agent=True, + ) +gpg.encoding = 'utf-8' + def set_current(conf, description=''): global current_config @@ -22,13 +29,14 @@ class Config: parser = configparser.ConfigParser(strict=True) - def __init__(self, config_dir): + def __init__(self, config_dir, decrypt=True): if not os.path.isdir(config_dir): raise OSError('{} is not a directory'.format(config_dir)) self.dir = os.path.realpath(config_dir) self.required = {} self.censored = {} self.store = {} + self.decrypt = decrypt def add(self, value, constant_name): @@ -97,8 +105,24 @@ class Config: set_current(self, description=self.dir) + + def _decrypt(self, k, v): + if self.decrypt: + m = re.match(r'^\!gpg\((.*)\)', v) + if m != None: + filename = m.group(1) + if filename[0] != '/': + filename = os.path.join(self.dir, filename) + f = open(filename, 'rb') + logg.debug('decrypting entry {} in file {}'.format(k, f)) + v = gpg.decrypt_file(f) + f.close() + return v + + def get(self, k): - return self.store.get(k) + v = self.store.get(k) + return self._decrypt(k, v) def __str__(self): diff --git a/scripts/parse.py b/scripts/parse.py @@ -1,11 +1,16 @@ import sys +import logging from confini import Config +#logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + if __name__ == "__main__": if len(sys.argv) < 2: sys.stderr.write('usage: config.py <config_dir>') sys.exit(1) c = Config(sys.argv[1]) c.process() - print(c) + for k in c.store.keys(): + print('{}: {}'.format(k, c.get(k))) diff --git a/setup.py b/setup.py @@ -2,13 +2,13 @@ from setuptools import setup setup( name='confini', - version='0.0.5', + version='0.1.0', description='Parse, verify and merge all ini files in a single directory', author='Louis Holbrook', author_email='dev@holbrook.no', license='GPL3', - packages=[ - 'confini', + install_requires=[ + 'python-gnupg>=0.4.6,<0.5.0', ], scripts = [ 'scripts/parse.py',