Merge "Combines all exceptions into a single file"

This commit is contained in:
Zuul 2019-07-09 20:29:59 +00:00 committed by Gerrit Code Review
commit 601f281191
9 changed files with 74 additions and 120 deletions

View File

@ -81,11 +81,13 @@ def intermediary_processor(plugin_type, **kwargs):
# Load the plugin class # Load the plugin class
LOG.info("Load the plugin class") LOG.info("Load the plugin class")
entry_point = "data_extractor_plugins"
try: try:
plugin_class = pkg_resources.load_entry_point( plugin_class = pkg_resources.load_entry_point(
"spyglass", "data_extractor_plugins", plugin_type) "spyglass", entry_point, plugin_type)
except ImportError: except ImportError:
raise exceptions.UnsupportedPlugin() raise exceptions.UnsupportedPlugin(
plugin_name=plugin_type, entry_point=entry_point)
# Extract data from plugin data source # Extract data from plugin data source
LOG.info("Extract data from plugin data source") LOG.info("Extract data from plugin data source")

View File

@ -1,65 +0,0 @@
# Copyright 2018 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.
import logging
import sys
LOG = logging.getLogger(__name__)
class BaseError(Exception):
def __init__(self, msg):
self.msg = msg
def display_error(self):
LOG.info(self.msg)
sys.exit(1)
class NoSpecMatched(BaseError):
def __init__(self, excel_specs):
self.specs = excel_specs
def display_error(self):
# FIXME (Ian Pittwood): use log instead of print
print(
"No spec matched. Following are the available specs:\n".format(
self.specs))
sys.exit(1)
class MissingAttributeError(BaseError):
pass
class MissingValueError(BaseError):
pass
class ApiClientError(BaseError):
pass
class TokenGenerationError(BaseError):
pass
class FormationConnectionError(BaseError):
pass
class InvalidIntermediary(BaseError):
pass

View File

@ -16,7 +16,7 @@ from copy import deepcopy
import ipaddress import ipaddress
import logging import logging
from spyglass.data_extractor.custom_exceptions import InvalidIntermediary from spyglass.exceptions import InvalidIntermediary
DATA_DEFAULT = "#CHANGE_ME" DATA_DEFAULT = "#CHANGE_ME"
@ -629,8 +629,7 @@ class SiteDocumentData(object):
def _validate_key_in_intermediary_dict(key: str, dictionary: dict): def _validate_key_in_intermediary_dict(key: str, dictionary: dict):
if key not in dictionary: if key not in dictionary:
raise InvalidIntermediary( raise InvalidIntermediary(key=key)
'%s is not defined in the given intermediary file.' % key)
def site_document_data_factory(intermediary_dict: dict) -> SiteDocumentData: def site_document_data_factory(intermediary_dict: dict) -> SiteDocumentData:

View File

@ -32,7 +32,57 @@ class SpyglassBaseException(Exception):
class UnsupportedPlugin(SpyglassBaseException): class UnsupportedPlugin(SpyglassBaseException):
"""Exception that occurs when a plugin is called that does not exist""" """Exception that occurs when a plugin is called that does not exist
:param plugin_name: name of the specified plugin
:param entry_point: the package used to access plugin_name
"""
message = ( message = (
'%(plugin_name) was not found in the package %(entry_point) ' '%(plugin_name) was not found in the package %(entry_point) '
'entry points.') 'entry points.')
# Data Extractor exceptions
class InvalidIntermediary(SpyglassBaseException):
"""Exception that occurs when data is missing from the intermediary file
:param key: dictionary key that Spyglass attempted to access
"""
message = '%(key) is not defined in the given intermediary file.'
# Validator exceptions
class PathDoesNotExistError(SpyglassBaseException):
"""Exception that occurs when the document or schema path does not exist
:param file_type: type of the files being accessed, documents or schemas
:param path: nonexistent path attempted to access
"""
message = '%(file_type) path: %(path) does not exist.'
class UnexpectedFileType(SpyglassBaseException):
"""Exception that occurs when an unexpected file type is given
:param found_ext: the extension of the file given
:param expected_ext: the extension that was expected for the file
"""
message = (
'Unexpected file type %(found_ext), '
'expected type %(expected_ext)')
class DirectoryEmptyError(SpyglassBaseException):
"""Exception for when a directory is empty
This exception can occur when either a directory is empty or if a directory
does not have any files with the correct file extension.
:param ext: file extension being searched for
:param path: path being searched for files of the specified extension
"""
message = 'No files with %(ext) extension found in document path %(path)'

View File

@ -1,32 +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 PathDoesNotExistError(OSError):
"""Exception that occurs when the document or schema path does not exist"""
pass
class UnexpectedFileType(OSError):
"""Exception that occurs when an unexpected file type is given"""
pass
class DirectoryEmptyError(OSError):
"""Exception for when a directory is empty
This exception can occur when either a directory is empty or if a directory
does not have any files with the correct file extension.
"""
pass

View File

@ -19,7 +19,7 @@ import os
from jsonschema import Draft7Validator from jsonschema import Draft7Validator
import yaml import yaml
from spyglass.validators import exceptions from spyglass import exceptions
from spyglass.validators.validator import BaseDocumentValidator from spyglass.validators.validator import BaseDocumentValidator
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -43,11 +43,11 @@ class JSONSchemaValidator(BaseDocumentValidator):
# Check that given paths are valid # Check that given paths are valid
if not os.path.exists(document_path): if not os.path.exists(document_path):
LOG.error('Document path: %s does not exist.', document_path) raise exceptions.PathDoesNotExistError(
raise exceptions.PathDoesNotExistError() file_type='Document', path=document_path)
if not os.path.exists(schema_path): if not os.path.exists(schema_path):
LOG.error('Schema path: %s does not exist.', document_path) raise exceptions.PathDoesNotExistError(
raise exceptions.PathDoesNotExistError() file_type='Schema', path=schema_path)
# Extract list of document file paths from path # Extract list of document file paths from path
if os.path.isdir(document_path): if os.path.isdir(document_path):
@ -57,17 +57,17 @@ class JSONSchemaValidator(BaseDocumentValidator):
# Directory should not be empty # Directory should not be empty
if not self.documents: if not self.documents:
LOG.error( raise exceptions.DirectoryEmptyError(
'No files with %s extension found in document path ' ext=document_extension, path=document_path)
'%s', document_extension, document_path)
raise exceptions.DirectoryEmptyError()
elif os.path.splitext(document_path) == document_extension: elif os.path.splitext(document_path) == document_extension:
# Single files can just be appended to the list to process the same # Single files can just be appended to the list to process the same
# so long as the extension matches # so long as the extension matches
self.documents.append(document_path) self.documents.append(document_path)
else: else:
# Throw error if unexpected file type given # Throw error if unexpected file type given
raise exceptions.UnexpectedFileType() raise exceptions.UnexpectedFileType(
found_ext=os.path.splitext(document_path),
expected_ext=document_extension)
# Extract list of schema file paths from path # Extract list of schema file paths from path
if os.path.isdir(schema_path): if os.path.isdir(schema_path):
@ -77,16 +77,16 @@ class JSONSchemaValidator(BaseDocumentValidator):
# Directory should not be empty # Directory should not be empty
if not self.schemas: if not self.schemas:
LOG.error( raise exceptions.DirectoryEmptyError(
'No files with %s extension found in document path ' ext=schema_extension, path=schema_path)
'%s', document_extension, document_path)
raise exceptions.DirectoryEmptyError()
elif os.path.splitext(schema_path) == schema_extension: elif os.path.splitext(schema_path) == schema_extension:
# Single files can just be appended to the list to process the same # Single files can just be appended to the list to process the same
self.schemas.append(schema_path) self.schemas.append(schema_path)
else: else:
# Throw error if unexpected file type given # Throw error if unexpected file type given
raise exceptions.UnexpectedFileType() raise exceptions.UnexpectedFileType(
found_ext=os.path.splitext(schema_path),
expected_ext=schema_extension)
# Initialize pairs list for next step # Initialize pairs list for next step
self.document_schema_pairs = [] self.document_schema_pairs = []

View File

@ -19,8 +19,8 @@ from unittest import mock
import yaml import yaml
from spyglass.data_extractor.custom_exceptions import InvalidIntermediary
from spyglass.data_extractor import models from spyglass.data_extractor import models
from spyglass.exceptions import InvalidIntermediary
FIXTURE_DIR = os.path.join( FIXTURE_DIR = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'shared') os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'shared')

View File

@ -16,7 +16,7 @@ import os
import pytest import pytest
from spyglass.validators.exceptions import PathDoesNotExistError from spyglass.exceptions import PathDoesNotExistError
from spyglass.validators.json_validator import JSONSchemaValidator from spyglass.validators.json_validator import JSONSchemaValidator
FIXTURE_DIR = os.path.join( FIXTURE_DIR = os.path.join(