aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2011-11-01 14:37:25 +0100
committerMatěj Cepl <mcepl@redhat.com>2011-11-01 14:37:25 +0100
commit3402623cd692bcbdb5e92125fb8b99291908f74f (patch)
treefd970774bb0f8d041458aaed77ef552acd889d35
parente10506226abbca14ebe99c987feb155f1253abec (diff)
downloadjson_diff-3402623cd692bcbdb5e92125fb8b99291908f74f.tar.gz
Making the script slightly more future-proof.
use from __future__ import division, absolute_import use argparse instead of optparse
-rwxr-xr-xjson_diff.py33
-rw-r--r--test/nested_html_output.html17
-rw-r--r--test_json_diff.py3
3 files changed, 24 insertions, 29 deletions
diff --git a/json_diff.py b/json_diff.py
index 8a2cc5c..f460af7 100755
--- a/json_diff.py
+++ b/json_diff.py
@@ -22,9 +22,10 @@ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
+from __future__ import division, absolute_import
import json
-from optparse import OptionParser
import logging
+import argparse
__author__ = "Matěj Cepl"
__version__ = "0.1.0"
@@ -73,10 +74,9 @@ class HTMLFormatter(object):
def _generate_page(self, in_dict, title="json_diff result"):
out_str = out_str_template % (title, title,
self._format_dict(in_dict))
- out_str += """
- </table>
- </body>
- </html>
+ out_str += """</table>
+ </body>
+</html>
"""
return out_str
@@ -126,7 +126,7 @@ class HTMLFormatter(object):
else:
out_str += self._format_dict(diff_dict[variable], None, level+1)
- return out_str
+ return out_str.strip()
def __str__(self):
@@ -262,20 +262,19 @@ class Comparator(object):
if __name__ == "__main__":
- usage = "usage: %prog [options] old.json new.json"
- parser = OptionParser(usage=usage)
- parser.add_option("-x", "--exclude",
- action="append", dest="exclude", metavar="ATTR", default=[],
+ parser = argparse.ArgumentParser(description="Generates diff between two JSON files.")
+ parser.add_argument("filenames", action="append", nargs=2,
+ metavar="FILENAME", help="names of the old and new JSON files")
+ parser.add_argument("-x", "--exclude",
+ action="append", dest="exclude", default=[],
help="attributes which should be ignored when comparing")
- parser.add_option("-H", "--HTML",
- action="store_true", dest="HTMLoutput", metavar="BOOL", default=False,
+ parser.add_argument("-H", "--HTML",
+ action="store_true", dest="HTMLoutput", default=False,
help="program should output to HTML report")
- (options, args) = parser.parse_args()
+ parser.add_argument('--version', action='version', version='%(prog)s 0.1.1')
+ options = parser.parse_args()
- if len(args) != 2:
- parser.error("Script requires two positional arguments, names for old and new JSON file.")
-
- diff = Comparator(file(args[0]), file(args[1]), options.exclude)
+ diff = Comparator(file(options.filenames[0][0]), file(options.filenames[0][1]), options.exclude)
if options.HTMLoutput:
diff_res = diff.compare_dicts()
logging.debug("diff_res:\n%s", json.dumps(diff_res, indent=True))
diff --git a/test/nested_html_output.html b/test/nested_html_output.html
index 6c5b83c..1d1e865 100644
--- a/test/nested_html_output.html
+++ b/test/nested_html_output.html
@@ -22,22 +22,17 @@ td {
<table>
<tr>
<td class='remove_class'>b = 2</td>
- </tr>
-<tr>
+ </tr><tr>
<td class='update_class'>a = 2</td>
</tr>
<tr>
<td>&nbsp;</td><td class='update_class'>son = Ivánek</td>
- </tr>
-<tr>
+ </tr><tr>
<td>&nbsp;</td><td class='append_class'>daughter = Maruška</td>
- </tr>
-<tr>
+ </tr><tr>
<td class='append_class'>c = 3</td>
</tr>
-
-
- </table>
- </body>
- </html>
+</table>
+ </body>
+</html>
diff --git a/test_json_diff.py b/test_json_diff.py
index 21faf70..246a29d 100644
--- a/test_json_diff.py
+++ b/test_json_diff.py
@@ -2,6 +2,7 @@
"""
PyUnit unit tests
"""
+from __future__ import division, absolute_import
import unittest
import json
import json_diff
@@ -145,4 +146,4 @@ class TestSadPath(unittest.TestCase):
if __name__ == "__main__":
- unittest.main() \ No newline at end of file
+ unittest.main()