From b0f7b952b4b83a7ed6f6b544c7bf54e99a05f122 Mon Sep 17 00:00:00 2001 From: "Ian H. Pittwood" Date: Mon, 24 Jun 2019 17:41:48 -0500 Subject: [PATCH] Combines all exceptions into a single file This change moves spyglass-plugin-xls's necessary exception definitions locally into the spyglass-plugin-xls project and changes their base to the SpyglassBaseException class. This change must be merged before Spyglass's exceptions may be consolidated. Related Change: https://review.opendev.org/#/c/667240/ Change-Id: I81c5ca2d9083aece3641bc8b5405dfd44baec810 --- spyglass_plugin_xls/check_exceptions.py | 52 ------------------------- spyglass_plugin_xls/excel.py | 5 ++- spyglass_plugin_xls/excel_parser.py | 6 +-- spyglass_plugin_xls/exceptions.py | 27 +++++++++++++ tests/unit/test_excel.py | 4 +- tests/unit/test_excel_parser.py | 2 +- 6 files changed, 35 insertions(+), 61 deletions(-) delete mode 100644 spyglass_plugin_xls/check_exceptions.py create mode 100644 spyglass_plugin_xls/exceptions.py diff --git a/spyglass_plugin_xls/check_exceptions.py b/spyglass_plugin_xls/check_exceptions.py deleted file mode 100644 index e7dd2d2..0000000 --- a/spyglass_plugin_xls/check_exceptions.py +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright 2019 AT&T Intellectual Property. All other rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -class BaseError(Exception): - pass - - -class NotEnoughIp(BaseError): - - def __init__(self, cidr, total_nodes): - self.cidr = cidr - self.total_nodes = total_nodes - - def display_error(self): - print("{} can not handle {} nodes".format(self.cidr, self.total_nodes)) - - -class NoSpecMatched(BaseError): - - def __init__(self, excel_specs): - self.specs = excel_specs - - def display_error(self): - print( - "No spec matched. Following are the available specs:\n".format( - self.specs)) - - -class ExcelFileNotSpecified(BaseError): - - @staticmethod - def display_error(): - print("Engineering excel file not specified") - - -class ExcelSpecNotSpecified(BaseError): - - @staticmethod - def display_error(): - print("Engineering excel spec not specified") diff --git a/spyglass_plugin_xls/excel.py b/spyglass_plugin_xls/excel.py index 41790ce..d2f5dd1 100644 --- a/spyglass_plugin_xls/excel.py +++ b/spyglass_plugin_xls/excel.py @@ -16,12 +16,13 @@ import itertools import logging import pprint import re + from spyglass.data_extractor.base import BaseDataSourcePlugin from spyglass.data_extractor import models -from spyglass_plugin_xls.check_exceptions import ExcelFileNotSpecified -from spyglass_plugin_xls.check_exceptions import ExcelSpecNotSpecified from spyglass_plugin_xls.excel_parser import ExcelParser +from spyglass_plugin_xls.exceptions import ExcelFileNotSpecified +from spyglass_plugin_xls.exceptions import ExcelSpecNotSpecified LOG = logging.getLogger(__name__) diff --git a/spyglass_plugin_xls/excel_parser.py b/spyglass_plugin_xls/excel_parser.py index afdc512..f13c2b0 100644 --- a/spyglass_plugin_xls/excel_parser.py +++ b/spyglass_plugin_xls/excel_parser.py @@ -20,9 +20,7 @@ import re import sys import yaml -from spyglass.data_extractor.custom_exceptions import NoSpecMatched - -# from spyglass.data_extractor.custom_exceptions +from spyglass_plugin_xls.exceptions import NoSpecMatched LOG = logging.getLogger(__name__) @@ -77,7 +75,7 @@ class ExcelParser(object): self.excel_specs["specs"][spec]["ipmi_sheet_name"] = sheet if self.validate_sheet(spec, sheet): return spec - raise NoSpecMatched(self.excel_specs) + raise NoSpecMatched(excel_specs=self.excel_specs) def _get_workbook(self): provided_sheetname = self.excel_specs["specs"][ diff --git a/spyglass_plugin_xls/exceptions.py b/spyglass_plugin_xls/exceptions.py new file mode 100644 index 0000000..a610009 --- /dev/null +++ b/spyglass_plugin_xls/exceptions.py @@ -0,0 +1,27 @@ +# Copyright 2019 AT&T Intellectual Property. All other rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from spyglass.exceptions import SpyglassBaseException + + +class NoSpecMatched(SpyglassBaseException): + message = 'No spec matched. The available specs are: %(excel_specs)' + + +class ExcelFileNotSpecified(SpyglassBaseException): + message = 'Engineering excel file not specified' + + +class ExcelSpecNotSpecified(SpyglassBaseException): + message = 'Engineering excel spec not specified' diff --git a/tests/unit/test_excel.py b/tests/unit/test_excel.py index 708cfac..1b72d9d 100644 --- a/tests/unit/test_excel.py +++ b/tests/unit/test_excel.py @@ -20,10 +20,10 @@ from unittest import mock import pytest from spyglass.data_extractor import models -from spyglass_plugin_xls.check_exceptions import ExcelFileNotSpecified -from spyglass_plugin_xls.check_exceptions import ExcelSpecNotSpecified from spyglass_plugin_xls.excel import ExcelPlugin from spyglass_plugin_xls.excel_parser import ExcelParser +from spyglass_plugin_xls.exceptions import ExcelFileNotSpecified +from spyglass_plugin_xls.exceptions import ExcelSpecNotSpecified FIXTURE_DIR = os.path.join( os.path.dirname(os.path.dirname(__file__)), 'shared') diff --git a/tests/unit/test_excel_parser.py b/tests/unit/test_excel_parser.py index 0d0a163..b8bf7dd 100644 --- a/tests/unit/test_excel_parser.py +++ b/tests/unit/test_excel_parser.py @@ -18,7 +18,7 @@ import unittest from openpyxl import Workbook from openpyxl.worksheet.worksheet import Worksheet import pytest -from spyglass.data_extractor.custom_exceptions import NoSpecMatched +from spyglass_plugin_xls.exceptions import NoSpecMatched import yaml from spyglass_plugin_xls.excel_parser import ExcelParser