From f3e4367d2a65ef86476afb15ccdfeb8700fae5fa Mon Sep 17 00:00:00 2001 From: Bee Webb Date: Wed, 31 Jul 2024 21:04:27 +0000 Subject: [PATCH] WIP use custom warnings --- flattentool/exceptions.py | 11 ++++++++++- flattentool/input.py | 10 ++++++++-- flattentool/json_input.py | 36 +++++++++++++++++++++++++----------- flattentool/schema.py | 21 +++++++++++++++------ flattentool/sort_xml.py | 7 ++++++- flattentool/xml_output.py | 7 +++++-- 6 files changed, 69 insertions(+), 23 deletions(-) diff --git a/flattentool/exceptions.py b/flattentool/exceptions.py index 8b979747..46e85d84 100644 --- a/flattentool/exceptions.py +++ b/flattentool/exceptions.py @@ -1,4 +1,13 @@ -class DataErrorWarning(UserWarning): +class FlattenToolWarning(UserWarning): + """ + A warning generated directly by flatten-tool. + + """ + + pass + + +class DataErrorWarning(FlattenToolWarning): """ A warnings that indicates an error in the data, rather than the schema. diff --git a/flattentool/input.py b/flattentool/input.py index 524b4c25..ae81664d 100644 --- a/flattentool/input.py +++ b/flattentool/input.py @@ -142,7 +142,10 @@ def convert_type(type_string, value, timezone=pytz.timezone("UTC"), convert_flag feature = geojson.Feature(geometry=geom, properties={}) return feature.geometry else: - warn("Install flattentool's optional geo dependencies to use geo features.") + warn( + "Install flattentool's optional geo dependencies to use geo features.", + FlattenToolWarning, + ) return str(value) elif type_string == "": if type(value) == datetime.datetime: @@ -156,7 +159,10 @@ def convert_type(type_string, value, timezone=pytz.timezone("UTC"), convert_flag def warnings_for_ignored_columns(v, extra_message): if isinstance(v, Cell): - warn("Column {} has been ignored, {}".format(v.cell_location[3], extra_message)) + warn( + "Column {} has been ignored, {}".format(v.cell_location[3], extra_message), + DataErrorWarning, + ) elif isinstance(v, dict): for x in v.values(): warnings_for_ignored_columns(x, extra_message) diff --git a/flattentool/json_input.py b/flattentool/json_input.py index 8fd8ac9c..487fe30c 100644 --- a/flattentool/json_input.py +++ b/flattentool/json_input.py @@ -28,7 +28,7 @@ import zc.zlibstorage import ZODB.FileStorage -from flattentool.exceptions import DataErrorWarning +from flattentool.exceptions import DataErrorWarning, FlattenToolWarning from flattentool.i18n import _ from flattentool.input import path_search from flattentool.schema import make_sub_sheet_name @@ -195,7 +195,10 @@ def __init__( if isinstance(rollup, (list,)) and ( len(rollup) > 1 or (len(rollup) == 1 and rollup[0] is not True) ): - warn(_("Using rollUp values from schema, ignoring direct input.")) + warn( + _("Using rollUp values from schema, ignoring direct input."), + FlattenToolWarning, + ) elif isinstance(rollup, (list,)): if len(rollup) == 1 and os.path.isfile(rollup[0]): # Parse file, one json path per line. @@ -209,7 +212,8 @@ def __init__( elif len(rollup) == 1 and rollup[0] is True: warn( _( - "No fields to rollup found (pass json path directly, as a list in a file, or via a schema)" + "No fields to rollup found (pass json path directly, as a list in a file, or via a schema)", + FlattenToolWarning, ) ) else: @@ -217,7 +221,8 @@ def __init__( else: warn( _( - "Invalid value passed for rollup (pass json path directly, as a list in a file, or via a schema)" + "Invalid value passed for rollup (pass json path directly, as a list in a file, or via a schema)", + FlattenToolWarning, ) ) @@ -276,7 +281,8 @@ def __init__( warn( _( "You wanted to preserve the following fields which are not present in the supplied schema: {}" - ).format(list(input_not_in_schema)) + ).format(list(input_not_in_schema)), + FlattenToolWarning, ) except AttributeError: # no schema @@ -344,7 +350,8 @@ def parse(self): warn( _( "You wanted to preserve the following fields which are not present in the input data: {}" - ).format(nonexistent_input_paths) + ).format(nonexistent_input_paths), + FlattenToolWarning, ) def parse_json_dict( @@ -383,13 +390,17 @@ def parse_json_dict( try: geom = shapely.geometry.shape(json_dict) except (shapely.errors.GeometryTypeError, TypeError, ValueError) as e: - warn(_("Invalid GeoJSON: {parser_msg}").format(parser_msg=repr(e))) + warn( + _("Invalid GeoJSON: {parser_msg}").format(parser_msg=repr(e)), + DataErrorWarning, + ) return flattened_dict[_sheet_key] = geom.wkt skip_type_and_coordinates = True else: warn( - "Install flattentool's optional geo dependencies to use geo features." + "Install flattentool's optional geo dependencies to use geo features.", + FlattenToolWarning, ) parent_id_fields = copy.copy(parent_id_fields) or OrderedDict() @@ -482,7 +493,8 @@ def parse_json_dict( if self.use_titles and not self.schema_parser: warn( _( - "Warning: No schema was provided so column headings are JSON keys, not titles." + "Warning: No schema was provided so column headings are JSON keys, not titles.", + FlattenToolWarning, ) ) @@ -583,7 +595,8 @@ def parse_json_dict( warn( _( 'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.' - ).format(parent_name + key) + ).format(parent_name + key), + FlattenToolWarning, ) flattened_dict[ sheet_key(sheet, parent_name + key + "/0/" + k) @@ -594,7 +607,8 @@ def parse_json_dict( warn( _( 'More than one value supplied for "{}". Could not provide rollup, so adding a warning to the relevant cell(s) in the spreadsheet.' - ).format(parent_name + key) + ).format(parent_name + key), + FlattenToolWarning, ) flattened_dict[ sheet_key(sheet, parent_name + key + "/0/" + k) diff --git a/flattentool/schema.py b/flattentool/schema.py index 2674f076..698aa6a8 100644 --- a/flattentool/schema.py +++ b/flattentool/schema.py @@ -10,6 +10,7 @@ import jsonref +from flattentool.exceptions import FlattenToolWarning from flattentool.i18n import _ from flattentool.sheet import Sheet @@ -187,7 +188,10 @@ def parse(self): for field, title in fields: if self.use_titles: if not title: - warn(_("Field {} does not have a title, skipping.").format(field)) + warn( + _("Field {} does not have a title, skipping.").format(field), + FlattenToolWarning, + ) else: self.main_sheet.append(title) self.main_sheet.titles[field] = title @@ -367,13 +371,15 @@ def parse_schema_dict( warn( _( "Field {}{}/0/{} is missing a title, skipping." - ).format(parent_path, property_name, field) + ).format(parent_path, property_name, field), + FlattenToolWarning, ) elif not title: warn( _( "Field {}{} does not have a title, skipping it and all its children." - ).format(parent_path, property_name) + ).format(parent_path, property_name), + FlattenToolWarning, ) else: # This code only works for arrays that are at 0 or 1 layer of nesting @@ -406,7 +412,8 @@ def parse_schema_dict( warn( "{} in rollUp but not in schema".format( ", ".join(missedRollUp) - ) + ), + FlattenToolWarning, ) else: @@ -447,12 +454,14 @@ def parse_schema_dict( _( 'Unrecognised types {} for property "{}" with context "{}",' "so this property has been ignored." - ).format(repr(property_type_set), property_name, parent_path) + ).format(repr(property_type_set), property_name, parent_path), + FlattenToolWarning, ) else: warn( _('Skipping field "{}", because it has no properties.').format( parent_path - ) + ), + FlattenToolWarning, ) diff --git a/flattentool/sort_xml.py b/flattentool/sort_xml.py index cfe13165..e4a30725 100644 --- a/flattentool/sort_xml.py +++ b/flattentool/sort_xml.py @@ -27,6 +27,8 @@ from collections import OrderedDict from warnings import warn +from flattentool.exceptions import FlattenToolWarning + try: import lxml.etree as ET @@ -36,7 +38,10 @@ except ImportError: import xml.etree.ElementTree as ET - warn("Using stdlib etree may work, but is not supported. Please install lxml.") + warn( + "Using stdlib etree may work, but is not supported. Please install lxml.", + FlattenToolWarning, + ) # Namespaces necessary for opening schema files namespaces = {"xsd": "http://www.w3.org/2001/XMLSchema"} diff --git a/flattentool/xml_output.py b/flattentool/xml_output.py index 726e9ccc..d26d9040 100644 --- a/flattentool/xml_output.py +++ b/flattentool/xml_output.py @@ -1,7 +1,7 @@ from collections import OrderedDict from warnings import warn -from flattentool.exceptions import DataErrorWarning +from flattentool.exceptions import DataErrorWarning, FlattenToolWarning from flattentool.sort_xml import XMLSchemaWalker, sort_element try: @@ -17,7 +17,10 @@ import xml.etree.ElementTree as ET USING_LXML = False - warn("Using stdlib etree may work, but is not supported. Please install lxml.") + warn( + "Using stdlib etree may work, but is not supported. Please install lxml.", + FlattenToolWarning, + ) def sort_attributes(data):