def test_findkey(): s = """ MAP LAYER NAME "Layer1" TYPE POLYGON END LAYER NAME "Layer2" TYPE POLYGON CLASS NAME "Class1" COLOR 0 0 -8 END END END """ d = mappyfile.loads(s) pth = ["layers", 1] cmp = mappyfile.findkey(d, *pth) assert cmp["name"] == "Layer2" pth = ["layers", 1, "classes", 0] cmp = mappyfile.findkey(d, *pth) assert cmp["name"] == "Class1"
def create_message(self, rootdict, path, error, add_comments): """ Add a validation comment to the dictionary path is the path to the error object, it can be empty if the error is in the root object http://python-jsonschema.readthedocs.io/en/latest/errors/#jsonschema.exceptions.ValidationError.absolute_path It can also reference an object in a list e.g. [u'layers', 0] Unfortunately it is not currently possible to get the name of the failing property from the JSONSchema error object, even though it is in the error message. See https://github.com/Julian/jsonschema/issues/119 """ if not path: # error applies to the root type d = rootdict key = d["__type__"] elif isinstance(path[-1], int): # the error is on an object in a list d = utils.findkey(rootdict, *path) key = d["__type__"] else: key = path[-1] d = utils.findkey(rootdict, *path[:-1]) error_message = "ERROR: Invalid value in {}".format(key.upper()) # add a comment to the dict structure if add_comments: if "__comments__" not in d: d["__comments__"] = OrderedDict() d["__comments__"][key] = "# {}".format(error_message) error_message = {"error": error.message, "message": error_message} # add in details of the error line, when Mapfile was parsed to # include position details if "__position__" in d: if not path or key not in d["__position__"]: # position for the root object is stored in the root of the dict pd = d["__position__"] else: pd = d["__position__"][key] error_message["line"] = pd.get("line") error_message["column"] = pd.get("column") return error_message
def create_message(self, rootdict, path, error, add_comments): """ Add a validation comment to the dictionary path is the path to the error object, it can be empty if the error is in the root object http://python-jsonschema.readthedocs.io/en/latest/errors/#jsonschema.exceptions.ValidationError.absolute_path It can also reference an object in a list e.g. [u'layers', 0] """ if not path: # error applies to the root type d = rootdict key = d["__type__"] elif isinstance(path[-1], int): # the error is on an object in a list d = utils.findkey(rootdict, *path) key = d["__type__"] else: key = path[-1] d = utils.findkey(rootdict, *path[:-1]) error_message = "ERROR: Invalid value for {}".format(key.upper()) # add a comment to the dict structure if add_comments: if "__comments__" not in d: d["__comments__"] = OrderedDict() d["__comments__"][key] = "# {}".format(error_message) error_message = {"error": error.message, "message": error_message} # add in details of the error line, when Mapfile was parsed to # include position details if "__position__" in d: pd = d["__position__"][key] error_message["line"] = pd.get("line") error_message["column"] = pd.get("column") return error_message
def create_message(self, rootdict, trace_o_incl, path, error, add_comments): """ Add a validation comment to the dictionary path is the path to the error object, it can be empty if the error is in the root object http://python-jsonschema.readthedocs.io/en/latest/errors/#jsonschema.exceptions.ValidationError.absolute_path It can also reference an object in a list e.g. [u'layers', 0] Unfortunately it is not currently possible to get the name of the failing property from the JSONSchema error object, even though it is in the error message. See https://github.com/Julian/jsonschema/issues/119 """ keyPathParent = None if not path: # error applies to the root type d = rootdict key = d["__type__"] else: if isinstance(path[-1], int): # the error is on an object in a list d = utils.findkey(rootdict, *path) key = d["__type__"] else: key = path[-1] d = utils.findkey(rootdict, *path[:-1]) # get the site of the parent key l = len(path) n = int(l / 2) keyPathParent = [] for i in range(1, n + 1): c = i * 2 keySource = utils.findkey(rootdict, *path[:c]) if (keySource.get('name', None) != None): keyPathParent.append( str(keySource["__type__"] + " name: " + keySource["name"])) elif (keySource.get('text', None) != None): keyPathParent.append( str(keySource["__type__"] + " text: " + keySource["text"])) else: keyPathParent.append(str(keySource["__type__"])) # add the site of the parent key error_message = "ERROR: Invalid value in {}, parent path: {}".format( key.upper(), keyPathParent) # add a comment to the dict structure if add_comments: if "__comments__" not in d: d["__comments__"] = OrderedDict() d["__comments__"][key] = "# {}".format(error_message) error_message = {"error": error.message, "message": error_message} # add in details of the error line, when Mapfile was parsed to # include position details if "__position__" in d: if not path or key not in d["__position__"]: # position for the root object is stored in the root of the dict pd = d["__position__"] else: pd = d["__position__"][key] if trace_o_incl: trace = trace_o_incl[pd.get("line")] originFile = trace_o_incl[0][trace] lineOrigin = trace_o_incl[1:pd.get("line")].count(trace) + 1 error_message["line"] = lineOrigin error_message["column"] = pd.get("column") error_message["file"] = originFile else: error_message["line"] = pd.get("line") error_message["column"] = pd.get("column") return error_message