# Path: zoe_lib/exceptions.py
# class InvalidApplicationDescription(ZoeAPIException):
#     """
#     An exception thrown while parsing an application description.
#     """
#     def __init__(self, msg):
#         super().__init__("Error: " + msg)
# 
# class ZoeLibException(Exception):
#     """
#     A generic exception.
#     """
#     def __init__(self, message='Something happened'):
#         super().__init__()
#         self.message = message
# 
#     def __str__(self):
#         return self.message


import logging
import json
import jsonschema
import zoe_lib.version
import zoe_lib.config
from zoe_lib.exceptions import InvalidApplicationDescription, ZoeLibException
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This module contains code to validate application descriptions.
"""




log = logging.getLogger(__name__)


def app_validate(data):
    """
    Validates an application description, making sure all required fields are present and of the correct type.
    If the description is not valid, an InvalidApplicationDescription exception is thrown.
    Uses a JSON schema definition.

    :param data: an open file descriptor containing JSON data
    :return: None if the application description is correct
    """

    schema = json.load(open('schemas/app_description_schema.json', 'r'))
    try:
        jsonschema.validate(data, schema)
    except jsonschema.ValidationError as e:
        raise InvalidApplicationDescription(str(e))
    except jsonschema.SchemaError:
        log.exception('BUG: invalid schema for application descriptions')

# Path: zoe_lib/exceptions.py
# class InvalidApplicationDescription(ZoeAPIException):
#     """
#     An exception thrown while parsing an application description.
#     """
#     def __init__(self, msg):
#         super().__init__("Error: " + msg)
# 
# class ZoeLibException(Exception):
#     """
#     A generic exception.
#     """
#     def __init__(self, message='Something happened'):
#         super().__init__()
#         self.message = message
# 
#     def __str__(self):
#         return self.message


# Path: zoe_lib/applications.py
import logging
import json
import jsonschema
import zoe_lib.version
import zoe_lib.config
from zoe_lib.exceptions import InvalidApplicationDescription, ZoeLibException

# See the License for the specific language governing permissions and
# limitations under the License.

"""
This module contains code to validate application descriptions.
"""




log = logging.getLogger(__name__)


def app_validate(data):
    """
    Validates an application description, making sure all required fields are present and of the correct type.
    If the description is not valid, an InvalidApplicationDescription exception is thrown.
    Uses a JSON schema definition.

    :param data: an open file descriptor containing JSON data
    :return: None if the application description is correct
    """

    schema = json.load(open('schemas/app_description_schema.json', 'r'))
    try:
        jsonschema.validate(data, schema)
    except jsonschema.ValidationError as e:
        raise InvalidApplicationDescription(str(e))
    except jsonschema.SchemaError:
        log.exception('BUG: invalid schema for application descriptions')

        raise ZoeLibException('BUG: invalid schema for application descriptions')