Skip to content

Commit

Permalink
Merge pull request #23 from vinitkumar/feature/add-conversation-from-…
Browse files Browse the repository at this point in the history
…string-support

fix crash in test and build
  • Loading branch information
Vinit Kumar authored Nov 10, 2017
2 parents dfdeadc + 536ab60 commit bec3d87
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
*.pyc
json2xml.egg-info
build
Expand Down
17 changes: 13 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ pip3 install json2xml

### Usage

#### Command Line
### Command Line

```
python -m src.cli --file="examples/example.json"
python -m src.cli --url="https://coderwall.com/vinitcool76.json"
python -m src.cli --data '{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
```

#### Inline in Code
### Inline in Code

- From a file
#### From a file

```python
from src.json2xml import Json2xml
Expand All @@ -31,7 +32,7 @@ data_object = Json2xml(data)
data_object.json2xml() #xml output
```

- From an URL
#### From an URL

```python
from src.json2xml import Json2xml
Expand All @@ -40,6 +41,14 @@ data_object = Json2xml(data)
data_object.json2xml() #xml output
```

#### From JSON string

```python
from src.json2xml import Json2xml
data = Json2xml.fromstring('{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}').data
data_object = Json2xml(data)
data_object.json2xml() #xml output
```

### Bugs, features

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from setuptools import setup, find_packages

version = '1.2.5'
version = '1.3.0'

setup(
name='json2xml',
version=version,
description='A simple python package to convert json to xml data',
description='A simple python package to convert json from file, URL or string to xml data',
author='Vinit Kumar',
author_email='[email protected]',
url='https://github.com/vinitkumar/json2xml',
Expand Down
6 changes: 6 additions & 0 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def main(argv=None):
parser = argparse.ArgumentParser(description='Utility to convert json to valid xml.')
parser.add_argument('--url', dest='url', action='store')
parser.add_argument('--file', dest='file', action='store')
parser.add_argument('--data', dest='data', action='store')
args = parser.parse_args()

if args.url:
Expand All @@ -19,6 +20,11 @@ def main(argv=None):
data = Json2xml.fromjsonfile(file)
print(Json2xml.json2xml(data))

if args.data:
str_data = args.data
data = Json2xml.fromstring(str_data)
print(Json2xml.json2xml(data))


if __name__ == "__main__":
main(sys.argv)
18 changes: 18 additions & 0 deletions src/json2xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import requests
import simplejson
import dict2xml
import json
from bs4 import BeautifulSoup


Expand Down Expand Up @@ -52,6 +53,18 @@ def fromurl(cls, url: str):
else:
raise Exception("Bad URl, Can't get JSON response")

@classmethod
def fromstring(cls, data: str):
if type(data) is not str:
raise("Sorry but it doesn't seem to be valid string")
try:
data = json.loads(data)
except Exception as e:
print("Sorry, failed to load json, seems the JSON is not right")
data = []
return cls(data)


# -------------------------------
##
# @Synopsis This method actually
Expand All @@ -71,6 +84,7 @@ def main(argv=None):
parser = argparse.ArgumentParser(description='Utility to convert json to valid xml.')
parser.add_argument('--url', dest='url', action='store')
parser.add_argument('--file', dest='file', action='store')
parser.add_argument('--data', dest='data', action='store')
args = parser.parse_args()

if args.url:
Expand All @@ -83,6 +97,10 @@ def main(argv=None):
data = Json2xml.fromjsonfile(file)
print(Json2xml.json2xml(data))

if args.data:
str_data = args.data
data = Json2xml.fromstring(str_data)
print(Json2xml.json2xml(data))

if __name__ == "__main__":
main(sys.argv)

0 comments on commit bec3d87

Please sign in to comment.