# Path: spyne/util/email.py
# def email_exception(exception_address, message="", bcc=None):
#     # http://stackoverflow.com/questions/1095601/find-module-name-of-the-originating-exception-in-python
#     frm = inspect.trace()[-1]
#     mod = inspect.getmodule(frm[0])
#     module_name = mod.__name__ if mod else frm[1]
# 
#     sender = 'robot@spyne.io'
#     recipients = [exception_address]
#     if bcc is not None:
#         recipients.extend(bcc)
# 
#     error_str = ("%s\n\n%s" % (message, traceback.format_exc()))
#     msg = MIMEText(error_str.encode('utf8'), 'plain', 'utf8')
#     msg['To'] = exception_address
#     msg['From'] = 'Spyne <robot@spyne.io>'
#     msg['Date'] = formatdate()
#     msg['Subject'] = "(%s@%s) %s" % (getpass.getuser(), gethostname(), module_name)
# 
#     try:
#         smtp_object = smtplib.SMTP('localhost')
#         smtp_object.sendmail(sender, recipients, msg.as_string())
#         logger.error("Error email sent")
# 
#     except Exception as e:
#         logger.error("Error: unable to send email")
#         logger.exception(e)


import logging
from spyne.application import Application
from spyne.error import Fault
from spyne.error import InternalError
from spyne.error import ResourceNotFoundError
from spyne.util.email import email_exception
from sqlalchemy.orm.exc import NoResultFound
from template.context import UserDefinedContext
#       used to endorse or promote products derived from this software without
#       specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

logger = logging.getLogger(__name__)




EXCEPTION_ADDRESS = "everybody@example.com"


def _on_method_call(ctx):
    ctx.udc = UserDefinedContext()


def _on_method_context_closed(ctx):
    if ctx.udc is not None:
        ctx.udc.session.commit()

# Path: spyne/util/email.py
# def email_exception(exception_address, message="", bcc=None):
#     # http://stackoverflow.com/questions/1095601/find-module-name-of-the-originating-exception-in-python
#     frm = inspect.trace()[-1]
#     mod = inspect.getmodule(frm[0])
#     module_name = mod.__name__ if mod else frm[1]
# 
#     sender = 'robot@spyne.io'
#     recipients = [exception_address]
#     if bcc is not None:
#         recipients.extend(bcc)
# 
#     error_str = ("%s\n\n%s" % (message, traceback.format_exc()))
#     msg = MIMEText(error_str.encode('utf8'), 'plain', 'utf8')
#     msg['To'] = exception_address
#     msg['From'] = 'Spyne <robot@spyne.io>'
#     msg['Date'] = formatdate()
#     msg['Subject'] = "(%s@%s) %s" % (getpass.getuser(), gethostname(), module_name)
# 
#     try:
#         smtp_object = smtplib.SMTP('localhost')
#         smtp_object.sendmail(sender, recipients, msg.as_string())
#         logger.error("Error email sent")
# 
#     except Exception as e:
#         logger.error("Error: unable to send email")
#         logger.exception(e)


# Path: examples/template/template/application.py
import logging
from spyne.application import Application
from spyne.error import Fault
from spyne.error import InternalError
from spyne.error import ResourceNotFoundError
from spyne.util.email import email_exception
from sqlalchemy.orm.exc import NoResultFound
from template.context import UserDefinedContext

#       used to endorse or promote products derived from this software without
#       specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

logger = logging.getLogger(__name__)




EXCEPTION_ADDRESS = "everybody@example.com"


def _on_method_call(ctx):
    ctx.udc = UserDefinedContext()


def _on_method_context_closed(ctx):
    if ctx.udc is not None:
        ctx.udc.session.commit()

        ctx.udc.session.close()