From 98a69a37bc8e33f3b53b59aa4bd2fb34d9ccc619 Mon Sep 17 00:00:00 2001 From: VG Date: Sun, 12 Jun 2016 20:27:02 +0200 Subject: Change confparser to a package, add it to setup.py --- confparser.py | 27 ------------------------ confparser/__init__.py | 27 ++++++++++++++++++++++++ confparser/confparser_test.py | 49 +++++++++++++++++++++++++++++++++++++++++++ confparser_test.py | 49 ------------------------------------------- setup.py | 2 +- 5 files changed, 77 insertions(+), 77 deletions(-) delete mode 100644 confparser.py create mode 100644 confparser/__init__.py create mode 100644 confparser/confparser_test.py delete mode 100644 confparser_test.py diff --git a/confparser.py b/confparser.py deleted file mode 100644 index e43443b..0000000 --- a/confparser.py +++ /dev/null @@ -1,27 +0,0 @@ -#!python3 - -__all__ = ['read_conf'] - -import csv - -class conf_dialect(csv.Dialect): - delimiter = '=' - quotechar = '"' - escapechar = '\\' - doublequote = True - skipinitialspace = True - lineterminator = '\n' - quoting = csv.QUOTE_MINIMAL - -def genestrip(geneorg): - for line in geneorg: - line = line.strip() - if not len(line) or line.startswith('#'): - continue - yield line - -def read_conf(filename): - with open(filename, 'r', encoding='utf8') as linegen: - reader = csv.reader(genestrip(linegen), dialect=conf_dialect) - dic = {row[0].strip(): row[1] for row in reader if len(row) >= 2} - return dic diff --git a/confparser/__init__.py b/confparser/__init__.py new file mode 100644 index 0000000..e43443b --- /dev/null +++ b/confparser/__init__.py @@ -0,0 +1,27 @@ +#!python3 + +__all__ = ['read_conf'] + +import csv + +class conf_dialect(csv.Dialect): + delimiter = '=' + quotechar = '"' + escapechar = '\\' + doublequote = True + skipinitialspace = True + lineterminator = '\n' + quoting = csv.QUOTE_MINIMAL + +def genestrip(geneorg): + for line in geneorg: + line = line.strip() + if not len(line) or line.startswith('#'): + continue + yield line + +def read_conf(filename): + with open(filename, 'r', encoding='utf8') as linegen: + reader = csv.reader(genestrip(linegen), dialect=conf_dialect) + dic = {row[0].strip(): row[1] for row in reader if len(row) >= 2} + return dic diff --git a/confparser/confparser_test.py b/confparser/confparser_test.py new file mode 100644 index 0000000..d5044f2 --- /dev/null +++ b/confparser/confparser_test.py @@ -0,0 +1,49 @@ +#!python3 + +import tempfile +import confparser + +def parse_test(): + msg = '''\ +# comment with ":" and "=" in it: := +a= Test + + # another comment with space and := tada +b = "Test of a long string" + +# third comment +c=What do YOU want ? '' +d = value with a squote at the end ' +e = value with a dquote at the end " +f = "value with a \\" inside" +x.y.z = there you can have a full tree of parameters +''' + with tempfile.NamedTemporaryFile(mode='w', encoding='utf8') as f: + f.write(msg) + print('Following lines have been written to the file:') + print('-'*40) + print(msg) + print('-'*40) + f.flush() + dic = confparser.read_conf(f.name) + print('read dic:', dic) + dictest = { + 'a': 'Test', + 'b': 'Test of a long string', + 'c': "What do YOU want ? ''", + 'd': 'value with a squote at the end \'', + 'e': 'value with a dquote at the end "', + 'f': 'value with a " inside', + 'x.y.z': 'there you can have a full tree of parameters', + } + if dic != dictest: + a = set(dictest.keys()) + b = set(dic.keys()) + if a != b: + diff = a.difference(b).union(b.difference(a)) + raise Exception('not same keys between two dicts:', diff) + a = set(dictest.values()) + b = set(dic.values()) + if a != b: + diff = a.difference(b).union(b.difference(a)) + raise Exception('differences between two set of values:', diff) diff --git a/confparser_test.py b/confparser_test.py deleted file mode 100644 index d5044f2..0000000 --- a/confparser_test.py +++ /dev/null @@ -1,49 +0,0 @@ -#!python3 - -import tempfile -import confparser - -def parse_test(): - msg = '''\ -# comment with ":" and "=" in it: := -a= Test - - # another comment with space and := tada -b = "Test of a long string" - -# third comment -c=What do YOU want ? '' -d = value with a squote at the end ' -e = value with a dquote at the end " -f = "value with a \\" inside" -x.y.z = there you can have a full tree of parameters -''' - with tempfile.NamedTemporaryFile(mode='w', encoding='utf8') as f: - f.write(msg) - print('Following lines have been written to the file:') - print('-'*40) - print(msg) - print('-'*40) - f.flush() - dic = confparser.read_conf(f.name) - print('read dic:', dic) - dictest = { - 'a': 'Test', - 'b': 'Test of a long string', - 'c': "What do YOU want ? ''", - 'd': 'value with a squote at the end \'', - 'e': 'value with a dquote at the end "', - 'f': 'value with a " inside', - 'x.y.z': 'there you can have a full tree of parameters', - } - if dic != dictest: - a = set(dictest.keys()) - b = set(dic.keys()) - if a != b: - diff = a.difference(b).union(b.difference(a)) - raise Exception('not same keys between two dicts:', diff) - a = set(dictest.values()) - b = set(dic.values()) - if a != b: - diff = a.difference(b).union(b.difference(a)) - raise Exception('differences between two set of values:', diff) diff --git a/setup.py b/setup.py index 40babcb..d6ff9f3 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ setuptools.setup( install_requires=['imapclient'], - packages = ['climl'], + packages = ['climl', 'confparser'], entry_points={ 'console_scripts': [ 'climl = climl:main' ], }, -- cgit v1.2.3