Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if json_filename is not None and root_json_dict is not None:
raise ValueError(
"Only one of json_file or root_json_dict should be supplied"
)
if json_filename:
with codecs.open(json_filename, encoding="utf-8") as json_file:
try:
self.root_json_dict = json.load(
json_file, object_pairs_hook=OrderedDict, parse_float=Decimal
)
except UnicodeError as err:
raise BadlyFormedJSONErrorUTF8(*err.args)
except ValueError as err:
raise BadlyFormedJSONError(*err.args)
else:
self.root_json_dict = root_json_dict
if preserve_fields:
# Extract fields to be preserved from input file (one path per line)
preserve_fields_all = []
preserve_fields_input = []
with open(preserve_fields) as preserve_fields_file:
for line in preserve_fields_file:
line = line.strip()
path_fields = line.rsplit("/", 1)
preserve_fields_all = (
preserve_fields_all + path_fields + [line.rstrip("/")]
)
preserve_fields_input = preserve_fields_input + [line.rstrip("/")]
def non_verbose_error_handler(type, value, traceback):
if type == BadlyFormedJSONError:
sys.stderr.write("JSON error: {}\n".format(value))
else:
sys.stderr.write(str(value) + "\n")
from warnings import warn
import xmltodict
from flattentool.input import path_search
from flattentool.schema import make_sub_sheet_name
from flattentool.sheet import Sheet
BASIC_TYPES = [str, bool, int, Decimal, type(None)]
class BadlyFormedJSONError(ValueError):
pass
class BadlyFormedJSONErrorUTF8(BadlyFormedJSONError):
pass
def sheet_key_field(sheet, key):
if key not in sheet:
sheet.append(key)
return key
def sheet_key_title(sheet, key):
"""
If the key has a corresponding title, return that. If doesn't, create it in the sheet and return it.
"""
if key in sheet.titles:
title = sheet.titles[key]