<commit_before># Filename: controlhost.py
"""
A set of classes and tools wich uses the ControlHost protocol.

"""
from __future__ import absolute_import, print_function, division

import socket
import struct
import time

try:
    from km3pipe.logging import get_logger
except ImportError:
    from logging import getLogger as get_logger

__author__ = "Tamas Gal"
__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration."
__credits__ = []
__license__ = "MIT"
__maintainer__ = "Tamas Gal"
__email__ = "tgal@km3net.de"
__status__ = "Development"

log = get_logger(__name__)

BUFFER_SIZE = 1024

class Client(object):
    """The ControlHost client"""

    def __init__(self, host, port=5553):
        self.host = host
        self.port = port
        self.socket = None
        self.tags = []
        self.valid_tags = []

    def subscribe(self, tag, mode='wait'):
        if mode not in ['wait', 'all']:
            raise ValueError("Possible subscription modes are 'wait' or 'all'")
        log.info("Subscribing to {} in mode {}".format(tag, mode))
        full_tag = self._full_tag(tag, mode)
        if full_tag not in self.tags:
            self.tags.append(full_tag)
        for t in tag.split():
            if t not in self.valid_tags:
                self.valid_tags.append(t)
        self._update_subscriptions()

    def unsubscribe(self, tag, mode='wait'):
        try:
            self.tags.remove(self._full_tag(tag, mode))
            self.valid_tags.remove(tag)
        except ValueError:
            pass
        else:
            self._update_subscriptions()

    def _full_tag(self, tag, mode):
        mode_flag = ' {} '.format(mode[0])
        full_tag = mode_flag + tag
        return full_tag

    def _update_subscriptions(self):
        log.debug("Subscribing to tags: {0}".format(self.tags))
        tags = ''.join(self.tags).encode("ascii")
        message = Message(b'_Subscri', tags)
        self.socket.send(message.data)
        message = Message(b'_Always')
        self.socket.send(message.data)

    def get_message(self):
        while True:
            log.info("     Waiting for control host Prefix")
            try:
                data = self.socket.recv(Prefix.SIZE)
                timestamp = time.time()
                log.info("    raw prefix data received: '{0}'".format(data))
                if data == b'':
                    raise EOFError
                prefix = Prefix(data=data, timestamp=timestamp)
            except (UnicodeDecodeError, OSError, struct.error):
                log.error("Failed to construct Prefix, reconnecting.")
                self._reconnect()
                continue

            try:
                prefix_tag = str(prefix.tag)
            except UnicodeDecodeError:
                log.error("The tag could not be decoded. Reconnecting.")
                self._reconnect()
                continue

            if prefix_tag not in self.valid_tags:
                log.error("Invalid tag '{0}' received, ignoring the message \n"
                          "and reconnecting.\n"
                          "  -> valid tags are: {1}".format(
                              prefix_tag, self.valid_tags))
                self._reconnect()
                continue
            else:
                break

        message = b''
        log.info("       got a Prefix with {0} bytes.".format(prefix.length))
        while len(message) < prefix.length:
            log.info("          message length: {0}".format(len(message)))
            log.info("            (getting next part)")
            buffer_size = min((BUFFER_SIZE, (prefix.length - len(message))))
            try:
                message += self.socket.recv(buffer_size)
            except OSError:
                log.error("Failed to construct message.")
                raise BufferError
        log.info("     ------ returning message with {0} bytes".format(
            len(message)))
        return prefix, message

    def _connect(self):
        """Connect to JLigier"""
        log.debug("Connecting to JLigier")
        self.socket = socket.socket()
        self.socket.connect((self.host, self.port))

    def _disconnect(self):
        """Close the socket"""
        log.debug("Disconnecting from JLigier")
        if self.socket:
            self.socket.close()

    def _reconnect(self):
        """Reconnect to JLigier and subscribe to the tags."""
        log.debug("Reconnecting to JLigier...")
        self._disconnect()
        self._connect()
        self._update_subscriptions()

    def __enter__(self):
        self._connect()
        return self

    def __exit__(self, type, value, traceback):
        self._disconnect()

class Message(object):
    """The representation of a ControlHost message."""

    def __init__(self, tag, message=b''):
        self.prefix = Prefix(tag, len(message))
        self.message = message

    @property
    def data(self):
        return self.prefix.data + self.message

class Tag(object):
    """Represents the tag in a ControlHost Prefix."""
    SIZE = 8

    def __init__(self, data=None):
        self._data = b''
        self.data = data

    @property
    def data(self):
        """The byte data"""
        return self._data

    @data.setter
    def data(self, value):
        """Set the byte data and fill up the bytes to fit the size."""
        if not value:
            value = b''
        if len(value) > self.SIZE:
            raise ValueError("The maximum tag size is {0}".format(self.SIZE))
        self._data = value
        while len(self._data) < self.SIZE:
            self._data += b'\x00'

    def __str__(self):
        return self.data.decode(encoding='UTF-8').strip('\x00')

    def __len__(self):
        return len(self._data)

class Prefix(object):
    """The prefix of a ControlHost message."""
    SIZE = 16

    def __init__(self, tag=None, length=None, data=None, timestamp=None):
        if data:
            self.data = data
        else:
            self.tag = Tag(tag)
            self.length = length
        if timestamp is None:
            self.timestamp = time.time()
        else:
            self.timestamp = timestamp

    @property
    def data(self):
        return self.tag.data + struct.pack('>i', self.length) + b'\x00' * 4

    @data.setter
    def data(self, value):
        self.tag = Tag(data=value[:Tag.SIZE])
        self.length = struct.unpack('>i', value[Tag.SIZE:Tag.SIZE + 4])[0]

    def __str__(self):
        return ("ControlHost Prefix with tag '{0}' ({1} bytes of data)".format(
            self.tag, self.length))
<commit_msg>Improve usability, cleanup"<commit_after># Filename: controlhost.py
"""
A set of classes and tools wich uses the ControlHost protocol.

"""
from __future__ import absolute_import, print_function, division

from collections import namedtuple
import socket
import struct
import time

try:
    from km3pipe.logging import get_logger
except ImportError:
    from logging import getLogger as get_logger

__author__ = "Tamas Gal"
__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration."
__credits__ = []
__license__ = "MIT"
__maintainer__ = "Tamas Gal"
__email__ = "tgal@km3net.de"
__status__ = "Development"

log = get_logger(__name__)

BUFFER_SIZE = 1024

CHResponse = namedtuple("CHResponse", ["prefix", "data"])

class Client(object):
    """The ControlHost client"""

    def __init__(self, host, port=5553, tag=None):
        self.host = host
        self.port = port
        self.socket = None
        self.tags = []
        self.valid_tags = []

        if tag is not None:
            self._connect()
            self.subscribe(tag)

    def subscribe(self, tag, mode='wait'):
        if mode not in ['wait', 'all']:
            raise ValueError("Possible subscription modes are 'wait' or 'all'")
        log.info("Subscribing to {} in mode {}".format(tag, mode))
        full_tag = self._full_tag(tag, mode)
        if full_tag not in self.tags:
            self.tags.append(full_tag)
        for t in tag.split():
            if t not in self.valid_tags:
                self.valid_tags.append(t)
        self._update_subscriptions()

    def unsubscribe(self, tag, mode='wait'):
        try:
            self.tags.remove(self._full_tag(tag, mode))
            self.valid_tags.remove(tag)
        except ValueError:
            pass
        else:
            self._update_subscriptions()

    def _full_tag(self, tag, mode):
        mode_flag = ' {} '.format(mode[0])
        full_tag = mode_flag + tag
        return full_tag

    def _update_subscriptions(self):
        log.debug("Subscribing to tags: {0}".format(self.tags))
        tags = ''.join(self.tags).encode("ascii")
        message = Message(b'_Subscri', tags)
        self.socket.send(message.data)
        message = Message(b'_Always')
        self.socket.send(message.data)

    def get_message(self):
        while True:
            log.info("     Waiting for control host Prefix")
            try:
                data = self.socket.recv(Prefix.SIZE)
                timestamp = time.time()
                log.info("    raw prefix data received: '{0}'".format(data))
                if data == b'':
                    raise EOFError
                prefix = Prefix(data=data, timestamp=timestamp)
            except (UnicodeDecodeError, OSError, struct.error):
                log.error("Failed to construct Prefix, reconnecting.")
                self._reconnect()
                continue

            try:
                prefix_tag = str(prefix.tag)
            except UnicodeDecodeError:
                log.error("The tag could not be decoded. Reconnecting.")
                self._reconnect()
                continue

            if prefix_tag not in self.valid_tags:
                log.error(
                    "Invalid tag '{0}' received, ignoring the message \n"
                    "and reconnecting.\n"
                    "  -> valid tags are: {1}".format(
                        prefix_tag, self.valid_tags
                    )
                )
                self._reconnect()
                continue
            else:
                break

        message = b''
        log.info("       got a Prefix with {0} bytes.".format(prefix.length))
        while len(message) < prefix.length:
            log.info("          message length: {0}".format(len(message)))
            log.info("            (getting next part)")
            buffer_size = min((BUFFER_SIZE, (prefix.length - len(message))))
            try:
                message += self.socket.recv(buffer_size)
            except OSError:
                log.error("Failed to construct message.")
                raise BufferError
        log.info(
            "     ------ returning message with {0} bytes".format(
                len(message)
            )
        )
        return CHResponse(prefix, message)

    def _connect(self):
        """Connect to JLigier"""
        log.debug("Connecting to JLigier")
        self.socket = socket.socket()
        self.socket.connect((self.host, self.port))

    def _disconnect(self):
        """Close the socket"""
        log.debug("Disconnecting from JLigier")
        if self.socket:
            self.socket.close()

    def _reconnect(self):
        """Reconnect to JLigier and subscribe to the tags."""
        log.debug("Reconnecting to JLigier...")
        self._disconnect()
        self._connect()
        self._update_subscriptions()

    def __enter__(self):
        self._connect()
        return self

    def __exit__(self, exit_type, value, traceback):
        self._disconnect()

class Message(object):
    """The representation of a ControlHost message."""

    def __init__(self, tag, message=b''):
        self.prefix = Prefix(tag, len(message))
        self.message = message

    @property
    def data(self):
        return self.prefix.data + self.message

class Tag(object):
    """Represents the tag in a ControlHost Prefix."""
    SIZE = 8

    def __init__(self, data=None):
        self._data = b''
        self.data = data

    @property
    def data(self):
        """The byte data"""
        return self._data

    @data.setter
    def data(self, value):
        """Set the byte data and fill up the bytes to fit the size."""
        if not value:
            value = b''
        if len(value) > self.SIZE:
            raise ValueError("The maximum tag size is {0}".format(self.SIZE))
        self._data = value
        while len(self._data) < self.SIZE:
            self._data += b'\x00'

    def __str__(self):
        return self.data.decode(encoding='UTF-8').strip('\x00')

    def __len__(self):
        return len(self._data)

class Prefix(object):
    """The prefix of a ControlHost message."""
    SIZE = 16

    def __init__(self, tag=None, length=None, data=None, timestamp=None):
        if data:
            self.data = data
        else:
            self.tag = Tag(tag)
            self.length = length
        if timestamp is None:
            self.timestamp = time.time()
        else:
            self.timestamp = timestamp

    @property
    def data(self):
        return self.tag.data + struct.pack('>i', self.length) + b'\x00' * 4

    @data.setter
    def data(self, value):
        self.tag = Tag(data=value[:Tag.SIZE])
        self.length = struct.unpack('>i', value[Tag.SIZE:Tag.SIZE + 4])[0]

    def __str__(self):
        return (
            "ControlHost Prefix with tag '{0}' ({1} bytes of data)".format(
                self.tag, self.length
            )
        )
<|endoftext|>


<commit_before>"""

Python Interchangeable Virtual Instrument Library

Copyright (c) 2012-2014 Alex Forencich

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

"""

import math

from .. import ivi
from .. import dmm
from . import common

MeasurementFunctionMapping = {
        'dc_volts': 'volt',
        'ac_volts': 'volt:ac',
        'dc_current': 'curr',
        'ac_current': 'curr:ac',
        'two_wire_resistance': 'res',
        'four_wire_resistance': 'fres',
        'frequency': 'freq',
        'period': 'per',
        'temperature': 'temp',
        'capacitance': 'cap',
        'continuity': 'cont',
        'diode': 'diod'}

MeasurementRangeMapping = {
        'dc_volts': 'volt:dc:range',
        'ac_volts': 'volt:ac:range',
        'dc_current': 'curr:dc:range',
        'ac_current': 'curr:ac:range',
        'two_wire_resistance': 'res:range',
        'four_wire_resistance': 'fres:range',
        'frequency': 'freq:range:lower',
        'period': 'per:range:lower',
        'capacitance': 'cap:range'}

MeasurementAutoRangeMapping = {
        'dc_volts': 'volt:dc:range:auto',
        'ac_volts': 'volt:ac:range:auto',
        'dc_current': 'curr:dc:range:auto',
        'ac_current': 'curr:ac:range:auto',
        'two_wire_resistance': 'res:range:auto',
        'four_wire_resistance': 'fres:range:auto',
        'capacitance': 'cap:range:auto'}

MeasurementResolutionMapping = {
        'dc_volts': 'volt:dc:resolution',
        'ac_volts': 'volt:ac:resolution',
        'dc_current': 'curr:dc:resolution',
        'ac_current': 'curr:ac:resolution',
        'two_wire_resistance': 'res:resolution',
        'four_wire_resistance': 'fres:resolution'}

TriggerSourceMapping = {
        'bus': 'bus',
        'external': 'ext',
        'immediate': 'imm'}

class Base(common.IdnCommand, common.ErrorQuery, common.Reset, common.SelfTest,
           ivi.Driver,
           dmm.Base):
    "Generic SCPI IVI DMM driver"

    def __init__(self, *args, **kwargs):
        self.__dict__.setdefault('_instrument_id', '')

        # early define of _do_scpi_init
        self.__dict__.setdefault('_do_scpi_init', True)

        super(Base, self).__init__(*args, **kwargs)

        self._self_test_delay = 40

        self._identity_description = "Generic SCPI IVI DMM driver"
        self._identity_identifier = ""
        self._identity_revision = ""
        self._identity_vendor = ""
        self._identity_instrument_manufacturer = ""
        self._identity_instrument_model = ""
        self._identity_instrument_firmware_revision = ""
        self._identity_specification_major_version = 4
        self._identity_specification_minor_version = 1
        self._identity_supported_instrument_models = ['DMM']

    def _initialize(self, resource = None, id_query = False, reset = False, **keywargs):
        "Opens an I/O session to the instrument."

        super(Base, self)._initialize(resource, id_query, reset, **keywargs)

        # interface clear
        if not self._driver_operation_simulate:
            self._clear()

        # check ID
        if id_query and not self._driver_operation_simulate:
            id = self.identity.instrument_model
            id_check = self._instrument_id
            id_short = id[:len(id_check)]
            if id_short != id_check:
                raise Exception("Instrument ID mismatch, expecting %s, got %s", id_check, id_short)

        # reset
        if reset:
            self.utility.reset()

    def _utility_disable(self):
        pass

    def _utility_lock_object(self):
        pass

    def _utility_unlock_object(self):
        pass

    def _get_measurement_function(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = self._ask(":sense:function?").lower().strip('"')
            value = [k for k,v in MeasurementFunctionMapping.items() if v==value][0]
            self._measurement_function = value
            self._set_cache_valid()
        return self._measurement_function

    def _set_measurement_function(self, value):
        if value not in MeasurementFunctionMapping:
            raise ivi.ValueNotSupportedException()
        if not self._driver_operation_simulate:
            self._write(":sense:function '%s'" % MeasurementFunctionMapping[value])
        self._measurement_function = value
        self._set_cache_valid()
        self._set_cache_valid(False, 'range')
        self._set_cache_valid(False, 'auto_range')
        self._set_cache_valid(False, 'resolution')

    def _get_range(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            func = self._get_measurement_function()
            if func in MeasurementRangeMapping:
                cmd = MeasurementRangeMapping[func]
                value = float(self._ask("%s?" % (cmd)))
                self._range = value
                self._set_cache_valid()
        return self._range

    def _set_range(self, value):
        value = float(value)
        # round up to even power of 10
        value = math.pow(10, math.ceil(math.log10(value)))
        if not self._driver_operation_simulate:
            func = self._get_measurement_function()
            if func in MeasurementRangeMapping:
                cmd = MeasurementRangeMapping[func]
                self._write("%s %g" % (cmd, value))
        self._range = value
        self._set_cache_valid()

    def _get_auto_range(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            func = self._get_measurement_function()
            if func in MeasurementAutoRangeMapping:
                cmd = MeasurementAutoRangeMapping[func]
                value = int(self._ask("%s?" % (cmd)))
                if value == 0:
                    value = 'off'
                elif value == 1:
                    value = 'on'
                self._auto_range = value
                self._set_cache_valid()
        return self._auto_range

    def _set_auto_range(self, value):
        if value not in dmm.Auto2:
            raise ivi.ValueNotSupportedException()
        if not self._driver_operation_simulate:
            func = self._get_measurement_function()
            if func in MeasurementAutoRangeMapping:
                cmd = MeasurementAutoRangeMapping[func]
                self._write("%s %d" % (cmd, int(value == 'on')))
        self._auto_range = value
        self._set_cache_valid()

    def _get_resolution(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            func = self._get_measurement_function()
            if func in MeasurementResolutionMapping:
                cmd = MeasurementResolutionMapping[func]
                value = float(self._ask("%s?" % (cmd)))
                self._resolution = value
                self._set_cache_valid()
        return self._resolution

    def _set_resolution(self, value):
        value = float(value)
        # round up to even power of 10
        value = math.pow(10, math.ceil(math.log10(value)))
        if not self._driver_operation_simulate:
            func = self._get_measurement_function()
            if func in MeasurementResolutionMapping:
                cmd = MeasurementResolutionMapping[func]
                self._write("%s %g" % (cmd, value))
        self._resolution = value
        self._set_cache_valid()

    def _get_trigger_delay(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = float(self._ask("trigger:delay?"))
            self._trigger_delay = value
            self._set_cache_valid()
        return self._trigger_delay

    def _set_trigger_delay(self, value):
        value = float(value)
        if not self._driver_operation_simulate:
            self._write('trigger:delay %g' % value)
        self._trigger_delay = value
        self._set_cache_valid()

    def _get_trigger_delay_auto(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = bool(int(self._ask("trigger:delay:auto?")))
            self._trigger_delay_auto = value
            self._set_cache_valid()
        return self._trigger_delay_auto

    def _set_trigger_delay_auto(self, value):
        value = bool(value)
        if not self._driver_operation_simulate:
            self._write('trigger:delay:auto %d' % int(value))
        self._trigger_delay_auto = value
        self._set_cache_valid()

    def _get_trigger_source(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = self._ask("trigger:source?").lower()
            value = [k for k,v in TriggerSourceMapping.items() if v==value][0]
            self._trigger_source = value
            self._set_cache_valid()
        return self._trigger_source

    def _set_trigger_source(self, value):
        if value not in TriggerSourceMapping:
            raise ivi.ValueNotSupportedException()
        if not self._driver_operation_simulate:
            self._write(":trigger:source %s" % TriggerSourceMapping[value])
        self._trigger_source = value

    def _measurement_abort(self):
        if not self._driver_operation_simulate:
            self._write(":abort")

    def _measurement_fetch(self, max_time):
        if not self._driver_operation_simulate:
            return float(self._ask(":fetch?"))
        return 0.0

    def _measurement_initiate(self):
        if not self._driver_operation_simulate:
            self._write(":initiate")

    def _measurement_is_out_of_range(self, value):
        return self._measurement_is_over_range(value) or self._measurement_is_under_range(value)

    def _measurement_is_over_range(self, value):
        return value == 9.9e+37

    def _measurement_is_under_range(self, value):
        return value == -9.9e+37

    def _measurement_read(self, max_time):
        if not self._driver_operation_simulate:
            return float(self._ask(":read?"))
        return 0.0

class MultiPoint(dmm.MultiPoint):
    "Extension IVI methods for DMMs capable of acquiring measurements based on multiple triggers"

    def _get_trigger_measurement_complete_destination(self):
        return self._trigger_measurement_complete_destination

    def _set_trigger_measurement_complete_destination(self, value):
        value = str(value)
        self._trigger_measurement_complete_destination = value

    def _get_trigger_multi_point_sample_count(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = int(self._ask("sample:count?"))
            self._trigger_multi_point_sample_count = value
            self._set_cache_valid()
        return self._trigger_multi_point_sample_count

    def _set_trigger_multi_point_sample_count(self, value):
        value = int(value)
        if not self._driver_operation_simulate:
            self._write("sample:count %d" % value)
        self._trigger_multi_point_sample_count = value
        self._set_cache_valid()

    def _get_trigger_multi_point_sample_interval(self):
        return self._trigger_multi_point_sample_interval

    def _set_trigger_multi_point_sample_interval(self, value):
        value = int(value)
        self._trigger_multi_point_sample_interval = value

    def _get_trigger_multi_point_sample_trigger(self):
        return self._trigger_multi_point_sample_trigger

    def _set_trigger_multi_point_sample_trigger(self, value):
        value = str(value)
        self._trigger_multi_point_sample_trigger = value

    def _get_trigger_multi_point_count(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = self._ask("trigger:count?")
            if float(value) >= 9.9e37:
                value = float('inf')
            else:
                value = int(value)
            self._trigger_multi_point_count = value
            self._set_cache_valid()
        return self._trigger_multi_point_count

    def _set_trigger_multi_point_count(self, value):
        if float(value) >= 9.9e37 or float(value) == float('inf'):
            value = float('inf')
        else:
            value = int(value)
        if not self._driver_operation_simulate:
            if value == float('inf'):
                self._write("trigger:count inf")
            else:
                self._write("trigger:count %d" % value)
        self._trigger_multi_point_count = value
        self._set_cache_valid()

    def _measurement_fetch_multi_point(self, max_time, num_of_measurements = 0):
        pass

    def _measurement_read_multi_point(self, max_time, num_of_measurements = 0):
        pass

class SoftwareTrigger(dmm.SoftwareTrigger):
    "Extension IVI methods for DMMs that can initiate a measurement based on a software trigger signal"

    def _send_software_trigger(self):
        if not self._driver_operation_simulate:
            self._write("*trg")

<commit_msg>Implement fetch/read for multipoint measurements.<commit_after>"""

Python Interchangeable Virtual Instrument Library

Copyright (c) 2012-2014 Alex Forencich

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

"""

import math

from .. import ivi
from .. import dmm
from . import common

MeasurementFunctionMapping = {
        'dc_volts': 'volt',
        'ac_volts': 'volt:ac',
        'dc_current': 'curr',
        'ac_current': 'curr:ac',
        'two_wire_resistance': 'res',
        'four_wire_resistance': 'fres',
        'frequency': 'freq',
        'period': 'per',
        'temperature': 'temp',
        'capacitance': 'cap',
        'continuity': 'cont',
        'diode': 'diod'}

MeasurementRangeMapping = {
        'dc_volts': 'volt:dc:range',
        'ac_volts': 'volt:ac:range',
        'dc_current': 'curr:dc:range',
        'ac_current': 'curr:ac:range',
        'two_wire_resistance': 'res:range',
        'four_wire_resistance': 'fres:range',
        'frequency': 'freq:range:lower',
        'period': 'per:range:lower',
        'capacitance': 'cap:range'}

MeasurementAutoRangeMapping = {
        'dc_volts': 'volt:dc:range:auto',
        'ac_volts': 'volt:ac:range:auto',
        'dc_current': 'curr:dc:range:auto',
        'ac_current': 'curr:ac:range:auto',
        'two_wire_resistance': 'res:range:auto',
        'four_wire_resistance': 'fres:range:auto',
        'capacitance': 'cap:range:auto'}

MeasurementResolutionMapping = {
        'dc_volts': 'volt:dc:resolution',
        'ac_volts': 'volt:ac:resolution',
        'dc_current': 'curr:dc:resolution',
        'ac_current': 'curr:ac:resolution',
        'two_wire_resistance': 'res:resolution',
        'four_wire_resistance': 'fres:resolution'}

TriggerSourceMapping = {
        'bus': 'bus',
        'external': 'ext',
        'immediate': 'imm'}

class Base(common.IdnCommand, common.ErrorQuery, common.Reset, common.SelfTest,
           ivi.Driver,
           dmm.Base):
    "Generic SCPI IVI DMM driver"

    def __init__(self, *args, **kwargs):
        self.__dict__.setdefault('_instrument_id', '')

        # early define of _do_scpi_init
        self.__dict__.setdefault('_do_scpi_init', True)

        super(Base, self).__init__(*args, **kwargs)

        self._self_test_delay = 40

        self._identity_description = "Generic SCPI IVI DMM driver"
        self._identity_identifier = ""
        self._identity_revision = ""
        self._identity_vendor = ""
        self._identity_instrument_manufacturer = ""
        self._identity_instrument_model = ""
        self._identity_instrument_firmware_revision = ""
        self._identity_specification_major_version = 4
        self._identity_specification_minor_version = 1
        self._identity_supported_instrument_models = ['DMM']

    def _initialize(self, resource = None, id_query = False, reset = False, **keywargs):
        "Opens an I/O session to the instrument."

        super(Base, self)._initialize(resource, id_query, reset, **keywargs)

        # interface clear
        if not self._driver_operation_simulate:
            self._clear()

        # check ID
        if id_query and not self._driver_operation_simulate:
            id = self.identity.instrument_model
            id_check = self._instrument_id
            id_short = id[:len(id_check)]
            if id_short != id_check:
                raise Exception("Instrument ID mismatch, expecting %s, got %s", id_check, id_short)

        # reset
        if reset:
            self.utility.reset()

    def _utility_disable(self):
        pass

    def _utility_lock_object(self):
        pass

    def _utility_unlock_object(self):
        pass

    def _get_measurement_function(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = self._ask(":sense:function?").lower().strip('"')
            value = [k for k,v in MeasurementFunctionMapping.items() if v==value][0]
            self._measurement_function = value
            self._set_cache_valid()
        return self._measurement_function

    def _set_measurement_function(self, value):
        if value not in MeasurementFunctionMapping:
            raise ivi.ValueNotSupportedException()
        if not self._driver_operation_simulate:
            self._write(":sense:function '%s'" % MeasurementFunctionMapping[value])
        self._measurement_function = value
        self._set_cache_valid()
        self._set_cache_valid(False, 'range')
        self._set_cache_valid(False, 'auto_range')
        self._set_cache_valid(False, 'resolution')

    def _get_range(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            func = self._get_measurement_function()
            if func in MeasurementRangeMapping:
                cmd = MeasurementRangeMapping[func]
                value = float(self._ask("%s?" % (cmd)))
                self._range = value
                self._set_cache_valid()
        return self._range

    def _set_range(self, value):
        value = float(value)
        # round up to even power of 10
        value = math.pow(10, math.ceil(math.log10(value)))
        if not self._driver_operation_simulate:
            func = self._get_measurement_function()
            if func in MeasurementRangeMapping:
                cmd = MeasurementRangeMapping[func]
                self._write("%s %g" % (cmd, value))
        self._range = value
        self._set_cache_valid()

    def _get_auto_range(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            func = self._get_measurement_function()
            if func in MeasurementAutoRangeMapping:
                cmd = MeasurementAutoRangeMapping[func]
                value = int(self._ask("%s?" % (cmd)))
                if value == 0:
                    value = 'off'
                elif value == 1:
                    value = 'on'
                self._auto_range = value
                self._set_cache_valid()
        return self._auto_range

    def _set_auto_range(self, value):
        if value not in dmm.Auto2:
            raise ivi.ValueNotSupportedException()
        if not self._driver_operation_simulate:
            func = self._get_measurement_function()
            if func in MeasurementAutoRangeMapping:
                cmd = MeasurementAutoRangeMapping[func]
                self._write("%s %d" % (cmd, int(value == 'on')))
        self._auto_range = value
        self._set_cache_valid()

    def _get_resolution(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            func = self._get_measurement_function()
            if func in MeasurementResolutionMapping:
                cmd = MeasurementResolutionMapping[func]
                value = float(self._ask("%s?" % (cmd)))
                self._resolution = value
                self._set_cache_valid()
        return self._resolution

    def _set_resolution(self, value):
        value = float(value)
        # round up to even power of 10
        value = math.pow(10, math.ceil(math.log10(value)))
        if not self._driver_operation_simulate:
            func = self._get_measurement_function()
            if func in MeasurementResolutionMapping:
                cmd = MeasurementResolutionMapping[func]
                self._write("%s %g" % (cmd, value))
        self._resolution = value
        self._set_cache_valid()

    def _get_trigger_delay(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = float(self._ask("trigger:delay?"))
            self._trigger_delay = value
            self._set_cache_valid()
        return self._trigger_delay

    def _set_trigger_delay(self, value):
        value = float(value)
        if not self._driver_operation_simulate:
            self._write('trigger:delay %g' % value)
        self._trigger_delay = value
        self._set_cache_valid()

    def _get_trigger_delay_auto(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = bool(int(self._ask("trigger:delay:auto?")))
            self._trigger_delay_auto = value
            self._set_cache_valid()
        return self._trigger_delay_auto

    def _set_trigger_delay_auto(self, value):
        value = bool(value)
        if not self._driver_operation_simulate:
            self._write('trigger:delay:auto %d' % int(value))
        self._trigger_delay_auto = value
        self._set_cache_valid()

    def _get_trigger_source(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = self._ask("trigger:source?").lower()
            value = [k for k,v in TriggerSourceMapping.items() if v==value][0]
            self._trigger_source = value
            self._set_cache_valid()
        return self._trigger_source

    def _set_trigger_source(self, value):
        if value not in TriggerSourceMapping:
            raise ivi.ValueNotSupportedException()
        if not self._driver_operation_simulate:
            self._write(":trigger:source %s" % TriggerSourceMapping[value])
        self._trigger_source = value

    def _measurement_abort(self):
        if not self._driver_operation_simulate:
            self._write(":abort")

    def _measurement_fetch(self, max_time):
        if not self._driver_operation_simulate:
            return float(self._ask(":fetch?"))
        return 0.0

    def _measurement_initiate(self):
        if not self._driver_operation_simulate:
            self._write(":initiate")

    def _measurement_is_out_of_range(self, value):
        return self._measurement_is_over_range(value) or self._measurement_is_under_range(value)

    def _measurement_is_over_range(self, value):
        return value == 9.9e+37

    def _measurement_is_under_range(self, value):
        return value == -9.9e+37

    def _measurement_read(self, max_time):
        if not self._driver_operation_simulate:
            return float(self._ask(":read?"))
        return 0.0

class MultiPoint(dmm.MultiPoint):
    "Extension IVI methods for DMMs capable of acquiring measurements based on multiple triggers"

    def _get_trigger_measurement_complete_destination(self):
        return self._trigger_measurement_complete_destination

    def _set_trigger_measurement_complete_destination(self, value):
        value = str(value)
        self._trigger_measurement_complete_destination = value

    def _get_trigger_multi_point_sample_count(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = int(self._ask("sample:count?"))
            self._trigger_multi_point_sample_count = value
            self._set_cache_valid()
        return self._trigger_multi_point_sample_count

    def _set_trigger_multi_point_sample_count(self, value):
        value = int(value)
        if not self._driver_operation_simulate:
            self._write("sample:count %d" % value)
        self._trigger_multi_point_sample_count = value
        self._set_cache_valid()

    def _get_trigger_multi_point_sample_interval(self):
        return self._trigger_multi_point_sample_interval

    def _set_trigger_multi_point_sample_interval(self, value):
        value = int(value)
        self._trigger_multi_point_sample_interval = value

    def _get_trigger_multi_point_sample_trigger(self):
        return self._trigger_multi_point_sample_trigger

    def _set_trigger_multi_point_sample_trigger(self, value):
        value = str(value)
        self._trigger_multi_point_sample_trigger = value

    def _get_trigger_multi_point_count(self):
        if not self._driver_operation_simulate and not self._get_cache_valid():
            value = self._ask("trigger:count?")
            if float(value) >= 9.9e37:
                value = float('inf')
            else:
                value = int(value)
            self._trigger_multi_point_count = value
            self._set_cache_valid()
        return self._trigger_multi_point_count

    def _set_trigger_multi_point_count(self, value):
        if float(value) >= 9.9e37 or float(value) == float('inf'):
            value = float('inf')
        else:
            value = int(value)
        if not self._driver_operation_simulate:
            if value == float('inf'):
                self._write("trigger:count inf")
            else:
                self._write("trigger:count %d" % value)
        self._trigger_multi_point_count = value
        self._set_cache_valid()

    def _measurement_fetch_multi_point(self, max_time, num_of_measurements = 0):
        if not self._driver_operation_simulate:
            return self._ask_for_values(":fetch?", array=False)
        return [0.0 for i in range(self._trigger_multi_point_count*self._trigger_multi_point_sample_count)]

    def _measurement_read_multi_point(self, max_time, num_of_measurements = 0):
        if not self._driver_operation_simulate:
            return self._ask_for_values(":read?", array=False)
        return [0.0 for i in range(self._trigger_multi_point_count*self._trigger_multi_point_sample_count)]

class SoftwareTrigger(dmm.SoftwareTrigger):
    "Extension IVI methods for DMMs that can initiate a measurement based on a software trigger signal"

    def _send_software_trigger(self):
        if not self._driver_operation_simulate:
            self._write("*trg")

<|endoftext|>


<commit_before>import io
from xml.etree import ElementTree as ET

from madam.core import Asset, MetadataProcessor, Processor, UnsupportedFormatError, operator, OperatorError

_INCH_TO_MM = 1 / 25.4
_PX_PER_INCH = 90
_PT_PER_INCH = 1 / 72
_FONT_SIZE_PT = 12
_X_HEIGHT = 0.7

def svg_length_to_px(length):
    if length is None:
        raise ValueError()

    unit_len = 2
    if length.endswith('%'):
        unit_len = 1
    try:
        value = float(length)
        unit = 'px'
    except ValueError:
        value = float(length[:-unit_len])
        unit = length[-unit_len:]

    if unit == 'em':
        return value * _PX_PER_INCH * _FONT_SIZE_PT * _PT_PER_INCH
    elif unit == 'ex':
        return value * _PX_PER_INCH * _X_HEIGHT * _FONT_SIZE_PT * _PT_PER_INCH
    elif unit == 'px':
        return value
    elif unit == 'in':
        return value * _PX_PER_INCH
    elif unit == 'cm':
        return value * _PX_PER_INCH * _INCH_TO_MM * 10
    elif unit == 'mm':
        return value * _PX_PER_INCH * _INCH_TO_MM
    elif unit == 'pt':
        return value * _PX_PER_INCH * _PT_PER_INCH
    elif unit == 'pc':
        return value * _PX_PER_INCH * _PT_PER_INCH * 12
    elif unit == '%':
        return value

XML_NS = dict(
    svg='http://www.w3.org/2000/svg',
    rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    dc='http://purl.org/dc/elements/1.1/',
)

class SVGProcessor(Processor):
    """
    Represents a processor that handles *Scalable Vector Graphics* (SVG) data.
    """
    def __init__(self):
        """
        Initializes a new `SVGProcessor`.
        """
        super().__init__()

    def can_read(self, file):
        for ns_prefix, ns_uri in XML_NS.items():
            ET.register_namespace(ns_prefix, ns_uri)

        try:
            ET.parse(file)
            return True
        except ET.ParseError:
            return False

    def read(self, file):
        for ns_prefix, ns_uri in XML_NS.items():
            ET.register_namespace(ns_prefix, ns_uri)

        try:
            tree = ET.parse(file)
        except ET.ParseError as e:
            raise UnsupportedFormatError('Error while parsing XML in line %d, column %d' % e.position)
        root = tree.getroot()

        metadata = dict(mime_type='image/svg+xml')
        if 'width' in root.keys():
            metadata['width'] = svg_length_to_px(root.get('width'))
        if 'height' in root.keys():
            metadata['height'] = svg_length_to_px(root.get('height'))

        file.seek(0)
        return Asset(essence=file, **metadata)

    @operator
    def shrink(self, asset):
        """
        Shrinks the size of an SVG asset.

        :param asset: Media asset to be shrunk
        :type asset: Asset
        :return: Shrunk vector asset
        """
        for ns_prefix, ns_uri in XML_NS.items():
            ET.register_namespace(ns_prefix, ns_uri)

        try:
            tree = ET.parse(asset.essence)
        except ET.ParseError as e:
            raise UnsupportedFormatError('Unsupported asset type: %s' % asset.mime_type)

        xml_result = io.BytesIO()
        tree.write(xml_result, xml_declaration=True, encoding='utf-8')
        xml_result.seek(0)

        metadata = dict(mime_type='image/svg+xml')
        for metadata_key in ('width', 'height'):
            if metadata_key in asset.metadata:
                metadata[metadata_key] = asset.metadata[metadata_key]

        return Asset(essence=xml_result, **metadata)

class SVGMetadataProcessor(MetadataProcessor):
    """
    Represents a metadata processor that handles Scalable Vector Graphics (SVG)
    data.

    It is assumed that the SVG XML uses UTF-8 encoding.
    """
    def __init__(self):
        """
        Initializes a new `SVGMetadataProcessor`.
        """
        super().__init__()

    @property
    def formats(self):
        return {'rdf'}

    @staticmethod
    def __parse(file):
        try:
            tree = ET.parse(file)
        except ET.ParseError as e:
            raise UnsupportedFormatError('Error while parsing XML: %s' % e)
        root = tree.getroot()
        return tree, root, root.find('./svg:metadata', XML_NS)

    @staticmethod
    def __register_xml_namespaces():
        for prefix, uri in XML_NS.items():
            if prefix == 'svg':
                prefix = ''
            ET.register_namespace(prefix, uri)

    def read(self, file):
        _, _, metadata_elem = SVGMetadataProcessor.__parse(file)
        if metadata_elem is None or len(metadata_elem) == 0:
            return {'rdf': {}}
        return {'rdf': {'xml': ET.tostring(metadata_elem[0], encoding='unicode')}}

    def strip(self, file):
        tree, root, metadata_elem = SVGMetadataProcessor.__parse(file)

        if metadata_elem is not None:
            root.remove(metadata_elem)

        result = io.BytesIO()
        SVGMetadataProcessor.__register_xml_namespaces()
        tree.write(result, xml_declaration=True, encoding='utf-8')
        result.seek(0)
        return result

    def combine(self, file, metadata):
        if not metadata:
            raise ValueError('No metadata provided.')
        if 'rdf' not in metadata:
            raise UnsupportedFormatError('No RDF metadata found.')
        rdf = metadata['rdf']
        if 'xml' not in rdf:
            raise ValueError('XML string missing from RDF metadata.')

        tree, root, metadata_elem = SVGMetadataProcessor.__parse(file)

        if metadata_elem is None:
            metadata_elem = ET.SubElement(root, '{%(svg)s}metadata' % XML_NS)
        metadata_elem.append(ET.fromstring(rdf['xml']))

        result = io.BytesIO()
        SVGMetadataProcessor.__register_xml_namespaces()
        tree.write(result, xml_declaration=True, encoding='utf-8')
        result.seek(0)
        return result
<commit_msg>SVGProcessor and SVGMetadataProcessor share code to register XML namespaces.<commit_after>import io
from xml.etree import ElementTree as ET

from madam.core import Asset, MetadataProcessor, Processor, UnsupportedFormatError, operator, OperatorError

_INCH_TO_MM = 1 / 25.4
_PX_PER_INCH = 90
_PT_PER_INCH = 1 / 72
_FONT_SIZE_PT = 12
_X_HEIGHT = 0.7

def svg_length_to_px(length):
    if length is None:
        raise ValueError()

    unit_len = 2
    if length.endswith('%'):
        unit_len = 1
    try:
        value = float(length)
        unit = 'px'
    except ValueError:
        value = float(length[:-unit_len])
        unit = length[-unit_len:]

    if unit == 'em':
        return value * _PX_PER_INCH * _FONT_SIZE_PT * _PT_PER_INCH
    elif unit == 'ex':
        return value * _PX_PER_INCH * _X_HEIGHT * _FONT_SIZE_PT * _PT_PER_INCH
    elif unit == 'px':
        return value
    elif unit == 'in':
        return value * _PX_PER_INCH
    elif unit == 'cm':
        return value * _PX_PER_INCH * _INCH_TO_MM * 10
    elif unit == 'mm':
        return value * _PX_PER_INCH * _INCH_TO_MM
    elif unit == 'pt':
        return value * _PX_PER_INCH * _PT_PER_INCH
    elif unit == 'pc':
        return value * _PX_PER_INCH * _PT_PER_INCH * 12
    elif unit == '%':
        return value

XML_NS = dict(
    svg='http://www.w3.org/2000/svg',
    rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    dc='http://purl.org/dc/elements/1.1/',
)

def _register_xml_namespaces():
    for prefix, uri in XML_NS.items():
        if prefix == 'svg':
            prefix = ''
        ET.register_namespace(prefix, uri)

class SVGProcessor(Processor):
    """
    Represents a processor that handles *Scalable Vector Graphics* (SVG) data.
    """
    def __init__(self):
        """
        Initializes a new `SVGProcessor`.
        """
        super().__init__()

    @staticmethod
    def __parse(file):
        _register_xml_namespaces()
        try:
            tree = ET.parse(file)
        except ET.ParseError as e:
            raise UnsupportedFormatError('Error while parsing XML in line %d, column %d' % e.position)
        root = tree.getroot()
        return tree, root

    def can_read(self, file):
        try:
            SVGProcessor.__parse(file)
            return True
        except UnsupportedFormatError:
            return False

    def read(self, file):
        _, root = SVGProcessor.__parse(file)

        metadata = dict(mime_type='image/svg+xml')
        if 'width' in root.keys():
            metadata['width'] = svg_length_to_px(root.get('width'))
        if 'height' in root.keys():
            metadata['height'] = svg_length_to_px(root.get('height'))

        file.seek(0)
        return Asset(essence=file, **metadata)

    @operator
    def shrink(self, asset):
        """
        Shrinks the size of an SVG asset.

        :param asset: Media asset to be shrunk
        :type asset: Asset
        :return: Shrunk vector asset
        """
        tree, root = SVGProcessor.__parse(asset.essence)

        xml_result = io.BytesIO()
        tree.write(xml_result, xml_declaration=True, encoding='utf-8')
        xml_result.seek(0)

        metadata = dict(mime_type='image/svg+xml')
        for metadata_key in ('width', 'height'):
            if metadata_key in asset.metadata:
                metadata[metadata_key] = asset.metadata[metadata_key]

        return Asset(essence=xml_result, **metadata)

class SVGMetadataProcessor(MetadataProcessor):
    """
    Represents a metadata processor that handles Scalable Vector Graphics (SVG)
    data.

    It is assumed that the SVG XML uses UTF-8 encoding.
    """
    def __init__(self):
        """
        Initializes a new `SVGMetadataProcessor`.
        """
        super().__init__()

    @property
    def formats(self):
        return {'rdf'}

    @staticmethod
    def __parse(file):
        _register_xml_namespaces()
        try:
            tree = ET.parse(file)
        except ET.ParseError as e:
            raise UnsupportedFormatError('Error while parsing XML in line %d, column %d' % e.position)
        root = tree.getroot()
        return tree, root, root.find('./svg:metadata', XML_NS)

    def read(self, file):
        _, _, metadata_elem = SVGMetadataProcessor.__parse(file)
        if metadata_elem is None or len(metadata_elem) == 0:
            return {'rdf': {}}
        return {'rdf': {'xml': ET.tostring(metadata_elem[0], encoding='unicode')}}

    def strip(self, file):
        tree, root, metadata_elem = SVGMetadataProcessor.__parse(file)

        if metadata_elem is not None:
            root.remove(metadata_elem)

        result = io.BytesIO()
        tree.write(result, xml_declaration=True, encoding='utf-8')
        result.seek(0)
        return result

    def combine(self, file, metadata):
        if not metadata:
            raise ValueError('No metadata provided.')
        if 'rdf' not in metadata:
            raise UnsupportedFormatError('No RDF metadata found.')
        rdf = metadata['rdf']
        if 'xml' not in rdf:
            raise ValueError('XML string missing from RDF metadata.')

        tree, root, metadata_elem = SVGMetadataProcessor.__parse(file)

        if metadata_elem is None:
            metadata_elem = ET.SubElement(root, '{%(svg)s}metadata' % XML_NS)
        metadata_elem.append(ET.fromstring(rdf['xml']))

        result = io.BytesIO()
        tree.write(result, xml_declaration=True, encoding='utf-8')
        result.seek(0)
        return result
<|endoftext|>


<commit_before>#!/usr/bin/env python
#########################################################################################
#
# Test function sct_propseg
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2014 Polytechnique Montreal <www.neuro.polymtl.ca>
# Author: Augustin Roux
# modified: 2014/10/09
#
# About the license: see the file LICENSE.TXT
#########################################################################################

#import sct_utils as sct
import commands
import shutil
import getopt
import sys
import time
import sct_utils as sct
import os
import nibabel
import numpy as np
import math
from tabulate import tabulate

class param:
    def __init__(self):
        self.download = 0
        self.remove_tmp_file = 0
        self.verbose = 1
        self.url_git = 'https://github.com/benjamindeleener/PropSeg_data.git'
        self.path_data = '/home/django/benjamindeleener/data/PropSeg_data/'

def main():
    # Check input parameters
    try:
        opts, args = getopt.getopt(sys.argv[1:],'h:d:p:f:r:a:')
    except getopt.GetoptError:
        usage()
    for opt, arg in opts:
        if opt == '-h':
            usage()
            sys.exit(0)
        if opt == '-d':
            param.download = int(arg)
        if opt == '-p':
            param.path_data = arg
        if opt == '-r':
            param.remove_tmp_file = int(arg)

    start_time = time.time()

    # download data
    if param.download:
        sct.printv('\nDownloading testing data...', param.verbose)
        # remove data folder if exist
        if os.path.exists('PropSeg_data'):
            sct.printv('WARNING: PropSeg_data already exists. Removing it...', param.verbose, 'warning')
            sct.run('rm -rf PropSeg_data')
        # clone git repos
        sct.run('git clone '+param.url_git)
        # update path_data field
        param.path_data = 'PropSeg_data'

    # get absolute path and add slash at the end
    param.path_data = sct.slash_at_the_end(os.path.abspath(param.path_data), 1)

    # segment all data in t2 folder
    results_t2 = []
    for dirname in os.listdir(param.path_data+"t2/"):
        if dirname not in ['._.DS_Store','.DS_Store']:
            for filename in os.listdir(param.path_data+"t2/"+dirname):
                if filename.startswith('t2_') and not filename.endswith('_seg.nii.gz') and not filename.endswith('_detection.nii.gz'):
                    [d_old,d_new],[r_old,r_new] = segmentation(param.path_data+"t2/"+dirname+"/"+filename,param.path_data+"t2/"+dirname+"/",'t2')
                    if d_old == 0: d_old = 'OK'
                    else: d_old = 'Not In'
                    if d_new == 0: d_new = 'OK'
                    else: d_new = 'Not In'
                    results_t2.append([dirname,d_old,d_new,round(r_old,2),round(r_new,2)])

    # Print results
    print ''
    print tabulate(results_t2, headers=["Subject-T2","Detect-old","Detect-new","DC-old", "DC-new"], floatfmt=".2f")

    # display elapsed time
    elapsed_time = time.time() - start_time
    print 'Finished! Elapsed time: '+str(int(round(elapsed_time)))+'s\n'

    # remove temp files
    if param.remove_tmp_file:
        sct.printv('\nRemove temporary files...', param.verbose)
        sct.run('rm -rf '+param.path_tmp, param.verbose)

    e = 0
    for i in range(0,len(results_t2)):
        if (results_t2[i][4] < 0.8 or results_t2[i][4] < results_t2[i][3]):
            e = e+1

    sys.exit(e)

def segmentation(fname_input, output_dir, image_type):
    # parameters
    path_in, file_in, ext_in = sct.extract_fname(fname_input)
    segmentation_filename_old = path_in + 'old/' + file_in + '_seg' + ext_in
    manual_segmentation_filename_old = path_in + 'manual_' + file_in + ext_in
    detection_filename_old = path_in + 'old/' + file_in + '_detection' + ext_in
    segmentation_filename_new = path_in + 'new/' + file_in + '_seg' + ext_in
    manual_segmentation_filename_new = path_in + 'manual_' + file_in + ext_in
    detection_filename_new = path_in + 'new/' + file_in + '_detection' + ext_in

    # initialize results of segmentation and detection
    results_detection = [0,0]
    results_segmentation = [0.0,0.0]

    # perform PropSeg old version
    sct.create_folder(output_dir+'old')
    cmd = 'sct_propseg_old -i ' + fname_input \
        + ' -o ' + output_dir+'old' \
        + ' -t ' + image_type \
        + ' -detect-nii'
    status_propseg_old, output_propseg_old = commands.getstatusoutput(cmd)

    # check if spinal cord is correctly detected with old version of PropSeg
    status_detection_old, output_detection_old = commands.getstatusoutput("isct_check_detection.py -i "+detection_filename_old+" -t "+manual_segmentation_filename_old)
    results_detection[0] = status_detection_old

    # compute Dice coefficient for old version of PropSeg
    cmd_validation = 'sct_dice_coefficient '+segmentation_filename_old \
                + ' '+manual_segmentation_filename_old \
                + ' -bzmax'
    status_validation_old, output_validation_old = commands.getstatusoutput(cmd_validation)
    results_segmentation[0] = float(output_validation_old.split()[-1])
    if math.isnan(results_segmentation[0]):
        results_segmentation[0] = 0.0

    # perform PropSeg new version
    sct.create_folder(output_dir+'new')
    cmd = 'sct_propseg -i ' + fname_input \
        + ' -o ' + output_dir+'new' \
        + ' -t ' + image_type \
        + ' -detect-nii'
    status_propseg_new, output_propseg_new = commands.getstatusoutput(cmd)

    # check if spinal cord is correctly detected with new version of PropSeg
    status_detection_new, output_detection_new = commands.getstatusoutput("isct_check_detection.py -i "+detection_filename_new+" -t "+manual_segmentation_filename_new)
    results_detection[1] = status_detection_new

    # compute Dice coefficient for new version of PropSeg
    cmd_validation = 'sct_dice_coefficient '+segmentation_filename_new \
                + ' '+manual_segmentation_filename_new \
                + ' -bzmax'
    status_validation_new, output_validation_new = commands.getstatusoutput(cmd_validation)
    results_segmentation[1] = float(output_validation_new.split()[-1])
    if math.isnan(results_segmentation[1]):
        results_segmentation[1] = 0.0

    return results_detection, results_segmentation

if __name__ == "__main__":
    # call main function
    param = param()
    main()<commit_msg>BUG: fixed bug on float casting<commit_after>#!/usr/bin/env python
#########################################################################################
#
# Test function sct_propseg
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2014 Polytechnique Montreal <www.neuro.polymtl.ca>
# Author: Augustin Roux
# modified: 2014/10/09
#
# About the license: see the file LICENSE.TXT
#########################################################################################

#import sct_utils as sct
import commands
import shutil
import getopt
import sys
import time
import sct_utils as sct
import os
import nibabel
import numpy as np
import math
from tabulate import tabulate

class param:
    def __init__(self):
        self.download = 0
        self.remove_tmp_file = 0
        self.verbose = 1
        self.url_git = 'https://github.com/benjamindeleener/PropSeg_data.git'
        self.path_data = '/home/django/benjamindeleener/data/PropSeg_data/'

def main():
    # Check input parameters
    try:
        opts, args = getopt.getopt(sys.argv[1:],'h:d:p:f:r:a:')
    except getopt.GetoptError:
        usage()
    for opt, arg in opts:
        if opt == '-h':
            usage()
            sys.exit(0)
        if opt == '-d':
            param.download = int(arg)
        if opt == '-p':
            param.path_data = arg
        if opt == '-r':
            param.remove_tmp_file = int(arg)

    start_time = time.time()

    # download data
    if param.download:
        sct.printv('\nDownloading testing data...', param.verbose)
        # remove data folder if exist
        if os.path.exists('PropSeg_data'):
            sct.printv('WARNING: PropSeg_data already exists. Removing it...', param.verbose, 'warning')
            sct.run('rm -rf PropSeg_data')
        # clone git repos
        sct.run('git clone '+param.url_git)
        # update path_data field
        param.path_data = 'PropSeg_data'

    # get absolute path and add slash at the end
    param.path_data = sct.slash_at_the_end(os.path.abspath(param.path_data), 1)

    # segment all data in t2 folder
    results_t2 = []
    for dirname in os.listdir(param.path_data+"t2/"):
        if dirname not in ['._.DS_Store','.DS_Store']:
            for filename in os.listdir(param.path_data+"t2/"+dirname):
                if filename.startswith('t2_') and not filename.endswith('_seg.nii.gz') and not filename.endswith('_detection.nii.gz'):
                    [d_old,d_new],[r_old,r_new] = segmentation(param.path_data+"t2/"+dirname+"/"+filename,param.path_data+"t2/"+dirname+"/",'t2')
                    if d_old == 0: d_old = 'OK'
                    else: d_old = 'Not In'
                    if d_new == 0: d_new = 'OK'
                    else: d_new = 'Not In'
                    results_t2.append([dirname,d_old,d_new,round(r_old,2),round(r_new,2)])

    # Print results
    print ''
    print tabulate(results_t2, headers=["Subject-T2","Detect-old","Detect-new","DC-old", "DC-new"], floatfmt=".2f")

    # display elapsed time
    elapsed_time = time.time() - start_time
    print 'Finished! Elapsed time: '+str(int(round(elapsed_time)))+'s\n'

    # remove temp files
    if param.remove_tmp_file:
        sct.printv('\nRemove temporary files...', param.verbose)
        sct.run('rm -rf '+param.path_tmp, param.verbose)

    e = 0
    for i in range(0,len(results_t2)):
        if (results_t2[i][4] < 0.8 or results_t2[i][4] < results_t2[i][3]):
            e = e+1

    sys.exit(e)

def segmentation(fname_input, output_dir, image_type):
    # parameters
    path_in, file_in, ext_in = sct.extract_fname(fname_input)
    segmentation_filename_old = path_in + 'old/' + file_in + '_seg' + ext_in
    manual_segmentation_filename_old = path_in + 'manual_' + file_in + ext_in
    detection_filename_old = path_in + 'old/' + file_in + '_detection' + ext_in
    segmentation_filename_new = path_in + 'new/' + file_in + '_seg' + ext_in
    manual_segmentation_filename_new = path_in + 'manual_' + file_in + ext_in
    detection_filename_new = path_in + 'new/' + file_in + '_detection' + ext_in

    # initialize results of segmentation and detection
    results_detection = [0,0]
    results_segmentation = [0.0,0.0]

    # perform PropSeg old version
    sct.create_folder(output_dir+'old')
    cmd = 'sct_propseg_old -i ' + fname_input \
        + ' -o ' + output_dir+'old' \
        + ' -t ' + image_type \
        + ' -detect-nii'
    sct.printv(cmd)
    status_propseg_old, output_propseg_old = commands.getstatusoutput(cmd)

    # check if spinal cord is correctly detected with old version of PropSeg
    cmd = "isct_check_detection.py -i "+detection_filename_old+" -t "+manual_segmentation_filename_old
    sct.printv(cmd)
    status_detection_old, output_detection_old = commands.getstatusoutput(cmd)
    results_detection[0] = status_detection_old

    # compute Dice coefficient for old version of PropSeg
    cmd_validation = 'sct_dice_coefficient '+segmentation_filename_old \
                + ' '+manual_segmentation_filename_old \
                + ' -bzmax'
    sct.printv(cmd_validation)
    status_validation_old, output_validation_old = commands.getstatusoutput(cmd_validation)
    print output_validation_old
    res = output_validation_old.split()[-1]
    if res != 'nan': results_segmentation[0] = float(res)
    else: results_segmentation[0] = 0.0

    # perform PropSeg new version
    sct.create_folder(output_dir+'new')
    cmd = 'sct_propseg -i ' + fname_input \
        + ' -o ' + output_dir+'new' \
        + ' -t ' + image_type \
        + ' -detect-nii'
    sct.printv(cmd)
    status_propseg_new, output_propseg_new = commands.getstatusoutput(cmd)

    # check if spinal cord is correctly detected with new version of PropSeg
    cmd = "isct_check_detection.py -i "+detection_filename_new+" -t "+manual_segmentation_filename_new
    sct.printv(cmd)
    status_detection_new, output_detection_new = commands.getstatusoutput(cmd)
    results_detection[1] = status_detection_new

    # compute Dice coefficient for new version of PropSeg
    cmd_validation = 'sct_dice_coefficient '+segmentation_filename_new \
                + ' '+manual_segmentation_filename_new \
                + ' -bzmax'
    sct.printv(cmd_validation)
    status_validation_new, output_validation_new = commands.getstatusoutput(cmd_validation)
    print output_validation_new
    res = output_validation_new.split()[-1]
    if res != 'nan': results_segmentation[1] = float(res)
    else: results_segmentation[1] = 0.0

    return results_detection, results_segmentation

if __name__ == "__main__":
    # call main function
    param = param()
    main()<|endoftext|>


<commit_before>"""Wrapper around Pandoc. Used by builder."""

from __future__ import print_function

import sys
import subprocess
from . import gitutils

PANDOC_PATH = "pandoc"

FROM_PARAMS = "-f markdown_strict+simple_tables+multiline_tables+grid_tables+\
pipe_tables+table_captions+fenced_code_blocks+line_blocks+definition_lists+\
all_symbols_escapable+strikeout+superscript+subscript+\
lists_without_preceding_blankline+implicit_figures+raw_tex+citations+\
tex_math_dollars+header_attributes+auto_identifiers+startnum+footnotes+\
inline_notes+fenced_code_attributes+intraword_underscores+escaped_line_breaks"

LATEX_PARAMS = "--no-tex-ligatures --smart --normalize --listings --number-sections \
--latex-engine=xelatex"

def generate_variable(key, value):
    """Generate a ``--variable key=value`` entry."""

    return '--variable "%s"="%s"' % (key, value)

def generate_command(params, output_file, src_file, cfg, set_template=False):
    """Generate the entire Pandoc command with params to invoke."""

    params = ["-o " + output_file, FROM_PARAMS, LATEX_PARAMS, params]

    for key, value in cfg.items():
        if key in ("title", "second_title", "year", "date", "title_page", "tof", "toc"):
            params.append(generate_variable(key, value))
        elif key == "template":
            if set_template:
                params.append('--template="%s.tex"' % value)
        elif key == "lang":
            if value in ("russian", "english"):
                params.append(generate_variable(value, "true"))
            else:
                params.append(generate_variable("russian", "true"))
        elif key == "version":
            if value == "auto":
                version = gitutils.get_version()
                if version:
                    params.append(generate_variable(key, version))
            else:
                params.append(generate_variable(key, value))
        elif key == "company":
            params.append(generate_variable(key, value))
        elif key in ("type", "alt_doc_type"):
            if value:
                params.append(generate_variable(key, value))
        elif key == "filters":
            for filt in value:
                params.append("-F %s" % filt)
        elif key == "file_name":
            pass
        elif key == "git":
            pass
        else:
            print("Unsupported config key: %s" % key)

    return ' '.join([PANDOC_PATH] + params + [src_file])

def run(command, src_dir):
    """Invoke the Pandoc executable with the generated params."""

    print("Baking output... ", end='')
    sys.stdout.flush()

    try:
        subprocess.check_output(
            command,
            cwd=src_dir,
            stderr=subprocess.PIPE,
            shell=True
        )

        print("Done!")

    except subprocess.CalledProcessError as e:
        quit(e.stderr.decode())

def to_pdf(src_file, output_file, tmp_path, cfg):
    """Convert Markdown to PDF via Pandoc."""

    pandoc_command = generate_command(
        "-t latex",
        output_file,
        src_file,
        cfg,
        set_template=True
    )
    run(pandoc_command, tmp_path)

def to_docx(src_file, output_file, tmp_path, cfg):
    """Convert Markdown to Docx via Pandoc."""

    pandoc_command = generate_command(
        '--reference-docx="ref.docx"',
        output_file,
        src_file,
        cfg
    )
    run(pandoc_command, tmp_path)

def to_odt(src_file, output_file, tmp_path, cfg):
    """Convert Markdown to OpenDocument via Pandoc."""

    pandoc_command = generate_command(
        '--reference-odt="ref.odt"',
        output_file,
        src_file,
        cfg
    )
    run(pandoc_command, tmp_path)

def to_tex(src_file, output_file, tmp_path, cfg):
    """Convert Markdown to TeX via Pandoc."""

    pandoc_command = generate_command(
        "-t latex",
        output_file,
        src_file,
        cfg,
        set_template=True
    )
    run(pandoc_command, tmp_path)
<commit_msg>Pandoc: Add backtick_code_blocks extension.<commit_after>"""Wrapper around Pandoc. Used by builder."""

from __future__ import print_function

import sys
import subprocess
from . import gitutils

PANDOC_PATH = "pandoc"

FROM_PARAMS = "-f markdown_strict+simple_tables+multiline_tables+grid_tables+\
pipe_tables+table_captions+fenced_code_blocks+line_blocks+definition_lists+\
all_symbols_escapable+strikeout+superscript+subscript+\
lists_without_preceding_blankline+implicit_figures+raw_tex+citations+\
tex_math_dollars+header_attributes+auto_identifiers+startnum+footnotes+\
inline_notes+fenced_code_attributes+intraword_underscores+escaped_line_breaks+\
backtick_code_blocks"

LATEX_PARAMS = "--no-tex-ligatures --smart --normalize --listings --number-sections \
--latex-engine=xelatex"

def generate_variable(key, value):
    """Generate a ``--variable key=value`` entry."""

    return '--variable "%s"="%s"' % (key, value)

def generate_command(params, output_file, src_file, cfg, set_template=False):
    """Generate the entire Pandoc command with params to invoke."""

    params = ["-o " + output_file, FROM_PARAMS, LATEX_PARAMS, params]

    for key, value in cfg.items():
        if key in ("title", "second_title", "year", "date", "title_page", "tof", "toc"):
            params.append(generate_variable(key, value))
        elif key == "template":
            if set_template:
                params.append('--template="%s.tex"' % value)
        elif key == "lang":
            if value in ("russian", "english"):
                params.append(generate_variable(value, "true"))
            else:
                params.append(generate_variable("russian", "true"))
        elif key == "version":
            if value == "auto":
                version = gitutils.get_version()
                if version:
                    params.append(generate_variable(key, version))
            else:
                params.append(generate_variable(key, value))
        elif key == "company":
            params.append(generate_variable(key, value))
        elif key in ("type", "alt_doc_type"):
            if value:
                params.append(generate_variable(key, value))
        elif key == "filters":
            for filt in value:
                params.append("-F %s" % filt)
        elif key == "file_name":
            pass
        elif key == "git":
            pass
        else:
            print("Unsupported config key: %s" % key)

    return ' '.join([PANDOC_PATH] + params + [src_file])

def run(command, src_dir):
    """Invoke the Pandoc executable with the generated params."""

    print("Baking output... ", end='')
    sys.stdout.flush()

    try:
        subprocess.check_output(
            command,
            cwd=src_dir,
            stderr=subprocess.PIPE,
            shell=True
        )

        print("Done!")

    except subprocess.CalledProcessError as e:
        quit(e.stderr.decode())

def to_pdf(src_file, output_file, tmp_path, cfg):
    """Convert Markdown to PDF via Pandoc."""

    pandoc_command = generate_command(
        "-t latex",
        output_file,
        src_file,
        cfg,
        set_template=True
    )
    run(pandoc_command, tmp_path)

def to_docx(src_file, output_file, tmp_path, cfg):
    """Convert Markdown to Docx via Pandoc."""

    pandoc_command = generate_command(
        '--reference-docx="ref.docx"',
        output_file,
        src_file,
        cfg
    )
    run(pandoc_command, tmp_path)

def to_odt(src_file, output_file, tmp_path, cfg):
    """Convert Markdown to OpenDocument via Pandoc."""

    pandoc_command = generate_command(
        '--reference-odt="ref.odt"',
        output_file,
        src_file,
        cfg
    )
    run(pandoc_command, tmp_path)

def to_tex(src_file, output_file, tmp_path, cfg):
    """Convert Markdown to TeX via Pandoc."""

    pandoc_command = generate_command(
        "-t latex",
        output_file,
        src_file,
        cfg,
        set_template=True
    )
    run(pandoc_command, tmp_path)
<|endoftext|>


<commit_before>#!/usr/bin/env python
import sys
import os
from optparse import OptionParser

import logging
logging.disable(logging.CRITICAL)

from django.conf import settings, global_settings

if not settings.configured:
    from oscar.defaults import *
    extra_settings = {}
    for key, value in locals().items():
        if key.startswith('OSCAR'):
            extra_settings[key] = value

    from oscar import get_core_apps, OSCAR_MAIN_TEMPLATE_DIR
    from accounts import TEMPLATE_DIR as ACCOUNTS_TEMPLATE_DIR

    settings.configure(
        DATABASES={
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
            }
        },
        STATICFILES_FINDERS=(
            'django.contrib.staticfiles.finders.FileSystemFinder',
            'django.contrib.staticfiles.finders.AppDirectoriesFinder',
            'compressor.finders.CompressorFinder',
        ),
        INSTALLED_APPS=[
            'django.contrib.auth',
            'django.contrib.admin',
            'django.contrib.contenttypes',
            'django.contrib.staticfiles',
            'django.contrib.sessions',
            'django.contrib.sites',
            'django.contrib.flatpages',
            'django.contrib.staticfiles',
            'accounts',
            'south',
            'compressor',
        ] + get_core_apps(),
        MIDDLEWARE_CLASSES=global_settings.MIDDLEWARE_CLASSES + (
            'oscar.apps.basket.middleware.BasketMiddleware',
        ),
        TEMPLATE_CONTEXT_PROCESSORS=global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
            'django.core.context_processors.request',
            'oscar.apps.search.context_processors.search_form',
            'oscar.apps.promotions.context_processors.promotions',
            'oscar.apps.checkout.context_processors.checkout',
            'oscar.core.context_processors.metadata',
        ),
        DEBUG=False,
        SOUTH_TESTS_MIGRATE=False,
        HAYSTACK_CONNECTIONS={
            'default': {
                'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
            },
        },
        ROOT_URLCONF='tests.urls',
        TEMPLATE_DIRS=(
            OSCAR_MAIN_TEMPLATE_DIR,
            os.path.join(OSCAR_MAIN_TEMPLATE_DIR, 'templates'),
            ACCOUNTS_TEMPLATE_DIR,
            # Include sandbox templates as they patch from templates that
            # are in Oscar 0.4 but not 0.3
            'sandbox/templates',
        ),
        STATIC_URL='/static/',
        COMPRESS_ENABLED=False,
        SITE_ID=1,
        ACCOUNTS_UNIT_NAME='Giftcard',
        NOSE_ARGS=['-s', '--with-spec', '-x'],
        USE_TZ=True,
        DDF_FILL_NULLABLE_FIELDS=False,
        ACCOUNTS_DEFERRED_INCOME_ACCOUNT_TYPES=('Test accounts',),
        **extra_settings
    )

from django_nose import NoseTestSuiteRunner

def run_tests(*test_args):
    if 'south' in settings.INSTALLED_APPS:
        from south.management.commands import patch_for_test_db_setup
        patch_for_test_db_setup()

    if not test_args:
        test_args = ['tests']

    # Run tests
    test_runner = NoseTestSuiteRunner(verbosity=1)
    num_failures = test_runner.run_tests(test_args)

    if num_failures > 0:
        sys.exit(num_failures)

def generate_migration():
    from south.management.commands.schemamigration import Command
    com = Command()
    com.handle(app='accounts', auto=True)

if __name__ == '__main__':
    parser = OptionParser()
    (options, args) = parser.parse_args()
    run_tests(*args)
<commit_msg>Add COMPRESS_ROOT to settings<commit_after>#!/usr/bin/env python
import sys
import os
from optparse import OptionParser

import logging
logging.disable(logging.CRITICAL)

from django.conf import settings, global_settings

if not settings.configured:
    from oscar.defaults import *  # noqa
    extra_settings = {}
    for key, value in locals().items():
        if key.startswith('OSCAR'):
            extra_settings[key] = value

    from oscar import get_core_apps, OSCAR_MAIN_TEMPLATE_DIR
    from accounts import TEMPLATE_DIR as ACCOUNTS_TEMPLATE_DIR

    settings.configure(
        DATABASES={
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
            }
        },
        STATICFILES_FINDERS=(
            'django.contrib.staticfiles.finders.FileSystemFinder',
            'django.contrib.staticfiles.finders.AppDirectoriesFinder',
            'compressor.finders.CompressorFinder',
        ),
        INSTALLED_APPS=[
            'django.contrib.auth',
            'django.contrib.admin',
            'django.contrib.contenttypes',
            'django.contrib.staticfiles',
            'django.contrib.sessions',
            'django.contrib.sites',
            'django.contrib.flatpages',
            'django.contrib.staticfiles',
            'accounts',
            'south',
            'compressor',
        ] + get_core_apps(),
        MIDDLEWARE_CLASSES=global_settings.MIDDLEWARE_CLASSES + (
            'oscar.apps.basket.middleware.BasketMiddleware',
        ),
        TEMPLATE_CONTEXT_PROCESSORS=global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
            'django.core.context_processors.request',
            'oscar.apps.search.context_processors.search_form',
            'oscar.apps.promotions.context_processors.promotions',
            'oscar.apps.checkout.context_processors.checkout',
            'oscar.core.context_processors.metadata',
        ),
        DEBUG=False,
        SOUTH_TESTS_MIGRATE=False,
        HAYSTACK_CONNECTIONS={
            'default': {
                'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
            },
        },
        ROOT_URLCONF='tests.urls',
        TEMPLATE_DIRS=(
            OSCAR_MAIN_TEMPLATE_DIR,
            os.path.join(OSCAR_MAIN_TEMPLATE_DIR, 'templates'),
            ACCOUNTS_TEMPLATE_DIR,
            # Include sandbox templates as they patch from templates that
            # are in Oscar 0.4 but not 0.3
            'sandbox/templates',
        ),
        STATIC_URL='/static/',
        COMPRESS_ROOT='',
        COMPRESS_ENABLED=False,
        SITE_ID=1,
        ACCOUNTS_UNIT_NAME='Giftcard',
        NOSE_ARGS=['-s', '--with-spec', '-x'],
        USE_TZ=True,
        DDF_FILL_NULLABLE_FIELDS=False,
        ACCOUNTS_DEFERRED_INCOME_ACCOUNT_TYPES=('Test accounts',),
        **extra_settings
    )

from django_nose import NoseTestSuiteRunner

def run_tests(*test_args):
    if 'south' in settings.INSTALLED_APPS:
        from south.management.commands import patch_for_test_db_setup
        patch_for_test_db_setup()

    if not test_args:
        test_args = ['tests']

    # Run tests
    test_runner = NoseTestSuiteRunner(verbosity=1)
    num_failures = test_runner.run_tests(test_args)

    if num_failures > 0:
        sys.exit(num_failures)

def generate_migration():
    from south.management.commands.schemamigration import Command
    com = Command()
    com.handle(app='accounts', auto=True)

if __name__ == '__main__':
    parser = OptionParser()
    (options, args) = parser.parse_args()  # noqa
    run_tests(*args)
<|endoftext|>


<commit_before>#!/usr/bin/python

"""
created_by:         Micah Halter
created_date:       2/28/2015
last_modified_by:   Aninda Manocha
last_modified date: 3/2/2015
"""

# imports
import constants
import mysql.connector
from mysql_connect_config import getConfig

# classes
class Question:
    'Question object to hold attributes and functions for a question'

    def __init__(self, id, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, topic_list):
        """
        self             - the question in question
        id               - the id number of the question 'self' in the database
        created          - the date when the question 'self' was created
        created_by       - the user that created the question 'self'
        language         - the programming language that the question 'self' was written for
        type             - the type of question 'self' is
        difficulty       - the difficulty of the question 'self'
        prev_question_id - the id number of a previous version of the question 'self'
        version_number   - the current version number of the question 'self'
        last_given       - the date that the question 'self' was last given
        content          - the content of the question 'self'
        topic_list       - a list of topics that apply to the question 'self'

        this function acts as the constructor to define a new question object
        """
        self.id               = id
        self.created          = created
        self.created_by       = created_by
        self.language         = language
        self.type             = type
        self.difficulty       = difficulty
        self.prev_question_id = prev_question_id
        self.version_number   = version_number
        self.last_given       = last_given
        self.content          = content
        self.topic_list       = topic_list

	def add(self):

		if self.id is not None:
			return

		cnx = mysql.connector.connect(**getConfig())
		cursor = cnx.cursor()

		insert = ("INSERT INTO question (id, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, active) VALUES (%s, '%s', %s, '%s', '%s', '%s', %s, %s, '%s', '%s', %s); SELECT LAST_INSERT_ID();" % (self.id, self.created, self.created_by.id, self.language, self.type, self.difficulty, self.prev_question_id, self.versioin_number, self.last_given, self.content, self.active))

		cursor.execute(insert)

		for (id) in cursor:
			self.id=id

		cnx.commit()
		cursor.close()
		cnx.close()

	def update(self):
		cnx = mysql.connector.connect(**getConfig())
		cursor = cnx.cursor()

		if self.id is not None:
			update = ("UPDATE question SET created = '%s', created_by = %s, language = '%s', type ='%s', difficulty = '%s', prev_question_id = %s, version_number = %s, last_given = '%s', content = '%s';" % (self.created, self.created_by.id, self.language, self.type, self. difficulty, self.prev_question_id, self.version_number, self.last_given, self.content))
			cursor.execute(update)
		cnx.commit()
		cursor.close()
		cnx.close()

	def activate(self, bool):
		cnx = mysql.connector.connect(**getConfig())
		cursor = cnx.cursor()

		if self.active is not None:

			self.active = int(bool)
			active = ("UPDATE question SET active =%s WHERE id =%s;" % (int(bool), self.id))

			cursor.execute(active)

		cnx.commit()
		cursor.close()
		cnx.close()

    @classmethod
    def get(self, search="all", searchCreatedBy= None, searchLanguage = "None", searchType = "None", searchDifficulty = "None", searchContent = "None", testActive = "1"):
		cnx = mysql.connector.connect(**getConfig())
		cursor = cnx.cursor()

		returnList = []
		query = ""
		if search == "all" and searchCreatedBy is None and searchLanguage == "None" and searchType == "None" and searchDifficulty == "None" and searchContent == "None":
			query = "SELECT * FROM question"
		elif searchCreatedBy is not None:
			query = ("SELECT * FROM question WHERE created_by = %s" % (searchCreatedBy))
		elif searchLanguage != "None":
			query = ("SELECT * FROM question WHERE language = '%s'" % (searchLanguage))
		elif searchType != "None":
			query = ("SELECT * FROM question WHERE type = '%s'" % (searchType))
		elif searchDifficulty != "None":
			query = ("SELECT * FROM question WHERE difficulty = '%s'" % (searchDifficulty))
		elif searchContent != "None":
			query = ("SELECT * FROM question WHERE content LIKE '%%%s%%'" % (searchContent))

		query += " AND active=%s;" % (testActive)
		cursor.execute(query)

		for (id, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, active) in cursor:
			user = User.get(created_by)[0]
			newQuestion = Question(id, created, user, language, type, difficulty, prev_question_id, version_number, last_given, content, active)

			if newQuestion not in returnList:
				returnList.append(newJob)
		cnx.commit()
		cursor.close()
		cnx.close()

    def noID(cls, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, topic_list):
        """
        the parameters correspond with the parameters in the constructor above

        returns a new question with the id parameter set as None and the other
        parameters set as they are passed in

        this function acts as a second constructor where you have created a
        question that has not yet been assigned an id from the database
        """
        return cls(None, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, topic_list)

    def __eq__(self, other):
        """
        self  - the question in question
        other - another question that you are comparing 'self' to

        return a boolean telling if the attributes of self and 'other'
        are the same

        this function overrides the way python compares two questions
        and decides if they are equal instead of checking if they are the
        exact same object
        """
        return (
        self.id               == other.id               and
        self.created          == other.created          and
        self.created_by       == other.created_by       and
        self.language         == other.language         and
        self.type             == other.type             and
        self.difficulty       == other.difficulty       and
        self.prev_question_id == other.prev_question_id and
        self.version_number   == other.version_number   and
        self.last_given       == other.last_given       and
        self.content          == other.content          and
        self.topic_list       == other.topic_list)

    def setID(self, id):
        """
        self - the question in question
        id   - the id for the question from the database

        this function allows you to assign an id to the question
        after inserting it into the database
        """
        self.id = id

    def __str__(self):
        """
        self - the question in question

        returns a string that fully describes the question 'self'

        this function allows you to convert the question object into
        a human-readable string for viewing the information in it
        """
        string = ""
        string += "id: "                   + str(self.id)               + "\n"
        string += "created: "              + str(self.created)          + "\n"
        string += "created by: "           + str(self.created_by)       + "\n"
        string += "language: "             +     self.language          + "\n"
        string += "type: "                 +     self.type              + "\n"
        string += "difficulty: "           + str(self.difficulty)       + "\n"
        string += "previous question id: " + str(self.prev_question_id) + "\n"
        string += "version number: "       + str(self.version_number)   + "\n"
        string += "last given: "           + str(self.last_given)       + "\n"
        string += "content: "              +     self.content           + "\n"

        string += "\nTopics:\n"
        for i in self.topic_list:
            string += "\t" + i.name + "\n"

        return string
    def toJson(self):
        data = {
        "id"			: self.id,
        "created"		: self.created,
        "created by"		: self.created_by,
        "language"		: self.language,
        "type"			: self.type,
        "difficulty"		: self.difficulty,
        "previous question id"	: self.prev_question_id,
        "version number"	: self.version_number,
        "last given"		: self.last_given,
        "content"		: self.content,
        "topics"		: self.topic_list
        }
        return json.dumps(data)
<commit_msg>working on question.py<commit_after>#!/usr/bin/python3

"""
created_by:         Micah Halter
created_date:       2/28/2015
last_modified_by:   EZ
last_modified date: 3/4/2015
"""

# imports
import constants
import mysql.connector
from mysql_connect_config import getConfig

# classes
class Question:
    'Question object to hold attributes and functions for a question'

    def __init__(self, id, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, topic_list):
        """
        self             - the question in question
        id               - the id number of the question 'self' in the database
        created          - the date when the question 'self' was created
        created_by       - the user that created the question 'self'
        language         - the programming language that the question 'self' was written for
        type             - the type of question 'self' is
        difficulty       - the difficulty of the question 'self'
        prev_question_id - the id number of a previous version of the question 'self'
        version_number   - the current version number of the question 'self'
        last_given       - the date that the question 'self' was last given
        content          - the content of the question 'self'
        topic_list       - a list of topics that apply to the question 'self'

        this function acts as the constructor to define a new question object
        """
        self.id               = id
        self.created          = created
        self.created_by       = created_by
        self.language         = language
        self.type             = type
        self.difficulty       = difficulty
        self.prev_question_id = prev_question_id
        self.version_number   = version_number
        self.last_given       = last_given
        self.content          = content
        self.topic_list       = topic_list

    def noID(cls, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, topic_list):
        """
        the parameters correspond with the parameters in the constructor above

        returns a new question with the id parameter set as None and the other
        parameters set as they are passed in

        this function acts as a second constructor where you have created a
        question that has not yet been assigned an id from the database
        """
        return cls(None, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, topic_list)

    def __eq__(self, other):
        """
        self  - the question in question
        other - another question that you are comparing 'self' to

        return a boolean telling if the attributes of self and 'other'
        are the same

        this function overrides the way python compares two questions
        and decides if they are equal instead of checking if they are the
        exact same object
        """
        return (
        self.id               == other.id               and
        self.created          == other.created          and
        self.created_by       == other.created_by       and
        self.language         == other.language         and
        self.type             == other.type             and
        self.difficulty       == other.difficulty       and
        self.prev_question_id == other.prev_question_id and
        self.version_number   == other.version_number   and
        self.last_given       == other.last_given       and
        self.content          == other.content          and
        self.topic_list       == other.topic_list)

    def setID(self, id):
        """
        self - the question in question
        id   - the id for the question from the database

        this function allows you to assign an id to the question
        after inserting it into the database
        """
        self.id = id

    def __str__(self):
        """
        self - the question in question

        returns a string that fully describes the question 'self'

        this function allows you to convert the question object into
        a human-readable string for viewing the information in it
        """
        string = ""
        string += "id: "                   + str(self.id)               + "\n"
        string += "created: "              + str(self.created)          + "\n"
        string += "created by: "           + str(self.created_by)       + "\n"
        string += "language: "             +     self.language          + "\n"
        string += "type: "                 +     self.type              + "\n"
        string += "difficulty: "           + str(self.difficulty)       + "\n"
        string += "previous question id: " + str(self.prev_question_id) + "\n"
        string += "version number: "       + str(self.version_number)   + "\n"
        string += "last given: "           + str(self.last_given)       + "\n"
        string += "content: "              +     self.content           + "\n"

        string += "\nTopics:\n"
        for i in self.topic_list:
            string += "\t" + i.name + "\n"

        return string

    def add(self):

        if self.id is not None:
            return

        cnx = mysql.connector.connect(**getConfig())
        cursor = cnx.cursor()

        insert = ("INSERT INTO question (id, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, active) VALUES (%s, '%s', %s, '%s', '%s', '%s', %s, %s, '%s', '%s', %s); SELECT LAST_INSERT_ID();" % (self.id, self.created, self.created_by.id, self.language, self.type, self.difficulty, self.prev_question_id, self.versioin_number, self.last_given, self.content, self.active))

        cursor.execute(insert)

        for (id) in cursor:
            self.id=id

        cnx.commit()
        cursor.close()
        cnx.close()

    @classmethod
    def get(self, search="all", searchCreatedBy= None, searchLanguage = "None", searchType = "None", searchDifficulty = "None", searchContent = "None", testActive = "1"):
        cnx = mysql.connector.connect(**getConfig())
        cursor = cnx.cursor()

        returnList = []
        query = ""
        if search == "all" and searchCreatedBy is None and searchLanguage == "None" and searchType == "None" and searchDifficulty == "None" and searchContent == "None":
            query = "SELECT * FROM question"
        elif searchCreatedBy is not None:
            query = ("SELECT * FROM question WHERE created_by = %s" % (searchCreatedBy))
        elif searchLanguage != "None":
            query = ("SELECT * FROM question WHERE language = '%s'" % (searchLanguage))
        elif searchType != "None":
            query = ("SELECT * FROM question WHERE type = '%s'" % (searchType))
        elif searchDifficulty != "None":
            query = ("SELECT * FROM question WHERE difficulty = '%s'" % (searchDifficulty))
        elif searchContent != "None":
            query = ("SELECT * FROM question WHERE content LIKE '%%%s%%'" % (searchContent))

        query += " AND active=%s;" % (testActive)
        cursor.execute(query)

        for (id, created, created_by, language, type, difficulty, prev_question_id, version_number, last_given, content, active) in cursor:
            user = User.get(created_by)[0]
            newQuestion = Question(id, created, user, language, type, difficulty, prev_question_id, version_number, last_given, content, active)

            if newQuestion not in returnList:
                returnList.append(newJob)
        cnx.commit()
        cursor.close()
        cnx.close()

    def update(self):
        cnx = mysql.connector.connect(**getConfig())
        cursor = cnx.cursor()

        if self.id is not None:
            update = ("UPDATE question SET created = '%s', created_by = %s, language = '%s', type ='%s', difficulty = '%s', prev_question_id = %s, version_number = %s, last_given = '%s', content = '%s';" % (self.created, self.created_by.id, self.language, self.type, self. difficulty, self.prev_question_id, self.version_number, self.last_given, self.content))
            cursor.execute(update)
        cnx.commit()
        cursor.close()
        cnx.close()

    def activate(self, bool):
        cnx = mysql.connector.connect(**getConfig())
        cursor = cnx.cursor()

        if self.active is not None:

            self.active = int(bool)
            active = ("UPDATE question SET active =%s WHERE id =%s;" % (int(bool), self.id))

            cursor.execute(active)

        cnx.commit()
        cursor.close()
        cnx.close()

    def toJson(self):
        data = {
        "id"            : self.id,
        "created"       : self.created,
        "created by"        : self.created_by,
        "language"      : self.language,
        "type"          : self.type,
        "difficulty"        : self.difficulty,
        "previous question id"  : self.prev_question_id,
        "version number"    : self.version_number,
        "last given"        : self.last_given,
        "content"       : self.content,
        "topics"        : self.topic_list
        }
        return json.dumps(data)
<|endoftext|>