Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom warnings and exceptions #452

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- Use custom warnings [#451](https://github.com/OpenDataServices/flatten-tool/issues/451)
- Use custom warnings and exceptions [#450](https://github.com/OpenDataServices/flatten-tool/issues/450) [#451](https://github.com/OpenDataServices/flatten-tool/issues/451)

## [0.25.0] - 2024-07-05

Expand Down
19 changes: 11 additions & 8 deletions flattentool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import OrderedDict
from decimal import Decimal

from flattentool.exceptions import FlattenToolError
from flattentool.input import FORMATS as INPUT_FORMATS
from flattentool.json_input import JSONParser
from flattentool.lib import parse_sheet_configuration
Expand Down Expand Up @@ -37,7 +38,7 @@ def create_template(
"""

if line_terminator not in LINE_TERMINATORS.keys():
raise Exception(f"{line_terminator} is not a valid line terminator")
raise FlattenToolError(f"{line_terminator} is not a valid line terminator")

convert_flags = {"wkt": convert_wkt}

Expand Down Expand Up @@ -76,7 +77,7 @@ def spreadsheet_output(spreadsheet_output_class, name):
spreadsheet_output(OUTPUT_FORMATS[output_format], output_name)

else:
raise Exception("The requested format is not available")
raise FlattenToolError("The requested format is not available")


def flatten(
Expand Down Expand Up @@ -111,10 +112,10 @@ def flatten(
if (filter_field is None and filter_value is not None) or (
filter_field is not None and filter_value is None
):
raise Exception("You must use filter_field and filter_value together")
raise FlattenToolError("You must use filter_field and filter_value together")

if line_terminator not in LINE_TERMINATORS.keys():
raise Exception(f"{line_terminator} is not a valid line terminator")
raise FlattenToolError(f"{line_terminator} is not a valid line terminator")

convert_flags = {"wkt": convert_wkt}

Expand Down Expand Up @@ -175,7 +176,7 @@ def spreadsheet_output(spreadsheet_output_class, name):
spreadsheet_output(OUTPUT_FORMATS[output_format], output_name)

else:
raise Exception("The requested format is not available")
raise FlattenToolError("The requested format is not available")


# From http://bugs.python.org/issue16535
Expand Down Expand Up @@ -239,11 +240,13 @@ def unflatten(
"""

if input_format is None:
raise Exception("You must specify an input format (may autodetect in future")
raise FlattenToolError(
"You must specify an input format (may autodetect in future"
Copy link
Contributor

@R2ZER0 R2ZER0 Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but I'm guessing there's meant to be a ) at the end of the message

)
elif input_format not in INPUT_FORMATS:
raise Exception("The requested format is not available")
raise FlattenToolError("The requested format is not available")
if metatab_name and base_json:
raise Exception("Not allowed to use base_json with metatab")
raise FlattenToolError("Not allowed to use base_json with metatab")

convert_flags = {"wkt": convert_wkt}

Expand Down
8 changes: 8 additions & 0 deletions flattentool/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
class FlattenToolError(Exception):
pass


class FlattenToolValueError(FlattenToolError, ValueError):
pass


class FlattenToolWarning(UserWarning):
"""
A warning generated directly by flatten-tool.
Expand Down
21 changes: 13 additions & 8 deletions flattentool/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@

from openpyxl.utils.cell import get_column_letter

from flattentool.exceptions import DataErrorWarning, FlattenToolWarning
from flattentool.exceptions import (
DataErrorWarning,
FlattenToolError,
FlattenToolValueError,
FlattenToolWarning,
)
from flattentool.i18n import _
from flattentool.lib import isint, parse_sheet_configuration
from flattentool.ODSReader import ODSReader
Expand Down Expand Up @@ -154,7 +159,7 @@ def convert_type(type_string, value, timezone=pytz.timezone("UTC"), convert_flag
return int(value)
return value if type(value) in [int] else str(value)
else:
raise ValueError('Unrecognised type: "{}"'.format(type_string))
raise FlattenToolValueError('Unrecognised type: "{}"'.format(type_string))


def warnings_for_ignored_columns(v, extra_message):
Expand All @@ -170,7 +175,7 @@ def warnings_for_ignored_columns(v, extra_message):
for x in v.to_list():
warnings_for_ignored_columns(x, extra_message)
else:
raise ValueError()
raise FlattenToolValueError()


def merge(base, mergee, debug_info=None):
Expand Down Expand Up @@ -589,7 +594,7 @@ def extract_dict_to_error_path(path, input):
).format(input[k].cell_value, sub_cell.cell_value)
output[p].append(sub_cell.cell_location)
else:
raise Exception(
raise FlattenToolError(
_("Unexpected result type in the JSON cell tree: {}").format(input[k])
)
return output
Expand All @@ -612,7 +617,7 @@ def extract_dict_to_value(input):
elif isinstance(input[k], Cell):
output[k] = input[k].cell_value
else:
raise Exception(
raise FlattenToolError(
_("Unexpected result type in the JSON cell tree: {}").format(input[k])
)
return output
Expand Down Expand Up @@ -699,7 +704,7 @@ def get_sheet_lines(self, sheet_name):
yield row


class BadXLSXZipFile(BadZipFile):
class BadXLSXZipFile(BadZipFile, FlattenToolError):
pass


Expand Down Expand Up @@ -1015,7 +1020,7 @@ def unflatten_main_with_parser(parser, line, timezone, xml, id_name, convert_fla
list_index = -1
if isint(next_path_item):
if current_type and current_type != "array":
raise ValueError(
raise FlattenToolValueError(
_(
"There is an array at '{}' when the schema says there should be a '{}'"
).format(path_till_now, current_type)
Expand Down Expand Up @@ -1067,7 +1072,7 @@ def unflatten_main_with_parser(parser, line, timezone, xml, id_name, convert_fla
and current_type not in ["object", "array"]
and next_path_item
):
raise ValueError(
raise FlattenToolValueError(
_(
"There is an object or list at '{}' but it should be an {}"
).format(path_till_now, current_type)
Expand Down
19 changes: 13 additions & 6 deletions flattentool/json_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
import zc.zlibstorage
import ZODB.FileStorage

from flattentool.exceptions import DataErrorWarning, FlattenToolWarning
from flattentool.exceptions import (
DataErrorWarning,
FlattenToolError,
FlattenToolValueError,
FlattenToolWarning,
)
from flattentool.i18n import _
from flattentool.input import path_search
from flattentool.schema import make_sub_sheet_name
Expand All @@ -37,7 +42,7 @@
BASIC_TYPES = [str, bool, int, Decimal, type(None)]


class BadlyFormedJSONError(ValueError):
class BadlyFormedJSONError(FlattenToolError, ValueError):
pass


Expand Down Expand Up @@ -240,12 +245,12 @@ def __init__(
json_filename = None

if json_filename is None and root_json_dict is None:
raise ValueError(
raise FlattenToolValueError(
_("Either json_filename or root_json_dict must be supplied")
)

if json_filename is not None and root_json_dict is not None:
raise ValueError(
raise FlattenToolValueError(
_("Only one of json_file or root_json_dict should be supplied")
)

Expand Down Expand Up @@ -509,7 +514,7 @@ def parse_json_dict(
continue

if type(v) not in BASIC_TYPES:
raise ValueError(
raise FlattenToolValueError(
_("Rolled up values must be basic types")
)
else:
Expand Down Expand Up @@ -652,7 +657,9 @@ def parse_json_dict(
top_level_of_sub_sheet=True,
)
else:
raise ValueError(_("Unsupported type {}").format(type(value)))
raise FlattenToolValueError(
_("Unsupported type {}").format(type(value))
)

if top:
sheet.append_line(flattened_dict)
Expand Down
16 changes: 10 additions & 6 deletions flattentool/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

import jsonref

from flattentool.exceptions import FlattenToolWarning
from flattentool.exceptions import (
FlattenToolError,
FlattenToolValueError,
FlattenToolWarning,
)
from flattentool.i18n import _
from flattentool.sheet import Sheet

Expand Down Expand Up @@ -94,7 +98,7 @@ def __contains__(self, key):
return key.replace(" ", "").lower() in self.data


class JsonLoaderLocalRefUsedWhenLocalRefsDisabled(Exception):
class JsonLoaderLocalRefUsedWhenLocalRefsDisabled(FlattenToolError):
pass


Expand Down Expand Up @@ -140,11 +144,11 @@ def __init__(
self.convert_flags = convert_flags

if root_schema_dict is None and schema_filename is None:
raise ValueError(
raise FlattenToolValueError(
_("One of schema_filename or root_schema_dict must be supplied")
)
if root_schema_dict is not None and schema_filename is not None:
raise ValueError(
raise FlattenToolValueError(
_("Only one of schema_filename or root_schema_dict should be supplied")
)
if schema_filename:
Expand Down Expand Up @@ -317,7 +321,7 @@ def parse_schema_dict(
if "string" in nested_type_set or "number" in nested_type_set:
yield property_name, title
else:
raise ValueError
raise FlattenToolValueError
elif "object" in type_set:
if title:
title_lookup[title].property_name = property_name
Expand Down Expand Up @@ -417,7 +421,7 @@ def parse_schema_dict(
)

else:
raise ValueError(
raise FlattenToolValueError(
_(
'Unknown type_set: {}, did you forget to explicitly set the "type" key on "items"?'
).format(type_set)
Expand Down
8 changes: 6 additions & 2 deletions flattentool/xml_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from collections import OrderedDict
from warnings import warn

from flattentool.exceptions import DataErrorWarning, FlattenToolWarning
from flattentool.exceptions import (
DataErrorWarning,
FlattenToolError,
FlattenToolWarning,
)
from flattentool.sort_xml import XMLSchemaWalker, sort_element

try:
Expand Down Expand Up @@ -56,7 +60,7 @@ def child_to_xml(parent_el, tagname, child, toplevel=False, nsmap=None):
elif tagname == "text()":
parent_el.text = str(child)
else:
raise ("Everything should end with text() or an attribute!")
raise FlattenToolError("Everything should end with text() or an attribute!")


def dict_to_xml(data, tagname, toplevel=True, nsmap=None):
Expand Down
Loading