<commit_before># -*- coding: utf-8 -*-
import hashlib
import os
import stat
from os.path import dirname, exists, join

def test_dropbox_is_created_if_it_does_not_exist():
    from shutil import rmtree
    # assemble a 'safe' directory location inside our package
    # and make sure it does not exist
    dropbox_root = join(dirname(__file__), '__test__dropbox_root__')
    if exists(dropbox_root):
        rmtree(dropbox_root)

    # instantiating our application will cause the specified directory
    # to be created
    assert not exists(dropbox_root)
    from briefkasten.dropbox import DropboxContainer
    DropboxContainer(root=dropbox_root, settings=dict(fs_pgp_pubkeys=None))
    assert exists(dropbox_root)
    # clean up after ourselves
    rmtree(dropbox_root)

def test_dropbox_status_no_message(dropbox_container):
    dropbox = dropbox_container.add_dropbox(drop_id=u'foo')
    assert dropbox.status == u'010 created'

def test_dropbox_status_no_file(dropbox):
    os.remove(join(dropbox.fs_path, 'status'))
    assert dropbox.status == u'000 no status file'

def test_dropbox_status_manual(dropbox):
    with open(join(dropbox.fs_path, 'status'), 'w') as status_file:
        status_file.write(u'23 in limbo'.encode('utf-8'))
    assert dropbox.status == u'23 in limbo'

def test_dropbox_status_initial(dropbox):
    """ the initial status of a dropbox is 'created'"""
    assert dropbox.status == u'010 created'

def test_dropbox_status_submitted_without_attachment(dropbox_without_attachment):
    """a dropbox without an attachment won't be cleansed and will be set to 'success' directly
       after processing"""
    dropbox_without_attachment.process()
    assert dropbox_without_attachment.status == u'900 success'

def test_dropbox_status_submitted(dropbox):
    """once a dropbox has initiated its processing, its status changes to 'quarantined',
    however, in our test setup this always succeeds immediately.
    """
    dropbox.process()
    assert dropbox.status == u'900 success'

def test_mocked_attachment_processor_status(dropbox):
    dropbox._process_attachments()
    assert dropbox.status == u'299 Cleansed'

def test_mocked_attachment_processor_cleansed(dropbox):
    fs_dirty = dropbox.fs_dirty_attachments[0]
    dirty_lines = open(fs_dirty, 'r').readlines()
    dropbox._process_attachments()
    fs_cleansed = dropbox.fs_cleansed_attachments[0]
    assert open(fs_cleansed, 'r').readlines() == dirty_lines

def test_dropbox_process_failure(dropbox):
    # TODO
    pass

def test_dropbox_retrieval(dropbox_container, dropbox):
    assert dropbox.drop_id == dropbox_container.get_dropbox(dropbox.drop_id).drop_id
    assert dropbox.editor_token == dropbox_container.get_dropbox(dropbox.drop_id).editor_token
    assert dropbox.message == dropbox_container.get_dropbox(dropbox.drop_id).message

def test_dropbox_permissions(dropbox):
    assert stat.S_IMODE(os.stat(dropbox.paths_created[0]).st_mode) == 0770

def test_message_permissions(dropbox):
    assert stat.S_IMODE(os.stat(dropbox.paths_created[1]).st_mode) == 0660

def test_editor_token_created(dropbox_container, dropbox):
    assert (dropbox_container.get_dropbox(
        dropbox.drop_id).editor_token ==
        open(dropbox.paths_created[1], 'r').readline())
    assert stat.S_IMODE(os.stat(dropbox.paths_created[1]).st_mode) == 0660

def test_attachment_creation_and_permissions(dropbox_container, drop_id, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('attachment.txt'), 'r'),
        'mimetype': 'text/plain',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'attachment.txt',
        'size': -1})
    dropbox = dropbox_container.add_dropbox(drop_id, message=u'Überraschung!', attachments=[attachment])
    assert stat.S_IMODE(os.stat(dropbox.paths_created[-2]).st_mode) == 0770
    assert dropbox.paths_created[-2].endswith("/attach")
    assert stat.S_IMODE(os.stat(dropbox.paths_created[-1]).st_mode) == 0660
    # we strip the original filename
    assert not dropbox.paths_created[-1].endswith("/attach/attachment.txt")
    # but preserve the file ending
    assert dropbox.paths_created[-1].endswith(".txt")
    assert open(dropbox.paths_created[-1]).read().decode('utf-8') == u'Schönen Guten Tag!'  # contents of attachment.txt

def test_attachment_creation_outside_container(dropbox_container, drop_id, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('attachment.txt'), 'r'),
        'mimetype': 'text/plain',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'../../authorized_keys',
        'size': -1})
    dropbox_container.add_dropbox(drop_id, message=u'Überraschung!', attachments=[attachment])
    assert not exists(join(dropbox_container.fs_path, 'authorized_keys'))

md5 = hashlib.md5()

def md5sum(f):
    for chunk in iter(lambda: f.read(8192), b''):
        md5.update(chunk)
    return md5.digest()

def test_attachment_is_image(dropbox_container, drop_id, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('attachment.png'), 'rb'),
        'mimetype': 'image/jpeg',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'attachment.png',
        'size': -1})
    dropbox = dropbox_container.add_dropbox(drop_id, message=u'Mit Foto', attachments=[attachment])
    # we strip the original filename
    assert not dropbox.paths_created[-1].endswith("/attach/attachment.png")
    # but preserve the file ending
    assert dropbox.paths_created[-1].endswith(".png")
    assert md5sum(open(dropbox.paths_created[-1], 'rb')) == md5sum(attachment.file)

def test_attachment_is_unicode(dropbox_container, drop_id, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('unicode.txt'), 'r'),
        'mimetype': 'text/plain',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'unicode.txt',
        'size': -1})
    # we only assert that this works
    dropbox_container.add_dropbox(drop_id, message=u'Überraschung!', attachments=[attachment])

def test_non_existent_drop_id_creates_dropbox_ad_hoc(dropbox_container):
    assert 'foobar' not in dropbox_container
    newbox = dropbox_container.get_dropbox('foobar')
    assert newbox.drop_id == 'foobar'
    assert 'foobar' in dropbox_container

def test_add_one_reply(dropbox_container, dropbox):
    reply = dict(
        message=u'Smørebrød',
        author=u'Børk')
    dropbox.add_reply(reply)
    assert dropbox.replies[0]['message'] == u'Smørebrød'
    assert dropbox.replies[0]['author'] == u'Børk'
    refetched_dropbox = dropbox_container.get_dropbox(dropbox.drop_id)
    assert refetched_dropbox.replies[0]['message'] == u'Smørebrød'
    assert refetched_dropbox.replies[0]['author'] == u'Børk'

def test_add_two_replies(app):
    NotImplemented

def test_access_replies(app):
    NotImplemented

def test_dropid_length():
    from briefkasten.dropbox import generate_drop_id
    assert len(generate_drop_id(12)) == 12

def test_filename_sanitizing():
    from briefkasten.dropbox import sanitize_filename
    assert not sanitize_filename('attachment.txt') == 'attachment.txt'
    assert sanitize_filename('attachment.txt').endswith('.txt')

def test_filename_has_no_ending():
    from briefkasten.dropbox import sanitize_filename
    assert not sanitize_filename('blubber') == 'blubber'

def test_drop_url(dropbox, app, testing):
    assert dropbox.drop_url == testing.route_url('dropbox_view', drop_id=dropbox.drop_id)

def test_editor_url(dropbox, app, testing):
    assert dropbox.editor_url == testing.route_url(
        'dropbox_editor',
        drop_id=dropbox.drop_id,
        editor_token=dropbox.editor_token)

def test_notification_text_message(dropbox_without_attachment):
    message = u'Sätz mit Ümlœuten'
    dropbox_without_attachment.message = message
    assert message in dropbox_without_attachment._notification_text

def test_notification_text_zero(dropbox_without_attachment):
    assert u'Die Einreichung enthielt keine Anhänge.' in dropbox_without_attachment._notification_text

def test_notification_text_one(dropbox):
    assert u'Die Einreichung enthielt einen Anhang' in dropbox._notification_text

def test_notification_text_unsupported(dropbox):
    dropbox.status = '800 unsupported file type'
    assert u'Mindestens ein Anhang wurde nicht bereinigt, da dessen Dateityp nicht unterstützt wird.' in dropbox._notification_text

def test_notification_text_two(dropbox, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('unicode.txt'), 'r'),
        'mimetype': 'text/plain',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'unicode.txt',
        'size': -1})
    dropbox.add_attachment(attachment)
    assert u'Die Einreichung enthielt 2 Anhänge.' in dropbox._notification_text
<commit_msg>Add failing test<commit_after># -*- coding: utf-8 -*-
import hashlib
import os
import stat
from os.path import dirname, exists, join

def test_dropbox_is_created_if_it_does_not_exist():
    from shutil import rmtree
    # assemble a 'safe' directory location inside our package
    # and make sure it does not exist
    dropbox_root = join(dirname(__file__), '__test__dropbox_root__')
    if exists(dropbox_root):
        rmtree(dropbox_root)

    # instantiating our application will cause the specified directory
    # to be created
    assert not exists(dropbox_root)
    from briefkasten.dropbox import DropboxContainer
    DropboxContainer(root=dropbox_root, settings=dict(fs_pgp_pubkeys=None))
    assert exists(dropbox_root)
    # clean up after ourselves
    rmtree(dropbox_root)

def test_dropbox_status_no_message(dropbox_container):
    dropbox = dropbox_container.add_dropbox(drop_id=u'foo')
    assert dropbox.status == u'010 created'

def test_dropbox_status_no_file(dropbox):
    os.remove(join(dropbox.fs_path, 'status'))
    assert dropbox.status == u'000 no status file'

def test_dropbox_status_manual(dropbox):
    with open(join(dropbox.fs_path, 'status'), 'w') as status_file:
        status_file.write(u'23 in limbo'.encode('utf-8'))
    assert dropbox.status == u'23 in limbo'

def test_dropbox_status_initial(dropbox):
    """ the initial status of a dropbox is 'created'"""
    assert dropbox.status == u'010 created'

def test_dropbox_status_submitted_without_attachment(dropbox_without_attachment):
    """a dropbox without an attachment won't be cleansed and will be set to 'success' directly
       after processing"""
    dropbox_without_attachment.process()
    assert dropbox_without_attachment.status == u'900 success'

def test_dropbox_status_submitted(dropbox):
    """once a dropbox has initiated its processing, its status changes to 'quarantined',
    however, in our test setup this always succeeds immediately.
    """
    dropbox.process()
    assert dropbox.status == u'900 success'

def test_mocked_attachment_processor_status(dropbox):
    dropbox._process_attachments()
    assert dropbox.status == u'299 Cleansed'

def test_mocked_attachment_processor_cleansed(dropbox):
    fs_dirty = dropbox.fs_dirty_attachments[0]
    dirty_lines = open(fs_dirty, 'r').readlines()
    dropbox._process_attachments()
    fs_cleansed = dropbox.fs_cleansed_attachments[0]
    assert open(fs_cleansed, 'r').readlines() == dirty_lines

def test_dropbox_process_failure(dropbox):
    # TODO
    pass

def test_dropbox_retrieval(dropbox_container, dropbox):
    assert dropbox.drop_id == dropbox_container.get_dropbox(dropbox.drop_id).drop_id
    assert dropbox.editor_token == dropbox_container.get_dropbox(dropbox.drop_id).editor_token
    assert dropbox.message == dropbox_container.get_dropbox(dropbox.drop_id).message

def test_dropbox_permissions(dropbox):
    assert stat.S_IMODE(os.stat(dropbox.paths_created[0]).st_mode) == 0770

def test_message_permissions(dropbox):
    assert stat.S_IMODE(os.stat(dropbox.paths_created[1]).st_mode) == 0660

def test_editor_token_created(dropbox_container, dropbox):
    assert (dropbox_container.get_dropbox(
        dropbox.drop_id).editor_token ==
        open(dropbox.paths_created[1], 'r').readline())
    assert stat.S_IMODE(os.stat(dropbox.paths_created[1]).st_mode) == 0660

def test_attachment_creation_and_permissions(dropbox_container, drop_id, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('attachment.txt'), 'r'),
        'mimetype': 'text/plain',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'attachment.txt',
        'size': -1})
    dropbox = dropbox_container.add_dropbox(drop_id, message=u'Überraschung!', attachments=[attachment])
    assert stat.S_IMODE(os.stat(dropbox.paths_created[-2]).st_mode) == 0770
    assert dropbox.paths_created[-2].endswith("/attach")
    assert stat.S_IMODE(os.stat(dropbox.paths_created[-1]).st_mode) == 0660
    # we strip the original filename
    assert not dropbox.paths_created[-1].endswith("/attach/attachment.txt")
    # but preserve the file ending
    assert dropbox.paths_created[-1].endswith(".txt")
    assert open(dropbox.paths_created[-1]).read().decode('utf-8') == u'Schönen Guten Tag!'  # contents of attachment.txt

def test_attachment_creation_outside_container(dropbox_container, drop_id, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('attachment.txt'), 'r'),
        'mimetype': 'text/plain',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'../../authorized_keys',
        'size': -1})
    dropbox_container.add_dropbox(drop_id, message=u'Überraschung!', attachments=[attachment])
    assert not exists(join(dropbox_container.fs_path, 'authorized_keys'))

md5 = hashlib.md5()

def md5sum(f):
    for chunk in iter(lambda: f.read(8192), b''):
        md5.update(chunk)
    return md5.digest()

def test_attachment_is_image(dropbox_container, drop_id, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('attachment.png'), 'rb'),
        'mimetype': 'image/jpeg',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'attachment.png',
        'size': -1})
    dropbox = dropbox_container.add_dropbox(drop_id, message=u'Mit Foto', attachments=[attachment])
    # we strip the original filename
    assert not dropbox.paths_created[-1].endswith("/attach/attachment.png")
    # but preserve the file ending
    assert dropbox.paths_created[-1].endswith(".png")
    assert md5sum(open(dropbox.paths_created[-1], 'rb')) == md5sum(attachment.file)

def test_attachment_is_unicode(dropbox_container, drop_id, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('unicode.txt'), 'r'),
        'mimetype': 'text/plain',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'unicode.txt',
        'size': -1})
    # we only assert that this works
    dropbox_container.add_dropbox(drop_id, message=u'Überraschung!', attachments=[attachment])

def test_non_existent_drop_id_creates_dropbox_ad_hoc(dropbox_container):
    assert 'foobar' not in dropbox_container
    newbox = dropbox_container.get_dropbox('foobar')
    assert newbox.drop_id == 'foobar'
    assert 'foobar' in dropbox_container

def test_add_one_reply(dropbox_container, dropbox):
    reply = dict(
        message=u'Smørebrød',
        author=u'Børk')
    dropbox.add_reply(reply)
    assert dropbox.replies[0]['message'] == u'Smørebrød'
    assert dropbox.replies[0]['author'] == u'Børk'
    refetched_dropbox = dropbox_container.get_dropbox(dropbox.drop_id)
    assert refetched_dropbox.replies[0]['message'] == u'Smørebrød'
    assert refetched_dropbox.replies[0]['author'] == u'Børk'

def test_add_two_replies(app):
    NotImplemented

def test_access_replies(app):
    NotImplemented

def test_dropid_length():
    from briefkasten.dropbox import generate_drop_id
    assert len(generate_drop_id(12)) == 12

def test_filename_sanitizing():
    from briefkasten.dropbox import sanitize_filename
    assert not sanitize_filename('attachment.txt') == 'attachment.txt'
    assert sanitize_filename('attachment.txt').endswith('.txt')

def test_filename_has_no_ending():
    from briefkasten.dropbox import sanitize_filename
    assert not sanitize_filename('blubber') == 'blubber'

def test_drop_url(dropbox, app, testing):
    assert dropbox.drop_url == testing.route_url('dropbox_view', drop_id=dropbox.drop_id)

def test_editor_url(dropbox, app, testing):
    assert dropbox.editor_url == testing.route_url(
        'dropbox_editor',
        drop_id=dropbox.drop_id,
        editor_token=dropbox.editor_token)

def test_notification_text_message(dropbox_without_attachment):
    message = u'Sätz mit Ümlœuten'
    dropbox_without_attachment.message = message
    assert message in dropbox_without_attachment._notification_text

def test_notification_text_zero(dropbox_without_attachment):
    assert u'Die Einreichung enthielt keine Anhänge.' in dropbox_without_attachment._notification_text

def test_notification_text_one(dropbox):
    assert u'Die Einreichung enthielt einen Anhang' in dropbox._notification_text

def test_notification_text_unsupported(dropbox):
    dropbox.status = '800 unsupported file type'
    assert u'Mindestens ein Anhang wurde nicht bereinigt, da dessen Dateityp nicht unterstützt wird.' in dropbox._notification_text

def test_notification_text_two(dropbox, testing):
    attachment = testing.attachment_factory(**{
        'file': open(testing.asset_path('unicode.txt'), 'r'),
        'mimetype': 'text/plain',
        'uid': 'foobar',
        'preview_url': None,
        'filename': u'unicode.txt',
        'size': -1})
    dropbox.add_attachment(attachment)
    assert u'Die Einreichung enthielt 2 Anhänge.' in dropbox._notification_text

def test_dirty_archive_url(dropbox, app, testing):
    assert dropbox.dirty_archive_url is not None

def test_dirty_archive_url_without_format(dropbox, app, testing):
    del dropbox.settings['dropbox_dirty_archive_url_format']
    assert dropbox.dirty_archive_url is None
<|endoftext|>


<commit_before># Copyright (c) 2013, Sascha Peilicke <saschpe@gmx.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

import unittest

from rapport.setup import parse_requirements

class SetupTestCase(unittest.TestCase):
    def setUp(self):
        self.install_requires_file = "fixtures/requirements.txt"
        self.test_requires_file = "fixtures/requirements.test.txt"

        self.install_requires = ["argparse", "launchpadlib", "lxml", "Jinja2",
                                 "paramiko", "requests"]
        self.test_requires = ["nose"]

    def test_parse_requirements(self):
        install_requires = parse_requirements("requirements.txt")
        install_requires_default = parse_requirements()

        self.assertEqual(install_requires, install_requires_default)
        self.assertEqual(install_requires, self.install_requires)

        test_requires = parse_requirements("requirements.test.txt")

        self.assertEqual(test_requires, self.test_requires)
<commit_msg>Correcly load fixtures in test/test_setup.py<commit_after># Copyright (c) 2013, Sascha Peilicke <saschpe@gmx.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

import unittest

from rapport.setup import parse_requirements

class SetupTestCase(unittest.TestCase):
    def setUp(self):
        self.install_requires_file = "test/fixtures/setup/install-requires.txt"
        self.test_requires_file = "test/fixtures/setup/test-requires.txt"

        self.install_requires = ["argparse", "launchpadlib", "lxml", "Jinja2", "paramiko", "requests"]
        self.test_requires = ["nose"]

    def test_parse_requirements(self):
        install_requires = parse_requirements(self.install_requires_file)
        test_requires = parse_requirements(self.test_requires_file)

        self.assertEqual(install_requires, self.install_requires)
        self.assertEqual(test_requires, self.test_requires)
<|endoftext|>


<commit_before>from __future__ import print_function
import jps
import json
import time
import signal
import os
from multiprocessing import Process
from threading import Thread
try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

class MessageHolder(object):

    def __init__(self):
        self._saved_msg = []

    def __call__(self, msg):
        self._saved_msg.append(msg)

    def get_msg(self):
        return self._saved_msg

def test_pubecho():
    echo_output = StringIO()
    echo_thread = Thread(target=jps.tools.echo,
                         args=('/test1', 1, echo_output))
    echo_thread.daemon = True
    echo_thread.start()
    time.sleep(0.01)
    jps.tools.pub('/test1', '{"json_msg1": 1.0}')
    echo_thread.join(5.0)
    assert echo_output.getvalue() == '{"json_msg1": 1.0}\n'
    echo_output.close()

def test_pubecho_repeat():
    echo_output = StringIO()
    echo_thread = Thread(target=jps.tools.echo,
                         args=('/test2', 2, echo_output))
    echo_thread.daemon = True
    echo_thread.start()
    time.sleep(0.01)
    pub_process = Process(target=jps.tools.pub, args=('/test2', 'a', 1.0))
    pub_process.start()
    time.sleep(1.5)
    os.kill(pub_process.pid, signal.SIGINT)
    pub_process.join(1.0)
    echo_thread.join(5.0)
    assert echo_output.getvalue() == 'a\na\n'
    echo_output.close()

def test_show_list():
    list_output = StringIO()
    show_thread = Thread(target=jps.tools.show_list, args=(0.5, list_output))
    show_thread.daemon = True
    show_thread.start()
    time.sleep(0.1)
    p1 = jps.Publisher('/test_topic1')
    p2 = jps.Publisher('/test_topic2')
    time.sleep(0.1)
    p1.publish('{a}')
    p2.publish('{b}')

    show_thread.join(2.0)
    assert list_output.getvalue() == '/test_topic1\n/test_topic2\n'
    list_output.close()

def atest_show_list_with_suffix():
    orig_suffix = jps.env.get_topic_suffix()
    os.environ['JPS_SUFFIX'] = '.r123'
    list_output = StringIO()
    show_thread = Thread(target=jps.tools.show_list, args=(0.5, list_output))
    show_thread.daemon = True
    show_thread.start()
    time.sleep(0.1)
    p1 = jps.Publisher('/test_topic1')
    p2 = jps.Publisher('/test_topic2')
    time.sleep(0.1)
    p1.publish('{a}')
    p2.publish('{b}')

    show_thread.join(2.0)
    assert list_output.getvalue() == '/test_topic1.r123\n/test_topic2.r123\n'
    list_output.close()
    os.environ['JPS_SUFFIX'] = orig_suffix

def test_recordplay():
    import tempfile
    import os
    file_path_all = '{0}/{1}{2}'.format(
        tempfile.gettempdir(), os.getpid(), 'record_all.json')
    file_path = '{0}/{1}{2}'.format(
        tempfile.gettempdir(), os.getpid(), 'record2.json')
    print(file_path_all)
    print(file_path)
    record_all = Process(target=jps.tools.record, args=(file_path_all, []))
    record_all.daemon = True
    record_all.start()
    record = Process(target=jps.tools.record, args=(file_path, ['/test_rec2']))
    record.daemon = True
    record.start()

    time.sleep(0.5)

    p1 = jps.Publisher('/test_rec1')
    p2 = jps.Publisher('/test_rec2')
    time.sleep(0.1)
    p1.publish('a')
    p2.publish('b')
    time.sleep(0.1)

    os.kill(record_all.pid, signal.SIGINT)
    os.kill(record.pid, signal.SIGINT)
    record_all.join(1.0)
    record.join(1.0)

    assert os.path.exists(file_path_all)
    assert os.path.exists(file_path)

    def print_file_and_check_json(path):
        with open(path) as f:
            data = f.read()
            print(data)
            json.loads(data)

    print_file_and_check_json(file_path_all)
    print_file_and_check_json(file_path)
    holder1 = MessageHolder()
    sub1 = jps.Subscriber('/test_rec1', holder1)
    sub2 = jps.Subscriber('/test_rec2')
    time.sleep(0.1)
    play_all = Process(target=jps.tools.play, args=[file_path_all])
    play_all.daemon = True
    play_all.start()
    time.sleep(0.1)
    play_all.join(2.0)

    assert sub1.next() == 'a'
    assert sub2.next() == 'b'

    play = Process(target=jps.tools.play, args=[file_path])
    play.daemon = True
    play.start()
    time.sleep(0.1)
    play.join(2.0)
    sub1.spin_once()

    assert holder1.get_msg() == []
    assert sub2.next() == 'b'

    os.remove(file_path_all)
    os.remove(file_path)
<commit_msg>Fix test for suffix from environment variable<commit_after>from __future__ import print_function
import jps
import json
import time
import signal
import os
from multiprocessing import Process
from threading import Thread
try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

class MessageHolder(object):

    def __init__(self):
        self._saved_msg = []

    def __call__(self, msg):
        self._saved_msg.append(msg)

    def get_msg(self):
        return self._saved_msg

def test_pubecho():
    echo_output = StringIO()
    echo_thread = Thread(target=jps.tools.echo,
                         args=('/test1', 1, echo_output))
    echo_thread.daemon = True
    echo_thread.start()
    time.sleep(0.01)
    jps.tools.pub('/test1', '{"json_msg1": 1.0}')
    echo_thread.join(5.0)
    assert echo_output.getvalue() == '{"json_msg1": 1.0}\n'
    echo_output.close()

def test_pubecho_repeat():
    echo_output = StringIO()
    echo_thread = Thread(target=jps.tools.echo,
                         args=('/test2', 2, echo_output))
    echo_thread.daemon = True
    echo_thread.start()
    time.sleep(0.01)
    pub_process = Process(target=jps.tools.pub, args=('/test2', 'a', 1.0))
    pub_process.start()
    time.sleep(1.5)
    os.kill(pub_process.pid, signal.SIGINT)
    pub_process.join(1.0)
    echo_thread.join(5.0)
    assert echo_output.getvalue() == 'a\na\n'
    echo_output.close()

def test_show_list():
    list_output = StringIO()
    show_thread = Thread(target=jps.tools.show_list, args=(0.5, list_output))
    show_thread.daemon = True
    show_thread.start()
    time.sleep(0.1)
    p1 = jps.Publisher('/test_topic1')
    p2 = jps.Publisher('/test_topic2')
    time.sleep(0.1)
    p1.publish('{a}')
    p2.publish('{b}')

    show_thread.join(2.0)
    assert list_output.getvalue() == '/test_topic1\n/test_topic2\n'
    list_output.close()

def test_show_list_with_suffix():
    list_output = StringIO()
    show_thread = Thread(target=jps.tools.show_list, args=(0.5, list_output))
    show_thread.daemon = True
    show_thread.start()
    time.sleep(0.1)
    orig_suffix = jps.env.get_topic_suffix()
    os.environ['JPS_SUFFIX'] = '.r123'
    p1 = jps.Publisher('/test_topic1')
    p2 = jps.Publisher('/test_topic2')
    time.sleep(0.1)
    p1.publish('{a}')
    p2.publish('{b}')

    show_thread.join(2.0)
    assert list_output.getvalue() == '/test_topic1.r123\n/test_topic2.r123\n'
    list_output.close()
    os.environ['JPS_SUFFIX'] = orig_suffix

def test_recordplay():
    import tempfile
    import os
    file_path_all = '{0}/{1}{2}'.format(
        tempfile.gettempdir(), os.getpid(), 'record_all.json')
    file_path = '{0}/{1}{2}'.format(
        tempfile.gettempdir(), os.getpid(), 'record2.json')
    print(file_path_all)
    print(file_path)
    record_all = Process(target=jps.tools.record, args=(file_path_all, []))
    record_all.daemon = True
    record_all.start()
    record = Process(target=jps.tools.record, args=(file_path, ['/test_rec2']))
    record.daemon = True
    record.start()

    time.sleep(0.5)

    p1 = jps.Publisher('/test_rec1')
    p2 = jps.Publisher('/test_rec2')
    time.sleep(0.1)
    p1.publish('a')
    p2.publish('b')
    time.sleep(0.1)

    os.kill(record_all.pid, signal.SIGINT)
    os.kill(record.pid, signal.SIGINT)
    record_all.join(1.0)
    record.join(1.0)

    assert os.path.exists(file_path_all)
    assert os.path.exists(file_path)

    def print_file_and_check_json(path):
        with open(path) as f:
            data = f.read()
            print(data)
            json.loads(data)

    print_file_and_check_json(file_path_all)
    print_file_and_check_json(file_path)
    holder1 = MessageHolder()
    sub1 = jps.Subscriber('/test_rec1', holder1)
    sub2 = jps.Subscriber('/test_rec2')
    time.sleep(0.1)
    play_all = Process(target=jps.tools.play, args=[file_path_all])
    play_all.daemon = True
    play_all.start()
    time.sleep(0.1)
    play_all.join(2.0)

    assert sub1.next() == 'a'
    assert sub2.next() == 'b'

    play = Process(target=jps.tools.play, args=[file_path])
    play.daemon = True
    play.start()
    time.sleep(0.1)
    play.join(2.0)
    sub1.spin_once()

    assert holder1.get_msg() == []
    assert sub2.next() == 'b'

    os.remove(file_path_all)
    os.remove(file_path)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import ibei
import numpy as np
import pytest
import unittest

from astropy import units

temp_sun = 5762.
temp_earth = 288.
bandgap = 1.15

class Issues(unittest.TestCase):
    """
    Tests output types of the calculator methods.
    """
    def test_issue_2_uibei(self):
        """
        Refactor of issue 2 focusing on uibei
        """
        try:
            ibei.uibei(2, bandgap, temp_sun, 1.2)
        except:
            self.fail("Error raised with arguments.")

    def test_issue_4(self):
        """
        uibei shouldn't fail when energy_lo == chem_potential
        """
        try:
            ibei.uibei(2, 1., 300., 1.)
        except:
            self.fail("uibei fails when energy_lo == chem_potential")

    def test_issue_31(self):
        """
        Passing `energy_lo=0` with `chem_potential=0` should yield nonzero result
        """
        energy_flux = ibei.uibei(3, 0., 300., 0.)
        self.assertGreater(energy_flux, 0)

class CalculatorsArgsWrongType(unittest.TestCase):
    """
    Tests calling with args of invalid type.
    """
    def test_uibei_order_nonint(self):
        """
        uibei should raise TypeError for non-int order.
        """
        order = "not even numeric"
        self.assertRaises(TypeError, ibei.uibei, [order, bandgap, temp_sun, 0.])

    def test_uibei_order_float(self):
        """
        uibei should raise TypeError for order of type float.
        """
        order = 2.3
        self.assertRaises(TypeError, ibei.uibei, [order, bandgap, temp_sun, 0.])

    def test_uibei_energy_lo_nonnumeric(self):
        """
        uibei should raise TypeError for non-numeric energy_lo.
        """
        energy_lo = "non numeric"
        self.assertRaises(TypeError, ibei.uibei, [2, energy_lo, temp_sun, 0.])

    def test_uibei_temp_nonnumeric(self):
        """
        uibei should raise TypeError for non-numeric temp.
        """
        temp = "non numeric"
        self.assertRaises(TypeError, ibei.uibei, [2, bandgap, temp, 0.])

    def test_uibei_chem_potential_nonnumeric(self):
        """
        uibei should raise TypeError for non-numeric chem_potential.
        """
        cp = "non numeric"
        self.assertRaises(TypeError, ibei.uibei, [2, bandgap, temp_sun, cp])

class CalculatorsArgsWrongUnits(unittest.TestCase):
    """
    Tests calling with args with incorrect units.
    """
    def test_uibei_energy_lo(self):
        """
        uibei should raise UnitsError for energy_lo with units not energy.
        """
        energy_lo = units.Quantity(1.)
        self.assertRaises(units.UnitsError, ibei.uibei, 2, energy_lo, temp_sun, 0.)

    def test_uibei_temp(self):
        """
        uibei should raise UnitsError for temp with units not temperature.
        """
        temp = units.Quantity(1.)
        self.assertRaises(units.UnitsError, ibei.uibei, 2, bandgap, temp, 0.)

    def test_uibei_chem_potential(self):
        """
        uibei should raise UnitsError for chem_potential with units not energy.
        """
        cp = units.Quantity(1.)
        self.assertRaises(units.UnitsError, ibei.uibei, 2, bandgap, temp_sun, cp)

class TestArgsOutsideConstraints():
    """
    Tests calling with args are outside constraints.
    """
    @pytest.mark.parametrize("argname", [
            "energy_lo",
            "temp",
            "chem_potential",
            ]
        )
    def test_arg_lt_0(self, valid_quantity_args, argname):
        invalid_args = valid_quantity_args.copy()
        invalid_args[argname] *= -1

        assert invalid_args[argname] < 0

        with pytest.raises(ValueError):
            val = ibei.uibei(**invalid_args)

@pytest.fixture
def valid_quantity_args():
    """
    Valid arguments for `ibei.uibei` function
    """
    args = {
        "order": 2,
        "energy_lo": units.Quantity(1.15, units.eV),
        "temp": units.Quantity(5762., units.K),
        "chem_potential": units.Quantity(0.5, units.eV),
    }

    return args
<commit_msg>Refactor class to function<commit_after># -*- coding: utf-8 -*-
import ibei
import numpy as np
import pytest
import unittest

from astropy import units

temp_sun = 5762.
temp_earth = 288.
bandgap = 1.15

class Issues(unittest.TestCase):
    """
    Tests output types of the calculator methods.
    """
    def test_issue_2_uibei(self):
        """
        Refactor of issue 2 focusing on uibei
        """
        try:
            ibei.uibei(2, bandgap, temp_sun, 1.2)
        except:
            self.fail("Error raised with arguments.")

    def test_issue_4(self):
        """
        uibei shouldn't fail when energy_lo == chem_potential
        """
        try:
            ibei.uibei(2, 1., 300., 1.)
        except:
            self.fail("uibei fails when energy_lo == chem_potential")

    def test_issue_31(self):
        """
        Passing `energy_lo=0` with `chem_potential=0` should yield nonzero result
        """
        energy_flux = ibei.uibei(3, 0., 300., 0.)
        self.assertGreater(energy_flux, 0)

class CalculatorsArgsWrongType(unittest.TestCase):
    """
    Tests calling with args of invalid type.
    """
    def test_uibei_order_nonint(self):
        """
        uibei should raise TypeError for non-int order.
        """
        order = "not even numeric"
        self.assertRaises(TypeError, ibei.uibei, [order, bandgap, temp_sun, 0.])

    def test_uibei_order_float(self):
        """
        uibei should raise TypeError for order of type float.
        """
        order = 2.3
        self.assertRaises(TypeError, ibei.uibei, [order, bandgap, temp_sun, 0.])

    def test_uibei_energy_lo_nonnumeric(self):
        """
        uibei should raise TypeError for non-numeric energy_lo.
        """
        energy_lo = "non numeric"
        self.assertRaises(TypeError, ibei.uibei, [2, energy_lo, temp_sun, 0.])

    def test_uibei_temp_nonnumeric(self):
        """
        uibei should raise TypeError for non-numeric temp.
        """
        temp = "non numeric"
        self.assertRaises(TypeError, ibei.uibei, [2, bandgap, temp, 0.])

    def test_uibei_chem_potential_nonnumeric(self):
        """
        uibei should raise TypeError for non-numeric chem_potential.
        """
        cp = "non numeric"
        self.assertRaises(TypeError, ibei.uibei, [2, bandgap, temp_sun, cp])

class CalculatorsArgsWrongUnits(unittest.TestCase):
    """
    Tests calling with args with incorrect units.
    """
    def test_uibei_energy_lo(self):
        """
        uibei should raise UnitsError for energy_lo with units not energy.
        """
        energy_lo = units.Quantity(1.)
        self.assertRaises(units.UnitsError, ibei.uibei, 2, energy_lo, temp_sun, 0.)

    def test_uibei_temp(self):
        """
        uibei should raise UnitsError for temp with units not temperature.
        """
        temp = units.Quantity(1.)
        self.assertRaises(units.UnitsError, ibei.uibei, 2, bandgap, temp, 0.)

    def test_uibei_chem_potential(self):
        """
        uibei should raise UnitsError for chem_potential with units not energy.
        """
        cp = units.Quantity(1.)
        self.assertRaises(units.UnitsError, ibei.uibei, 2, bandgap, temp_sun, cp)

@pytest.mark.parametrize("argname", [
        "energy_lo",
        "temp",
        "chem_potential",
        ]
    )
def test_arg_lt_0(valid_quantity_args, argname):
    """
    Tests calling with args are outside constraints.
    """
    invalid_args = valid_quantity_args.copy()
    invalid_args[argname] *= -1

    assert invalid_args[argname] < 0

    with pytest.raises(ValueError):
        val = ibei.uibei(**invalid_args)

@pytest.fixture
def valid_quantity_args():
    """
    Valid arguments for `ibei.uibei` function
    """
    args = {
        "order": 2,
        "energy_lo": units.Quantity(1.15, units.eV),
        "temp": units.Quantity(5762., units.K),
        "chem_potential": units.Quantity(0.5, units.eV),
    }

    return args
<|endoftext|>


<commit_before>import unittest
from paas_manager.app import users

class TestUsers(unittest.TestCase):
    def setUp(self):
        users.register_user('test@test', 'test')

    def tearDown(self):
        users.delete_user('test@test')

    def test_is_registered(self):
        self.assertTrue((users.is_registered('test@test')))

    def test_verify_success(self):
        self.assertTrue(users.verify_password('test@test', 'test'))

    def test_verify_failed(self):
        self.assertFalse(users.verify_password('test@test', 'pass'))

    def test_user_id(self):
        id = users.user_id('test@test')

if __name__ == '__main__':
    unittest.main()
<commit_msg>Fix tests<commit_after>import unittest
from paas_manager.app.users import Users

class TestUsers(unittest.TestCase):
    users = Users()

    def setUp(self):
        self.users.register_user('test@test', 'test')

    def tearDown(self):
        self.users.delete_user('test@test')

    def test_is_registered(self):
        self.assertTrue((self.users.is_registered('test@test')))

    def test_verify_success(self):
        self.assertTrue(self.users.verify_password('test@test', 'test'))

    def test_verify_failed(self):
        self.assertFalse(self.users.verify_password('test@test', 'pass'))

    def test_user_id(self):
        id = self.users.user_id('test@test')

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>"""Unit tests for cartoframes.utils"""
import unittest
from cartoframes.utils import dict_items, cssify, norm_colname
from collections import OrderedDict

class TestUtils(unittest.TestCase):
    """Tests for functions in utils module"""
    def setUp(self):
        self.point_style = {
            "#layer['mapnik::geometry_type'=1]": OrderedDict([
                                              ('marker-width', "6"),
                                              ('marker-fill', "yellow"),
                                              ('marker-fill-opacity', "1"),
                                              ('marker-allow-overlap', "true"),
                                              ('marker-line-width', "0.5"),
                                              ('marker-line-color', "black"),
                                              ("marker-line-opacity", "1")])
            }

        self.polygon_style = {
                "#layer['mapnik::geometry_type'=3]": OrderedDict([
                    ('polygon-fill', ('ramp([column], (#ffc6c4, #ee919b, '
                                      '#cc607d, #9e3963, #672044), '
                                      'quantiles)')),
                    ('polygon-opacity', '0.9'),
                    ('polygon-gamma', '0.5'),
                    ('line-color', '#FFF'),
                    ('line-width', '0.5'),
                    ('line-opacity', '0.25'),
                    ('line-comp-op', 'hard-light')])}

        self.complex_style = OrderedDict([
            ("#layer['mapnik::geometry_type'=1]", OrderedDict([
                                              ('marker-width', "5"),
                                              ('marker-fill', "yellow"),
                                              ('marker-fill-opacity', '1'),
                                              ('marker-allow-overlap', 'true'),
                                              ('marker-line-width', '0.5'),
                                              ('marker-line-color', "black"),
                                              ('marker-line-opacity', '1')])),
            ("#layer['mapnik::geometry_type'=2]", OrderedDict([
                ('line-width', '1.5'),
                ('line-color', "black")])),
            ("#layer['mapnik::geometry_type'=3]", OrderedDict([
                ('polygon-fill', "blue"),
                ('polygon-opacity', '0.9'),
                ('polygon-gamma', '0.5'),
                ('line-color', '#FFF'),
                ('line-width', '0.5'),
                ('line-opacity', '0.25'),
                ('line-comp-op', 'hard-light')]))
        ])

    def test_dict_items(self):
        """utils.dict_items"""
        # ensure correct formation of dict items from provided styling
        polygon_style_dict = dict_items(self.polygon_style)
        self.assertDictEqual(OrderedDict(polygon_style_dict),
                             self.polygon_style,
                             msg="pollygon styling")
        # point style
        point_style_dict = dict_items(self.point_style)
        self.assertDictEqual(OrderedDict(point_style_dict),
                             self.point_style,
                             msg="point styling")
        # multi layer styling
        complex_style_dict = dict_items(self.complex_style)
        self.assertDictEqual(OrderedDict(complex_style_dict),
                             self.complex_style,
                             msg="multi-layer styling")

    def test_cssify(self):
        """utils.cssify"""
        # point style
        point_stylecss = cssify(self.point_style)
        self.assertEqual(point_stylecss,
                         ("#layer['mapnik::geometry_type'=1] {  "
                          "marker-width: 6; marker-fill: yellow; "
                          "marker-fill-opacity: 1; marker-allow-overlap: "
                          "true; marker-line-width: 0.5; marker-line-color: "
                          "black; marker-line-opacity: 1;} "),
                         msg="point style")

        # polygon style
        polygon_stylecss = cssify(self.polygon_style)
        self.assertEqual(polygon_stylecss,
                         ("#layer['mapnik::geometry_type'=3] {  "
                          "polygon-fill: ramp([column], (#ffc6c4, #ee919b, "
                          "#cc607d, #9e3963, #672044), quantiles); "
                          "polygon-opacity: 0.9; polygon-gamma: 0.5; "
                          "line-color: #FFF; line-width: 0.5; line-opacity: "
                          "0.25; line-comp-op: hard-light;} "),
                         msg="polygon style")

        # complex style
        complex_stylecss = cssify(self.complex_style)
        self.assertEqual(complex_stylecss,
                         ("#layer['mapnik::geometry_type'=1] {  "
                          "marker-width: 5; marker-fill: yellow; "
                          "marker-fill-opacity: 1; marker-allow-overlap: "
                          "true; marker-line-width: 0.5; marker-line-color: "
                          "black; marker-line-opacity: 1;} "
                          "#layer['mapnik::geometry_type'=2] {  "
                          "line-width: 1.5; line-color: black;} "
                          "#layer['mapnik::geometry_type'=3] {  "
                          "polygon-fill: blue; polygon-opacity: 0.9; "
                          "polygon-gamma: 0.5; line-color: #FFF; line-width: "
                          "0.5; line-opacity: 0.25; "
                          "line-comp-op: hard-light;} "),
                         msg="multi-layer styling")

    def test_norm_colname(self):
        """utils.norm_colname"""
        cols = ['Unnamed: 0', '201moore', 'Acadia 1.2.3', 'old_soaker',
                '_testingTesting', ]
        ans = ['unnamed_0', '_201moore', 'acadia_1_2_3', 'old_soaker',
               '_testingtesting', ]
        for c, a in zip(cols, ans):
            # changed cols should match answers
            self.assertEqual(norm_colname(c), a)
            # already sql-normed cols should match themselves
            self.assertEqual(norm_colname(a), a)
<commit_msg>adds test for normalize_colnames<commit_after>"""Unit tests for cartoframes.utils"""
import unittest
from cartoframes.utils import (dict_items, cssify, norm_colname,
                               normalize_colnames)
from collections import OrderedDict

class TestUtils(unittest.TestCase):
    """Tests for functions in utils module"""
    def setUp(self):
        self.point_style = {
            "#layer['mapnik::geometry_type'=1]": OrderedDict([
                                              ('marker-width', "6"),
                                              ('marker-fill', "yellow"),
                                              ('marker-fill-opacity', "1"),
                                              ('marker-allow-overlap', "true"),
                                              ('marker-line-width', "0.5"),
                                              ('marker-line-color', "black"),
                                              ("marker-line-opacity", "1")])
            }

        self.polygon_style = {
                "#layer['mapnik::geometry_type'=3]": OrderedDict([
                    ('polygon-fill', ('ramp([column], (#ffc6c4, #ee919b, '
                                      '#cc607d, #9e3963, #672044), '
                                      'quantiles)')),
                    ('polygon-opacity', '0.9'),
                    ('polygon-gamma', '0.5'),
                    ('line-color', '#FFF'),
                    ('line-width', '0.5'),
                    ('line-opacity', '0.25'),
                    ('line-comp-op', 'hard-light')])}

        self.complex_style = OrderedDict([
            ("#layer['mapnik::geometry_type'=1]", OrderedDict([
                                              ('marker-width', "5"),
                                              ('marker-fill', "yellow"),
                                              ('marker-fill-opacity', '1'),
                                              ('marker-allow-overlap', 'true'),
                                              ('marker-line-width', '0.5'),
                                              ('marker-line-color', "black"),
                                              ('marker-line-opacity', '1')])),
            ("#layer['mapnik::geometry_type'=2]", OrderedDict([
                ('line-width', '1.5'),
                ('line-color', "black")])),
            ("#layer['mapnik::geometry_type'=3]", OrderedDict([
                ('polygon-fill', "blue"),
                ('polygon-opacity', '0.9'),
                ('polygon-gamma', '0.5'),
                ('line-color', '#FFF'),
                ('line-width', '0.5'),
                ('line-opacity', '0.25'),
                ('line-comp-op', 'hard-light')]))
        ])

        self.cols = ['Unnamed: 0', '201moore', 'Acadia 1.2.3',
                     'old_soaker', '_testingTesting', ]
        self.cols_ans = ['unnamed_0', '_201moore', 'acadia_1_2_3',
                         'old_soaker', '_testingtesting', ]

    def test_dict_items(self):
        """utils.dict_items"""
        # ensure correct formation of dict items from provided styling
        polygon_style_dict = dict_items(self.polygon_style)
        self.assertDictEqual(OrderedDict(polygon_style_dict),
                             self.polygon_style,
                             msg="pollygon styling")
        # point style
        point_style_dict = dict_items(self.point_style)
        self.assertDictEqual(OrderedDict(point_style_dict),
                             self.point_style,
                             msg="point styling")
        # multi layer styling
        complex_style_dict = dict_items(self.complex_style)
        self.assertDictEqual(OrderedDict(complex_style_dict),
                             self.complex_style,
                             msg="multi-layer styling")

    def test_cssify(self):
        """utils.cssify"""
        # point style
        point_stylecss = cssify(self.point_style)
        self.assertEqual(point_stylecss,
                         ("#layer['mapnik::geometry_type'=1] {  "
                          "marker-width: 6; marker-fill: yellow; "
                          "marker-fill-opacity: 1; marker-allow-overlap: "
                          "true; marker-line-width: 0.5; marker-line-color: "
                          "black; marker-line-opacity: 1;} "),
                         msg="point style")

        # polygon style
        polygon_stylecss = cssify(self.polygon_style)
        self.assertEqual(polygon_stylecss,
                         ("#layer['mapnik::geometry_type'=3] {  "
                          "polygon-fill: ramp([column], (#ffc6c4, #ee919b, "
                          "#cc607d, #9e3963, #672044), quantiles); "
                          "polygon-opacity: 0.9; polygon-gamma: 0.5; "
                          "line-color: #FFF; line-width: 0.5; line-opacity: "
                          "0.25; line-comp-op: hard-light;} "),
                         msg="polygon style")

        # complex style
        complex_stylecss = cssify(self.complex_style)
        self.assertEqual(complex_stylecss,
                         ("#layer['mapnik::geometry_type'=1] {  "
                          "marker-width: 5; marker-fill: yellow; "
                          "marker-fill-opacity: 1; marker-allow-overlap: "
                          "true; marker-line-width: 0.5; marker-line-color: "
                          "black; marker-line-opacity: 1;} "
                          "#layer['mapnik::geometry_type'=2] {  "
                          "line-width: 1.5; line-color: black;} "
                          "#layer['mapnik::geometry_type'=3] {  "
                          "polygon-fill: blue; polygon-opacity: 0.9; "
                          "polygon-gamma: 0.5; line-color: #FFF; line-width: "
                          "0.5; line-opacity: 0.25; "
                          "line-comp-op: hard-light;} "),
                         msg="multi-layer styling")

    def test_norm_colname(self):
        """utils.norm_colname"""
        for c, a in zip(self.cols, self.cols_ans):
            # changed cols should match answers
            self.assertEqual(norm_colname(c), a)
            # already sql-normed cols should match themselves
            self.assertEqual(norm_colname(a), a)

    def test_normalize_colnames(self):
        """utils.normalize_colnames"""
        self.assertListEqual(normalize_colnames(self.cols),
                             self.cols_ans,
                             msg='unnormalized should be SQL-normalized')
        self.assertListEqual(normalize_colnames(self.cols_ans),
                             self.cols_ans,
                             msg='already normalize columns should not change')
<|endoftext|>


<commit_before>import ast
import pkg_resources
import unittest
from rgkit import game
from rgkit.gamestate import GameState

map_data = ast.literal_eval(open(pkg_resources.resource_filename('rgkit', 'maps/default.py')).read())
settings = game.init_settings(map_data)

class TestValid(unittest.TestCase):
    def test_ok(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertTrue(state.is_valid_action((9, 9), ['guard']))
        self.assertTrue(state.is_valid_action((9, 9), ['suicide']))
        self.assertTrue(state.is_valid_action((9, 9), ['attack', (9, 10)]))
        self.assertTrue(state.is_valid_action((9, 9), ['move', (9, 10)]))

    def test_none(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), None))

    def test_strange(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), "ALL YOUR BASE ARE BELONG TO US"))

    def test_wrong_command(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['exterminate']))

    def test_str_too_long(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)

        class long_action(list):
            def __str__(self):
                return 'leethaxxor' * 100

        action = long_action(['guard'])
        self.assertFalse(state.is_valid_action((9, 9), action))

    def test_repr_too_long(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)

        class long_action(list):
            def __repr__(self):
                return 'leethaxxor' * 100

        action = long_action(['guard'])
        self.assertFalse(state.is_valid_action((9, 9), action))

    def test_move_to_self(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['move', (9, 9)]))

    def test_move_too_far(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['move', (9, 11)]))

    def test_move_to_obstacle(self):
        state = GameState(settings)
        state.add_robot((2, 5), 0)
        self.assertFalse(state.is_valid_action((2, 5), ['move', (2, 4)]))

    def test_attack_self(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['attack', (9, 9)]))

    def test_attack_too_far(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['attack', (9, 11)]))

    def test_attack_obstacle(self):
        state = GameState(settings)
        state.add_robot((2, 5), 0)
        self.assertFalse(state.is_valid_action((2, 5), ['attack', (2, 4)]))
<commit_msg>added positive test<commit_after>import ast
import pkg_resources
import unittest
from rgkit import game
from rgkit.gamestate import GameState

map_data = ast.literal_eval(open(pkg_resources.resource_filename('rgkit', 'maps/default.py')).read())
settings = game.init_settings(map_data)

class TestValid(unittest.TestCase):
    def test_ok(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertTrue(state.is_valid_action((9, 9), ['guard']))
        self.assertTrue(state.is_valid_action((9, 9), ['suicide']))
        self.assertTrue(state.is_valid_action((9, 9), ['attack', (9, 10)]))
        self.assertTrue(state.is_valid_action((9, 9), ['move', (9, 10)]))

    def test_none(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), None))

    def test_strange(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), "ALL YOUR BASE ARE BELONG TO US"))

    def test_wrong_command(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['exterminate']))

    def test_str_too_long(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)

        class long_action(list):
            def __str__(self):
                return 'leethaxxor' * 100

        action = long_action(['guard'])
        self.assertFalse(state.is_valid_action((9, 9), action))

    def test_repr_too_long(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)

        class long_action(list):
            def __repr__(self):
                return 'leethaxxor' * 100

        action = long_action(['guard'])
        self.assertFalse(state.is_valid_action((9, 9), action))

    def test_additional_info(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)

        self.assertTrue(state.is_valid_action((9, 9), ['guard', 'additional info']))

    def test_move_to_self(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['move', (9, 9)]))

    def test_move_too_far(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['move', (9, 11)]))

    def test_move_to_obstacle(self):
        state = GameState(settings)
        state.add_robot((2, 5), 0)
        self.assertFalse(state.is_valid_action((2, 5), ['move', (2, 4)]))

    def test_attack_self(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['attack', (9, 9)]))

    def test_attack_too_far(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        self.assertFalse(state.is_valid_action((9, 9), ['attack', (9, 11)]))

    def test_attack_obstacle(self):
        state = GameState(settings)
        state.add_robot((2, 5), 0)
        self.assertFalse(state.is_valid_action((2, 5), ['attack', (2, 4)]))
<|endoftext|>


<commit_before>"""Main module."""
import time as _time
from threading import Thread as _Thread, Event as _Event
import math

from copy import deepcopy as _dcopy
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as mpl_gs

from siriuspy.devices import PowerSupply, Tune

import pyaccel
from pymodels import si
from .base import BaseClass

class BetaParams:
    """."""

    SYM = 0
    UNI = 1

    def __init__(self):
        """."""
        self.nr_measures = 1
        self.quad_deltakl = 0.01  # [1/m]
        self.quad_nrcycles = 0
        self.wait_quadrupole = 2  # [s]
        self.wait_tune = 3  # [s]
        self.timeout_quad_turnon = 10  # [s]

    def __str__(self):
        """."""
        ftmp = '{0:24s} = {1:9.3f}  {2:s}\n'.format
        dtmp = '{0:24s} = {1:9d}  {2:s}\n'.format
        st = dtmp('nr_measures', self.nr_measures, '')
        st += ftmp('quad_deltakl [1/m]', self.quad_deltakl, '')
        st += ftmp('quad_nrcycles', self.quad_nrcycles, '')
        st += ftmp('wait_quadrupole [s]', self.wait_quadrupole, '')
        st += ftmp('wait_tune [s]', self.wait_tune, '')
        st += ftmp('timeout_quad_turnon [s]', self.timeout_quad_turnon, '')
        return st

class MeasBeta(BaseClass):
    """."""

    def __init__(self, model, famdata=None):
        """."""
        super().__init__()
        self.quads_betax = []
        self.quads_betay = []
        self.params = BetaParams()
        self.devices['tune'] = Tune(Tune.DEVICES.ALL)
        self.data['quadnames'] = list()
        self.data['betax_in'] = dict()
        self.data['betay_in'] = dict()
        self.data['betax_mid'] = dict()
        self.data['betay_mid'] = dict()
        self.data['betax_out'] = dict()
        self.data['betay_out'] = dict()
        self.data['measure'] = dict()
        self.analysis = dict()
        self._quads2meas = list()
        self._stopevt = _Event()
        self._thread = _Thread(target=self._meas_beta, daemon=True)
        self.model = model
        self.famdata = famdata or si.get_family_data(model)
        self._initialize_data()
        self._connect_to_objects()

    def start(self):
        """."""
        if self._thread.is_alive():
            return
        self._stopevt.clear()
        self._thread = _Thread(target=self._meas_beta, daemon=True)
        self._thread.start()

    def stop(self):
        """."""
        self._stopevt.set()

    @property
    def ismeasuring(self):
        return self._thread.is_alive()

    @property
    def measuredquads(self):
        return sorted(self.data['measure'])

    @property
    def quads2meas(self):
        """."""
        if self._quads2meas:
            return self._quads2meas
        return sorted(
            set(self.data['quadnames']) - self.data['measure'].keys())

    @quads2meas.setter
    def quads2meas(self, quadslist):
        """."""
        self._quads2meas = _dcopy(quadslist)

    def _connect_to_objects(self):
        """."""
        for qname in self.data['quadnames']:
            if qname not in self.devices:
                self.devices[qname] = PowerSupply(qname)

    def _initialize_data(self):
        """."""
        quadnames, quadsidx = MeasBeta.get_quads(self.model, self.famdata)
        twi, *_ = pyaccel.optics.calc_twiss(self.model, indices='open')
        for idx, qname in zip(quadsidx, quadnames):
            L = self.model[idx].length/2
            K = self.model[idx].K
            Kr = np.sqrt(abs(K))
            Cx = math.cos(Kr*L) if K > 0 else math.cosh(Kr*L)
            Sx = math.sin(Kr*L) if K > 0 else math.sinh(Kr*L)
            Sx = (Sx / Kr) if Kr > 0 else L
            Cy = math.cos(Kr*L) if K < 0 else math.cosh(Kr*L)
            Sy = math.sin(Kr*L) if K < 0 else math.sinh(Kr*L)
            Sy = (Sy / Kr) if Kr > 0 else L
            bxi = twi.betax[idx]
            byi = twi.betay[idx]
            axi = twi.alphax[idx]
            ayi = twi.alphay[idx]
            gxi = (1+axi*axi)/bxi
            gyi = (1+ayi*ayi)/byi
            bxm = Cx*Cx*bxi - 2*Cx*Sx*axi + Sx*Sx*gxi
            bym = Cy*Cy*byi - 2*Cy*Sy*ayi + Sy*Sy*gyi
            self.data['betax_in'][qname] = bxi
            self.data['betay_in'][qname] = byi
            self.data['betax_mid'][qname] = bxm
            self.data['betay_mid'][qname] = bym
            self.data['betax_out'][qname] = twi.betax[idx+1]
            self.data['betay_out'][qname] = twi.betay[idx+1]
        self.data['quadnames'] = quadnames

    def _meas_beta(self):
        """."""
        for quadname in self.quads2meas:
            if self._stopevt.is_set():
                return
            self._meas_beta_single_quad(quadname)
        print('finished!')

    @staticmethod
    def get_cycling_curve():
        return [-1/2, 1/2, -1/8, 0]

    def _meas_beta_single_quad(self, quadname):
        """."""
        quad = self.devices[quadname]
        tune = self.devices['tune']

        print('turning quadrupole ' + quadname + ' On', end='')
        quad.cmd_turn_on(self.params.timeout_quad_turnon)
        if not quad.pwrstate:
            print('\n    error: quadrupole ' + quadname + ' is Off.')
            self._stopevt.set()
            print('    exiting...')
            return

        deltakl = self.params.quad_deltakl
        korig = quad.strength
        cycling_curve = MeasBeta.get_cycling_curve()

        print(' and cycling it: ', end='')
        for _ in range(self.params.quad_nrcycles):
            print('.', end='')
            for fac in cycling_curve:
                quad.strength = korig + deltakl*fac
                _time.sleep(self.params.wait_quadrupole)
        print(' Ok!')

        tunex_ini, tunex_neg, tunex_pos = [], [], []
        tuney_ini, tuney_neg, tuney_pos = [], [], []
        tunex_wfm_ini, tunex_wfm_neg, tunex_wfm_pos = [], [], []
        tuney_wfm_ini, tuney_wfm_neg, tuney_wfm_pos = [], [], []

        for i in range(self.params.nr_measures):
            if self._stopevt.is_set():
                print('exiting...')
                break
            print('    {0:02d}/{1:02d} --> '.format(
                i+1, self.params.nr_measures), end='')

            tunex_ini.append(tune.tunex)
            tuney_ini.append(tune.tuney)
            tunex_wfm_ini.append(tune.tunex_wfm)
            tuney_wfm_ini.append(tune.tuney_wfm)
            for j, fac in enumerate(cycling_curve):
                quad.strength = korig + deltakl*fac
                _time.sleep(self.params.wait_quadrupole)
                if not j:
                    print(' -dk/2 ', end='')
                    _time.sleep(self.params.wait_tune)
                    tunex_neg.append(tune.tunex)
                    tuney_neg.append(tune.tuney)
                    tunex_wfm_neg.append(tune.tunex_wfm)
                    tuney_wfm_neg.append(tune.tuney_wfm)
                elif j == 1:
                    print(' +dk/2 ', end='')
                    _time.sleep(self.params.wait_tune)
                    tunex_pos.append(tune.tunex)
                    tuney_pos.append(tune.tuney)
                    tunex_wfm_pos.append(tune.tunex_wfm)
                    tuney_wfm_pos.append(tune.tuney_wfm)
            print('--> dnux = {:.5f}, dnuy = {:.5f}'.format(
               tunex_pos[-1] - tunex_neg[-1], tuney_pos[-1] - tuney_neg[-1]))

        meas = dict()
        meas['tunex_ini'] = np.array(tunex_ini)
        meas['tuney_ini'] = np.array(tuney_ini)
        meas['tunex_neg'] = np.array(tunex_neg)
        meas['tuney_neg'] = np.array(tuney_neg)
        meas['tunex_pos'] = np.array(tunex_pos)
        meas['tuney_pos'] = np.array(tuney_pos)
        meas['tunex_wfm_ini'] = np.array(tunex_wfm_ini)
        meas['tuney_wfm_ini'] = np.array(tuney_wfm_ini)
        meas['tunex_wfm_neg'] = np.array(tunex_wfm_neg)
        meas['tuney_wfm_neg'] = np.array(tuney_wfm_neg)
        meas['tunex_wfm_pos'] = np.array(tunex_wfm_pos)
        meas['tuney_wfm_pos'] = np.array(tuney_wfm_pos)
        meas['delta_kl'] = deltakl

        self.data['measure'][quadname] = meas

        print('turning quadrupole ' + quadname + ' Off \n')
        quad.cmd_turn_off(self.params.timeout_quad_turnon)
        if quad.pwrstate:
            print('    error: quadrupole ' + quadname + ' is still On.')
            self._stopevt.set()

    def process_data(self, mode='symm', discardpoints=None):
        """."""
        for quad in self.data['measure']:
            self.analysis[quad] = self.calc_beta(
                quad, mode=mode, discardpoints=discardpoints)

    def calc_beta(self, quadname, mode='symm', discardpoints=None):
        """."""
        anl = dict()
        datameas = self.data['measure'][quadname]

        if mode.lower().startswith('symm'):
            dnux = datameas['tunex_pos'] - datameas['tunex_neg']
            dnuy = datameas['tuney_pos'] - datameas['tuney_neg']
        elif mode.lower().startswith('pos'):
            dnux = datameas['tunex_pos'] - datameas['tunex_ini']
            dnuy = datameas['tuney_pos'] - datameas['tuney_ini']
        else:
            dnux = datameas['tunex_ini'] - datameas['tunex_neg']
            dnuy = datameas['tuney_ini'] - datameas['tuney_neg']

        usepts = set(range(dnux.shape[0]))
        if discardpoints is not None:
            usepts = set(usepts) - set(discardpoints)
        usepts = sorted(usepts)

        dkl = datameas['delta_kl']
        anl['betasx'] = +4 * np.pi * dnux[usepts] / dkl
        anl['betasy'] = -4 * np.pi * dnuy[usepts] / dkl
        anl['betax_ave'] = np.mean(anl['betasx'])
        anl['betay_ave'] = np.mean(anl['betasy'])
        return anl

    @staticmethod
    def get_quads(model, fam_data):
        """."""
        quads_idx = _dcopy(fam_data['QN']['index'])
        quads_idx = np.array([idx[len(idx)//2] for idx in quads_idx])

        qnames = list()
        for qidx in quads_idx:
            name = model[qidx].fam_name
            idc = fam_data[name]['index'].index([qidx, ])
            sub = fam_data[name]['subsection'][idc]
            inst = fam_data[name]['instance'][idc]
            qname = 'SI-{0:s}:PS-{1:s}-{2:s}'.format(sub, name, inst)
            qnames.append(qname.strip('-'))
        return qnames, quads_idx

    @staticmethod
    def get_quad_families(quads):
        fams = {quad[11:]: [] for quad in quads}
        for quad in quads:
            fams[quad[11:]].append(quad)
        return fams

    def plot_results(self, quads=None, title=''):

        f = plt.figure(figsize=(9, 7))
        gs = mpl_gs.GridSpec(ncols=1, nrows=2, figure=f)
        gs.update(
            left=0.1, right=0.8, bottom=0.15, top=0.9,
            hspace=0.0, wspace=0.35)

        ax1 = f.add_subplot(gs[0, 0])
        ax2 = f.add_subplot(gs[1, 0], sharex=ax1)

        if title:
            f.suptitle(title)

        quads = quads or self.data['quadnames']
        indcs, nom_bx, nom_by = [], [], []
        mes_bx, mes_by, bx, by = [], [], [], []
        for quad in quads:
            if quad not in self.analysis:
                continue
            indcs.append(self.data['quadnames'].index(quad))
            nom_bx.append(self.data['betax_mid'][quad])
            nom_by.append(self.data['betay_mid'][quad])
            mes_bx.append(self.analysis[quad]['betasx'])
            mes_by.append(self.analysis[quad]['betasy'])
            bx.append(self.analysis[quad]['betax_ave'])
            by.append(self.analysis[quad]['betay_ave'])

        ax1.plot(indcs, bx, '--bx')
        ax1.plot(indcs, nom_bx, '-bo')
        ax1.plot(indcs, mes_bx, '.b')
        ax2.plot(indcs, by, '--rx')
        ax2.plot(indcs, nom_by, '-ro')
        ax2.plot(indcs, mes_by, '.r')

        ax1.set_ylabel(r'$\beta_x$ [m]')
        ax2.set_ylabel(r'$\beta_y$ [m]')
        ax1.legend(
            ['measure', 'nominal'], loc='center left',
            bbox_to_anchor=(1, 0), fontsize='x-small')
        ax1.grid(True)
        ax2.grid(True)
        f.show()
<commit_msg>COMMIS.BETA.ENH: Disable RF from SOFB loop before beta measurement<commit_after>"""Main module."""
import time as _time
from threading import Thread as _Thread, Event as _Event
import math

from copy import deepcopy as _dcopy
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as mpl_gs

from siriuspy.devices import PowerSupply, Tune, SOFB

import pyaccel
from pymodels import si
from .base import BaseClass

class BetaParams:
    """."""

    SYM = 0
    UNI = 1

    def __init__(self):
        """."""
        self.nr_measures = 1
        self.quad_deltakl = 0.01  # [1/m]
        self.quad_nrcycles = 0
        self.wait_quadrupole = 2  # [s]
        self.wait_tune = 3  # [s]
        self.timeout_quad_turnon = 10  # [s]

    def __str__(self):
        """."""
        ftmp = '{0:24s} = {1:9.3f}  {2:s}\n'.format
        dtmp = '{0:24s} = {1:9d}  {2:s}\n'.format
        st = dtmp('nr_measures', self.nr_measures, '')
        st += ftmp('quad_deltakl [1/m]', self.quad_deltakl, '')
        st += ftmp('quad_nrcycles', self.quad_nrcycles, '')
        st += ftmp('wait_quadrupole [s]', self.wait_quadrupole, '')
        st += ftmp('wait_tune [s]', self.wait_tune, '')
        st += ftmp('timeout_quad_turnon [s]', self.timeout_quad_turnon, '')
        return st

class MeasBeta(BaseClass):
    """."""

    def __init__(self, model, famdata=None):
        """."""
        super().__init__()
        self.quads_betax = []
        self.quads_betay = []
        self.params = BetaParams()
        self.devices['tune'] = Tune(Tune.DEVICES.ALL)
        self.devices['sofb'] = SOFB(SOFB.DEVICES.SI)
        self.data['quadnames'] = list()
        self.data['betax_in'] = dict()
        self.data['betay_in'] = dict()
        self.data['betax_mid'] = dict()
        self.data['betay_mid'] = dict()
        self.data['betax_out'] = dict()
        self.data['betay_out'] = dict()
        self.data['measure'] = dict()
        self.analysis = dict()
        self._quads2meas = list()
        self._stopevt = _Event()
        self._thread = _Thread(target=self._meas_beta, daemon=True)
        self.model = model
        self.famdata = famdata or si.get_family_data(model)
        self._initialize_data()
        self._connect_to_objects()

    def start(self):
        """."""
        if self._thread.is_alive():
            return
        self._stopevt.clear()
        self._thread = _Thread(target=self._meas_beta, daemon=True)
        self._thread.start()

    def stop(self):
        """."""
        self._stopevt.set()

    @property
    def ismeasuring(self):
        return self._thread.is_alive()

    @property
    def measuredquads(self):
        return sorted(self.data['measure'])

    @property
    def quads2meas(self):
        """."""
        if self._quads2meas:
            return self._quads2meas
        return sorted(
            set(self.data['quadnames']) - self.data['measure'].keys())

    @quads2meas.setter
    def quads2meas(self, quadslist):
        """."""
        self._quads2meas = _dcopy(quadslist)

    def _connect_to_objects(self):
        """."""
        for qname in self.data['quadnames']:
            if qname not in self.devices:
                self.devices[qname] = PowerSupply(qname)

    def _initialize_data(self):
        """."""
        quadnames, quadsidx = MeasBeta.get_quads(self.model, self.famdata)
        twi, *_ = pyaccel.optics.calc_twiss(self.model, indices='open')
        for idx, qname in zip(quadsidx, quadnames):
            L = self.model[idx].length/2
            K = self.model[idx].K
            Kr = np.sqrt(abs(K))
            Cx = math.cos(Kr*L) if K > 0 else math.cosh(Kr*L)
            Sx = math.sin(Kr*L) if K > 0 else math.sinh(Kr*L)
            Sx = (Sx / Kr) if Kr > 0 else L
            Cy = math.cos(Kr*L) if K < 0 else math.cosh(Kr*L)
            Sy = math.sin(Kr*L) if K < 0 else math.sinh(Kr*L)
            Sy = (Sy / Kr) if Kr > 0 else L
            bxi = twi.betax[idx]
            byi = twi.betay[idx]
            axi = twi.alphax[idx]
            ayi = twi.alphay[idx]
            gxi = (1+axi*axi)/bxi
            gyi = (1+ayi*ayi)/byi
            bxm = Cx*Cx*bxi - 2*Cx*Sx*axi + Sx*Sx*gxi
            bym = Cy*Cy*byi - 2*Cy*Sy*ayi + Sy*Sy*gyi
            self.data['betax_in'][qname] = bxi
            self.data['betay_in'][qname] = byi
            self.data['betax_mid'][qname] = bxm
            self.data['betay_mid'][qname] = bym
            self.data['betax_out'][qname] = twi.betax[idx+1]
            self.data['betay_out'][qname] = twi.betay[idx+1]
        self.data['quadnames'] = quadnames

    def _meas_beta(self):
        """."""
        sofb = self.devices['sofb']
        loop_on_rf = False
        if sofb.autocorrsts and sofb.rfenbl:
            loop_on_rf = True
            print('RF is enable in SOFB feedback, disabling it...')
            sofb.rfenbl = 0

        for quadname in self.quads2meas:
            if self._stopevt.is_set():
                return
            self._meas_beta_single_quad(quadname)

        if loop_on_rf:
            print(
                'RF was enable in SOFB feedback, restoring original state...')
            sofb.rfenbl = 1
        print('finished!')

    @staticmethod
    def get_cycling_curve():
        return [-1/2, 1/2, -1/8, 0]

    def _meas_beta_single_quad(self, quadname):
        """."""
        quad = self.devices[quadname]
        tune = self.devices['tune']

        print('turning quadrupole ' + quadname + ' On', end='')
        quad.cmd_turn_on(self.params.timeout_quad_turnon)
        if not quad.pwrstate:
            print('\n    error: quadrupole ' + quadname + ' is Off.')
            self._stopevt.set()
            print('    exiting...')
            return

        deltakl = self.params.quad_deltakl
        korig = quad.strength
        cycling_curve = MeasBeta.get_cycling_curve()

        print(' and cycling it: ', end='')
        for _ in range(self.params.quad_nrcycles):
            print('.', end='')
            for fac in cycling_curve:
                quad.strength = korig + deltakl*fac
                _time.sleep(self.params.wait_quadrupole)
        print(' Ok!')

        tunex_ini, tunex_neg, tunex_pos = [], [], []
        tuney_ini, tuney_neg, tuney_pos = [], [], []
        tunex_wfm_ini, tunex_wfm_neg, tunex_wfm_pos = [], [], []
        tuney_wfm_ini, tuney_wfm_neg, tuney_wfm_pos = [], [], []

        for i in range(self.params.nr_measures):
            if self._stopevt.is_set():
                print('exiting...')
                break
            print('    {0:02d}/{1:02d} --> '.format(
                i+1, self.params.nr_measures), end='')

            tunex_ini.append(tune.tunex)
            tuney_ini.append(tune.tuney)
            tunex_wfm_ini.append(tune.tunex_wfm)
            tuney_wfm_ini.append(tune.tuney_wfm)
            for j, fac in enumerate(cycling_curve):
                quad.strength = korig + deltakl*fac
                _time.sleep(self.params.wait_quadrupole)
                if not j:
                    print(' -dk/2 ', end='')
                    _time.sleep(self.params.wait_tune)
                    tunex_neg.append(tune.tunex)
                    tuney_neg.append(tune.tuney)
                    tunex_wfm_neg.append(tune.tunex_wfm)
                    tuney_wfm_neg.append(tune.tuney_wfm)
                elif j == 1:
                    print(' +dk/2 ', end='')
                    _time.sleep(self.params.wait_tune)
                    tunex_pos.append(tune.tunex)
                    tuney_pos.append(tune.tuney)
                    tunex_wfm_pos.append(tune.tunex_wfm)
                    tuney_wfm_pos.append(tune.tuney_wfm)
            print('--> dnux = {:.5f}, dnuy = {:.5f}'.format(
               tunex_pos[-1] - tunex_neg[-1], tuney_pos[-1] - tuney_neg[-1]))

        meas = dict()
        meas['tunex_ini'] = np.array(tunex_ini)
        meas['tuney_ini'] = np.array(tuney_ini)
        meas['tunex_neg'] = np.array(tunex_neg)
        meas['tuney_neg'] = np.array(tuney_neg)
        meas['tunex_pos'] = np.array(tunex_pos)
        meas['tuney_pos'] = np.array(tuney_pos)
        meas['tunex_wfm_ini'] = np.array(tunex_wfm_ini)
        meas['tuney_wfm_ini'] = np.array(tuney_wfm_ini)
        meas['tunex_wfm_neg'] = np.array(tunex_wfm_neg)
        meas['tuney_wfm_neg'] = np.array(tuney_wfm_neg)
        meas['tunex_wfm_pos'] = np.array(tunex_wfm_pos)
        meas['tuney_wfm_pos'] = np.array(tuney_wfm_pos)
        meas['delta_kl'] = deltakl

        self.data['measure'][quadname] = meas

        print('turning quadrupole ' + quadname + ' Off \n')
        quad.cmd_turn_off(self.params.timeout_quad_turnon)
        if quad.pwrstate:
            print('    error: quadrupole ' + quadname + ' is still On.')
            self._stopevt.set()

    def process_data(self, mode='symm', discardpoints=None):
        """."""
        for quad in self.data['measure']:
            self.analysis[quad] = self.calc_beta(
                quad, mode=mode, discardpoints=discardpoints)

    def calc_beta(self, quadname, mode='symm', discardpoints=None):
        """."""
        anl = dict()
        datameas = self.data['measure'][quadname]

        if mode.lower().startswith('symm'):
            dnux = datameas['tunex_pos'] - datameas['tunex_neg']
            dnuy = datameas['tuney_pos'] - datameas['tuney_neg']
        elif mode.lower().startswith('pos'):
            dnux = datameas['tunex_pos'] - datameas['tunex_ini']
            dnuy = datameas['tuney_pos'] - datameas['tuney_ini']
        else:
            dnux = datameas['tunex_ini'] - datameas['tunex_neg']
            dnuy = datameas['tuney_ini'] - datameas['tuney_neg']

        usepts = set(range(dnux.shape[0]))
        if discardpoints is not None:
            usepts = set(usepts) - set(discardpoints)
        usepts = sorted(usepts)

        dkl = datameas['delta_kl']
        anl['betasx'] = +4 * np.pi * dnux[usepts] / dkl
        anl['betasy'] = -4 * np.pi * dnuy[usepts] / dkl
        anl['betax_ave'] = np.mean(anl['betasx'])
        anl['betay_ave'] = np.mean(anl['betasy'])
        return anl

    @staticmethod
    def get_quads(model, fam_data):
        """."""
        quads_idx = _dcopy(fam_data['QN']['index'])
        quads_idx = np.array([idx[len(idx)//2] for idx in quads_idx])

        qnames = list()
        for qidx in quads_idx:
            name = model[qidx].fam_name
            idc = fam_data[name]['index'].index([qidx, ])
            sub = fam_data[name]['subsection'][idc]
            inst = fam_data[name]['instance'][idc]
            qname = 'SI-{0:s}:PS-{1:s}-{2:s}'.format(sub, name, inst)
            qnames.append(qname.strip('-'))
        return qnames, quads_idx

    @staticmethod
    def get_quad_families(quads):
        fams = {quad[11:]: [] for quad in quads}
        for quad in quads:
            fams[quad[11:]].append(quad)
        return fams

    def plot_results(self, quads=None, title=''):

        f = plt.figure(figsize=(9, 7))
        gs = mpl_gs.GridSpec(ncols=1, nrows=2, figure=f)
        gs.update(
            left=0.1, right=0.8, bottom=0.15, top=0.9,
            hspace=0.0, wspace=0.35)

        ax1 = f.add_subplot(gs[0, 0])
        ax2 = f.add_subplot(gs[1, 0], sharex=ax1)

        if title:
            f.suptitle(title)

        quads = quads or self.data['quadnames']
        indcs, nom_bx, nom_by = [], [], []
        mes_bx, mes_by, bx, by = [], [], [], []
        for quad in quads:
            if quad not in self.analysis:
                continue
            indcs.append(self.data['quadnames'].index(quad))
            nom_bx.append(self.data['betax_mid'][quad])
            nom_by.append(self.data['betay_mid'][quad])
            mes_bx.append(self.analysis[quad]['betasx'])
            mes_by.append(self.analysis[quad]['betasy'])
            bx.append(self.analysis[quad]['betax_ave'])
            by.append(self.analysis[quad]['betay_ave'])

        ax1.plot(indcs, bx, '--bx')
        ax1.plot(indcs, nom_bx, '-bo')
        ax1.plot(indcs, mes_bx, '.b')
        ax2.plot(indcs, by, '--rx')
        ax2.plot(indcs, nom_by, '-ro')
        ax2.plot(indcs, mes_by, '.r')

        ax1.set_ylabel(r'$\beta_x$ [m]')
        ax2.set_ylabel(r'$\beta_y$ [m]')
        ax1.legend(
            ['measure', 'nominal'], loc='center left',
            bbox_to_anchor=(1, 0), fontsize='x-small')
        ax1.grid(True)
        ax2.grid(True)
        f.show()
<|endoftext|>


<commit_before>import BaseHTTPServer
import SimpleHTTPServer
import sys
import os
import ssl

chunked_encoding = False

class http_server_with_timeout(BaseHTTPServer.HTTPServer):
	allow_reuse_address = True
	timeout = 120

	def handle_timeout(self):
		raise Exception('timeout')

class http_handler(SimpleHTTPServer.SimpleHTTPRequestHandler):

	def do_GET(s):

		#print s.requestline
		global chunked_encoding

		# if the request contains the hostname and port. strip it
		if s.path.startswith('http://') or s.path.startswith('https://'):
			s.path = s.path[8:]
			s.path = s.path[s.path.find('/'):]

		s.path = os.path.normpath(s.path)

		if s.path == '/redirect':
			s.send_response(301)
			s.send_header("Location", "/test_file")
			s.send_header("Connection", "close")
			s.end_headers()
		elif s.path == '/infinite_redirect':
			s.send_response(301)
			s.send_header("Location", "/infinite_redirect")
			s.send_header("Connection", "close")
			s.end_headers()
		elif s.path == '/relative/redirect':
			s.send_response(301)
			s.send_header("Location", "../test_file")
			s.send_header("Connection", "close")
			s.end_headers()
		elif s.path.startswith('/announce'):
			s.send_response(200)
			response = 'd8:intervali1800e8:completei1e10:incompletei1e5:peers0:e'
			s.send_header("Content-Length", "%d" % len(response))
			s.send_header("Connection", "close")
			s.end_headers()
			s.wfile.write(response)
		elif os.path.split(s.path)[1].startswith('seed?'):
			query = s.path[6:]
			args_raw = query.split('&')
			args = {}
			for a in args_raw:
				kvp = a.split('=')
				args[kvp[0]] = kvp[1]
			piece = int(args['piece'])
			ranges = args['ranges'].split('-')

			try:
				filename = s.path[1:s.path.find('seed?') + 4]
				#print 'filename = %s' % filename
				f = open(filename, 'rb')
				f.seek(piece * 64 * 1024 + int(ranges[0]))
				data = f.read(int(ranges[1]) - int(ranges[0]) + 1)
				f.close()

				s.send_response(200)
				print 'sending %d bytes' % len(data)
				s.send_header("Content-Length", "%d" % len(data))
				s.end_headers()
				s.wfile.write(data);
			except Exception, e:
				print 'FILE NOT FOUND: ', os.getcwd(), filename
				s.send_response(404)
				s.send_header("Content-Length", "0")
				s.end_headers()
		else:
			try:
				filename = s.path[1:]
				# serve file by invoking default handler
				f = open(filename, 'rb')
				size = int(os.stat(filename).st_size)
				start_range = 0
				end_range = size
				if 'Range' in s.headers:
					s.send_response(206)
					s.send_header('Content-Range', 'bytes ' + str(start_range) + '-' + str(end_range - 1) + '/' + str(size))
					st, e = s.headers['range'][6:].split('-', 1)
					sl = len(st)
					el = len(e)
					if sl > 0:
						start_range = int(st)
						if el > 0:
							end_range = int(e) + 1
					elif el > 0:
						ei = int(e)
						if ei < size:
							start_range = size - ei
				else:
					s.send_response(200)
				s.send_header('Accept-Ranges', 'bytes')
				if chunked_encoding:
					s.send_header('Transfer-Encoding', 'chunked')
				s.send_header('Content-Length', end_range - start_range)
				if filename.endswith('.gz'):
					s.send_header('Content-Encoding', 'gzip')
				s.end_headers()

				f.seek(start_range)
				length = end_range - start_range
				while length > 0:
					to_send = min(length, 0x900)
					if chunked_encoding:
						s.wfile.write('%x\r\n' % to_send)
					data = f.read(to_send)
					s.wfile.write(data)
					if chunked_encoding:
						s.wfile.write('\r\n')
					length -= to_send
				if chunked_encoding:
					s.wfile.write('0\r\n\r\n')
			except Exception, e:
				print 'FILE NOT FOUND: ', os.getcwd(), e
				s.send_response(404)
				s.send_header("Content-Length", "0")
				s.end_headers()

if __name__ == '__main__':
	port = int(sys.argv[1])
	chunked_encoding = sys.argv[2] != '0'
	use_ssl = sys.argv[3] != '0'

	http_handler.protocol_version = 'HTTP/1.1'
	httpd = http_server_with_timeout(('127.0.0.1', port), http_handler)
	if use_ssl:
		httpd.socket = ssl.wrap_socket(httpd.socket, certfile='../ssl/server.pem', server_side=True)

	while True:
		httpd.handle_request()
<commit_msg>fix windows support in web_server.py for unit tests<commit_after>import BaseHTTPServer
import SimpleHTTPServer
import sys
import os
import ssl

chunked_encoding = False

class http_server_with_timeout(BaseHTTPServer.HTTPServer):
	allow_reuse_address = True
	timeout = 120

	def handle_timeout(self):
		raise Exception('timeout')

class http_handler(SimpleHTTPServer.SimpleHTTPRequestHandler):

	def do_GET(s):

		#print s.requestline
		global chunked_encoding

		# if the request contains the hostname and port. strip it
		if s.path.startswith('http://') or s.path.startswith('https://'):
			s.path = s.path[8:]
			s.path = s.path[s.path.find('/'):]

		file_path = os.path.normpath(s.path)

		if s.path == '/redirect':
			s.send_response(301)
			s.send_header("Location", "/test_file")
			s.send_header("Connection", "close")
			s.end_headers()
		elif s.path == '/infinite_redirect':
			s.send_response(301)
			s.send_header("Location", "/infinite_redirect")
			s.send_header("Connection", "close")
			s.end_headers()
		elif s.path == '/relative/redirect':
			s.send_response(301)
			s.send_header("Location", "../test_file")
			s.send_header("Connection", "close")
			s.end_headers()
		elif s.path.startswith('/announce'):
			s.send_response(200)
			response = 'd8:intervali1800e8:completei1e10:incompletei1e5:peers0:e'
			s.send_header("Content-Length", "%d" % len(response))
			s.send_header("Connection", "close")
			s.end_headers()
			s.wfile.write(response)
		elif os.path.split(s.path)[1].startswith('seed?'):
			query = s.path[6:]
			args_raw = query.split('&')
			args = {}
			for a in args_raw:
				kvp = a.split('=')
				args[kvp[0]] = kvp[1]
			piece = int(args['piece'])
			ranges = args['ranges'].split('-')

			try:
				filename = s.path[1:s.path.find('seed?') + 4]
				#print 'filename = %s' % filename
				f = open(filename, 'rb')
				f.seek(piece * 64 * 1024 + int(ranges[0]))
				data = f.read(int(ranges[1]) - int(ranges[0]) + 1)
				f.close()

				s.send_response(200)
				print 'sending %d bytes' % len(data)
				s.send_header("Content-Length", "%d" % len(data))
				s.end_headers()
				s.wfile.write(data);
			except Exception, e:
				print 'FILE NOT FOUND: ', os.getcwd(), filename
				s.send_response(404)
				s.send_header("Content-Length", "0")
				s.end_headers()
		else:
			try:
				filename = file_path[1:]
				# serve file by invoking default handler
				f = open(filename, 'rb')
				size = int(os.stat(filename).st_size)
				start_range = 0
				end_range = size
				if 'Range' in s.headers:
					s.send_response(206)
					s.send_header('Content-Range', 'bytes ' + str(start_range) + '-' + str(end_range - 1) + '/' + str(size))
					st, e = s.headers['range'][6:].split('-', 1)
					sl = len(st)
					el = len(e)
					if sl > 0:
						start_range = int(st)
						if el > 0:
							end_range = int(e) + 1
					elif el > 0:
						ei = int(e)
						if ei < size:
							start_range = size - ei
				else:
					s.send_response(200)
				s.send_header('Accept-Ranges', 'bytes')
				if chunked_encoding:
					s.send_header('Transfer-Encoding', 'chunked')
				s.send_header('Content-Length', end_range - start_range)
				if filename.endswith('.gz'):
					s.send_header('Content-Encoding', 'gzip')
				s.end_headers()

				f.seek(start_range)
				length = end_range - start_range
				while length > 0:
					to_send = min(length, 0x900)
					if chunked_encoding:
						s.wfile.write('%x\r\n' % to_send)
					data = f.read(to_send)
					s.wfile.write(data)
					if chunked_encoding:
						s.wfile.write('\r\n')
					length -= to_send
				if chunked_encoding:
					s.wfile.write('0\r\n\r\n')
			except Exception, e:
				print 'FILE NOT FOUND: ', os.getcwd(), e
				s.send_response(404)
				s.send_header("Content-Length", "0")
				s.end_headers()

if __name__ == '__main__':
	port = int(sys.argv[1])
	chunked_encoding = sys.argv[2] != '0'
	use_ssl = sys.argv[3] != '0'

	http_handler.protocol_version = 'HTTP/1.1'
	httpd = http_server_with_timeout(('127.0.0.1', port), http_handler)
	if use_ssl:
		httpd.socket = ssl.wrap_socket(httpd.socket, certfile='../ssl/server.pem', server_side=True)

	while True:
		httpd.handle_request()
<|endoftext|>