# Path: henet/events.py
# def subscribe(event, callback):
#     _SUBSCRIBERS[event].append(callback)
# 
# ALL_EVENTS = -1
# 
# def event2str(event):
#     if event == EVENT_CHANGED_CONTENT:
#         return _('Content changed.')
#     elif event == EVENT_DELETED_CONTENT:
#         return _('Content deleted.')
#     elif event == EVENT_CREATED_CONTENT:
#         return _('Content created')
#     elif event == EVENT_START_BUILDING:
#         return _('Update started.')
#     elif event == EVENT_BUILT:
#         return _('Update finished.')
#     elif event == EVENT_ALREADY_BUILDING:
#         return _('Update already in progress.')
#     return event
# 
# Path: henet/workers.py
# class MemoryWorkers(object):
#     def __init__(self, size=4):
#         self.size = 4
#         self._results = {}
#         self._in_progress = defaultdict(list)
#         self._pool = Pool(self.size, self._init_proc)
# 
#     def get_result(self, res_id):
#         return self._results[res_id]
# 
#     def _store_result(self, name, res_id, res, callback=None):
#         logger.debug('Got result back for %s' % res_id)
#         self._in_progress[name].remove(res_id)
#         success, result = res
#         self._results[res_id] = res
#         if not success:
#             logger.error(result)
#         if callback is not None:
#             callback(name, res_id, success, result)
# 
#     def _init_proc(self):
#         signal.signal(signal.SIGINT, signal.SIG_IGN)
# 
#     def in_progress(self, name):
#         return self._in_progress[name]
# 
#     def close(self):
#         self._pool.terminate()
#         self._pool.join()
# 
#     def apply_async(self, name, func, args=None, callback=None):
#         if args is None:
#             args = tuple()
#         res_id = str(uuid4())
#         self._in_progress[name].append(res_id)
#         logger.debug('Running %s async - id: %s' % (func, res_id))
#         async_callback = partial(self._store_result, name, res_id,
#                                  callback=callback)
#         cmd = partial(_run, func)
#         res = self._pool.apply_async(cmd, args, callback=async_callback)
#         time.sleep(.1)
#         if res.ready() and not res.successful():
#             try:
#                 res.get()
#             except Exception, e:
#                 async_callback((False, str(e)), callback=callback)
#         return res_id
# 
# Path: henet/util.py
# def send_email(tos, subject, body, smtp_config):
#     msg = MIMEText(body.encode('utf8'), 'plain', 'utf-8')
#     msg['To'] = email.utils.formataddr(('Recipient', ','.join(tos)))
#     msg['From'] = email.utils.formataddr(('Author', smtp_config['from']))
#     msg['Subject'] = Header(subject, 'utf8')
# 
#     server = smtplib.SMTP(smtp_config['host'], smtp_config.get('port', 25))
#     try:
#         server.ehlo()
#         if server.has_extn('STARTTLS'):
#             server.starttls()
#             server.ehlo()
# 
#         username = smtp_config.get('username')
#         if username is not None:
#             server.login(username, smtp_config['password'])
#         server.sendmail(smtp_config['from'], tos, msg.as_string())
#         logger.debug("Mail sent")
#     except Exception:
#         logger.exception("Could not send the email")
#     finally:
#         server.quit()


import signal
import sys
import os
import codecs
import bottle
import konfig
import multiprocessing_logging
from functools import partial
from logging.config import fileConfig
from collections import defaultdict
from bottle import route, run, view, app as app_stack, request
from bottle import static_file
from henet.events import subscribe, ALL_EVENTS, event2str
from henet.workers import MemoryWorkers
from henet.util import send_email
from bottle_utils.i18n import I18NPlugin
    from henet import views  # NOQA
        values = dict(config[page].items())
        # defaults
        if 'can_create' not in values:
            values['can_create'] = True
        pages.append((page, values))

    use_comments = config['henet'].get('comments', True)
    use_media = config['henet'].get('media', True)

    app_stack.vars = app.vars = {'pages': pages,
                                 'categories': cats,
                                 'get_alerts': get_alerts,
                                 'site_url': config['henet']['site_url'],
                                 'use_comments': use_comments,
                                 'use_media': use_media,
                                 'langs': langs}

    app_stack.view = partial(view, **app.vars)
    app_stack._config = app._config = config
    app_stack.workers = app.workers = MemoryWorkers()
    app_stack.use_comments = app.use_comments = use_comments
    app_stack.use_media = app.use_media = use_media
    app_stack.add_alert = app.add_alert = add_alert

    smtp_config = dict(config.items('smtp'))

    def _send_email(*args):
        args = list(args) + [smtp_config]
        app.workers.apply_async('send-email', send_email, args)


# Path: henet/events.py
# def subscribe(event, callback):
#     _SUBSCRIBERS[event].append(callback)
# 
# ALL_EVENTS = -1
# 
# def event2str(event):
#     if event == EVENT_CHANGED_CONTENT:
#         return _('Content changed.')
#     elif event == EVENT_DELETED_CONTENT:
#         return _('Content deleted.')
#     elif event == EVENT_CREATED_CONTENT:
#         return _('Content created')
#     elif event == EVENT_START_BUILDING:
#         return _('Update started.')
#     elif event == EVENT_BUILT:
#         return _('Update finished.')
#     elif event == EVENT_ALREADY_BUILDING:
#         return _('Update already in progress.')
#     return event
# 
# Path: henet/workers.py
# class MemoryWorkers(object):
#     def __init__(self, size=4):
#         self.size = 4
#         self._results = {}
#         self._in_progress = defaultdict(list)
#         self._pool = Pool(self.size, self._init_proc)
# 
#     def get_result(self, res_id):
#         return self._results[res_id]
# 
#     def _store_result(self, name, res_id, res, callback=None):
#         logger.debug('Got result back for %s' % res_id)
#         self._in_progress[name].remove(res_id)
#         success, result = res
#         self._results[res_id] = res
#         if not success:
#             logger.error(result)
#         if callback is not None:
#             callback(name, res_id, success, result)
# 
#     def _init_proc(self):
#         signal.signal(signal.SIGINT, signal.SIG_IGN)
# 
#     def in_progress(self, name):
#         return self._in_progress[name]
# 
#     def close(self):
#         self._pool.terminate()
#         self._pool.join()
# 
#     def apply_async(self, name, func, args=None, callback=None):
#         if args is None:
#             args = tuple()
#         res_id = str(uuid4())
#         self._in_progress[name].append(res_id)
#         logger.debug('Running %s async - id: %s' % (func, res_id))
#         async_callback = partial(self._store_result, name, res_id,
#                                  callback=callback)
#         cmd = partial(_run, func)
#         res = self._pool.apply_async(cmd, args, callback=async_callback)
#         time.sleep(.1)
#         if res.ready() and not res.successful():
#             try:
#                 res.get()
#             except Exception, e:
#                 async_callback((False, str(e)), callback=callback)
#         return res_id
# 
# Path: henet/util.py
# def send_email(tos, subject, body, smtp_config):
#     msg = MIMEText(body.encode('utf8'), 'plain', 'utf-8')
#     msg['To'] = email.utils.formataddr(('Recipient', ','.join(tos)))
#     msg['From'] = email.utils.formataddr(('Author', smtp_config['from']))
#     msg['Subject'] = Header(subject, 'utf8')
# 
#     server = smtplib.SMTP(smtp_config['host'], smtp_config.get('port', 25))
#     try:
#         server.ehlo()
#         if server.has_extn('STARTTLS'):
#             server.starttls()
#             server.ehlo()
# 
#         username = smtp_config.get('username')
#         if username is not None:
#             server.login(username, smtp_config['password'])
#         server.sendmail(smtp_config['from'], tos, msg.as_string())
#         logger.debug("Mail sent")
#     except Exception:
#         logger.exception("Could not send the email")
#     finally:
#         server.quit()


# Path: henet/app.py
import signal
import sys
import os
import codecs
import bottle
import konfig
import multiprocessing_logging
from functools import partial
from logging.config import fileConfig
from collections import defaultdict
from bottle import route, run, view, app as app_stack, request
from bottle import static_file
from henet.events import subscribe, ALL_EVENTS, event2str
from henet.workers import MemoryWorkers
from henet.util import send_email
from bottle_utils.i18n import I18NPlugin
    from henet import views  # NOQA

        values = dict(config[page].items())
        # defaults
        if 'can_create' not in values:
            values['can_create'] = True
        pages.append((page, values))

    use_comments = config['henet'].get('comments', True)
    use_media = config['henet'].get('media', True)

    app_stack.vars = app.vars = {'pages': pages,
                                 'categories': cats,
                                 'get_alerts': get_alerts,
                                 'site_url': config['henet']['site_url'],
                                 'use_comments': use_comments,
                                 'use_media': use_media,
                                 'langs': langs}

    app_stack.view = partial(view, **app.vars)
    app_stack._config = app._config = config
    app_stack.workers = app.workers = MemoryWorkers()
    app_stack.use_comments = app.use_comments = use_comments
    app_stack.use_media = app.use_media = use_media
    app_stack.add_alert = app.add_alert = add_alert

    smtp_config = dict(config.items('smtp'))

    def _send_email(*args):
        args = list(args) + [smtp_config]
        app.workers.apply_async('send-email', send_email, args)


    app_stack.send_email = app.send_email = _send_email