From d87431fcfa3a7dc66d4b4773ed75f7e83855e4fc Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Mon, 13 Nov 2017 23:20:31 +0530 Subject: [PATCH] fix(src): refactor main code for generating xml - remove the dependency on BeautifulSoup and just use the dict2xml code - fixes #21, issue with wrong outout on multiple records - remove unused packages - Github Issue: - Trello Ticket: Signed-off-by: Vinit Kumar --- setup.py | 2 +- src/json2xml.py | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/setup.py b/setup.py index c62ae91..7346cd0 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -version = '1.3.0' +version = '1.4.0' setup( name='json2xml', diff --git a/src/json2xml.py b/src/json2xml.py index 2cc5f16..a7a66cd 100644 --- a/src/json2xml.py +++ b/src/json2xml.py @@ -1,11 +1,10 @@ #! /usr/bin/env python import argparse +import json import sys -import requests -import simplejson + import dict2xml -import json -from bs4 import BeautifulSoup +import requests class Json2xml(object): @@ -32,7 +31,7 @@ def __init__(self, data: str) -> None: def fromjsonfile(cls, filename: str): try: json_data = open(filename) - data = simplejson.load(json_data) + data = json.load(json_data) json_data.close() except IOError as e: print("I/O error({0}): {1}".format(e.errno, e.strerror)) @@ -46,8 +45,8 @@ def fromjsonfile(cls, filename: str): # # --------------------------------- @classmethod - def fromurl(cls, url: str): - response = requests.get(url) + def fromurl(cls, url: str, params=None): + response = requests.get(url, params=params) if response.status_code == 200: return cls(response.json()) else: @@ -75,9 +74,8 @@ def fromstring(cls, data: str): # --------------------------------- def json2xml(self): if self.data: - xmldata = dict2xml.dict2xml(self.data) - xml = BeautifulSoup(xmldata, "html.parser") - return xml + xmldata = dict2xml.dict2xml(self.data, wrap="all", indent=" ") + return xmldata def main(argv=None): @@ -90,17 +88,17 @@ def main(argv=None): if args.url: url = args.url data = Json2xml.fromurl(url) - print(Json2xml.json2xml(data)) + return Json2xml.json2xml(data) if args.file: file = args.file data = Json2xml.fromjsonfile(file) - print(Json2xml.json2xml(data)) + return Json2xml.json2xml(data) if args.data: str_data = args.data data = Json2xml.fromstring(str_data) - print(Json2xml.json2xml(data)) + return Json2xml.json2xml(data) if __name__ == "__main__": main(sys.argv)