<commit_before>""" This module provides a wrapper for API calls related to CloudCompute.

"""
#from slick.client.dns import all_zones
#from slick.utils.memoized import memoized
from slick.utils.core import get_client
from slick.utils.nested_dict import lookup
from SoftLayer import CCIManager, SoftLayerAPIError

#@memoized
def all_instance_options(username):
    """ Retrieves all of the available instance options and caches them.

    Note that this function takes a username as an argument for the express
    purpose of guaranteeing things are memoized on a per-user basis. Otherwise,
    the wrong instance options may be cached in the event that multiple users
    hit the tool simultaneously.
    """

    all_options = get_vm_manager().get_create_options()

    # Prime our results dictionary.
    results = {
        'cpus': _extract_processor_options(all_options),
        'domains': _extract_domain_options(username),
        'datacenter': _extract_datacenter_options(all_options),
        'memory': _extract_memory_options(all_options),
        'os': _extract_os_options(all_options),
    }

    return results

def all_instances(instance_filter=None):
    """ This method will retrieve all CloudCompute instances and return
    them as a list.
    """
    instances = []

    if not instance_filter:
        instance_filter = {}

    for instance in get_vm_manager().list_instances(**instance_filter):
        instances.append(_extract_instance_data(instance))

    return instances

def change_port_speed(instance_id, nic, speed):
    public = True
    if 'eth0' == nic:
        public = False

    try:
        get_vm_manager().change_port_speed(instance_id, public, speed)
        success = True
        message = "Port speed changed. It may take up to a minute for this " \
                  "to take effect"
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def cancel_instance(instance_id):
    """ Wrapper for the CCIManager's cancel_instance() call.

    :param int instance_id: The ID of the CloudCompute instance to cancel.
    """

    try:
        get_vm_manager().cancel_instance(instance_id)
        success = True
        message = 'Cancel instance successful. Please check your email for ' \
                  'more information.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def edit_instance(vm_id, hostname='', domain='', notes='', userdata=''):
    """ This method wraps the CCIManager's edit() call.

    :param string hostname: The new hostname for the VM.
    :param string domain: The new domain for the VM.
    :param string notes: Notes to set on the instance.
    :param string userdata: User data to set on the VM.
    """
    instance = {
        'id': vm_id,
        'hostname': hostname,
        'domain': domain,
        'notes': notes,
        'userdata': userdata,
    }

    try:
        get_vm_manager().edit(**instance)
        success = True
        message = 'Edit instance successful. Please check your email ' \
                  'for more information.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def get_vm_manager():
    return CCIManager(get_client())

def get_instance(instance_id, full_data=False):
    """ Wrapper for the CCIManager's get_instance() call.

    :param int instance_id: The ID of the CloudCompute instance to cancel.
    """
    try:
        instance = get_vm_manager().get_instance(instance_id)
    except SoftLayerAPIError:
        return None

    if not full_data:
        return _extract_instance_data(instance)

    return instance

def launch_instance(hostname, domain, os, cpus, memory, network, tags, datacenter=''):
    """ This method wraps the CCIManager's create_instance() call.

    :param string datacenter: The datacenter in which to spin up the new VM.
    :param string hostname: The hostname for the new VM.
    :param string domain: The domain for the new VM.
    :param string os: The code for the operating system to load onto the VM.
    :param int cpus: The number of CPUs needed for the new VM.
    :param int memory: The amount of memory (in MB) for the new VM.
    :param int network: The speed (in Mbps) for the new VM.
    :param string tags: The tags associated with the new VM.
    """
    instance = {
        'datacenter': datacenter,
        'hostname': hostname,
        'domain': domain,
        'os_code': os,
        'cpus': cpus,
        'memory': memory,
        'nic_speed': network,
        'tags': tags
    }

    try:
        get_vm_manager().create_instance(**instance)
        success = True
        message = 'Create instance successful. Please check your email ' \
                  'for more information.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def reboot_instance(instance_id, soft=True):
    """ Provides a single interface function for rebooting a VM.

    :param int instance_id: The ID of the CloudCompute instance to reboot.
    :param bool soft: Flag to determine if this should be a soft or hard
                      reboot. [Default: true (soft)]
    """
    try:
        vg = get_client()['Virtual_Guest']
        if soft:
            vg.rebootSoft(id=instance_id)
        else:
            vg.rebootHard(id=instance_id)
        success = True
        message = 'Reboot request sent to instance.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def reload_instance(instance_id):
    """ Wrapper for the CCIManager's reload_instance() call.

    :param int instance_id: The ID of the CloudCompute instance to reload.
    """

    try:
        get_vm_manager().reload_instance(instance_id)
        success = True
        message = 'Reload request issued. You will receive an email when ' \
                  'the reload is complete.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def start_vm(instance_id):
    """ Starts a halted virtual machine.

    :param int instance_id: The ID of the virtual machine to start.
    """
    vg_client = get_client()['Virtual_Guest']

    try:
        vg_client.powerOn(id=instance_id)
        success = True
        message = 'Instance is being started.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def stop_vm(instance_id):
    """ Stops a running virtual machine.

    :param int instance_id: The ID of the virtual machine to stop.
    """
    vg_client = get_client()['Virtual_Guest']

    try:
        vg_client.powerOff(id=instance_id)
        success = True
        message = 'Instance is being stopped.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def validate_instance(hostname, domain, os, cpus, memory, network, datacenter=''):
    """ This method wraps the CCIManager's verify_create_instance() call.

    Useful if you want to get a price quote or just check the parameters before
    actually placing an order.

    :param string datacenter: The datacenter in which to spin up the new VM.
    :param string hostname: The hostname for the new VM.
    :param string domain: The domain for the new VM.
    :param string os: The code for the operating system to load onto the VM.
    :param int cpus: The number of CPUs needed for the new VM.
    :param int memory: The amount of memory (in MB) for the new VM.
    :param int network: The speed (in Mbps) for the new VM.
    :returns: Returns a dict containing the order template for a valid order
    """
    instance = {
        'datacenter': datacenter,
        'hostname': hostname,
        'domain': domain,
        'os_code': os,
        'cpus': cpus,
        'memory': memory,
        'nic_speed': network
    }

    return get_vm_manager().verify_create_instance(**instance)

#########################
# Begin private methods #
#########################
def _extract_domain_options(username):
    """ This method creates the list of available zones using the DNS client.

    :param string username: The name of the current user. Used for memoization.
    """
    results = []

#    for zone in all_zones(username):
#        results.append((zone['name'], zone['name']))

    return results

def _extract_datacenter_options(all_options):
    """ This method extracts all available datacenters from the
    all_options dict.

    :param dict all_options: The all_options dictionary produced by
    the get_create_options() API call.

    """
    # Reference for data center codes to give them a prettier name.
    datacenters = {
        'ams01': 'Amsterdam',
        'dal01': 'Dallas (1)',
        'dal05': 'Dallas (5)',
        'sea01': 'Seattle',
        'sjc01': 'San Jose',
        'sng01': 'Singapore',
        'wdc01': 'Washington, D.C.'
    }

    results = []
    # Load and sort all data centers
    for option in all_options['datacenters']:
        name = lookup(option, 'template', 'datacenter', 'name')

        if name and name in datacenters:
            name = datacenters[name]
        results.append((lookup(option, 'template', 'datacenter', 'name'),
                        name))
    results = sorted(results)

    return results

def _extract_instance_data(instance):
    """ This option takes an instance record from the API and extracts
    its useful data into a flattened dictionary.

    :param dict instance: The instance dictionary from the API.
    """
    if instance.get('activeTransaction'):
        active = False
        status = lookup(instance, 'activeTransaction', 'transactionStatus',
                        'friendlyName')

        if not status:
            status = 'Unknown status'
    elif instance['powerState'].get('keyName') == 'HALTED':
        active = False
        status = 'Stopped'
    else:
        active = True
        status = 'Running'

    return_data = {
        'id': instance.get('id', None),
        'hostname': instance.get('hostname'),
        'domain': instance.get('domain'),
        'fqdn': instance.get('fullyQualifiedDomainName', None),
        'datacenter': instance.get('datacenter', {}).get('name', None),
        'public': instance.get('primaryIpAddress', None),
        'private': instance.get('primaryBackendIpAddress', None),
        'cpu': instance.get('maxCpu', None),
        'memory': instance.get('maxMemory', None),
        'active': active,
        'status': status,
        'notes': instance.get('notes'),
        'userdata': instance.get('userdata'),
    }

    os_block = lookup(instance, 'operatingSystem', 'softwareLicense',
                      'softwareDescription')

    if os_block:
        return_data['os'] = os_block['name'] + ' ' + os_block['version']
        return_data['os_code'] = os_block['referenceCode']

    if lookup(instance, 'operatingSystem', 'passwords'):
        usernames = []
        for username in lookup(instance, 'operatingSystem', 'passwords'):
            usernames.append(username['username'])
        return_data['usernames'] = usernames

    if instance.get('networkComponents'):
        network = []
        for comp in instance.get('networkComponents'):
            net = {'status': comp['status'],
                   'speed': comp['speed'],
                   'maxSpeed': comp['maxSpeed'],
                   'name': comp['name'],
                   'port': comp.get('port'),
                   'id': comp['id'],
                   }

            if comp.get('macAddress'):
                net['mac'] = comp.get('macAddress')

            if comp.get('primaryIpAddress'):
                net['ip'] = comp.get('primaryIpAddress')

            if comp.get('primarySubnet'):
                subnet = {'netmask': lookup(comp, 'primarySubnet', 'netmask'),
                          'broadcast': lookup(comp, 'primarySubnet',
                                              'broadcastAddress'),
                          'gateway': lookup(comp, 'primarySubnet', 'gateway'),
                          'network_identifier': lookup(comp, 'primarySubnet',
                                                       'networkIdentifier'),
                }
                net['subnet'] = subnet

            network.append(net)

        return_data['network'] = network

    return return_data

def _extract_memory_options(all_options):
    """ This method extracts all memory options from the all_options dict.

    :param dict all_options: The all_options dictionary produced by
    the get_create_options() API call.

    """
    results = []

    # Load and sort memory options
    for option in all_options['memory']:
        results.append((str(lookup(option, 'template', 'maxMemory')),
                        lookup(option, 'itemPrice', 'item', 'description')))

    return results

def _extract_os_options(all_options):
    """ This method extracts all O/S options from the all_options dict.

    :param dict all_options: The all_options dictionary produced by
    the get_create_options() API call.

    """
    results = []

    # Load and sort available operating systems
    # TODO - Turn this into relational fields
    for option in all_options['operatingSystems']:
        # Remove premium operating systems from the list
        recurring_fee = int(lookup(option, 'itemPrice', 'recurringFee'))
        if not recurring_fee:
            results.append((lookup(option, 'template',
                                   'operatingSystemReferenceCode'),
                            lookup(option, 'itemPrice', 'item',
                                   'description')))

    return sorted(results)

def _extract_processor_options(all_options):
    """ This method extracts all processor options from the all_options dict.

    :param dict all_options: The all_options dictionary produced by
    the get_create_options() API call.

    """
    results = []

    # Load all processor options except for those tied to
    # dedicated account hosts
    for option in all_options['processors']:
        if 'dedicatedAccountHostOnlyFlag' not in option['template'] \
           or not lookup(option, 'template', 'dedicatedAccountHostOnlyFlag'):
            results.append((str(lookup(option, 'template', 'startCpus')),
                            lookup(option, 'itemPrice', 'item', 'description')
                            ))

    return results
<commit_msg>Update manager.py<commit_after>""" This module provides a wrapper for API calls related to CloudCompute.

"""
#from slick.client.dns import all_zones
#from slick.utils.memoized import memoized
from slick.utils.core import get_client
from slick.utils.nested_dict import lookup
from SoftLayer import CCIManager, SoftLayerAPIError

#@memoized
def all_instance_options(username):
    """ Retrieves all of the available instance options and caches them.

    Note that this function takes a username as an argument for the express
    purpose of guaranteeing things are memoized on a per-user basis. Otherwise,
    the wrong instance options may be cached in the event that multiple users
    hit the tool simultaneously.
    """

    all_options = get_vm_manager().get_create_options()

    # Prime our results dictionary.
    results = {
        'cpus': _extract_processor_options(all_options),
        'domains': _extract_domain_options(username),
        'datacenter': _extract_datacenter_options(all_options),
        'memory': _extract_memory_options(all_options),
        'os': _extract_os_options(all_options),
    }

    return results

def all_instances(instance_filter=None):
    """ This method will retrieve all CloudCompute instances and return
    them as a list.
    """
    instances = []

    if not instance_filter:
        instance_filter = {}

    for instance in get_vm_manager().list_instances(**instance_filter):
        instances.append(_extract_instance_data(instance))

    return instances

def change_port_speed(instance_id, nic, speed):
    public = True
    if 'eth0' == nic:
        public = False

    try:
        get_vm_manager().change_port_speed(instance_id, public, speed)
        success = True
        message = "Port speed changed. It may take up to a minute for this " \
                  "to take effect"
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def cancel_instance(instance_id):
    """ Wrapper for the CCIManager's cancel_instance() call.

    :param int instance_id: The ID of the CloudCompute instance to cancel.
    """

    try:
        get_vm_manager().cancel_instance(instance_id)
        success = True
        message = 'Cancel instance successful. Please check your email for ' \
                  'more information.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def edit_instance(vm_id, hostname='', domain='', notes='', userdata=''):
    """ This method wraps the CCIManager's edit() call.

    :param string hostname: The new hostname for the VM.
    :param string domain: The new domain for the VM.
    :param string notes: Notes to set on the instance.
    :param string userdata: User data to set on the VM.
    """
    instance = {
        'id': vm_id,
        'hostname': hostname,
        'domain': domain,
        'notes': notes,
        'userdata': userdata,
    }

    try:
        get_vm_manager().edit(**instance)
        success = True
        message = 'Edit instance successful. Please check your email ' \
                  'for more information.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def get_vm_manager():
    return CCIManager(get_client())

def get_instance(instance_id, full_data=False):
    """ Wrapper for the CCIManager's get_instance() call.

    :param int instance_id: The ID of the CloudCompute instance to cancel.
    """
    try:
        instance = get_vm_manager().get_instance(instance_id)
    except SoftLayerAPIError:
        return None

    if not full_data:
        return _extract_instance_data(instance)

    return instance

def launch_instance(hostname, domain, os, cpus, memory, network, tags='', datacenter=''):
    """ This method wraps the CCIManager's create_instance() call.

    :param string datacenter: The datacenter in which to spin up the new VM.
    :param string hostname: The hostname for the new VM.
    :param string domain: The domain for the new VM.
    :param string os: The code for the operating system to load onto the VM.
    :param int cpus: The number of CPUs needed for the new VM.
    :param int memory: The amount of memory (in MB) for the new VM.
    :param int network: The speed (in Mbps) for the new VM.
    :param string tags: The tags associated with the new VM.
    """
    instance = {
        'datacenter': datacenter,
        'hostname': hostname,
        'domain': domain,
        'os_code': os,
        'cpus': cpus,
        'memory': memory,
        'nic_speed': network,
        'tags': tags
    }

    try:
        get_vm_manager().create_instance(**instance)
        success = True
        message = 'Create instance successful. Please check your email ' \
                  'for more information.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def reboot_instance(instance_id, soft=True):
    """ Provides a single interface function for rebooting a VM.

    :param int instance_id: The ID of the CloudCompute instance to reboot.
    :param bool soft: Flag to determine if this should be a soft or hard
                      reboot. [Default: true (soft)]
    """
    try:
        vg = get_client()['Virtual_Guest']
        if soft:
            vg.rebootSoft(id=instance_id)
        else:
            vg.rebootHard(id=instance_id)
        success = True
        message = 'Reboot request sent to instance.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def reload_instance(instance_id):
    """ Wrapper for the CCIManager's reload_instance() call.

    :param int instance_id: The ID of the CloudCompute instance to reload.
    """

    try:
        get_vm_manager().reload_instance(instance_id)
        success = True
        message = 'Reload request issued. You will receive an email when ' \
                  'the reload is complete.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def start_vm(instance_id):
    """ Starts a halted virtual machine.

    :param int instance_id: The ID of the virtual machine to start.
    """
    vg_client = get_client()['Virtual_Guest']

    try:
        vg_client.powerOn(id=instance_id)
        success = True
        message = 'Instance is being started.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def stop_vm(instance_id):
    """ Stops a running virtual machine.

    :param int instance_id: The ID of the virtual machine to stop.
    """
    vg_client = get_client()['Virtual_Guest']

    try:
        vg_client.powerOff(id=instance_id)
        success = True
        message = 'Instance is being stopped.'
    except SoftLayerAPIError as exception:
        success = False
        message = str(exception.message)

    return (success, message)

def validate_instance(hostname, domain, os, cpus, memory, network, datacenter=''):
    """ This method wraps the CCIManager's verify_create_instance() call.

    Useful if you want to get a price quote or just check the parameters before
    actually placing an order.

    :param string datacenter: The datacenter in which to spin up the new VM.
    :param string hostname: The hostname for the new VM.
    :param string domain: The domain for the new VM.
    :param string os: The code for the operating system to load onto the VM.
    :param int cpus: The number of CPUs needed for the new VM.
    :param int memory: The amount of memory (in MB) for the new VM.
    :param int network: The speed (in Mbps) for the new VM.
    :returns: Returns a dict containing the order template for a valid order
    """
    instance = {
        'datacenter': datacenter,
        'hostname': hostname,
        'domain': domain,
        'os_code': os,
        'cpus': cpus,
        'memory': memory,
        'nic_speed': network
    }

    return get_vm_manager().verify_create_instance(**instance)

#########################
# Begin private methods #
#########################
def _extract_domain_options(username):
    """ This method creates the list of available zones using the DNS client.

    :param string username: The name of the current user. Used for memoization.
    """
    results = []

#    for zone in all_zones(username):
#        results.append((zone['name'], zone['name']))

    return results

def _extract_datacenter_options(all_options):
    """ This method extracts all available datacenters from the
    all_options dict.

    :param dict all_options: The all_options dictionary produced by
    the get_create_options() API call.

    """
    # Reference for data center codes to give them a prettier name.
    datacenters = {
        'ams01': 'Amsterdam',
        'dal01': 'Dallas (1)',
        'dal05': 'Dallas (5)',
        'sea01': 'Seattle',
        'sjc01': 'San Jose',
        'sng01': 'Singapore',
        'wdc01': 'Washington, D.C.'
    }

    results = []
    # Load and sort all data centers
    for option in all_options['datacenters']:
        name = lookup(option, 'template', 'datacenter', 'name')

        if name and name in datacenters:
            name = datacenters[name]
        results.append((lookup(option, 'template', 'datacenter', 'name'),
                        name))
    results = sorted(results)

    return results

def _extract_instance_data(instance):
    """ This option takes an instance record from the API and extracts
    its useful data into a flattened dictionary.

    :param dict instance: The instance dictionary from the API.
    """
    if instance.get('activeTransaction'):
        active = False
        status = lookup(instance, 'activeTransaction', 'transactionStatus',
                        'friendlyName')

        if not status:
            status = 'Unknown status'
    elif instance['powerState'].get('keyName') == 'HALTED':
        active = False
        status = 'Stopped'
    else:
        active = True
        status = 'Running'

    return_data = {
        'id': instance.get('id', None),
        'hostname': instance.get('hostname'),
        'domain': instance.get('domain'),
        'fqdn': instance.get('fullyQualifiedDomainName', None),
        'datacenter': instance.get('datacenter', {}).get('name', None),
        'public': instance.get('primaryIpAddress', None),
        'private': instance.get('primaryBackendIpAddress', None),
        'cpu': instance.get('maxCpu', None),
        'memory': instance.get('maxMemory', None),
        'active': active,
        'status': status,
        'notes': instance.get('notes'),
        'userdata': instance.get('userdata'),
        'tags': instance.get('tags'),
    }

    os_block = lookup(instance, 'operatingSystem', 'softwareLicense',
                      'softwareDescription')

    if os_block:
        return_data['os'] = os_block['name'] + ' ' + os_block['version']
        return_data['os_code'] = os_block['referenceCode']

    if lookup(instance, 'operatingSystem', 'passwords'):
        usernames = []
        for username in lookup(instance, 'operatingSystem', 'passwords'):
            usernames.append(username['username'])
        return_data['usernames'] = usernames

    if instance.get('networkComponents'):
        network = []
        for comp in instance.get('networkComponents'):
            net = {'status': comp['status'],
                   'speed': comp['speed'],
                   'maxSpeed': comp['maxSpeed'],
                   'name': comp['name'],
                   'port': comp.get('port'),
                   'id': comp['id'],
                   }

            if comp.get('macAddress'):
                net['mac'] = comp.get('macAddress')

            if comp.get('primaryIpAddress'):
                net['ip'] = comp.get('primaryIpAddress')

            if comp.get('primarySubnet'):
                subnet = {'netmask': lookup(comp, 'primarySubnet', 'netmask'),
                          'broadcast': lookup(comp, 'primarySubnet',
                                              'broadcastAddress'),
                          'gateway': lookup(comp, 'primarySubnet', 'gateway'),
                          'network_identifier': lookup(comp, 'primarySubnet',
                                                       'networkIdentifier'),
                }
                net['subnet'] = subnet

            network.append(net)

        return_data['network'] = network

    return return_data

def _extract_memory_options(all_options):
    """ This method extracts all memory options from the all_options dict.

    :param dict all_options: The all_options dictionary produced by
    the get_create_options() API call.

    """
    results = []

    # Load and sort memory options
    for option in all_options['memory']:
        results.append((str(lookup(option, 'template', 'maxMemory')),
                        lookup(option, 'itemPrice', 'item', 'description')))

    return results

def _extract_os_options(all_options):
    """ This method extracts all O/S options from the all_options dict.

    :param dict all_options: The all_options dictionary produced by
    the get_create_options() API call.

    """
    results = []

    # Load and sort available operating systems
    # TODO - Turn this into relational fields
    for option in all_options['operatingSystems']:
        # Remove premium operating systems from the list
        recurring_fee = int(lookup(option, 'itemPrice', 'recurringFee'))
        if not recurring_fee:
            results.append((lookup(option, 'template',
                                   'operatingSystemReferenceCode'),
                            lookup(option, 'itemPrice', 'item',
                                   'description')))

    return sorted(results)

def _extract_processor_options(all_options):
    """ This method extracts all processor options from the all_options dict.

    :param dict all_options: The all_options dictionary produced by
    the get_create_options() API call.

    """
    results = []

    # Load all processor options except for those tied to
    # dedicated account hosts
    for option in all_options['processors']:
        if 'dedicatedAccountHostOnlyFlag' not in option['template'] \
           or not lookup(option, 'template', 'dedicatedAccountHostOnlyFlag'):
            results.append((str(lookup(option, 'template', 'startCpus')),
                            lookup(option, 'itemPrice', 'item', 'description')
                            ))

    return results
<|endoftext|>


<commit_before>__author__ = 'Gareth Coles'

import urlparse
import urllib
import urllib2

from kitchen.text.converters import to_unicode

from utils.data import YamlData

from system.command_manager import CommandManager
from system.event_manager import EventManager
from system.plugin import PluginObject

from system.events.general import MessageReceived

from system.protocols.generic.channel import Channel
from system.protocols.generic.user import User

from bs4 import BeautifulSoup

class Plugin(PluginObject):

    channels = None
    commands = None
    events = None

    handlers = {}
    shorteners = {}

    content_types = ["text/html", "text/webviewhtml", "message/rfc822",
                     "text/x-server-parsed-html", "application/xhtml+xml"]

    def setup(self):
        self.logger.debug("Entered setup method.")
        self.channels = YamlData("plugins/urls/channels.yml")
        self.commands = CommandManager.instance()
        self.events = EventManager.instance()

        def message_event_filter(event=MessageReceived):
            target = event.target
            type_ = event.type

            return type_ == "message" \
                or isinstance(target, Channel) \
                or isinstance(target, User)

        self.add_shortener("tinyurl", self.tinyurl)

        self.events.add_callback("MessageReceived", self, self.message_handler,
                                 1, message_event_filter)
        self.commands.register_command("urls", self.urls_command, self,
                                       "urls.manage")
        self.commands.register_command("shorten", self.shorten_command, self,
                                       "urls.shorten")

    def message_handler(self, event=MessageReceived):
        protocol = event.caller
        source = event.source
        target = event.target
        message = event.message

        allowed = self.commands.perm_handler.check("urls.title", source,
                                                   target, protocol)
        # This second check is a hack to check default group, since there is
        # not currently inheritance
        if not allowed:
            allowed = self.commands.perm_handler.check("urls.title", None,
                                                       target, protocol)

        if not allowed:
            return

        # Strip formatting characters if possible
        message_stripped = message
        try:
            message_stripped = event.caller.utils.strip_formatting(message)
        except AttributeError as e:
            pass

        for word in message_stripped.split(" "):
            pos = word.lower().find("http://")
            if pos == -1:
                pos = word.lower().find("https://")
            if pos > -1:
                end = word.lower().find(" ", pos)
                if end > -1:
                    url = word[pos:end]
                else:
                    url = word[pos:]

                title, domain = self.parse_title(url)

                self.logger.debug("Title: %s" % title)

                if isinstance(target, Channel):
                    if not protocol.name in self.channels:
                        with self.channels:
                            self.channels[protocol.name] = {
                                target.name: {"last": url,
                                              "status": "on",
                                              "shortener":
                                              "tinyurl"}
                            }
                    if not target.name in self.channels[protocol.name]:
                        with self.channels:
                            self.channels[protocol.name][target.name] = {
                                "last": url,
                                "status": "on",
                                "shortener":
                                "tinyurl"
                            }
                    else:
                        with self.channels:
                            self.channels[protocol.name][target.name]["last"] \
                                = url
                    if title is None:
                        return

                    if domain is not None and "/" in domain:
                        domain = domain.split("/")[0]
                    if domain is None:
                        target.respond(title)
                    else:
                        target.respond("\"%s\" at %s" % (title, domain))
                elif isinstance(target, User):
                    if title is None:
                        return

                    if domain is not None and "/" in domain:
                        domain = domain.split("/")[0]
                    if domain is None:
                        source.respond(title)
                    else:
                        source.respond("\"%s\" at %s" % (title, domain))
                else:
                    self.logger.warn("Unknown target type: %s [%s]"
                                     % (target, target.__class__))

    def urls_command(self, caller, source, args, protocol):
        if not isinstance(source, Channel):
            caller.respond("This command can only be used in a channel.")
            return
        if len(args) < 2:
            caller.respond("Usage: {CHARS}urls <setting> <value>")
            caller.respond("Operations: set <on/off> - Enable or disable title"
                           " parsing for the current channel")
            caller.respond("            shortener <name> - Set which URL "
                           "shortener to use for the current channel")
            return

        operation = args[0].lower()
        value = args[1].lower()

        if not protocol.name in self.channels:
            with self.channels:
                self.channels[protocol.name] = {
                    source.name: {
                        "status": "on",
                        "last": "",
                        "shortener": "tinyurl"
                    }
                }
        if not source.name in self.channels[protocol.name]:
            with self.channels:
                self.channels[protocol.name][source.name] = {
                    "status": "on",
                    "last": "",
                    "shortener": "tinyurl"
                }

        if operation == "set":
            if not value in ["on", "off"]:
                caller.respond("Usage: {CHARS}urls set <on|off>")
            else:
                with self.channels:
                    self.channels[protocol.name][source.name]["status"] = value
                caller.respond("Title passing for %s turned %s."
                               % (source.name, value))
        elif operation == "shortener":
            if value.lower() in self.shorteners:
                with self.channels:
                    self.channels[protocol.name][source.name]["shortener"] \
                        = value.lower()
                caller.respond("URL shortener for %s set to %s."
                               % (source.name, value))
            else:
                caller.respond("Unknown shortener: %s" % value)
        else:
            caller.respond("Unknown operation: '%s'." % operation)

    def shorten_command(self, caller, source, args, protocol):
        if not isinstance(source, Channel):
            if len(args) == 0:
                caller.respond("Usage: {CHARS}shorten [url]")
                return
            else:
                handler = "tinyurl"
                url = args[1]
                shortened = self.shorten_url(url, handler)

                caller.respond(shortened)
        else:
            if not protocol.name in self.channels \
               or not source.name in self.channels[protocol.name] \
               or not len(self.channels[protocol.name][source.name]["last"]):
                caller.respond("Nobody's pasted a URL here yet!")
                return
            handler = self.channels[protocol.name][source.name]["shortener"]
            if len(handler) == 0:
                with self.channels:
                    self.channels[protocol.name][source.name]["shortener"]\
                        = "tinyurl"
                handler = "tinyurl"
            if handler not in self.shorteners:
                caller.respond("Shortener '%s' not found - please set a new "
                               "one!" % handler)
                return

            url = self.channels[protocol.name][source.name]["last"]

            if len(args) > 0:
                url = args[0]

            shortened = self.shorten_url(url, handler)

            source.respond("%s: %s" % (caller.nickname, shortened))

    def tinyurl(self, url):
        return urllib2.urlopen("http://tinyurl.com/api-create.php?url="
                               + urllib.quote_plus(url)).read()

    def parse_title(self, url, use_handler=True):
        domain = ""
        self.logger.debug("Url: %s" % url)
        try:
            parsed = urlparse.urlparse(url)
            domain = parsed.hostname

            if domain.startswith("www."):
                domain = domain[4:]

            if domain in self.handlers and use_handler:
                try:
                    result = self.handlers[domain](url).decode("UTF-8")
                    if result:
                        return result, None
                except Exception:
                    self.logger.exception("Error running handler, parsing "
                                          "title normally.")

            self.logger.debug("Parsed domain: %s" % domain)

            request = urllib2.Request(url)
            request.add_header('User-agent', 'Mozilla/5.0 (X11; U; Linux i686;'
                                             ' en-US; rv:1.9.0.1) Gecko/200807'
                                             '1615 Fedora/3.0.1-1.fc9-1.fc9 Fi'
                                             'refox/3.0.1')
            response = urllib2.urlopen(request)

            self.logger.debug("Info: %s" % response.info())

            headers = response.info().headers
            new_url = response.geturl()
            if "//" in new_url:
                new_url = new_url.split("//")[1]
            domain = new_url.split("/")[0]

            headers_dict = {}

            for x in headers:
                k, v = x.split(": ")

                headers_dict[k.lower()] = v.strip("\r\n")

            status_code = response.getcode()

            if status_code in [301, 302, 303, 307, 308]:
                return self.parse_title(headers["location"])

            ct = headers_dict["content-type"]
            if ";" in ct:
                ct = ct.split(";")[0]

            self.logger.debug("Content-type: %s" % repr(ct))

            if ct not in self.content_types:
                return None, None

            page = response.read()
            soup = BeautifulSoup(page)
            title = to_unicode(soup.title.string.strip())
            domain = to_unicode(domain)
            return title, domain
        except Exception as e:
            if not str(e).lower() == "not viewing html":
                self.logger.exception("Error parsing title.")
                return str(e), domain
            return None, None

    def shorten_url(self, url, handler):
        if handler in self.shorteners:
            return self.shorteners[handler](url)
        return None

    def add_handler(self, domain, handler):
        if domain.startswith("www."):
            raise ValueError("Domain should not start with 'www.'")
        if domain not in self.handlers:
            #self.logger.debug("Handler registered for '%s': %s"
            #                  % (domain, handler))
            self.handlers[domain] = handler
            return True
        return False

    def add_shortener(self, name, handler):
        if name not in self.shorteners:
            #self.logger.debug("Shortener '%s' registered: %s"
            #                  % (name, handler))
            self.shorteners[name] = handler
            return True
        return False
<commit_msg>URLs - patch for when a header contains more than one colon<commit_after>__author__ = 'Gareth Coles'

import urlparse
import urllib
import urllib2

from kitchen.text.converters import to_unicode

from utils.data import YamlData

from system.command_manager import CommandManager
from system.event_manager import EventManager
from system.plugin import PluginObject

from system.events.general import MessageReceived

from system.protocols.generic.channel import Channel
from system.protocols.generic.user import User

from bs4 import BeautifulSoup

class Plugin(PluginObject):

    channels = None
    commands = None
    events = None

    handlers = {}
    shorteners = {}

    content_types = ["text/html", "text/webviewhtml", "message/rfc822",
                     "text/x-server-parsed-html", "application/xhtml+xml"]

    def setup(self):
        self.logger.debug("Entered setup method.")
        self.channels = YamlData("plugins/urls/channels.yml")
        self.commands = CommandManager.instance()
        self.events = EventManager.instance()

        def message_event_filter(event=MessageReceived):
            target = event.target
            type_ = event.type

            return type_ == "message" \
                or isinstance(target, Channel) \
                or isinstance(target, User)

        self.add_shortener("tinyurl", self.tinyurl)

        self.events.add_callback("MessageReceived", self, self.message_handler,
                                 1, message_event_filter)
        self.commands.register_command("urls", self.urls_command, self,
                                       "urls.manage")
        self.commands.register_command("shorten", self.shorten_command, self,
                                       "urls.shorten")

    def message_handler(self, event=MessageReceived):
        protocol = event.caller
        source = event.source
        target = event.target
        message = event.message

        allowed = self.commands.perm_handler.check("urls.title", source,
                                                   target, protocol)
        # This second check is a hack to check default group, since there is
        # not currently inheritance
        if not allowed:
            allowed = self.commands.perm_handler.check("urls.title", None,
                                                       target, protocol)

        if not allowed:
            return

        # Strip formatting characters if possible
        message_stripped = message
        try:
            message_stripped = event.caller.utils.strip_formatting(message)
        except AttributeError as e:
            pass

        for word in message_stripped.split(" "):
            pos = word.lower().find("http://")
            if pos == -1:
                pos = word.lower().find("https://")
            if pos > -1:
                end = word.lower().find(" ", pos)
                if end > -1:
                    url = word[pos:end]
                else:
                    url = word[pos:]

                title, domain = self.parse_title(url)

                self.logger.debug("Title: %s" % title)

                if isinstance(target, Channel):
                    if not protocol.name in self.channels:
                        with self.channels:
                            self.channels[protocol.name] = {
                                target.name: {"last": url,
                                              "status": "on",
                                              "shortener":
                                              "tinyurl"}
                            }
                    if not target.name in self.channels[protocol.name]:
                        with self.channels:
                            self.channels[protocol.name][target.name] = {
                                "last": url,
                                "status": "on",
                                "shortener":
                                "tinyurl"
                            }
                    else:
                        with self.channels:
                            self.channels[protocol.name][target.name]["last"] \
                                = url
                    if title is None:
                        return

                    if domain is not None and "/" in domain:
                        domain = domain.split("/")[0]
                    if domain is None:
                        target.respond(title)
                    else:
                        target.respond("\"%s\" at %s" % (title, domain))
                elif isinstance(target, User):
                    if title is None:
                        return

                    if domain is not None and "/" in domain:
                        domain = domain.split("/")[0]
                    if domain is None:
                        source.respond(title)
                    else:
                        source.respond("\"%s\" at %s" % (title, domain))
                else:
                    self.logger.warn("Unknown target type: %s [%s]"
                                     % (target, target.__class__))

    def urls_command(self, caller, source, args, protocol):
        if not isinstance(source, Channel):
            caller.respond("This command can only be used in a channel.")
            return
        if len(args) < 2:
            caller.respond("Usage: {CHARS}urls <setting> <value>")
            caller.respond("Operations: set <on/off> - Enable or disable title"
                           " parsing for the current channel")
            caller.respond("            shortener <name> - Set which URL "
                           "shortener to use for the current channel")
            return

        operation = args[0].lower()
        value = args[1].lower()

        if not protocol.name in self.channels:
            with self.channels:
                self.channels[protocol.name] = {
                    source.name: {
                        "status": "on",
                        "last": "",
                        "shortener": "tinyurl"
                    }
                }
        if not source.name in self.channels[protocol.name]:
            with self.channels:
                self.channels[protocol.name][source.name] = {
                    "status": "on",
                    "last": "",
                    "shortener": "tinyurl"
                }

        if operation == "set":
            if not value in ["on", "off"]:
                caller.respond("Usage: {CHARS}urls set <on|off>")
            else:
                with self.channels:
                    self.channels[protocol.name][source.name]["status"] = value
                caller.respond("Title passing for %s turned %s."
                               % (source.name, value))
        elif operation == "shortener":
            if value.lower() in self.shorteners:
                with self.channels:
                    self.channels[protocol.name][source.name]["shortener"] \
                        = value.lower()
                caller.respond("URL shortener for %s set to %s."
                               % (source.name, value))
            else:
                caller.respond("Unknown shortener: %s" % value)
        else:
            caller.respond("Unknown operation: '%s'." % operation)

    def shorten_command(self, caller, source, args, protocol):
        if not isinstance(source, Channel):
            if len(args) == 0:
                caller.respond("Usage: {CHARS}shorten [url]")
                return
            else:
                handler = "tinyurl"
                url = args[1]
                shortened = self.shorten_url(url, handler)

                caller.respond(shortened)
        else:
            if not protocol.name in self.channels \
               or not source.name in self.channels[protocol.name] \
               or not len(self.channels[protocol.name][source.name]["last"]):
                caller.respond("Nobody's pasted a URL here yet!")
                return
            handler = self.channels[protocol.name][source.name]["shortener"]
            if len(handler) == 0:
                with self.channels:
                    self.channels[protocol.name][source.name]["shortener"]\
                        = "tinyurl"
                handler = "tinyurl"
            if handler not in self.shorteners:
                caller.respond("Shortener '%s' not found - please set a new "
                               "one!" % handler)
                return

            url = self.channels[protocol.name][source.name]["last"]

            if len(args) > 0:
                url = args[0]

            shortened = self.shorten_url(url, handler)

            source.respond("%s: %s" % (caller.nickname, shortened))

    def tinyurl(self, url):
        return urllib2.urlopen("http://tinyurl.com/api-create.php?url="
                               + urllib.quote_plus(url)).read()

    def parse_title(self, url, use_handler=True):
        domain = ""
        self.logger.debug("Url: %s" % url)
        try:
            parsed = urlparse.urlparse(url)
            domain = parsed.hostname

            if domain.startswith("www."):
                domain = domain[4:]

            if domain in self.handlers and use_handler:
                try:
                    result = self.handlers[domain](url).decode("UTF-8")
                    if result:
                        return result, None
                except Exception:
                    self.logger.exception("Error running handler, parsing "
                                          "title normally.")

            self.logger.debug("Parsed domain: %s" % domain)

            request = urllib2.Request(url)
            request.add_header('User-agent', 'Mozilla/5.0 (X11; U; Linux i686;'
                                             ' en-US; rv:1.9.0.1) Gecko/200807'
                                             '1615 Fedora/3.0.1-1.fc9-1.fc9 Fi'
                                             'refox/3.0.1')
            response = urllib2.urlopen(request)

            self.logger.debug("Info: %s" % response.info())

            headers = response.info().headers
            new_url = response.geturl()
            if "//" in new_url:
                new_url = new_url.split("//")[1]
            domain = new_url.split("/")[0]

            headers_dict = {}

            for x in headers:
                split = x.split(": ")
                k, v = split[0], ": ".join(split[1:])

                headers_dict[k.lower()] = v.strip("\r\n")

            status_code = response.getcode()

            if status_code in [301, 302, 303, 307, 308]:
                return self.parse_title(headers["location"])

            ct = headers_dict["content-type"]
            if ";" in ct:
                ct = ct.split(";")[0]

            self.logger.debug("Content-type: %s" % repr(ct))

            if ct not in self.content_types:
                return None, None

            page = response.read()
            soup = BeautifulSoup(page)
            title = to_unicode(soup.title.string.strip())
            domain = to_unicode(domain)
            return title, domain
        except Exception as e:
            if not str(e).lower() == "not viewing html":
                self.logger.exception("Error parsing title.")
                return str(e), domain
            return None, None

    def shorten_url(self, url, handler):
        if handler in self.shorteners:
            return self.shorteners[handler](url)
        return None

    def add_handler(self, domain, handler):
        if domain.startswith("www."):
            raise ValueError("Domain should not start with 'www.'")
        if domain not in self.handlers:
            #self.logger.debug("Handler registered for '%s': %s"
            #                  % (domain, handler))
            self.handlers[domain] = handler
            return True
        return False

    def add_shortener(self, name, handler):
        if name not in self.shorteners:
            #self.logger.debug("Shortener '%s' registered: %s"
            #                  % (name, handler))
            self.shorteners[name] = handler
            return True
        return False
<|endoftext|>


<commit_before><commit_msg>log panel resizing on window resize<commit_after><|endoftext|>


<commit_before>try:
    from django.views.generic import DateDetailView
except ImportError:
    from cbv import DateDetailView

from menus.utils import set_language_changer
from cmsplugin_blog.models import Entry

class EntryDateDetailView(DateDetailView):

    slug_field = 'entrytitle__slug'
    date_field = 'pub_date'
    month_format = '%m'
    queryset = Entry.objects.all()

    def get_object(self):
        obj = super(EntryDateDetailView, self).get_object()
        set_language_changer(self.request, obj.get_absolute_url)
        return obj

    def get_queryset(self):
        queryset = super(EntryDateDetailView, self).get_queryset()
        if request.user.is_staff:
            return queryset
        else:
            return queryset.published()
<commit_msg>self.request<commit_after>try:
    from django.views.generic import DateDetailView
except ImportError:
    from cbv import DateDetailView

from menus.utils import set_language_changer
from cmsplugin_blog.models import Entry

class EntryDateDetailView(DateDetailView):

    slug_field = 'entrytitle__slug'
    date_field = 'pub_date'
    month_format = '%m'
    queryset = Entry.objects.all()

    def get_object(self):
        obj = super(EntryDateDetailView, self).get_object()
        set_language_changer(self.request, obj.get_absolute_url)
        return obj

    def get_queryset(self):
        queryset = super(EntryDateDetailView, self).get_queryset()
        if self.request.user.is_staff:
            return queryset
        else:
            return queryset.published()
<|endoftext|>


<commit_before>import h5py
from atom.api import Atom, Typed, Str, Bool

import enaml
from enaml.qt.qt_application import QtApplication

import argparse, sys

from instruments.InstrumentManager import InstrumentLibrary
import Sweeps
import MeasFilters
import QGL.Channels
import json
import os
import config
import ExpSettingsVal

class ExpSettings(Atom):

    sweeps = Typed(Sweeps.SweepLibrary)
    instruments = Typed(InstrumentLibrary)
    measurements = Typed(MeasFilters.MeasFilterLibrary)
    channels = Typed(QGL.Channels.ChannelLibrary)
    CWMode = Bool(False)
    curFileName = Str('DefaultExpSettings.json')

    def __init__(self, **kwargs):
        super(ExpSettings, self).__init__(**kwargs)
        self.update_instr_list()

        # setup on change AWG
        self.instruments.AWGs.onChangeDelegate = self.channels.on_awg_change

    # TODO: get this to work
    # @on_trait_change('instruments.instrDict_items')
    def update_instr_list(self):
        if self.sweeps:
            del self.sweeps.possibleInstrs[:]
            for key in self.instruments.instrDict.keys():
                self.sweeps.possibleInstrs.append(key)

    def load_from_file(self, fileName):
        import JSONHelpers
        pass

    def write_to_file(self):
        import JSONHelpers
        with open(self.curFileName,'w') as FID:
            json.dump(self, FID, cls=JSONHelpers.ScripterEncoder, indent=2, sort_keys=True, CWMode=self.CWMode)

    def write_libraries(self):
        """Write all the libraries to their files.

        """

        if ExpSettingsVal.validate_lib():
            self.channels.write_to_file()
            self.instruments.write_to_file()
            self.measurements.write_to_file()
            self.sweeps.write_to_file()
        else:
            print "JSON Files did not validate"
            return False
        return True

    def apply_quickpick(self, name):
        try:
            with open(config.quickpickFile, 'r') as FID:
                quickPicks = json.load(FID)
        except IOError:
            print('No quick pick file found.')
            return

        quickPick = quickPicks[name]

        #Apply sequence name
        if 'seqFile' in quickPick and 'seqDir' in quickPick:
            for awgName in self.instruments.AWGs.displayList:
                self.instruments[awgName].seqFile = os.path.normpath(os.path.join(config.AWGDir, quickPick['seqDir'],
                                 '{}-{}{}'.format(quickPick['seqFile'], awgName, self.instruments[awgName].seqFileExt)))

        #Apply sweep info
        if 'sweeps' in quickPick:
            for sweep in quickPick['sweeps']:
                if sweep in self.sweeps.sweepDict:
                    for k,v in quickPick['sweeps'][sweep].items():
                        setattr(self.sweeps.sweepDict[sweep], k, v)
        if 'sweepOrder' in quickPick:
            self.sweeps.sweepOrder = quickPick['sweepOrder']

        #Setup the digitizer number of segments
        if 'nbrSegments' in quickPick:
            self.instruments['scope'].nbrSegments = quickPick['nbrSegments']

    def json_encode(self, matlabCompatible=True):
        #We encode this for an experiment settings file so no channels
        return {'instruments':self.instruments, 'sweeps':self.sweeps, 'measurements':self.measurements, 'CWMode':self.CWMode}

if __name__ == '__main__':
    import Libraries

    from ExpSettingsGUI import ExpSettings
    expSettings= ExpSettings(sweeps=Libraries.sweepLib, instruments=Libraries.instrumentLib,
                     measurements=Libraries.measLib,  channels=Libraries.channelLib)

    #If we were passed a scripter file to write to then use it
    parser = argparse.ArgumentParser()
    parser.add_argument('--scripterFile', action='store', dest='scripterFile', default=None)
    options =  parser.parse_args(sys.argv[1:])
    if options.scripterFile:
        expSettings.curFileName = options.scripterFile

    with enaml.imports():
        from ExpSettingsView import ExpSettingsView

    app = QtApplication()
    view = ExpSettingsView(expSettings=expSettings)
    view.show()
    app.start()

<commit_msg>Fixed pull conflict after rebase<commit_after>import h5py
from atom.api import Atom, Typed, Str, Bool

import enaml
from enaml.qt.qt_application import QtApplication

import argparse, sys

from instruments.InstrumentManager import InstrumentLibrary
import Sweeps
import MeasFilters
import QGL.Channels
import json
import os
import config
import ExpSettingsVal

class ExpSettings(Atom):

    sweeps = Typed(Sweeps.SweepLibrary)
    instruments = Typed(InstrumentLibrary)
    measurements = Typed(MeasFilters.MeasFilterLibrary)
    channels = Typed(QGL.Channels.ChannelLibrary)
    CWMode = Bool(False)
    curFileName = Str('DefaultExpSettings.json')

    def __init__(self, **kwargs):
        super(ExpSettings, self).__init__(**kwargs)
        self.update_instr_list()

        # setup on change AWG
        self.instruments.AWGs.onChangeDelegate = self.channels.on_awg_change

    # TODO: get this to work
    # @on_trait_change('instruments.instrDict_items')
    def update_instr_list(self):
        if self.sweeps:
            del self.sweeps.possibleInstrs[:]
            for key in self.instruments.instrDict.keys():
                self.sweeps.possibleInstrs.append(key)

    def load_from_file(self, fileName):
        import JSONHelpers
        pass

    def write_to_file(self):
        import JSONHelpers
        with open(self.curFileName,'w') as FID:
            json.dump(self, FID, cls=JSONHelpers.ScripterEncoder, indent=2, sort_keys=True, CWMode=self.CWMode)

    def write_libraries(self):
        """Write all the libraries to their files.

        """

        if ExpSettingsVal.validate_lib():
            self.channels.write_to_file()
            self.instruments.write_to_file()
            self.measurements.write_to_file()
            self.sweeps.write_to_file()
        else:
            print "JSON Files did not validate"
            return False
        return True

    def apply_quickpick(self, name):
        try:
            with open(config.quickpickFile, 'r') as FID:
                quickPicks = json.load(FID)
        except IOError:
            print('No quick pick file found.')
            return

        quickPick = quickPicks[name]

        #Apply sequence name
        if 'seqFile' in quickPick and 'seqDir' in quickPick:
            for awgName in self.instruments.AWGs.displayList:
                self.instruments[awgName].seqFile = os.path.normpath(os.path.join(config.AWGDir, quickPick['seqDir'],
                                 '{}-{}{}'.format(quickPick['seqFile'], awgName, self.instruments[awgName].seqFileExt)))

        #Apply sweep info
        if 'sweeps' in quickPick:
            for sweep in quickPick['sweeps']:
                if sweep in self.sweeps.sweepDict:
                    for k,v in quickPick['sweeps'][sweep].items():
                        setattr(self.sweeps.sweepDict[sweep], k, v)
        if 'sweepOrder' in quickPick:
            self.sweeps.sweepOrder = quickPick['sweepOrder']

        #Setup the digitizer number of segments
        if 'nbrSegments' in quickPick:
            self.instruments['scope'].nbrSegments = quickPick['nbrSegments']

    def json_encode(self, matlabCompatible=True):
        #We encode this for an experiment settings file so no channels
        return {'instruments':self.instruments, 'sweeps':self.sweeps, 'measurements':self.measurements, 'CWMode':self.CWMode}

if __name__ == '__main__':
    import Libraries

    from ExpSettingsGUI import ExpSettings
    expSettings= ExpSettings(sweeps=Libraries.sweepLib, instruments=Libraries.instrumentLib,
                     measurements=Libraries.measLib,  channels=Libraries.channelLib)

    # setup on change AWG
    expSettings.instruments.AWGs.onChangeDelegate = expSettings.channels.on_awg_change

    #If we were passed a scripter file to write to then use it
    parser = argparse.ArgumentParser()
    parser.add_argument('--scripterFile', action='store', dest='scripterFile', default=None)
    options =  parser.parse_args(sys.argv[1:])
    if options.scripterFile:
        expSettings.curFileName = options.scripterFile

    with enaml.imports():
        from ExpSettingsView import ExpSettingsView

    app = QtApplication()
    view = ExpSettingsView(expSettings=expSettings)
    view.show()
    app.start()

<|endoftext|>


<commit_before>from data_importers.management.commands import BaseHalaroseCsvImporter
from django.contrib.gis.geos import Point

class Command(BaseHalaroseCsvImporter):
    council_id = "NTL"
    addresses_name = (
        "2021-03-25T10:36:12.245581/Neath PT polling_station_export-2021-03-23.csv"
    )
    stations_name = (
        "2021-03-25T10:36:12.245581/Neath PT polling_station_export-2021-03-23.csv"
    )
    elections = ["2021-05-06"]
    csv_delimiter = ","

    def station_record_to_dict(self, record):

        if record.pollingstationname == "St. Joseph's R.C Church Hall":
            rec = super().station_record_to_dict(record)
            rec["location"] = Point(-3.800506, 51.6536899, srid=4326)
            return rec

        # Godrergraig Workingmens Club Glanyrafon Road Ystalyfera SA9 2HA
        if (
            record.pollingstationnumber == "22"
            and record.pollingstationpostcode == "SA9 2HA"
        ):
            record = record._replace(pollingstationpostcode="SA9 2DE")

        # Dyffryn Clydach Memorial Hall The Drive Longford Neath
        if record.pollingstationname == "Dyffryn Clydach Memorial Hall":
            record = record._replace(pollingstationpostcode="SA10 7HD")

        # Clyne Community Centre Clyne Resolven
        if record.pollingstationname == "Clyne Community Centre":
            record = record._replace(pollingstationpostcode="SA11 4BP")

        return super().station_record_to_dict(record)

    def address_record_to_dict(self, record):
        uprn = record.uprn.strip().lstrip("0")

        if uprn in [
            "10009182194",  # FFRWD VALE BUNGALOW, DWR Y FELIN ROAD, NEATH
            "100100609132",  # 35B PENYWERN ROAD, NEATH
            "10009177319",  # MAES MELYN BUNGALOW, DRUMMAU ROAD, SKEWEN, NEATH
            "100101040244",  # 11A REGENT STREET EAST, NEATH
            "100100598607",  # 134 SHELONE ROAD, NEATH
            "10023946967",  # 134A SHELONE ROAD, BRITON FERRY
            "100100600419",  # 113 GNOLL PARK ROAD, NEATH
            "100100991905",  # BROOKLYN, TONMAWR ROAD, PONTRHYDYFEN, PORT TALBOT
            "10009184466",  # CILGARN FARM COTTAGE, CWMAVON, PORT TALBOT
            "10009186526",  # TYN Y CAEAU, MARGAM ROAD, MARGAM, PORT TALBOT
            "10014164971",  # FLAT TYN-Y-CAEAU MARGAM ROAD, MARGAM
            "10009184513",  # 1 ALMS HOUSE, MARGAM, PORT TALBOT
        ]:
            return None

        if record.housepostcode in [
            "SA12 8EP",
            "SA10 9DJ",
            "SA10 6DE",
            "SA11 3PW",
            "SA11 1TS",
            "SA11 1TW",
            "SA12 9ST",
            "SA8 4PX",
            "SA11 3QE",
        ]:
            return None

        return super().address_record_to_dict(record)
<commit_msg>Import script for Neath Port Talbot County (2022-05-05) (closes #4171)<commit_after>from django.contrib.gis.geos import Point

from data_importers.management.commands import BaseHalaroseCsvImporter

class Command(BaseHalaroseCsvImporter):
    council_id = "NTL"
    addresses_name = (
        "2022-05-05/2022-02-28T14:59:06.740989/polling_station_export-2022-02-28.csv"
    )
    stations_name = (
        "2022-05-05/2022-02-28T14:59:06.740989/polling_station_export-2022-02-28.csv"
    )
    elections = ["2022-05-05"]

    def station_record_to_dict(self, record):

        if record.pollingstationname == "St. Joseph's R.C Church Hall":
            rec = super().station_record_to_dict(record)
            rec["location"] = Point(-3.800506, 51.6536899, srid=4326)
            return rec

        # Godrergraig Workingmens Club Glanyrafon Road Ystalyfera SA9 2HA
        if (
            record.pollingstationnumber == "72"
            and record.pollingstationpostcode == "SA9 2HA"
        ):
            record = record._replace(pollingstationpostcode="SA9 2DE")

        # Dyffryn Clydach Memorial Hall The Drive Longford Neath
        if record.pollingstationname == "Dyffryn Clydach Memorial Hall":
            record = record._replace(pollingstationpostcode="SA10 7HD")

        # Clyne Community Centre Clyne Resolven
        if record.pollingstationname == "Clyne Community Centre":
            record = record._replace(pollingstationpostcode="SA11 4BP")

        return super().station_record_to_dict(record)

    def address_record_to_dict(self, record):
        if record.housepostcode in [
            "SA10 6DE",
            "SA10 9DJ",
            "SA11 1TS",
            "SA11 1TW",
            "SA11 3PW",
            "SA12 8EP",
            "SA8 4PX",
            "SA8 4TS",
        ]:
            return None

        return super().address_record_to_dict(record)
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: ping
version_added: historical
short_description: Try to connect to host and return C(pong) on success.
description:
   - A trivial test module, this module always returns C(pong) on successful
     contact. It does not make sense in playbooks, but it is useful from
     C(/usr/bin/ansible)
options: {}
author:
    - "Ansible Core Team"
    - "Michael DeHaan"
'''

EXAMPLES = '''
# Test 'webservers' status
ansible webservers -m ping
'''

import exceptions

def main():
    module = AnsibleModule(
        argument_spec = dict(
            data=dict(required=False, default=None),
        ),
        supports_check_mode = True
    )
    result = dict(ping='pong')
    if module.params['data']:
        if module.params['data'] == 'crash':
            raise exceptions.Exception("boom")
        result['ping'] = module.params['data']
    module.exit_json(**result)

from ansible.module_utils.basic import *

main()

<commit_msg>clarified ping module purpose<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: ping
version_added: historical
short_description: Try to connect to host, veryify a usable python and return C(pong) on success.
description:
   - A trivial test module, this module always returns C(pong) on successful
     contact. It does not make sense in playbooks, but it is useful from
     C(/usr/bin/ansible) to verify the ability to login and that a usable python is configured.
   - This is NOT ICMP ping, this is just a trivial test module.
options: {}
author:
    - "Ansible Core Team"
    - "Michael DeHaan"
'''

EXAMPLES = '''
# Test we can logon to 'webservers' and execute python with json lib.
ansible webservers -m ping
'''

import exceptions

def main():
    module = AnsibleModule(
        argument_spec = dict(
            data=dict(required=False, default=None),
        ),
        supports_check_mode = True
    )
    result = dict(ping='pong')
    if module.params['data']:
        if module.params['data'] == 'crash':
            raise exceptions.Exception("boom")
        result['ping'] = module.params['data']
    module.exit_json(**result)

from ansible.module_utils.basic import *

main()

<|endoftext|>


<commit_before># Copyright 2015 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
    "name": "XAF auditfile export",
    "version": "13.0.1.0.0",
    "author": "Therp BV, Odoo Community Association (OCA)",
    "website": "https://github.com/OCA/l10n-netherlands",
    "license": "AGPL-3",
    "category": "Invoices & Payments",
    "summary": "Export XAF auditfiles for Dutch tax authorities",
    "depends": ["account"],
    "data": [
        "security/ir.model.access.csv",
        "views/xaf_auditfile_export.xml",
        "views/templates.xml",
        "views/menu.xml",
    ],
    "installable": True,
}
<commit_msg>l10n_nl_xaf_auditfile_export 13.0.1.0.1<commit_after># Copyright 2015 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
    "name": "XAF auditfile export",
    "version": "13.0.1.0.1",
    "author": "Therp BV, Odoo Community Association (OCA)",
    "website": "https://github.com/OCA/l10n-netherlands",
    "license": "AGPL-3",
    "category": "Invoices & Payments",
    "summary": "Export XAF auditfiles for Dutch tax authorities",
    "depends": ["account"],
    "data": [
        "security/ir.model.access.csv",
        "views/xaf_auditfile_export.xml",
        "views/templates.xml",
        "views/menu.xml",
    ],
    "installable": True,
}
<|endoftext|>


<commit_before># This file is part of the pyMOR project (http://www.pymor.org).
# Copyright Holders: Felix Albrecht, Rene Milk, Stephan Rave
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

from __future__ import absolute_import, division, print_function

import time
from itertools import izip

from pymor.algorithms.basisextension import trivial_basis_extension
from pymor.core import getLogger
from pymor.core.exceptions import ExtensionError

def greedy(discretization, reductor, samples, initial_basis=None, use_estimator=True, error_norm=None,
           extension_algorithm=trivial_basis_extension, target_error=None, max_extensions=None):
    '''Greedy extension algorithm.

    Parameters
    ----------
    discretization
        The discretization to reduce.
    reductor
        Reductor for reducing the given discretization. This has to be a
        function of the form `reduce(discretization, data)` where data is
        the detailed data required by the reductor. If your reductor takes
        more arguments, use functools.partial.
    samples
        The set of parameter samples on which to perform the greedy search.
        Currently this set is fixed for the whole process.
    initial_basis
        The initial reduced basis with which the algorithm starts.
    use_estimator
        If True, use reduced_discretization.estimate() to estimate the errors
        on the sample set. Otherwise a detailed simulation is used to calculate
        the error.
    error_norm
        If use_estimator == Flase, use this function to calculate the norm of
        the error. [Default l2_norm]
    extension_algorithm
        The extension algorithm to use to extend the current reduced basis with
        the maximum error snapshot.
    target_error
        If not None, stop the search if the maximum error on the sample set
        drops below this value.
    max_extensions
        If not None, stop algorithm after `max_extensions` extension steps.

    Returns
    -------
    Dict with the following fields:
        'basis'
            The reduced basis.
        'reduced_discretization'
            The last reduced discretization which has been computed.
        'reconstructor'
            Reconstructor for `reduced_discretization`.
        'max_err'
            Last estimated maximum error on the sample set.
        'max_err_mu'
            The parameter that corresponds to `max_err`.
        'max_errs'
            Sequence of maximum errors during the greedy run.
        'max_errs_mu'
            The parameters corresponding to `max_errs`.
    '''

    logger = getLogger('pymor.algorithms.greedy.greedy')
    samples = list(samples)
    logger.info('Started greedy search on {} samples'.format(len(samples)))
    basis = initial_basis

    tic = time.time()
    extensions = 0
    max_errs = []
    max_err_mus = []
    hierarchic = False

    while True:
        logger.info('Reducing ...')
        rd, rc, reduction_data = reductor(discretization, basis) if not hierarchic \
            else reductor(discretization, basis, extends=(rd, rc, reduction_data))

        logger.info('Estimating errors ...')
        if use_estimator:
            errors = [rd.estimate(rd.solve(mu), mu) for mu in samples]
        elif error_norm is not None:
            errors = [error_norm(discretization.solve(mu) - rc.reconstruct(rd.solve(mu))) for mu in samples]
        else:
            errors = [(discretization.solve(mu) - rc.reconstruct(rd.solve(mu))).l2_norm() for mu in samples]

        # most error_norms will return an array of length 1 instead of a number, so we extract the numbers
        # if necessary
        errors = map(lambda x: x[0] if hasattr(x, '__len__') else x, errors)

        max_err, max_err_mu = max(((err, mu) for err, mu in izip(errors, samples)), key=lambda t: t[0])
        max_errs.append(max_err)
        max_err_mus.append(max_err_mu)
        logger.info('Maximum error after {} extensions: {} (mu = {})'.format(extensions, max_err, max_err_mu))

        if target_error is not None and max_err <= target_error:
            logger.info('Reached maximal error on snapshots of {} <= {}'.format(max_err, target_error))
            break

        logger.info('Extending with snapshot for mu = {}'.format(max_err_mu))
        U = discretization.solve(max_err_mu)
        try:
            basis, extension_data = extension_algorithm(basis, U)
        except ExtensionError:
            logger.info('Extension failed. Stopping now.')
            break
        extensions += 1
        if not 'hierarchic' in extension_data:
            logger.warn('Extension algorithm does not report if extension was hierarchic. Assuming it was\'nt ..')
            hierarchic = False
        else:
            hierarchic = extension_data['hierarchic']

        logger.info('')

        if max_extensions is not None and extensions >= max_extensions:
            logger.info('Maximal number of {} extensions reached.'.format(max_extensions))
            logger.info('Reducing once more ...')
            rd, rc, reduction_data = reductor(discretization, basis) if not hierarchic \
                else reductor(discretization, basis, extends=(rd, rc, reduction_data))
            break

    tictoc = time.time() - tic
    logger.info('Greedy search took {} seconds'.format(tictoc))
    return {'basis': basis, 'reduced_discretization': rd, 'reconstructor': rc, 'max_err': max_err,
            'max_err_mu': max_err_mu, 'max_errs': max_errs, 'max_err_mus': max_err_mus, 'extensions': extensions,
            'time': tictoc, 'reduction_data': reduction_data}
<commit_msg>[algorithms.greedy] update documentation<commit_after># This file is part of the pyMOR project (http://www.pymor.org).
# Copyright Holders: Felix Albrecht, Rene Milk, Stephan Rave
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

from __future__ import absolute_import, division, print_function

import time
from itertools import izip

from pymor.algorithms.basisextension import trivial_basis_extension
from pymor.core import getLogger
from pymor.core.exceptions import ExtensionError

def greedy(discretization, reductor, samples, initial_basis=None, use_estimator=True, error_norm=None,
           extension_algorithm=trivial_basis_extension, target_error=None, max_extensions=None):
    '''Greedy extension algorithm.

    Parameters
    ----------
    discretization
        The discretization to reduce.
    reductor
        Reductor for reducing the given discretization. This has to be a
        function of the form `reduce(discretization, basis)`.
        If your reductor takes more arguments, use functools.partial.
    samples
        The set of parameter samples on which to perform the greedy search.
        Currently this set is fixed for the whole process.
    initial_basis
        The initial reduced basis with which the algorithm starts.
    use_estimator
        If True, use reduced_discretization.estimate() to estimate the errors
        on the sample set. Otherwise a detailed simulation is used to calculate
        the error.
    error_norm
        If use_estimator == Flase, use this function to calculate the norm of
        the error. [Default l2_norm]
    extension_algorithm
        The extension algorithm to use to extend the current reduced basis with
        the maximum error snapshot.
    target_error
        If not None, stop the search if the maximum error on the sample set
        drops below this value.
    max_extensions
        If not None, stop algorithm after `max_extensions` extension steps.

    Returns
    -------
    Dict with the following fields:
        'basis'
            The reduced basis.
        'reduced_discretization'
            The last reduced discretization which has been computed.
        'reconstructor'
            Reconstructor for `reduced_discretization`.
        'max_err'
            Last estimated maximum error on the sample set.
        'max_err_mu'
            The parameter that corresponds to `max_err`.
        'max_errs'
            Sequence of maximum errors during the greedy run.
        'max_errs_mu'
            The parameters corresponding to `max_errs`.
    '''

    logger = getLogger('pymor.algorithms.greedy.greedy')
    samples = list(samples)
    logger.info('Started greedy search on {} samples'.format(len(samples)))
    basis = initial_basis

    tic = time.time()
    extensions = 0
    max_errs = []
    max_err_mus = []
    hierarchic = False

    while True:
        logger.info('Reducing ...')
        rd, rc, reduction_data = reductor(discretization, basis) if not hierarchic \
            else reductor(discretization, basis, extends=(rd, rc, reduction_data))

        logger.info('Estimating errors ...')
        if use_estimator:
            errors = [rd.estimate(rd.solve(mu), mu) for mu in samples]
        elif error_norm is not None:
            errors = [error_norm(discretization.solve(mu) - rc.reconstruct(rd.solve(mu))) for mu in samples]
        else:
            errors = [(discretization.solve(mu) - rc.reconstruct(rd.solve(mu))).l2_norm() for mu in samples]

        # most error_norms will return an array of length 1 instead of a number, so we extract the numbers
        # if necessary
        errors = map(lambda x: x[0] if hasattr(x, '__len__') else x, errors)

        max_err, max_err_mu = max(((err, mu) for err, mu in izip(errors, samples)), key=lambda t: t[0])
        max_errs.append(max_err)
        max_err_mus.append(max_err_mu)
        logger.info('Maximum error after {} extensions: {} (mu = {})'.format(extensions, max_err, max_err_mu))

        if target_error is not None and max_err <= target_error:
            logger.info('Reached maximal error on snapshots of {} <= {}'.format(max_err, target_error))
            break

        logger.info('Extending with snapshot for mu = {}'.format(max_err_mu))
        U = discretization.solve(max_err_mu)
        try:
            basis, extension_data = extension_algorithm(basis, U)
        except ExtensionError:
            logger.info('Extension failed. Stopping now.')
            break
        extensions += 1
        if not 'hierarchic' in extension_data:
            logger.warn('Extension algorithm does not report if extension was hierarchic. Assuming it was\'nt ..')
            hierarchic = False
        else:
            hierarchic = extension_data['hierarchic']

        logger.info('')

        if max_extensions is not None and extensions >= max_extensions:
            logger.info('Maximal number of {} extensions reached.'.format(max_extensions))
            logger.info('Reducing once more ...')
            rd, rc, reduction_data = reductor(discretization, basis) if not hierarchic \
                else reductor(discretization, basis, extends=(rd, rc, reduction_data))
            break

    tictoc = time.time() - tic
    logger.info('Greedy search took {} seconds'.format(tictoc))
    return {'basis': basis, 'reduced_discretization': rd, 'reconstructor': rc, 'max_err': max_err,
            'max_err_mu': max_err_mu, 'max_errs': max_errs, 'max_err_mus': max_err_mus, 'extensions': extensions,
            'time': tictoc, 'reduction_data': reduction_data}
<|endoftext|>


<commit_before>from enum import Enum

__author__ = 'Gareth Coles'

class Priority(Enum):
    MONITOR = -100  # Only really for the website handler
    LATEST = 0
    LATER = 20
    LATE = 40
    EARLY = 60
    EARLIER = 80
    EARLIEST = 100
<commit_msg>Enums aren't orderable<commit_after>from enum import Enum

__author__ = 'Gareth Coles'

class Priority(object):
    MONITOR = -100  # Only really for the website handler
    LATEST = 0
    LATER = 20
    LATE = 40
    EARLY = 60
    EARLIER = 80
    EARLIEST = 100
<|endoftext|>