aboutsummaryrefslogtreecommitdiffstats
path: root/json_diff.py
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 /json_diff.py
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
Diffstat (limited to 'json_diff.py')
-rwxr-xr-xjson_diff.py33
1 files changed, 16 insertions, 17 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))