# Path: pushmanager/core/db.py
# def UnsignedInteger():
# def init_db():
# def finalize_db():
# def execute_cb(query, callback_fn):
# def execute_transaction_cb(queries, callback_fn, condition=None):
# def prefix_insert_ignore(insert_ignore, compiler, **kw):
# def prefix_insert_ignore_sqlite(insert_ignore, compiler, **kw):
# class DatabaseError(Exception):
# class PushCheckList(Base):
# class PushPlans(Base):
# class PushPushContents(Base):
# class PushPushes(Base):
# class PushRemovals(Base):
# class PushRequests(Base):
# class InsertIgnore(Insert):
# 
# Path: pushmanager/servlets/msg.py
# class MsgServlet(RequestHandler):
#     people = []
# 
#     def post(self):
#         if not self.current_user:
#             return self.send_error(403)
# 
#         push_id = self.request.arguments.get('id', [None])[0]
#         if not push_id:
#             return self.send_error(404)
#         contents_query = self.generate_pushcontent_query(push_id)
#         db.execute_cb(contents_query, self.get_push_request_users)
#         people = self.people
#         message = self.request.arguments.get('message', [None])[0]
#         if not message:
#             return self.send_error(500)
# 
#         irc_nick = Settings['irc']['nickname'].format(
#             pushmaster=self.current_user
#         )
# 
#         if not people:
#             irc_message = u'[[pushmaster {0}]] {1}'.format(
#                 self.current_user,
#                 message,
#             )
# 
#             subprocess.call([
#                 '/nail/sys/bin/nodebot',
#                 '-i',
#                 irc_nick,
#                 Settings['irc']['channel'],
#                 irc_message
#             ])
#             return
# 
#         send_people_msg_in_groups(
#             people,
#             message,
#             irc_nick,
#             Settings['irc']['channel'],
#             person_per_group=5,
#             prefix_msg='[[pushmaster %s]]' % self.current_user
#         )
# 
#     def generate_pushcontent_query(self, push_id):
#         state = self.request.arguments.get('state', [None])[0]
#         contents_query = ''
#         if state == 'requested':
#             contents_query = db.push_requests.select(
#                     db.push_pushcontents.c.push == push_id
#             )
#         else:
#             contents_query = db.push_requests.select(
#                 SA.and_(
#                     db.push_requests.c.id == db.push_pushcontents.c.request,
#                     db.push_pushcontents.c.push == push_id,
#                 ),
#                 order_by=(db.push_requests.c.user, db.push_requests.c.title)
#             )
#         return contents_query
# 
#     def get_push_request_users(self, success, db_results):
#         request_list = self.filter_request_by_state(success, db_results)
#         people = []
#         for request in request_list:
#             people.append((request['user']))
#             for watcher in request['watchers'].split(','):
#                 people.append((watcher))
#         people = list(set(people))
#         people = filter(None, people)
#         self.people = people
# 
#     def filter_request_by_state(self, success, db_results):
#         user_requests = []
#         state = self.request.arguments.get('state', [None])[0]
#         for request in db_results:
#             if (state == 'all' and request['state'] != 'pickme') or (
#                     state == request['state']):
#                 user_requests.append(request)
#         return user_requests
# 
# Path: pushmanager/testing/mocksettings.py



import contextlib
import subprocess
import mock
import testify as T
from pushmanager.core import db
from pushmanager.core.util import get_servlet_urlspec
from pushmanager.servlets.msg import MsgServlet
from pushmanager.testing.mocksettings import MockedSettings
from pushmanager.testing.testservlet import ServletTestMixin
        },
                            {
        'reviewid': None, 'watchers': u'',
        'description': u'', 'tags': u'', 'created': 1234567890.1234567,
        'modified': 1234567899.1234568, 'comments': u'', 'repo': u'testuser',
        'state': u'pickme', 'user': u'testuser_2', 'branch': u'', 'conflicts': None,
        'title': u'testing', 'id': 1, 'revision': u'0'
        },
                            {
        'reviewid': None, 'watchers': u'',
        'description': u'', 'tags': u'', 'created': 1234567890.1234567,
        'modified': 1234567899.1234568, 'comments': u'', 'repo': u'testuser',
        'state': u'verified', 'user': u'testuser_3', 'branch': u'', 'conflicts': None,
        'title': u'testing', 'id': 1, 'revision': u'0'
        }]


class MsgServletTest(T.TestCase, ServletTestMixin):
    @T.setup_teardown
    def mock_logged_in_user(self):
        with contextlib.nested(
            mock.patch.object(subprocess, 'call'),
            mock.patch.object(
                MsgServlet, 'get_current_user', return_value='testuser',
            ),
            mock.patch.dict(db.Settings, MockedSettings),
        ) as (self.call_mock, _, _):
            yield

    def get_handlers(self):

# Path: pushmanager/core/db.py
# def UnsignedInteger():
# def init_db():
# def finalize_db():
# def execute_cb(query, callback_fn):
# def execute_transaction_cb(queries, callback_fn, condition=None):
# def prefix_insert_ignore(insert_ignore, compiler, **kw):
# def prefix_insert_ignore_sqlite(insert_ignore, compiler, **kw):
# class DatabaseError(Exception):
# class PushCheckList(Base):
# class PushPlans(Base):
# class PushPushContents(Base):
# class PushPushes(Base):
# class PushRemovals(Base):
# class PushRequests(Base):
# class InsertIgnore(Insert):
# 
# Path: pushmanager/servlets/msg.py
# class MsgServlet(RequestHandler):
#     people = []
# 
#     def post(self):
#         if not self.current_user:
#             return self.send_error(403)
# 
#         push_id = self.request.arguments.get('id', [None])[0]
#         if not push_id:
#             return self.send_error(404)
#         contents_query = self.generate_pushcontent_query(push_id)
#         db.execute_cb(contents_query, self.get_push_request_users)
#         people = self.people
#         message = self.request.arguments.get('message', [None])[0]
#         if not message:
#             return self.send_error(500)
# 
#         irc_nick = Settings['irc']['nickname'].format(
#             pushmaster=self.current_user
#         )
# 
#         if not people:
#             irc_message = u'[[pushmaster {0}]] {1}'.format(
#                 self.current_user,
#                 message,
#             )
# 
#             subprocess.call([
#                 '/nail/sys/bin/nodebot',
#                 '-i',
#                 irc_nick,
#                 Settings['irc']['channel'],
#                 irc_message
#             ])
#             return
# 
#         send_people_msg_in_groups(
#             people,
#             message,
#             irc_nick,
#             Settings['irc']['channel'],
#             person_per_group=5,
#             prefix_msg='[[pushmaster %s]]' % self.current_user
#         )
# 
#     def generate_pushcontent_query(self, push_id):
#         state = self.request.arguments.get('state', [None])[0]
#         contents_query = ''
#         if state == 'requested':
#             contents_query = db.push_requests.select(
#                     db.push_pushcontents.c.push == push_id
#             )
#         else:
#             contents_query = db.push_requests.select(
#                 SA.and_(
#                     db.push_requests.c.id == db.push_pushcontents.c.request,
#                     db.push_pushcontents.c.push == push_id,
#                 ),
#                 order_by=(db.push_requests.c.user, db.push_requests.c.title)
#             )
#         return contents_query
# 
#     def get_push_request_users(self, success, db_results):
#         request_list = self.filter_request_by_state(success, db_results)
#         people = []
#         for request in request_list:
#             people.append((request['user']))
#             for watcher in request['watchers'].split(','):
#                 people.append((watcher))
#         people = list(set(people))
#         people = filter(None, people)
#         self.people = people
# 
#     def filter_request_by_state(self, success, db_results):
#         user_requests = []
#         state = self.request.arguments.get('state', [None])[0]
#         for request in db_results:
#             if (state == 'all' and request['state'] != 'pickme') or (
#                     state == request['state']):
#                 user_requests.append(request)
#         return user_requests
# 
# Path: pushmanager/testing/mocksettings.py



# Path: pushmanager/tests/test_servlet_msg.py
import contextlib
import subprocess
import mock
import testify as T
from pushmanager.core import db
from pushmanager.core.util import get_servlet_urlspec
from pushmanager.servlets.msg import MsgServlet
from pushmanager.testing.mocksettings import MockedSettings
from pushmanager.testing.testservlet import ServletTestMixin

        },
                            {
        'reviewid': None, 'watchers': u'',
        'description': u'', 'tags': u'', 'created': 1234567890.1234567,
        'modified': 1234567899.1234568, 'comments': u'', 'repo': u'testuser',
        'state': u'pickme', 'user': u'testuser_2', 'branch': u'', 'conflicts': None,
        'title': u'testing', 'id': 1, 'revision': u'0'
        },
                            {
        'reviewid': None, 'watchers': u'',
        'description': u'', 'tags': u'', 'created': 1234567890.1234567,
        'modified': 1234567899.1234568, 'comments': u'', 'repo': u'testuser',
        'state': u'verified', 'user': u'testuser_3', 'branch': u'', 'conflicts': None,
        'title': u'testing', 'id': 1, 'revision': u'0'
        }]


class MsgServletTest(T.TestCase, ServletTestMixin):
    @T.setup_teardown
    def mock_logged_in_user(self):
        with contextlib.nested(
            mock.patch.object(subprocess, 'call'),
            mock.patch.object(
                MsgServlet, 'get_current_user', return_value='testuser',
            ),
            mock.patch.dict(db.Settings, MockedSettings),
        ) as (self.call_mock, _, _):
            yield

    def get_handlers(self):

        return [get_servlet_urlspec(MsgServlet)]