Skip to content

Commit

Permalink
WIP use custom warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Jul 31, 2024
1 parent 1f0b320 commit f3e4367
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 23 deletions.
11 changes: 10 additions & 1 deletion flattentool/exceptions.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
10 changes: 8 additions & 2 deletions flattentool/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
36 changes: 25 additions & 11 deletions flattentool/json_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -209,15 +212,17 @@ 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:
self.rollup = set(rollup)
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,
)
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
)
)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
21 changes: 15 additions & 6 deletions flattentool/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import jsonref

from flattentool.exceptions import FlattenToolWarning
from flattentool.i18n import _
from flattentool.sheet import Sheet

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -406,7 +412,8 @@ def parse_schema_dict(
warn(
"{} in rollUp but not in schema".format(
", ".join(missedRollUp)
)
),
FlattenToolWarning,
)

else:
Expand Down Expand Up @@ -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,
)
7 changes: 6 additions & 1 deletion flattentool/sort_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from collections import OrderedDict
from warnings import warn

from flattentool.exceptions import FlattenToolWarning

try:
import lxml.etree as ET

Expand All @@ -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"}
Expand Down
7 changes: 5 additions & 2 deletions flattentool/xml_output.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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):
Expand Down

0 comments on commit f3e4367

Please sign in to comment.