2016-05-29 18:56:50 +02:00
|
|
|
import json, urllib.request
|
|
|
|
from exceptions.HieraException import HieraException
|
|
|
|
|
|
|
|
class JsonParser(object):
|
|
|
|
def __init__(self, fileName):
|
|
|
|
self.printStatus = True
|
|
|
|
self.__jsonData__ = self.__getFile__(fileName)
|
|
|
|
|
|
|
|
def __getFile__(self, fileName):
|
|
|
|
if fileName.startswith('https://') or fileName.startswith('http://'):
|
|
|
|
if self.printStatus:
|
2017-04-23 22:57:07 +02:00
|
|
|
print('Download', fileName, 'from URL:', fileName)
|
2016-05-29 18:56:50 +02:00
|
|
|
resource = urllib.request.urlopen(fileName)
|
2016-07-19 17:02:58 +02:00
|
|
|
try:
|
|
|
|
data = json.loads(resource.read().decode('utf-8'))
|
|
|
|
except:
|
|
|
|
raise HieraException('Error while parsing a json file (perhapes misformed file): ' + fileName)
|
|
|
|
finally:
|
|
|
|
resource.close()
|
2016-05-29 18:56:50 +02:00
|
|
|
else:
|
|
|
|
if self.printStatus:
|
2017-04-23 22:57:07 +02:00
|
|
|
print('Open', fileName, 'from file:', fileName)
|
2016-07-19 17:02:58 +02:00
|
|
|
with open(fileName) as data_file:
|
|
|
|
try:
|
|
|
|
data = json.load(data_file)
|
|
|
|
except:
|
|
|
|
raise HieraException('Error while parsing a json file (perhapes misformed file): ' + fileName)
|
|
|
|
|
2016-05-29 18:56:50 +02:00
|
|
|
|
|
|
|
return data
|