# Path: chaosmonkey/api/hal.py
# class BaseDocument(object):
#     """Constructs a ``HAL`` document.
#     """
# 
#     def __init__(self, data=None, links=None, embedded=None):
#         """Base ``HAL`` Document. If no arguments are provided a minimal viable
#         ``HAL`` Document is created.
# 
#         Keyword Args:
#             data (dict): Data for the document
#             links (chaosmonkey.api.hal.link.Collection): A collection of ``HAL`` links
#             embedded: TBC
# 
#         Raises:
#             TypeError: If ``links`` is not a :class:`chaosmonkey.api.hal.link.Collection`
#         """
# 
#         self.data = data
#         self.embedded = embedded or {}
#         self.links = links or Collection()
# 
#     @property
#     def links(self):
#         return self._links
# 
#     @links.setter
#     def links(self, value):
#         if not isinstance(value, Collection):
#             if isinstance(value, (list, set, tuple)):
#                 value = Collection(*value)
#             else:
#                 raise TypeError('links must be a {0} or {1} instance'.format(
#                                 Collection, list))
#         self._links = value
# 
#     @property
#     def embedded(self):
#         return self._embedded
# 
#     @embedded.setter
#     def embedded(self, value):
#         if not isinstance(value, dict):
#             raise TypeError('embedded must be a {0} instance'.format(dict))
#         self._embedded = value
# 
#     def to_dict(self):
#         """Converts the ``Document`` instance into an appropriate data
#         structure for HAL formatted documents.
# 
#         Returns:
#             dict: The ``HAL`` document data structure
#         """
# 
#         document = {}
# 
#         # Add Data to the Document
#         if isinstance(self.data, dict):
#             document.update(self.data)
# 
#         # Add Links
#         if self.links:
#             document.update(self.links.to_dict())
# 
#         # Add Embedded: Embedded API TBC
#         if self.embedded:
#             embedded = {}
#             for n, v in self.embedded.items():
#                 if isinstance(v, list):
#                     embedded[n] = []
#                     for item in v:
#                         embedded[n].append(item.to_dict() if not isinstance(item, dict) else item)
#                 else:
#                     embedded[n] = v.to_dict()
# 
#             document.update({
#                 '_embedded': embedded
#             })
# 
#         return document
# 
#     def to_json(self):
#         """Converts :class:`.Document` to a ``JSON`` data structure.
# 
#         Returns:
#             str: ``JSON`` document
#         """
# 
#         return json.dumps(self.to_dict())
# 
# class Link(object):
#     """Build ``HAL`` specification ``_links`` object.
# 
#     Example:
#         >>> from chaosmonkey.api.hal.link import Link
#         >>> l = Link('foo', 'http://foo.com/bar')
#         >>> print l.to_json()
#         ... '{"foo": {"href": "http://foo.com/bar"}}'
#         >>> l.title = 'Foo'
#         >>> print l.to_json()
#         ... '{"foo": {"href": "http://foo.com/bar", "name": "Foo"}}'
# 
#     """
# 
#     def __init__(self, rel, href, **kwargs):
#         """Initialise a new ``Link`` object.
# 
#         Args:
#             rel (str): The links ``rel`` or name
#             href (str): The URI to the resource
# 
#         Keyword Args:
#             name (str): The links name attribute, optional
#             title (str): The links title attribute, optional
#             type (str): The links type attribute, optional
#             deprecation (str): The deprecation attribute, optional
#             profile (str): The profile  attribute, optional
#             templated (bool): The templated attribute, optional
#             hreflang (str): The hreflang attribute, optional
#         """
# 
#         self.rel = rel
#         self.href = href
# 
#         for attr in VALID_LINK_ATTRS:
#             if attr in kwargs:
#                 setattr(self, attr, kwargs.pop(attr))
# 
#     def to_dict(self):
#         """Returns the Python ``dict`` representation of the ``Link`` instance.
# 
#         Example:
#             >>> from chaosmonkey.api.hal.link import Link
#             >>> l = Link('foo', 'http://foo.com')
#             >>> l.to_dict()
#             ... {'foo': {'href': 'http://foo.com'}}
# 
#         Returns:
#             dict
#         """
# 
#         # Minimum viable link
#         link = {
#             'href': self.href
#         }
# 
#         # Add extra attributes if they exist
#         for attr in VALID_LINK_ATTRS:
#             if hasattr(self, attr):
#                 link[attr] = getattr(self, attr)
# 
#         return {
#             self.rel: link
#         }
# 
#     def to_json(self):
#         """Returns the ``JSON`` encoded representation of the ``Link`` object.
# 
#         Example:
#             >>> from chaosmonkey.api.hal.link import Link
#             >>> l = Link('foo', 'http://foo.com', name='Foo')
#             >>> print l.to_json()
#             ... '{"foo": {"href": "http://foo.com", "name": "Foo"}}'
# 
#         Returns:
#             str: The ``JSON`` encoded object
#         """
#         return json.dumps(self.to_dict())
# 
# Path: chaosmonkey/dal/database.py



from flask import request
from chaosmonkey.api.hal import BaseDocument, Link
from chaosmonkey.dal.database import db


class Executor(db.Model):
    """
    Executors are persistent representations of scheduled jobs.
    This model is shared between the cme and apscheduler.
    """

    __tablename__ = 'cme_executors'

    id = db.Column(db.String(80), primary_key=True)  #: unique identifier
    next_run_time = db.Column(db.DateTime, index=True)  #: DateTime for the executor to be executed
    job_state = db.Column(db.LargeBinary, nullable=False)  #: store the full state of the executor (with pickle)
    plan_id = db.Column(db.Integer, db.ForeignKey('cme_plans.id'))  #: plan id reference

# Path: chaosmonkey/api/hal.py
# class BaseDocument(object):
#     """Constructs a ``HAL`` document.
#     """
# 
#     def __init__(self, data=None, links=None, embedded=None):
#         """Base ``HAL`` Document. If no arguments are provided a minimal viable
#         ``HAL`` Document is created.
# 
#         Keyword Args:
#             data (dict): Data for the document
#             links (chaosmonkey.api.hal.link.Collection): A collection of ``HAL`` links
#             embedded: TBC
# 
#         Raises:
#             TypeError: If ``links`` is not a :class:`chaosmonkey.api.hal.link.Collection`
#         """
# 
#         self.data = data
#         self.embedded = embedded or {}
#         self.links = links or Collection()
# 
#     @property
#     def links(self):
#         return self._links
# 
#     @links.setter
#     def links(self, value):
#         if not isinstance(value, Collection):
#             if isinstance(value, (list, set, tuple)):
#                 value = Collection(*value)
#             else:
#                 raise TypeError('links must be a {0} or {1} instance'.format(
#                                 Collection, list))
#         self._links = value
# 
#     @property
#     def embedded(self):
#         return self._embedded
# 
#     @embedded.setter
#     def embedded(self, value):
#         if not isinstance(value, dict):
#             raise TypeError('embedded must be a {0} instance'.format(dict))
#         self._embedded = value
# 
#     def to_dict(self):
#         """Converts the ``Document`` instance into an appropriate data
#         structure for HAL formatted documents.
# 
#         Returns:
#             dict: The ``HAL`` document data structure
#         """
# 
#         document = {}
# 
#         # Add Data to the Document
#         if isinstance(self.data, dict):
#             document.update(self.data)
# 
#         # Add Links
#         if self.links:
#             document.update(self.links.to_dict())
# 
#         # Add Embedded: Embedded API TBC
#         if self.embedded:
#             embedded = {}
#             for n, v in self.embedded.items():
#                 if isinstance(v, list):
#                     embedded[n] = []
#                     for item in v:
#                         embedded[n].append(item.to_dict() if not isinstance(item, dict) else item)
#                 else:
#                     embedded[n] = v.to_dict()
# 
#             document.update({
#                 '_embedded': embedded
#             })
# 
#         return document
# 
#     def to_json(self):
#         """Converts :class:`.Document` to a ``JSON`` data structure.
# 
#         Returns:
#             str: ``JSON`` document
#         """
# 
#         return json.dumps(self.to_dict())
# 
# class Link(object):
#     """Build ``HAL`` specification ``_links`` object.
# 
#     Example:
#         >>> from chaosmonkey.api.hal.link import Link
#         >>> l = Link('foo', 'http://foo.com/bar')
#         >>> print l.to_json()
#         ... '{"foo": {"href": "http://foo.com/bar"}}'
#         >>> l.title = 'Foo'
#         >>> print l.to_json()
#         ... '{"foo": {"href": "http://foo.com/bar", "name": "Foo"}}'
# 
#     """
# 
#     def __init__(self, rel, href, **kwargs):
#         """Initialise a new ``Link`` object.
# 
#         Args:
#             rel (str): The links ``rel`` or name
#             href (str): The URI to the resource
# 
#         Keyword Args:
#             name (str): The links name attribute, optional
#             title (str): The links title attribute, optional
#             type (str): The links type attribute, optional
#             deprecation (str): The deprecation attribute, optional
#             profile (str): The profile  attribute, optional
#             templated (bool): The templated attribute, optional
#             hreflang (str): The hreflang attribute, optional
#         """
# 
#         self.rel = rel
#         self.href = href
# 
#         for attr in VALID_LINK_ATTRS:
#             if attr in kwargs:
#                 setattr(self, attr, kwargs.pop(attr))
# 
#     def to_dict(self):
#         """Returns the Python ``dict`` representation of the ``Link`` instance.
# 
#         Example:
#             >>> from chaosmonkey.api.hal.link import Link
#             >>> l = Link('foo', 'http://foo.com')
#             >>> l.to_dict()
#             ... {'foo': {'href': 'http://foo.com'}}
# 
#         Returns:
#             dict
#         """
# 
#         # Minimum viable link
#         link = {
#             'href': self.href
#         }
# 
#         # Add extra attributes if they exist
#         for attr in VALID_LINK_ATTRS:
#             if hasattr(self, attr):
#                 link[attr] = getattr(self, attr)
# 
#         return {
#             self.rel: link
#         }
# 
#     def to_json(self):
#         """Returns the ``JSON`` encoded representation of the ``Link`` object.
# 
#         Example:
#             >>> from chaosmonkey.api.hal.link import Link
#             >>> l = Link('foo', 'http://foo.com', name='Foo')
#             >>> print l.to_json()
#             ... '{"foo": {"href": "http://foo.com", "name": "Foo"}}'
# 
#         Returns:
#             str: The ``JSON`` encoded object
#         """
#         return json.dumps(self.to_dict())
# 
# Path: chaosmonkey/dal/database.py



# Path: chaosmonkey/dal/executor_model.py
from flask import request
from chaosmonkey.api.hal import BaseDocument, Link
from chaosmonkey.dal.database import db



class Executor(db.Model):
    """
    Executors are persistent representations of scheduled jobs.
    This model is shared between the cme and apscheduler.
    """

    __tablename__ = 'cme_executors'

    id = db.Column(db.String(80), primary_key=True)  #: unique identifier
    next_run_time = db.Column(db.DateTime, index=True)  #: DateTime for the executor to be executed
    job_state = db.Column(db.LargeBinary, nullable=False)  #: store the full state of the executor (with pickle)
    plan_id = db.Column(db.Integer, db.ForeignKey('cme_plans.id'))  #: plan id reference

    executed = db.Column(db.Boolean)  #: if the job was executed