How to use the flattentool.json_input.BadlyFormedJSONError function in flattentool

To help you get started, we’ve selected a few flattentool examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github OpenDataServices / flatten-tool / flattentool / json_input.py View on Github external
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("/")]
github OpenDataServices / flatten-tool / flattentool / cli.py View on Github external
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")
github OpenDataServices / flatten-tool / flattentool / json_input.py View on Github external
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]