Skip to content

Commit

Permalink
diff tool: ini as input format supported
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Aug 2, 2019
1 parent 9418df7 commit cba71e6
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
39 changes: 39 additions & 0 deletions nested_diff/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""
import argparse
import sys

import nested_diff

Expand Down Expand Up @@ -213,6 +214,44 @@ def decode(self, data):
return self.decoder.decode(data)


class IniLoader(Loader):
"""
INI loader
"""
def __init__(self, **kwargs):
super(IniLoader, self).__init__()

if sys.version[0] == '2':
from ConfigParser import ConfigParser

class Py2ConfigParserWrapper(ConfigParser):
# old ConfigParser has no method read_string
def read_string(self, data):
from StringIO import StringIO
sio = StringIO(data)
return ConfigParser.readfp(self, sio)

self.decoder = Py2ConfigParserWrapper(**kwargs)
else:
from configparser import ConfigParser
self.decoder = ConfigParser(**kwargs)

def decode(self, data):
self.decoder.read_string(data)

out = {}
for section in self.decoder.sections():
out[section] = {}
for option in self.decoder.options(section):
out[section][option] = self.decoder.get(section, option)

# cleanup (parser accumulates all readed confs)
self.decoder.remove_section(section)

return out


class YamlDumper(Dumper):
"""
YAML dumper
Expand Down
14 changes: 14 additions & 0 deletions nested_diff/diff_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ def get_argparser(self, description=None):
parser.add_argument('file1', type=argparse.FileType())
parser.add_argument('file2', type=argparse.FileType())

parser.add_argument(
'--ifmt',
type=str,
default='json',
choices=('ini', 'json', 'yaml'),
help='input files format; "json" used by default',
)

parser.add_argument(
'--ofmt',
type=str,
Expand Down Expand Up @@ -101,6 +109,12 @@ def get_dumper(self, fmt, **kwargs):

return super(App, self).get_dumper(fmt, **kwargs)

def get_loader(self, fmt, **kwargs):
if fmt == 'ini':
return nested_diff.cli.IniLoader(**kwargs)

return super(App, self).get_loader(fmt, **kwargs)

def run(self):
diff = self.diff(
self.load(self.args.file1),
Expand Down
7 changes: 7 additions & 0 deletions tests/cli/diff_tool/shared.lists.a.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[one]
one_1 = 1
one_2 = 2

[two]
two_1 = 1

4 changes: 4 additions & 0 deletions tests/cli/diff_tool/shared.lists.b.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[one]
one_1 = 1
one_2 = 42

18 changes: 18 additions & 0 deletions tests/cli/diff_tool/test_diff_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ def test_json_ofmt_opts(capsys, expected, fullname, PY2):
else:
assert expected == captured.out


def test_ini_ifmt(capsys, expected, fullname, PY2):
DiffApp(args=(
fullname('lists.a.ini', shared=True),
fullname('lists.b.ini', shared=True),
'--ifmt', 'ini',
'--ofmt', 'json',
)).run()

captured = capsys.readouterr()
assert '' == captured.err

if PY2: # json in python2 emit trailing spaces
assert json.loads(expected) == json.loads(captured.out)
else:
assert expected == captured.out


def test_text_ofmt(capsys, expected, fullname):
DiffApp(args=(
fullname('lists.a.json', shared=True),
Expand Down
17 changes: 17 additions & 0 deletions tests/cli/diff_tool/test_diff_tool.test_ini_ifmt.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"D": {
"one": {
"D": {
"one_2": {
"N": "42",
"O": "2"
}
}
},
"two": {
"R": {
"two_1": "1"
}
}
}
}

0 comments on commit cba71e6

Please sign in to comment.