<commit_before># -*- coding: utf-8 -*-
'''
Mounting of filesystems
=======================

Mount any type of mountable filesystem with the mounted function:

.. code-block:: yaml

    /mnt/sdb:
      mount.mounted:
        - device: /dev/sdb1
        - fstype: ext4
        - mkmnt: True
        - opts:
          - defaults
'''

# Import python libs
import os.path

# Import salt libs
from salt._compat import string_types

def mounted(name,
            device,
            fstype,
            mkmnt=False,
            opts=None,
            dump=0,
            pass_num=0,
            config='/etc/fstab',
            persist=True):
    '''
    Verify that a device is mounted

    name
        The path to the location where the device is to be mounted

    device
        The device name, typically the device node, such as /dev/sdb1

    fstype
        The filesystem type, this will be xfs, ext2/3/4 in the case of classic
        filesystems, and fuse in the case of fuse mounts

    mkmnt
        If the mount point is not present then the state will fail, set mkmnt
        to True to create the mount point if it is otherwise not present

    opts
        A list object of options or a comma delimited list

    dump
        The dump value to be passed into the fstab, default to 0

    pass_num
        The pass value to be passed into the fstab, default to 0

    config
        Set an alternative location for the fstab, default to /etc/fstab

    remount
        Set if the file system can be remounted with the remount option,
        default to True

    persist
        Set if the mount should be saved in the fstab, default to True
    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': ''}

    # Make sure that opts is correct, it can be a list or a comma delimited
    # string
    if isinstance(opts, string_types):
        opts = opts.split(',')
    elif opts is None:
        opts = ['defaults']

    # remove possible trailing slash
    name = name.rstrip("/")

    # Get the active data
    active = __salt__['mount.active']()
    real_name = os.path.realpath(name)
    if device[0:1] == "/":
        real_device = os.path.realpath(device)
    else:
        real_device = device
    device_list = []
    if real_name in active:
        device_list.append(active[real_name]['device'])
        device_list.append(os.path.realpath(device_list[0]))
        if active[real_name]['alt_device'] not in device_list:
            device_list.append(active[real_name]['alt_device'])
        if real_device not in device_list:
            # name matches but device doesn't - need to umount
            ret['changes']['umount'] = "Forced unmount because devices " \
                                       + "don't match. Wanted: " + device
            if real_device != device:
                ret['changes']['umount'] += " (" + real_device + ")"
            ret['changes']['umount'] += ", current: " + ', '.join(device_list)
            out = __salt__['mount.umount'](real_name)
            active = __salt__['mount.active']()
            if real_name in active:
                ret['comment'] = "Unable to unmount"
                ret['result'] = None
                return ret
        else:
            ret['comment'] = 'Target was already mounted'
    # using a duplicate check so I can catch the results of a umount
    if real_name not in active:
        # The mount is not present! Mount it
        if __opts__['test']:
            ret['result'] = None
            ret['comment'] = '{0} would be mounted'.format(name)
            return ret

        out = __salt__['mount.mount'](name, device, mkmnt, fstype, opts)
        active = __salt__['mount.active']()
        if isinstance(out, string_types):
            # Failed to (re)mount, the state has failed!
            ret['comment'] = out
            ret['result'] = False
            return ret
        elif real_name in active:
            # (Re)mount worked!
            ret['comment'] = 'Target was successfully mounted'
            ret['changes']['mount'] = True

    if persist and real_name in active:
        if __opts__['test']:
            out = __salt__['mount.set_fstab'](name,
                                              device,
                                              fstype,
                                              opts,
                                              dump,
                                              pass_num,
                                              config,
                                              test=True)
            if out != 'present':
                ret['result'] = None
                if out == 'new':
                    ret['comment'] = ('{0} is mounted, but needs to be '
                                      'written to the fstab in order to be '
                                      'made persistent').format(name)
                elif out == 'change':
                    ret['comment'] = ('{0} is mounted, but its fstab entry '
                                      'must be updated').format(name)
                else:
                    ret['result'] = False
                    ret['comment'] = ('Unable to detect fstab status for '
                                      'mount point {0} due to unexpected '
                                      'output \'{1}\' from call to '
                                      'mount.set_fstab. This is most likely '
                                      'a bug.').format(name, out)
                return ret

        else:
            out = __salt__['mount.set_fstab'](name,
                                              device,
                                              fstype,
                                              opts,
                                              dump,
                                              pass_num,
                                              config)

        if out == 'present':
            return ret
        if out == 'new':
            ret['changes']['persist'] = 'new'
            ret['comment'] += '. Added new entry to the fstab.'
            return ret
        if out == 'change':
            ret['changes']['persist'] = 'update'
            ret['comment'] += '. Updated the entry in the fstab.'
            return ret
        if out == 'bad config':
            ret['result'] = False
            ret['comment'] += '. However, the fstab was not found.'
            return ret

    return ret

def swap(name, persist=True, config='/etc/fstab'):
    '''
    Activates a swap device

    .. code-block:: yaml

        /root/swapfile:
          mount.swap
    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': ''}
    on_ = __salt__['mount.swaps']()

    if name in on_:
        ret['comment'] = 'Swap {0} already active'.format(name)
    elif __opts__['test']:
        ret['result'] = None
        ret['comment'] = 'Swap {0} is set to be activated'.format(name)
    else:
        __salt__['mount.swapon'](name)

        on_ = __salt__['mount.swaps']()

        if name in on_:
            ret['comment'] = 'Swap {0} activated'.format(name)
            ret['changes'] = on_[name]
        else:
            ret['comment'] = 'Swap {0} failed to activate'.format(name)
            ret['result'] = False

    if persist:
        fstab_data = __salt__['mount.fstab'](config)
        if __opts__['test']:
            if name not in fstab_data:
                ret['result'] = None
                if name in on_:
                    ret['comment'] = ('Swap {0} is set to be added to the '
                                      'fstab and to be activated').format(name)
            return ret

        if 'none' in fstab_data:
            if fstab_data['none']['device'] == name and fstab_data['none']['fstype'] != 'swap':
                return ret

        # present, new, change, bad config
        # Make sure the entry is in the fstab
        out = __salt__['mount.set_fstab']('none',
                                          name,
                                          'swap',
                                          ['defaults'],
                                          0,
                                          0,
                                          config)
        if out == 'present':
            return ret
        if out == 'new':
            ret['changes']['persist'] = 'new'
            ret['comment'] += '. Added new entry to the fstab.'
            return ret
        if out == 'change':
            ret['changes']['persist'] = 'update'
            ret['comment'] += '. Updated the entry in the fstab.'
            return ret
        if out == 'bad config':
            ret['result'] = False
            ret['comment'] += '. However, the fstab was not found.'
            return ret
    return ret

def unmounted(name,
              config='/etc/fstab',
              persist=False):
    '''
    .. note::
        This state will be available in version 0.17.0.

    Verify that a device is not mounted

    name
        The path to the location where the device is to be unmounted from

    config
        Set an alternative location for the fstab, default to /etc/fstab

    persist
        Set if the mount should be purged from the fstab, default to False
    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': ''}

    # Get the active data
    active = __salt__['mount.active']()
    if name not in active:
        # Nothing to unmount
        ret['comment'] = 'Target was already unmounted'
    if name in active:
        # The mount is present! Unmount it
        if __opts__['test']:
            ret['result'] = None
            ret['comment'] = ('Mount point {0} is mounted but should not '
                              'be').format(name)
            return ret
        out = __salt__['mount.umount'](name)
        if isinstance(out, string_types):
            # Failed to umount, the state has failed!
            ret['comment'] = out
            ret['result'] = False
        elif out is True:
            # umount worked!
            ret['comment'] = 'Target was successfully unmounted'
            ret['changes']['umount'] = True

    if persist:
        if __opts__['test']:
            fstab_data = __salt__['mount.fstab'](config)
            if name in fstab_data:
                ret['result'] = None
                ret['comment'] = ('Mount point {0} is unmounted but needs to '
                                  'be purged from {1} to be made '
                                  'persistent').format(name, config)
                return ret

        if ret['changes'].get('umount', False):
            out = __salt__['mount.rm_fstab'](name, config)
        else:
            out = 'bad mount'

        if out is True:
            ret['changes']['persist'] = 'purged'
            return ret
        if out == 'bad mount':
            ret['result'] = False
            ret['comment'] += '. Unfortunately the mount could not be purged'

    return ret
<commit_msg>handling lack of fstab as not-error<commit_after># -*- coding: utf-8 -*-
'''
Mounting of filesystems
=======================

Mount any type of mountable filesystem with the mounted function:

.. code-block:: yaml

    /mnt/sdb:
      mount.mounted:
        - device: /dev/sdb1
        - fstype: ext4
        - mkmnt: True
        - opts:
          - defaults
'''

# Import python libs
import os.path

# Import salt libs
from salt._compat import string_types

def mounted(name,
            device,
            fstype,
            mkmnt=False,
            opts=None,
            dump=0,
            pass_num=0,
            config='/etc/fstab',
            persist=True):
    '''
    Verify that a device is mounted

    name
        The path to the location where the device is to be mounted

    device
        The device name, typically the device node, such as /dev/sdb1

    fstype
        The filesystem type, this will be xfs, ext2/3/4 in the case of classic
        filesystems, and fuse in the case of fuse mounts

    mkmnt
        If the mount point is not present then the state will fail, set mkmnt
        to True to create the mount point if it is otherwise not present

    opts
        A list object of options or a comma delimited list

    dump
        The dump value to be passed into the fstab, default to 0

    pass_num
        The pass value to be passed into the fstab, default to 0

    config
        Set an alternative location for the fstab, default to /etc/fstab

    remount
        Set if the file system can be remounted with the remount option,
        default to True

    persist
        Set if the mount should be saved in the fstab, default to True
    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': ''}

    # Make sure that opts is correct, it can be a list or a comma delimited
    # string
    if isinstance(opts, string_types):
        opts = opts.split(',')
    elif opts is None:
        opts = ['defaults']

    # remove possible trailing slash
    name = name.rstrip("/")

    # Get the active data
    active = __salt__['mount.active']()
    real_name = os.path.realpath(name)
    if device[0:1] == "/":
        real_device = os.path.realpath(device)
    else:
        real_device = device
    device_list = []
    if real_name in active:
        device_list.append(active[real_name]['device'])
        device_list.append(os.path.realpath(device_list[0]))
        if active[real_name]['alt_device'] not in device_list:
            device_list.append(active[real_name]['alt_device'])
        if real_device not in device_list:
            # name matches but device doesn't - need to umount
            ret['changes']['umount'] = "Forced unmount because devices " \
                                       + "don't match. Wanted: " + device
            if real_device != device:
                ret['changes']['umount'] += " (" + real_device + ")"
            ret['changes']['umount'] += ", current: " + ', '.join(device_list)
            out = __salt__['mount.umount'](real_name)
            active = __salt__['mount.active']()
            if real_name in active:
                ret['comment'] = "Unable to unmount"
                ret['result'] = None
                return ret
        else:
            ret['comment'] = 'Target was already mounted'
    # using a duplicate check so I can catch the results of a umount
    if real_name not in active:
        # The mount is not present! Mount it
        if __opts__['test']:
            ret['result'] = None
            ret['comment'] = '{0} would be mounted'.format(name)
            return ret

        out = __salt__['mount.mount'](name, device, mkmnt, fstype, opts)
        active = __salt__['mount.active']()
        if isinstance(out, string_types):
            # Failed to (re)mount, the state has failed!
            ret['comment'] = out
            ret['result'] = False
            return ret
        elif real_name in active:
            # (Re)mount worked!
            ret['comment'] = 'Target was successfully mounted'
            ret['changes']['mount'] = True

    if persist and real_name in active:
        if __opts__['test']:
            out = __salt__['mount.set_fstab'](name,
                                              device,
                                              fstype,
                                              opts,
                                              dump,
                                              pass_num,
                                              config,
                                              test=True)
            if out != 'present':
                ret['result'] = None
                if out == 'new':
                    ret['comment'] = ('{0} is mounted, but needs to be '
                                      'written to the fstab in order to be '
                                      'made persistent').format(name)
                elif out == 'change':
                    ret['comment'] = ('{0} is mounted, but its fstab entry '
                                      'must be updated').format(name)
                else:
                    ret['result'] = False
                    ret['comment'] = ('Unable to detect fstab status for '
                                      'mount point {0} due to unexpected '
                                      'output \'{1}\' from call to '
                                      'mount.set_fstab. This is most likely '
                                      'a bug.').format(name, out)
                return ret

        else:
            out = __salt__['mount.set_fstab'](name,
                                              device,
                                              fstype,
                                              opts,
                                              dump,
                                              pass_num,
                                              config)

        if out == 'present':
            return ret
        if out == 'new':
            ret['changes']['persist'] = 'new'
            ret['comment'] += '. Added new entry to the fstab.'
            return ret
        if out == 'change':
            ret['changes']['persist'] = 'update'
            ret['comment'] += '. Updated the entry in the fstab.'
            return ret
        if out == 'bad config':
            ret['result'] = False
            ret['comment'] += '. However, the fstab was not found.'
            return ret

    return ret

def swap(name, persist=True, config='/etc/fstab'):
    '''
    Activates a swap device

    .. code-block:: yaml

        /root/swapfile:
          mount.swap
    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': ''}
    on_ = __salt__['mount.swaps']()

    if name in on_:
        ret['comment'] = 'Swap {0} already active'.format(name)
    elif __opts__['test']:
        ret['result'] = None
        ret['comment'] = 'Swap {0} is set to be activated'.format(name)
    else:
        __salt__['mount.swapon'](name)

        on_ = __salt__['mount.swaps']()

        if name in on_:
            ret['comment'] = 'Swap {0} activated'.format(name)
            ret['changes'] = on_[name]
        else:
            ret['comment'] = 'Swap {0} failed to activate'.format(name)
            ret['result'] = False

    if persist:
        fstab_data = __salt__['mount.fstab'](config)
        if __opts__['test']:
            if name not in fstab_data:
                ret['result'] = None
                if name in on_:
                    ret['comment'] = ('Swap {0} is set to be added to the '
                                      'fstab and to be activated').format(name)
            return ret

        if 'none' in fstab_data:
            if fstab_data['none']['device'] == name and fstab_data['none']['fstype'] != 'swap':
                return ret

        # present, new, change, bad config
        # Make sure the entry is in the fstab
        out = __salt__['mount.set_fstab']('none',
                                          name,
                                          'swap',
                                          ['defaults'],
                                          0,
                                          0,
                                          config)
        if out == 'present':
            return ret
        if out == 'new':
            ret['changes']['persist'] = 'new'
            ret['comment'] += '. Added new entry to the fstab.'
            return ret
        if out == 'change':
            ret['changes']['persist'] = 'update'
            ret['comment'] += '. Updated the entry in the fstab.'
            return ret
        if out == 'bad config':
            ret['result'] = False
            ret['comment'] += '. However, the fstab was not found.'
            return ret
    return ret

def unmounted(name,
              config='/etc/fstab',
              persist=False):
    '''
    .. note::
        This state will be available in version 0.17.0.

    Verify that a device is not mounted

    name
        The path to the location where the device is to be unmounted from

    config
        Set an alternative location for the fstab, default to /etc/fstab

    persist
        Set if the mount should be purged from the fstab, default to False
    '''
    ret = {'name': name,
           'changes': {},
           'result': True,
           'comment': ''}

    # Get the active data
    active = __salt__['mount.active']()
    if name not in active:
        # Nothing to unmount
        ret['comment'] = 'Target was already unmounted'
    if name in active:
        # The mount is present! Unmount it
        if __opts__['test']:
            ret['result'] = None
            ret['comment'] = ('Mount point {0} is mounted but should not '
                              'be').format(name)
            return ret
        out = __salt__['mount.umount'](name)
        if isinstance(out, string_types):
            # Failed to umount, the state has failed!
            ret['comment'] = out
            ret['result'] = False
        elif out is True:
            # umount worked!
            ret['comment'] = 'Target was successfully unmounted'
            ret['changes']['umount'] = True

    if persist:
        fstab_data = __salt__['mount.fstab'](config)
        if name not in fstab_data:
            ret['comment'] += '. fstab entry not found'
        else:
          if __opts__['test']:
                ret['result'] = None
                ret['comment'] = ('Mount point {0} is unmounted but needs to '
                                  'be purged from {1} to be made '
                                  'persistent').format(name, config)
                return ret
          else:
            out = __salt__['mount.rm_fstab'](name, config)
            if out is not True:
                ret['result'] = False
                ret['comment'] += '. Failed to persist purge'
            else:
                ret['changes']['persist'] = 'purged'

    return ret
<|endoftext|>


<commit_before># Der's more to travelling baby!
from data import sbox
from random import randint
from travel import (
    substitution,
    getfilename,
    limit,
    value_nonl,
    per,
    is_bijective,
    dumps,
    cost
)

def more_travel(array):
    """
    """
    if is_bijective(array):
        all_perms = list(per(range(8)))
        for var in range(64):
            array_mod = []
            for num in range(32):
                array_mod, graph = substitution(
                    all_perms, array, array_mod, num)
                graphs.append(graph)
            nn_array_mod = value_nonl(array_mod)
            print(var, nn_array_mod)
            if nn_array_mod > limit:
                non_sbox[nn_array_mod] = [array_mod, graphs]
                print(value_nonl(array), nn_array_mod)
    else:
        print('Is not bijective!')
    return 1

def graphy():
    """
    Gives an undirected graph for the 8 vertices.
    """
    graph = {}
    for vertex in range(8):
        graph[vertex] = []
        for x in range(8 - vertex - 1):
            graph[vertex].append(randint(1, 255))
    return graph

def travelling(all_perms):
    """
    """
    graph = graphy()
    cost_path = []
    paths = {}
    for perm in all_perms:
        tupl = perm + (perm[0], )
        path_val = cost(graph, tupl)
        if path_val not in cost_path:
            cost_path.append(path_val)
        if path_val not in paths:
            paths[path_val] = tupl
    return cost_path, paths

def selected_travel(array):
    """
    """
    # select 8 max nodes
    nodes = list(range(255, 247, -1))
    # get indices of all the above nodes
    indices = {}
    for node in nodes:
        indices[array.index(node)] = node
    # list_indicies = list(indices.keys())
    maps, num = {}, 0
    for node in indices:
        maps[num] = node
        num += 1
    all_perms = list(per(range(8)))
    cost_path, paths = travelling(all_perms)
    min_path = paths[min(cost_path)]
    new_mapping = {}
    for num in range(len(min_path) - 1):
        new_mapping[maps[num]] = indices[maps[num]]
    print(new_mapping)
    return maps

if __name__ == '__main__':
    graphs = []
    non_sbox = {value_nonl(sbox): sbox}
    # more_travel(sbox)
    # if max(non_sbox) > limit:
    #     with open('data/part-2/'+getfilename(), 'a') as f:
    #         f.write(dumps(non_sbox))
    print(selected_travel(sbox))
    # print(non_sbox.keys(), max(non_sbox.keys()))
<commit_msg>for 8 selected vertices, min path is used and substitued, after  which the property is calculated!<commit_after># Der's more to travelling baby!
from data import sbox
from random import randint
from travel import (
    substitution,
    getfilename,
    limit,
    value_nonl,
    per,
    is_bijective,
    dumps,
    cost
)

def more_travel(array):
    """
    """
    if is_bijective(array):
        all_perms = list(per(range(8)))
        for var in range(64):
            array_mod = []
            for num in range(32):
                array_mod, graph = substitution(
                    all_perms, array, array_mod, num)
                graphs.append(graph)
            nn_array_mod = value_nonl(array_mod)
            print(var, nn_array_mod)
            if nn_array_mod > limit:
                non_sbox[nn_array_mod] = [array_mod, graphs]
                print(value_nonl(array), nn_array_mod)
    else:
        print('Is not bijective!')
    return 1

def graphy():
    """
    Gives an undirected graph for the 8 vertices.
    """
    graph = {}
    for vertex in range(8):
        graph[vertex] = []
        for x in range(8 - vertex - 1):
            graph[vertex].append(randint(1, 255))
    return graph

def travelling(all_perms):
    """
    """
    graph = graphy()
    cost_path = []
    paths = {}
    for perm in all_perms:
        tupl = perm + (perm[0], )
        path_val = cost(graph, tupl)
        if path_val not in cost_path:
            cost_path.append(path_val)
        if path_val not in paths:
            paths[path_val] = tupl
    return cost_path, paths

def selected_travel(array):
    """
    """
    # select 8 max nodes
    nodes = list(range(255, 247, -1))
    # get indices of all the above nodes
    indices = {}
    for node in nodes:
        indices[array.index(node)] = node
    maps, num = {}, 0
    for node in indices:
        maps[num] = node
        num += 1
    all_perms = list(per(range(8)))
    cost_path, paths = travelling(all_perms)
    min_path = paths[min(cost_path)]
    new_mapping = {}
    for num in range(len(min_path) - 1):
        new_mapping[indices[maps[num]]] = maps[min_path[num]]
    print(new_mapping, indices)
    # Substitute! the sequence
    for node in new_mapping:
        array[node] = new_mapping[node]
    return array

if __name__ == '__main__':
    graphs = []
    non_sbox = {value_nonl(sbox): sbox}
    for var in range(64):
        print(var)
        sbox_mod = selected_travel(sbox)
        if value_nonl(sbox_mod) > limit:
            non_sbox[value_nonl(sbox_mod)] = sbox_mod
    # if max(non_sbox) > limit:
    #     with open('data/part-2/'+getfilename(), 'a') as f:
    #         f.write(dumps(non_sbox))
    # print(non_sbox.keys(), max(non_sbox.keys()))
<|endoftext|>


<commit_before>#!/usr/bin/env python
def main():
  import sys
  from scripts.utils import get_stencil_num, load_csv
  raw_data = load_csv(sys.argv[1])

  req_fields = [('MStencil/s  MAX', float), ('Precision', int), ('Global NX', int), ('Number of time steps', int), ('Number of tests', int)]

  hw_ctr_fields = {
                    '':[],
                    'TLB':[('L1 DTLB miss rate sum', float)],
                    'DATA':[('Load to Store ratio avg', float)],
                    'L2':[('L2 data volume sum', float)],
                    'L3':[('L3 data volume sum', float)],
                    'MEM':[('Total Memory Transfer', float),('Sustained Memory BW', float)],
                    'ENERGY':[('Energy', float), ('Energy DRAM', float), ('Power',float), ('Power DRAM', float)]}

  hw_ctr_labels = {
                    '':(),
                    'TLB':('L1 DTLB miss rate sum', 'tlb_'),
                    'DATA':('Load to Store ratio avg', 'cpu_'),
                    'L2':('Bytes/LUP', 'L2_'),
                    'L3':('Bytes/LUP', 'L3_'),
                    'MEM':('GB/s', 'bw_'),
                    'ENERGY':('pJ/LUP', 'energy_') }

  duplicates = set()
  plots = dict()
  for k in raw_data:

    # Use single field to represent the performance
    if 'Total RANK0 MStencil/s MAX' in k.keys():
      if(k['Total RANK0 MStencil/s MAX']!=''):
        k['MStencil/s  MAX'] = k['MWD main-loop RANK0 MStencil/s MAX']
    # temporary for deprecated format
    if 'RANK0 MStencil/s  MAX' in k.keys():
      if k['RANK0 MStencil/s  MAX']!='':
        k['MStencil/s  MAX'] = k['RANK0 MStencil/s  MAX']

    # add stencil operator
    k['stencil'] = get_stencil_num(k)
    if   k['stencil'] == 0:
      k['stencil_name'] = '25_pt_const'
    elif k['stencil'] == 1:
      k['stencil_name'] = '7_pt_const'
    elif k['stencil'] == 4:
      k['stencil_name']  = '25_pt_var'
    elif k['stencil'] == 5:
      k['stencil_name']  = '7_pt_var'
    elif k['stencil'] == 6:
      k['stencil_name']  = 'solar'

    # add the approach
    if(k['Time stepper orig name'] == 'Spatial Blocking'):
      k['method'] = 'Spt.blk.'
    elif(k['Time stepper orig name'] in ['PLUTO', 'Pochoir']):
      k['method'] = k['Time stepper orig name']
    elif(k['Time stepper orig name'] == 'Diamond'):
      if('_tgs1_' in k['file_name']):
        k['method'] = 'CATS2'
      else:
        k['method'] = 'MWD'
    else:
      print("ERROR: Unknow time stepper")
      raise

    # add mwd type
    k['mwdt']='none'
    if(k['method'] == 'MWD'):
      mwd = k['Wavefront parallel strategy'].lower()
      if('fixed' in mwd) and ('relaxed' in mwd):
        k['mwdt'] = 'fers'
      elif('fixed' in mwd):
        k['mwdt'] = 'fe'
      elif('relaxed' in mwd):
        k['mwdt'] = 'rs'
      elif('wavefront' in mwd):
        k['mwdt'] = 'block'

    # add precision information
    p = 1 if k['Precision'] in 'DP' else 0
    k['Precision'] = p

    # TLB measurement
    if 'L1 DTLB load miss rate avg' in k.keys():
      if k['L1 DTLB load miss rate avg']!='':
        hw_ctr_fields['TLB'] =  [('L1 DTLB load miss rate avg', float)]
        hw_ctr_labels['TLB'] =  ('L1 DTLB load miss rate avg', 'tlb_')

    entry = {}
    # parse the general fileds' format
    for f in req_fields + hw_ctr_fields[k['LIKWID performance counter']]:
      try:
        entry[f[0]] = map(f[1], [k[f[0]]] )[0]
      except:
        print("ERROR: results entry missing essential data at file:%s"%(k['file_name']))
        print f[0]
        print k
        return

    #find repeated data
    key = (entry['Precision'], k['stencil_name'], k['LIKWID performance counter'], k['mwdt'], k['method'], entry['Global NX'])
    if key not in duplicates:
      duplicates.add(key)
    else:
      print("Repeated result at: %s"%(k['file_name']))
      continue

#    for m,n in entry.iteritems(): print m,n
    plot_key = (entry['Precision'], k['stencil_name'], k['LIKWID performance counter'])
    line_key = (k['mwdt'], k['method'])
    if plot_key not in plots.keys():
      plots[plot_key] = {}
    if line_key not in plots[plot_key].keys():
      plots[plot_key][line_key] = [[], [], []]

    # append the data
    plots[plot_key][line_key][0].append(entry['Global NX'])
    plots[plot_key][line_key][1].append(entry['MStencil/s  MAX']/1e3)
    N = entry['Global NX']**3 * entry['Number of time steps'] * entry['Number of tests']/1e9
    # Memory
    if k['LIKWID performance counter'] == 'MEM':
      plots[plot_key][line_key][2].append(entry['Sustained Memory BW']/1e3)
    # Energy
    if k['LIKWID performance counter'] == 'ENERGY':
      entry['cpu energy pj/lup'] = entry['Energy']/N
      entry['dram energy pj/lup'] = entry['Energy DRAM']/N
      entry['total energy pj/lup'] = entry['cpu energy pj/lup'] + entry['dram energy pj/lup']
      if (entry['total energy pj/lup'] > 1e5): entry['total energy pj/lup'] = 0
      plots[plot_key][line_key][2].append(entry['total energy pj/lup'])
    # TLB
    if k['LIKWID performance counter'] == 'TLB':
      plots[plot_key][line_key][2].append(entry[ hw_ctr_fields['TLB'][0][0] ])
    # L2
    if k['LIKWID performance counter'] == 'L2':
      plots[plot_key][line_key][2].append(entry['L2 data volume sum']/N)
    #L3
    if k['LIKWID performance counter'] == 'L3':
      plots[plot_key][line_key][2].append(entry['L3 data volume sum']/N)
    #CPU
    if k['LIKWID performance counter'] == 'DATA':
      plots[plot_key][line_key][2].append(entry['Load to Store ratio avg'])

  del raw_data
  #sort the plot lines
  for p in plots:
    for l in plots[p]:
      x = plots[p][l][0]
      y = plots[p][l][1]
      z = plots[p][l][2]
      xyz = sorted(zip(x,y,z))
      plots[p][l][0] = [x for (x,y,z) in xyz]
      plots[p][l][1] = [y for (x,y,z) in xyz]
      plots[p][l][2] = [z for (x,y,z) in xyz]

#  for m,n in plots.iteritems():
#    print "##############",m
#    for i,j in n.iteritems():
#      print i,j

  for p in plots:
    y_label, file_prefix = hw_ctr_labels[p[2]]
    stencil = p[1]
    plot_line(plots[p], stencil, y_label, file_prefix)

def plot_line(p, stencil, y_label, file_prefix):
  from operator import itemgetter
  import matplotlib.pyplot as plt
  import matplotlib
  import pylab
  from pylab import arange,pi,sin,cos,sqrt
  from scripts.utils import get_stencil_num

  m = 3.0
  fig_width = 4.0*0.393701*m # inches
  fig_height = 1.0*fig_width #* 210.0/280.0#433.62/578.16

  fig_size =  [fig_width,fig_height]
  params = {
         'axes.labelsize': 6*m,
         'axes.linewidth': 0.25*m,
         'lines.linewidth': 0.75*m,
         'font.size': 7*m,
         'legend.fontsize': 4*m,
         'xtick.labelsize': 6*m,
         'ytick.labelsize': 6*m,
         'lines.markersize': 1,
         'text.usetex': True,
         'figure.figsize': fig_size}
  pylab.rcParams.update(params)

  marker_s = 7
  line_w = 1
  line_s = '-'
  method_style = {'Spt.blk.':('g','o'), 'MWD':('k','x'), 'CATS2':('r','+'),
                  'PLUTO':('m','*'), 'Pochoir':('b','^')}
  for l in p:
    label = l[1]
    col, marker = method_style[label]
    x = p[l][0]
    y_p = p[l][1]
    y = p[l][2]

    plt.figure(0)
    plt.plot(x, y_p, color=col, marker=marker, markersize=marker_s, linestyle=line_s, linewidth=line_w, label=label)

    plt.figure(1)
    plt.plot(x, y, color=col, marker=marker, markersize=marker_s, linestyle=line_s, linewidth=line_w, label=label)

  f_name = stencil+'_inc_grid_size'

  plt.figure(0)
  plt.ylabel('GLUP/s')
  plt.grid()
  plt.xlabel('Size in each dimension')
  plt.legend(loc='best')
  pylab.savefig(file_prefix + 'perf_' + f_name+'.pdf', format='pdf', bbox_inches="tight", pad_inches=0)
  plt.clf()

  plt.figure(1)
  plt.ylabel(y_label)
  plt.grid()
  plt.xlabel('Size in each dimension')
  plt.legend(loc='best')
  pylab.savefig(file_prefix + f_name+'.pdf', format='pdf', bbox_inches="tight", pad_inches=0)
  plt.clf()

if __name__ == "__main__":
  main()
<commit_msg>allowing more than a plot per HW group<commit_after>#!/usr/bin/env python

hw_ctr_labels = {
                    '':(),
                    'TLB':[('L1 DTLB miss rate sum', 'tlb_', 'tlb')],
                    'DATA':[('Load to Store ratio avg', 'cpu_', 'data')],
                    'L2':[('Bytes/LUP', 'L2_', 'l2 vol')],
                    'L3':[('Bytes/LUP', 'L3_', 'l3 vol')],
                    'MEM':[('GB/s', 'bw_', 'mem bw'), ('Bytes/LUP', 'MEM_vol_', 'mem vol')],
                    'ENERGY':[('pJ/LUP', 'energy_', 'total energy')] }

def main():
  import sys
  from scripts.utils import get_stencil_num, load_csv
  raw_data = load_csv(sys.argv[1])

  req_fields = [('MStencil/s  MAX', float), ('Precision', int), ('Global NX', int), ('Number of time steps', int), ('Number of tests', int)]

  hw_ctr_fields = {
                    '':[],
                    'TLB':[('L1 DTLB miss rate sum', float)],
                    'DATA':[('Load to Store ratio avg', float)],
                    'L2':[('L2 data volume sum', float)],
                    'L3':[('L3 data volume sum', float)],
                    'MEM':[('Total Memory Transfer', float),('Sustained Memory BW', float)],
                    'ENERGY':[('Energy', float), ('Energy DRAM', float), ('Power',float), ('Power DRAM', float)]}

  duplicates = set()
  plots = dict()
  for k in raw_data:

    # Use single field to represent the performance
    if 'Total RANK0 MStencil/s MAX' in k.keys():
      if(k['Total RANK0 MStencil/s MAX']!=''):
        k['MStencil/s  MAX'] = k['MWD main-loop RANK0 MStencil/s MAX']
    # temporary for deprecated format
    if 'RANK0 MStencil/s  MAX' in k.keys():
      if k['RANK0 MStencil/s  MAX']!='':
        k['MStencil/s  MAX'] = k['RANK0 MStencil/s  MAX']

    # add stencil operator
    k['stencil'] = get_stencil_num(k)
    if   k['stencil'] == 0:
      k['stencil_name'] = '25_pt_const'
    elif k['stencil'] == 1:
      k['stencil_name'] = '7_pt_const'
    elif k['stencil'] == 4:
      k['stencil_name']  = '25_pt_var'
    elif k['stencil'] == 5:
      k['stencil_name']  = '7_pt_var'
    elif k['stencil'] == 6:
      k['stencil_name']  = 'solar'

    # add the approach
    if(k['Time stepper orig name'] == 'Spatial Blocking'):
      k['method'] = 'Spt.blk.'
    elif(k['Time stepper orig name'] in ['PLUTO', 'Pochoir']):
      k['method'] = k['Time stepper orig name']
    elif(k['Time stepper orig name'] == 'Diamond'):
      if('_tgs1_' in k['file_name']):
        k['method'] = 'CATS2'
      else:
        k['method'] = 'MWD'
    else:
      print("ERROR: Unknow time stepper")
      raise

    # add mwd type
    k['mwdt']='none'
    if(k['method'] == 'MWD'):
      mwd = k['Wavefront parallel strategy'].lower()
      if('fixed' in mwd) and ('relaxed' in mwd):
        k['mwdt'] = 'fers'
      elif('fixed' in mwd):
        k['mwdt'] = 'fe'
      elif('relaxed' in mwd):
        k['mwdt'] = 'rs'
      elif('wavefront' in mwd):
        k['mwdt'] = 'block'

    # add precision information
    p = 1 if k['Precision'] in 'DP' else 0
    k['Precision'] = p

    # TLB measurement for LIKWID 4
    if 'L1 DTLB load miss rate avg' in k.keys():
      if k['L1 DTLB load miss rate avg']!='':
        hw_ctr_fields['TLB'] =  [('L1 DTLB load miss rate avg', float)]
        hw_ctr_labels['TLB'] =  [('L1 DTLB load miss rate avg', 'tlb_', 'tlb')]

    entry = {}
    # parse the general fileds' format
    for f in req_fields + hw_ctr_fields[k['LIKWID performance counter']]:
      try:
        entry[f[0]] = map(f[1], [k[f[0]]] )[0]
      except:
        print("ERROR: results entry missing essential data at file:%s"%(k['file_name']))
        print f[0]
        print k
        return

    #find repeated data
    key = (entry['Precision'], k['stencil_name'], k['LIKWID performance counter'], k['mwdt'], k['method'], entry['Global NX'])
    if key not in duplicates:
      duplicates.add(key)
    else:
      print("Repeated result at: %s"%(k['file_name']))
      continue

    # Initialize plot entry if does not exist for current data entry
#    for m,n in entry.iteritems(): print m,n
    measure_list = ['n', 'perf', 'total energy', 'tlb', 'mem bw', 'l2 bw', 'l3 bw', 'mem vol', 'l2 vol', 'l3 vol', 'data']
    plot_key = (entry['Precision'], k['stencil_name'], k['LIKWID performance counter'])
    line_key = (k['mwdt'], k['method'])
    if plot_key not in plots.keys():
      plots[plot_key] = {}
    if line_key not in plots[plot_key].keys():
      plots[plot_key][line_key] = {meas:[] for meas in measure_list}

    # append the data
    plots[plot_key][line_key]['n'].append(entry['Global NX'])
    plots[plot_key][line_key]['perf'].append(entry['MStencil/s  MAX']/1e3)
    N = entry['Global NX']**3 * entry['Number of time steps'] * entry['Number of tests']/1e9
    # Memory
    if k['LIKWID performance counter'] == 'MEM':
      plots[plot_key][line_key]['mem bw'].append(entry['Sustained Memory BW']/1e3)
      plots[plot_key][line_key]['mem vol'].append(entry['Total Memory Transfer']/N)
    # Energy
    elif k['LIKWID performance counter'] == 'ENERGY':
      entry['cpu energy pj/lup'] = entry['Energy']/N
      entry['dram energy pj/lup'] = entry['Energy DRAM']/N
      entry['total energy pj/lup'] = entry['cpu energy pj/lup'] + entry['dram energy pj/lup']
      if (entry['total energy pj/lup'] > 1e5): entry['total energy pj/lup'] = 0
      plots[plot_key][line_key]['total energy'].append(entry['total energy pj/lup'])
    # TLB
    elif k['LIKWID performance counter'] == 'TLB':
      plots[plot_key][line_key]['tlb'].append(entry[ hw_ctr_fields['TLB'][0][0] ])
    # L2
    elif k['LIKWID performance counter'] == 'L2':
      plots[plot_key][line_key]['l2 vol'].append(entry['L2 data volume sum']/N)
    #L3
    elif k['LIKWID performance counter'] == 'L3':
      plots[plot_key][line_key]['l3 vol'].append(entry['L3 data volume sum']/N)
    #CPU
    elif k['LIKWID performance counter'] == 'DATA':
      plots[plot_key][line_key]['data'].append(entry['Load to Store ratio avg'])

  del raw_data

  #sort the plot lines
  for p in plots:
    for l in plots[p]:
      pl = plots[p][l]
      #remove unused fields
      empty = []
      for key, val in pl.iteritems():
        if(val==[]):
          empty.append(key)
      for key in empty:
          del pl[key]
      lines = []
      [lines.append(pl[val]) for val in measure_list if val in pl.keys()]

      lines = sorted(zip(*lines))
      idx=0
      for val in measure_list:
        if(val in pl.keys()):
          if(pl[val]):
            pl[val] = [x[idx] for x in lines]
            idx = idx+1

#  for m,n in plots.iteritems():
#    print "##############",m
#    for i,j in n.iteritems():
#      print i,j

  for p in plots:
    plot_line(plots[p], stencil=p[1], plt_key=p[2])

def plot_line(p, stencil, plt_key):
  from operator import itemgetter
  import matplotlib.pyplot as plt
  import matplotlib
  import pylab
  from pylab import arange,pi,sin,cos,sqrt
  from scripts.utils import get_stencil_num

  m = 3.0
  fig_width = 4.0*0.393701*m # inches
  fig_height = 1.0*fig_width #* 210.0/280.0#433.62/578.16

  fig_size =  [fig_width,fig_height]
  params = {
         'axes.labelsize': 6*m,
         'axes.linewidth': 0.25*m,
         'lines.linewidth': 0.75*m,
         'font.size': 7*m,
         'legend.fontsize': 4*m,
         'xtick.labelsize': 6*m,
         'ytick.labelsize': 6*m,
         'lines.markersize': 1,
         'text.usetex': True,
         'figure.figsize': fig_size}
  pylab.rcParams.update(params)

  marker_s = 7
  line_w = 1
  line_s = '-'
  method_style = {'Spt.blk.':('g','o'), 'MWD':('k','x'), 'CATS2':('r','+'),
                  'PLUTO':('m','*'), 'Pochoir':('b','^')}

  f_name = stencil+'_inc_grid_size'

  # performance
  plt.figure(0)
  for l in p:
    label = l[1]
    col, marker = method_style[label]
    x = p[l]['n']
    y_p = p[l]['perf']
    plt.plot(x, y_p, color=col, marker=marker, markersize=marker_s, linestyle=line_s, linewidth=line_w, label=label)

  plt.ylabel('GLUP/s')
  plt.grid()
  plt.xlabel('Size in each dimension')
  plt.legend(loc='best')
  pylab.savefig('perf_' + f_name + '_' + plt_key + '.pdf', format='pdf', bbox_inches="tight", pad_inches=0)
  plt.clf()

  # HW measurements
  plt_idx=1
  for y_label, file_prefix, measure in hw_ctr_labels[plt_key]:
    plt.figure(plt_idx)
    plt_idx = plt_idx + 1
    for l in p:
      label = l[1]
      col, marker = method_style[label]
      x = p[l]['n']
      y = p[l][measure]
      plt.plot(x, y, color=col, marker=marker, markersize=marker_s, linestyle=line_s, linewidth=line_w, label=label)

    plt.ylabel(y_label)
    plt.grid()
    plt.xlabel('Size in each dimension')
    plt.legend(loc='best')
    pylab.savefig(file_prefix + f_name+'.pdf', format='pdf', bbox_inches="tight", pad_inches=0)
    plt.clf()

if __name__ == "__main__":
  main()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import weakref
# from six.moves.urllib.parse import urlparse
from urlparse import urlparse

reload(sys)
sys.setdefaultencoding('utf-8')

_urlparse_cache = weakref.WeakKeyDictionary()

def urlparse_cached(request_or_response):
    """Return urlparse.urlparse caching the result, where the argument can be a
    Request or Response object
    """
    if request_or_response not in _urlparse_cache:
        _urlparse_cache[request_or_response] = urlparse(request_or_response.url)
    return _urlparse_cache[request_or_response]
<commit_msg>sasila<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import weakref
# from six.moves.urllib.parse import urlparse
from urlparse import urlparse

reload(sys)
sys.setdefaultencoding('utf-8')

_urlparse_cache = weakref.WeakKeyDictionary()

def urlparse_cached(request_or_response):
    if request_or_response not in _urlparse_cache:
        _urlparse_cache[request_or_response] = urlparse(request_or_response.url)
    return _urlparse_cache[request_or_response]
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2015 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: Emanuel Cino <ecino@compassion.ch>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
from datetime import date

from odoo import api, fields, models, _

class Contracts(models.Model):
    """ Add correspondence information in contracts
    """

    _inherit = 'recurring.contract'

    ##########################################################################
    #                                 FIELDS                                 #
    ##########################################################################

    writing_language = fields.Many2one(
        'res.lang.compassion', related='reading_language',
        help='By now equals to reading language. Could be used in the future')
    child_letter_ids = fields.Many2many(
        'correspondence', string='Child letters',
        compute='_compute_get_letters')
    sponsor_letter_ids = fields.Many2many(
        'correspondence', string='Sponsor letters',
        compute='_compute_get_letters')
    nb_letters = fields.Integer(
        compute='_compute_get_letters'
    )
    last_letter = fields.Integer(
        'Days since sponsor wrote',
        compute='_compute_last_letter'
    )

    ##########################################################################
    #                             FIELDS METHODS                             #
    ##########################################################################
    def _compute_get_letters(self):
        """ Retrieve correspondence of sponsorship contracts. """
        for sponsorship in self:
            letters_obj = self.env['correspondence']
            letters = letters_obj.search([
                ('sponsorship_id', '=', sponsorship.id)])
            sponsorship.child_letter_ids = letters.filtered(
                lambda l: l.direction == 'Beneficiary To Supporter')
            sponsorship.sponsor_letter_ids = letters.filtered(
                lambda l: l.direction == 'Supporter To Beneficiary')
            sponsorship.nb_letters = len(letters)

    def _compute_last_letter(self):
        """
        Get the date of the last letter sent to the child by the sponsor and
        compute since how many days he didn't write him any letter.
        If sponsor never wrote him, return -1
        :return: int value
        """
        for contract in self:
            try:
                # Try to get days difference between today and last letter
                contract.last_letter = (
                    date.today() - fields.Date.from_string(
                        contract.sponsor_letter_ids[:1].scanned_date)).days
            except AttributeError:
                contract.last_letter = -1

    ##########################################################################
    #                             VIEW CALLBACKS                             #
    ##########################################################################
    @api.onchange('correspondent_id', 'child_id')
    def onchange_relationship(self):
        """ Define the preferred reading language for the correspondent.
            1. Sponsor main language if child speaks that language
            2. Any language spoken by both sponsor and child other than
               English
            3. English
        """
        for sponsorship in self:
            if sponsorship.correspondent_id and sponsorship.child_id:
                sponsor = sponsorship.correspondent_id
                child_languages = sponsorship.child_id.field_office_id.\
                    spoken_language_ids
                sponsor_languages = sponsor.spoken_lang_ids
                lang_obj = self.env['res.lang.compassion']
                sponsor_main_lang = lang_obj.search([
                    ('lang_id.code', '=', sponsor.lang)])
                if sponsor_main_lang in child_languages:
                    sponsorship.reading_language = sponsor_main_lang
                else:
                    english = self.env.ref(
                        'child_compassion.lang_compassion_english')
                    common_langs = (child_languages &
                                    sponsor_languages) - english
                    if common_langs:
                        sponsorship.reading_language = common_langs[0]
                    else:
                        sponsorship.reading_language = english

    @api.multi
    def open_letters(self):
        return {
            'name': _('Letters'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'correspondence',
            'context': self.with_context(
                group_by=False,
                search_default_sponsorship_id=self.id
            ).env.context,
            'target': 'current',
        }

    ##########################################################################
    #                            WORKFLOW METHODS                            #
    ##########################################################################
    @api.multi
    def contract_active(self):
        """ Send letters that were on hold. """
        super(Contracts, self).contract_active()
        for contract in self.filtered(
                lambda c: 'S' in c.type and not
                c.project_id.hold_s2b_letters):
            contract.sponsor_letter_ids.reactivate_letters(
                'Sponsorship activated')
<commit_msg>FIX last_letter compute method<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2015 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: Emanuel Cino <ecino@compassion.ch>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
from datetime import date

from odoo import api, fields, models, _

class Contracts(models.Model):
    """ Add correspondence information in contracts
    """

    _inherit = 'recurring.contract'

    ##########################################################################
    #                                 FIELDS                                 #
    ##########################################################################

    writing_language = fields.Many2one(
        'res.lang.compassion', related='reading_language',
        help='By now equals to reading language. Could be used in the future')
    child_letter_ids = fields.Many2many(
        'correspondence', string='Child letters',
        compute='_compute_get_letters')
    sponsor_letter_ids = fields.Many2many(
        'correspondence', string='Sponsor letters',
        compute='_compute_get_letters')
    nb_letters = fields.Integer(
        compute='_compute_get_letters'
    )
    last_letter = fields.Integer(
        'Days since sponsor wrote',
        compute='_compute_last_letter'
    )

    ##########################################################################
    #                             FIELDS METHODS                             #
    ##########################################################################
    def _compute_get_letters(self):
        """ Retrieve correspondence of sponsorship contracts. """
        for sponsorship in self:
            letters_obj = self.env['correspondence']
            letters = letters_obj.search([
                ('sponsorship_id', '=', sponsorship.id)])
            sponsorship.child_letter_ids = letters.filtered(
                lambda l: l.direction == 'Beneficiary To Supporter')
            sponsorship.sponsor_letter_ids = letters.filtered(
                lambda l: l.direction == 'Supporter To Beneficiary')
            sponsorship.nb_letters = len(letters)

    def _compute_last_letter(self):
        """
        Get the date of the last letter sent to the child by the sponsor and
        compute since how many days he didn't write him any letter.
        If sponsor never wrote him, return -1
        :return: int value
        """
        for contract in self:
            try:
                # Try to get days difference between today and last letter
                contract.last_letter = (
                    date.today() - fields.Date.from_string(
                        contract.sponsor_letter_ids[:1].scanned_date)).days
            except TypeError:
                contract.last_letter = -1

    ##########################################################################
    #                             VIEW CALLBACKS                             #
    ##########################################################################
    @api.onchange('correspondent_id', 'child_id')
    def onchange_relationship(self):
        """ Define the preferred reading language for the correspondent.
            1. Sponsor main language if child speaks that language
            2. Any language spoken by both sponsor and child other than
               English
            3. English
        """
        for sponsorship in self:
            if sponsorship.correspondent_id and sponsorship.child_id:
                sponsor = sponsorship.correspondent_id
                child_languages = sponsorship.child_id.field_office_id.\
                    spoken_language_ids
                sponsor_languages = sponsor.spoken_lang_ids
                lang_obj = self.env['res.lang.compassion']
                sponsor_main_lang = lang_obj.search([
                    ('lang_id.code', '=', sponsor.lang)])
                if sponsor_main_lang in child_languages:
                    sponsorship.reading_language = sponsor_main_lang
                else:
                    english = self.env.ref(
                        'child_compassion.lang_compassion_english')
                    common_langs = (child_languages &
                                    sponsor_languages) - english
                    if common_langs:
                        sponsorship.reading_language = common_langs[0]
                    else:
                        sponsorship.reading_language = english

    @api.multi
    def open_letters(self):
        return {
            'name': _('Letters'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'correspondence',
            'context': self.with_context(
                group_by=False,
                search_default_sponsorship_id=self.id
            ).env.context,
            'target': 'current',
        }

    ##########################################################################
    #                            WORKFLOW METHODS                            #
    ##########################################################################
    @api.multi
    def contract_active(self):
        """ Send letters that were on hold. """
        super(Contracts, self).contract_active()
        for contract in self.filtered(
                lambda c: 'S' in c.type and not
                c.project_id.hold_s2b_letters):
            contract.sponsor_letter_ids.reactivate_letters(
                'Sponsorship activated')
<|endoftext|>


<commit_before># (c) 2016 Werner Mayer
# (c) 2016 Eivind Kvedalen
# LGPL

import os
import sys
import unittest
import FreeCAD
import Part
import Sketcher
import tempfile
from FreeCAD import Base

v = Base.Vector

#----------------------------------------------------------------------------------
# define the functions to test the FreeCAD Spreadsheet module and expression engine
#----------------------------------------------------------------------------------

class SpreadsheetCases(unittest.TestCase):
    def setUp(self):
        self.doc = FreeCAD.newDocument()
        self.TempPath = tempfile.gettempdir()
        FreeCAD.Console.PrintLog( '  Using temp path: ' + self.TempPath + '\n')

    def testRelationalOperators(self):
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        # All should be 1 as result
        sheet.set('A1',  '=1 == 1     ? 1 : 0')
        sheet.set('A2',  '=1 != 1     ? 0 : 1')
        sheet.set('A3',  '=1e9 == 1e9 ? 1 : 0')
        sheet.set('A4',  '=1e9 != 1e9 ? 0 : 1')
        sheet.set('A5',  '=1 > 1      ? 0 : 1')
        sheet.set('A6',  '=2 > 1      ? 1 : 0')
        sheet.set('A7',  '=1 > 2      ? 0 : 1')
        sheet.set('A8',  '=1 < 1      ? 0 : 1')
        sheet.set('A9',  '=1 < 2      ? 1 : 0')
        sheet.set('A10', '=2 < 1      ? 0 : 1')
        sheet.set('A11', '=1 >= 1     ? 1 : 0')
        sheet.set('A12', '=2 >= 1     ? 1 : 0')
        sheet.set('A13', '=1 >= 2     ? 0 : 1')
        sheet.set('A14', '=1 <= 1     ? 1 : 1')
        sheet.set('A15', '=1 <= 2     ? 1 : 0')
        sheet.set('A16', '=2 <= 1     ? 0 : 1')
        sheet.set('A17', '=1 >= 1.000000000000001 ? 1 : 0')
        sheet.set('A18', '=1 >= 1.0000000000000001 ? 0 : 1')
        sheet.set('A19', '=1 <= 1.000000000000001 ? 0 : 1')
        sheet.set('A20', '=1 <= 1.0000000000000001 ? 1 : 0')
        sheet.set('A21', '=1 == 1.000000000000001 ? 0 : 1')
        sheet.set('A22', '=1 == 1.0000000000000001 ? 1 : 0')
        sheet.set('A23', '=1 != 1.000000000000001 ? 1 : 0')
        sheet.set('A24', '=1 != 1.0000000000000001 ? 0 : 1')

        self.doc.recompute()
        self.assertEqual(sheet.A1, 1)
        self.assertEqual(sheet.A2, 1)
        self.assertEqual(sheet.A3, 1)
        self.assertEqual(sheet.A4, 1)
        self.assertEqual(sheet.A5, 1)
        self.assertEqual(sheet.A6, 1)
        self.assertEqual(sheet.A7, 1)
        self.assertEqual(sheet.A8, 1)
        self.assertEqual(sheet.A9, 1)
        self.assertEqual(sheet.A10, 1)
        self.assertEqual(sheet.A11, 1)
        self.assertEqual(sheet.A12, 1)
        self.assertEqual(sheet.A13, 1)
        self.assertEqual(sheet.A14, 1)
        self.assertEqual(sheet.A15, 1)
        self.assertEqual(sheet.A16, 1)
        self.assertEqual(sheet.A17, 1)
        self.assertEqual(sheet.A18, 1)
        self.assertEqual(sheet.A19, 1)
        self.assertEqual(sheet.A20, 1)
        self.assertEqual(sheet.A21, 1)
        self.assertEqual(sheet.A22, 1)
        self.assertEqual(sheet.A23, 1)
        self.assertEqual(sheet.A24, 1)

    def testRemoveRows(self):
        """ Removing rows -- check renaming of internal cells """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('A3', '123')
        sheet.set('A1', '=A3')
        sheet.removeRows('2', 1)
        self.assertEqual(sheet.getContents("A1"),"=A2")

    def testInsertRows(self):
        """ Inserting rows -- check renaming of internal cells """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('B1', '=B2')
        sheet.set('B2', '124')
        sheet.insertRows('2', 1)
        self.assertEqual(sheet.getContents("B1"),"=B3")

    def testRenameAlias(self):
        """ Test renaming of alias1 to alias2 in a spreadsheet """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('B1', '124')
        sheet.setAlias('B1', 'alias1')
        sheet.set('B2', '=alias1')
        self.doc.recompute()
        self.assertEqual(sheet.get("alias1"), 124)
        self.assertEqual(sheet.get("B1"), 124)
        self.assertEqual(sheet.get("B2"), 124)
        sheet.setAlias('B1', 'alias2')
        self.doc.recompute()
        self.assertEqual(sheet.get("alias2"), 124)
        self.assertEqual(sheet.getContents("B2"),"=alias2")

    def testRenameAlias2(self):
        """ Test renaming of alias1 to alias2 in a spreadsheet, when referenced from another object """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('B1', '124')
        sheet.setAlias('B1', 'alias1')
        box = self.doc.addObject('Part::Box', 'Box')
        box.setExpression('Length', 'Spreadsheet.alias1')
        sheet.setAlias('B1', 'alias2')
        self.assertEqual(box.ExpressionEngine[0][1], "Spreadsheet.alias2");

    def testRenameAlias3(self):
        """ Test renaming of document object referenced from another object """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('B1', '124')
        sheet.setAlias('B1', 'alias1')
        box = self.doc.addObject('Part::Box', 'Box')
        box.setExpression('Length', 'Spreadsheet.alias1')
        sheet.Label = "Params"
        self.assertEqual(box.ExpressionEngine[0][1], "Params.alias1");

    def testAlias(self):
        """ Playing with aliases """
        sheet = self.doc.addObject("Spreadsheet::Sheet","Calc")
        sheet.setAlias("A1","Test")
        self.assertEqual(sheet.getAlias("A1"),"Test")

        sheet.set("A1","4711")
        self.doc.recompute()
        self.assertEqual(sheet.get("Test"),4711)
        self.assertEqual(sheet.get("Test"),sheet.get("A1"))

    def testAmbiguousAlias(self):
        """ Try to set the same alias twice (bug #2402) """
        sheet = self.doc.addObject("Spreadsheet::Sheet","Calc")
        sheet.setAlias("A1","Test")
        try:
            sheet.setAlias("A2","Test")
            self.fail("An ambiguous alias was set which shouldn't be allowed")
        except:
            self.assertEqual(sheet.getAlias("A2"),None)

    def testClearAlias(self):
        """ This was causing a crash """
        sheet = self.doc.addObject("Spreadsheet::Sheet","Calc")
        sheet.setAlias("A1","Test")
        sheet.setAlias("A1","")
        self.assertEqual(sheet.getAlias("A1"),None)

    def testSetInvalidAlias(self):
        """ Try to use a cell address as alias name """
        sheet = self.doc.addObject("Spreadsheet::Sheet","Calc")
        try:
            sheet.setAlias("A1","B1")
        except:
            self.assertEqual(sheet.getAlias("A1"),None)
        else:
            self.fail("A cell address was used as alias which shouldn't be allowed")

    def testPlacementName(self):
        """ Object name is equal to property name (bug #2389) """
        if not FreeCAD.GuiUp:
            return

        import FreeCADGui
        o = self.doc.addObject("Part::FeaturePython","Placement")
        FreeCADGui.Selection.addSelection(o)

    def testInvoluteGear(self):
        """ Support of boolean or integer values """
        try:
            import InvoluteGearFeature
        except ImportError:
            return
        InvoluteGearFeature.makeInvoluteGear('InvoluteGear')
        self.doc.recompute()
        sketch=self.doc.addObject('Sketcher::SketchObject','Sketch')
        sketch.addGeometry(Part.Line(v(0,0,0),v(10,10,0)),False)
        sketch.addConstraint(Sketcher.Constraint('Distance',0,65.285388))
        sketch.setExpression('Constraints[0]', 'InvoluteGear.NumberOfTeeth')
        self.doc.recompute()
        self.assertIn('Up-to-date',sketch.State)

    def testSketcher(self):
        """ Mixup of Label and Name (bug #2407)"""
        sketch=self.doc.addObject('Sketcher::SketchObject','Sketch')
        sheet=self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.setAlias('A1', 'Length')
        self.doc.recompute()
        sheet.set('A1', '47,11')
        self.doc.recompute()

        index=sketch.addGeometry(Part.Line(v(0,0,0),v(10,10,0)),False)
        sketch.addConstraint(Sketcher.Constraint('Distance',index,14.0))
        self.doc.recompute()
        sketch.setExpression('Constraints[0]', u'Spreadsheet.Length')
        self.doc.recompute()
        sheet.Label="Calc"
        self.doc.recompute()
        self.assertEqual(sketch.ExpressionEngine[0][1],'Calc.Length')
        self.assertIn('Up-to-date',sketch.State)

    def testCrossDocumentLinks(self):
        """ Expressions accross files are not saved (bug #2442) """

        # Create a box
        box = self.doc.addObject('Part::Box', 'Box')

        # Create a second document with a cylinder
        doc2 = FreeCAD.newDocument()
        cylinder = doc2.addObject('Part::Cylinder', 'Cylinder')
        cylinder.setExpression('Radius', 'cube#Cube.Height')

        # Save and close first document
        self.doc.saveAs(self.TempPath + os.sep + 'cube.fcstd')
        FreeCAD.closeDocument(self.doc.Name)

        # Save and close second document
        doc2.saveAs(self.TempPath + os.sep + 'cylinder.fcstd')
        FreeCAD.closeDocument(doc2.Name)

        # Open both documents again
        self.doc = FreeCAD.openDocument(self.TempPath + os.sep + 'cube.fcstd')
        doc2 = FreeCAD.openDocument(self.TempPath + os.sep + 'cylinder.fcstd')

        # Check reference between them
        self.assertEqual(doc2.getObject('Cylinder').ExpressionEngine[0][1], 'cube#Cube.Height')

        # Close second document
        FreeCAD.closeDocument(doc2.Name)

    def tearDown(self):
        #closing doc
        FreeCAD.closeDocument(self.doc.Name)
<commit_msg>+ fix unit tests for spreadsheet<commit_after># (c) 2016 Werner Mayer
# (c) 2016 Eivind Kvedalen
# LGPL

import os
import sys
import unittest
import FreeCAD
import Part
import Sketcher
import tempfile
from FreeCAD import Base

v = Base.Vector

#----------------------------------------------------------------------------------
# define the functions to test the FreeCAD Spreadsheet module and expression engine
#----------------------------------------------------------------------------------

class SpreadsheetCases(unittest.TestCase):
    def setUp(self):
        self.doc = FreeCAD.newDocument()
        self.TempPath = tempfile.gettempdir()
        FreeCAD.Console.PrintLog( '  Using temp path: ' + self.TempPath + '\n')

    def testRelationalOperators(self):
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        # All should be 1 as result
        sheet.set('A1',  '=1 == 1     ? 1 : 0')
        sheet.set('A2',  '=1 != 1     ? 0 : 1')
        sheet.set('A3',  '=1e9 == 1e9 ? 1 : 0')
        sheet.set('A4',  '=1e9 != 1e9 ? 0 : 1')
        sheet.set('A5',  '=1 > 1      ? 0 : 1')
        sheet.set('A6',  '=2 > 1      ? 1 : 0')
        sheet.set('A7',  '=1 > 2      ? 0 : 1')
        sheet.set('A8',  '=1 < 1      ? 0 : 1')
        sheet.set('A9',  '=1 < 2      ? 1 : 0')
        sheet.set('A10', '=2 < 1      ? 0 : 1')
        sheet.set('A11', '=1 >= 1     ? 1 : 0')
        sheet.set('A12', '=2 >= 1     ? 1 : 0')
        sheet.set('A13', '=1 >= 2     ? 0 : 1')
        sheet.set('A14', '=1 <= 1     ? 1 : 1')
        sheet.set('A15', '=1 <= 2     ? 1 : 0')
        sheet.set('A16', '=2 <= 1     ? 0 : 1')
        sheet.set('A17', '=1 >= 1.000000000000001 ? 0 : 1')
        sheet.set('A18', '=1 >= 1.0000000000000001 ? 1 : 0')
        sheet.set('A19', '=1 <= 1.000000000000001 ? 1 : 0')
        sheet.set('A20', '=1 <= 1.0000000000000001 ? 1 : 0')
        sheet.set('A21', '=1 == 1.000000000000001 ? 0 : 1')
        sheet.set('A22', '=1 == 1.0000000000000001 ? 1 : 0')
        sheet.set('A23', '=1 != 1.000000000000001 ? 1 : 0')
        sheet.set('A24', '=1 != 1.0000000000000001 ? 0 : 1')

        self.doc.recompute()
        self.assertEqual(sheet.A1, 1)
        self.assertEqual(sheet.A2, 1)
        self.assertEqual(sheet.A3, 1)
        self.assertEqual(sheet.A4, 1)
        self.assertEqual(sheet.A5, 1)
        self.assertEqual(sheet.A6, 1)
        self.assertEqual(sheet.A7, 1)
        self.assertEqual(sheet.A8, 1)
        self.assertEqual(sheet.A9, 1)
        self.assertEqual(sheet.A10, 1)
        self.assertEqual(sheet.A11, 1)
        self.assertEqual(sheet.A12, 1)
        self.assertEqual(sheet.A13, 1)
        self.assertEqual(sheet.A14, 1)
        self.assertEqual(sheet.A15, 1)
        self.assertEqual(sheet.A16, 1)
        self.assertEqual(sheet.A17, 1)
        self.assertEqual(sheet.A18, 1)
        self.assertEqual(sheet.A19, 1)
        self.assertEqual(sheet.A20, 1)
        self.assertEqual(sheet.A21, 1)
        self.assertEqual(sheet.A22, 1)
        self.assertEqual(sheet.A23, 1)
        self.assertEqual(sheet.A24, 1)

    def testRemoveRows(self):
        """ Removing rows -- check renaming of internal cells """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('A3', '123')
        sheet.set('A1', '=A3')
        sheet.removeRows('2', 1)
        self.assertEqual(sheet.getContents("A1"),"=A2")

    def testInsertRows(self):
        """ Inserting rows -- check renaming of internal cells """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('B1', '=B2')
        sheet.set('B2', '124')
        sheet.insertRows('2', 1)
        self.assertEqual(sheet.getContents("B1"),"=B3")

    def testRenameAlias(self):
        """ Test renaming of alias1 to alias2 in a spreadsheet """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('B1', '124')
        sheet.setAlias('B1', 'alias1')
        sheet.set('B2', '=alias1')
        self.doc.recompute()
        self.assertEqual(sheet.get("alias1"), 124)
        self.assertEqual(sheet.get("B1"), 124)
        self.assertEqual(sheet.get("B2"), 124)
        sheet.setAlias('B1', 'alias2')
        self.doc.recompute()
        self.assertEqual(sheet.get("alias2"), 124)
        self.assertEqual(sheet.getContents("B2"),"=alias2")

    def testRenameAlias2(self):
        """ Test renaming of alias1 to alias2 in a spreadsheet, when referenced from another object """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('B1', '124')
        sheet.setAlias('B1', 'alias1')
        box = self.doc.addObject('Part::Box', 'Box')
        box.setExpression('Length', 'Spreadsheet.alias1')
        sheet.setAlias('B1', 'alias2')
        self.assertEqual(box.ExpressionEngine[0][1], "Spreadsheet.alias2");

    def testRenameAlias3(self):
        """ Test renaming of document object referenced from another object """
        sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.set('B1', '124')
        sheet.setAlias('B1', 'alias1')
        box = self.doc.addObject('Part::Box', 'Box')
        box.setExpression('Length', 'Spreadsheet.alias1')
        sheet.Label = "Params"
        self.assertEqual(box.ExpressionEngine[0][1], "Params.alias1");

    def testAlias(self):
        """ Playing with aliases """
        sheet = self.doc.addObject("Spreadsheet::Sheet","Calc")
        sheet.setAlias("A1","Test")
        self.assertEqual(sheet.getAlias("A1"),"Test")

        sheet.set("A1","4711")
        self.doc.recompute()
        self.assertEqual(sheet.get("Test"),4711)
        self.assertEqual(sheet.get("Test"),sheet.get("A1"))

    def testAmbiguousAlias(self):
        """ Try to set the same alias twice (bug #2402) """
        sheet = self.doc.addObject("Spreadsheet::Sheet","Calc")
        sheet.setAlias("A1","Test")
        try:
            sheet.setAlias("A2","Test")
            self.fail("An ambiguous alias was set which shouldn't be allowed")
        except:
            self.assertEqual(sheet.getAlias("A2"),None)

    def testClearAlias(self):
        """ This was causing a crash """
        sheet = self.doc.addObject("Spreadsheet::Sheet","Calc")
        sheet.setAlias("A1","Test")
        sheet.setAlias("A1","")
        self.assertEqual(sheet.getAlias("A1"),None)

    def testSetInvalidAlias(self):
        """ Try to use a cell address as alias name """
        sheet = self.doc.addObject("Spreadsheet::Sheet","Calc")
        try:
            sheet.setAlias("A1","B1")
        except:
            self.assertEqual(sheet.getAlias("A1"),None)
        else:
            self.fail("A cell address was used as alias which shouldn't be allowed")

    def testPlacementName(self):
        """ Object name is equal to property name (bug #2389) """
        if not FreeCAD.GuiUp:
            return

        import FreeCADGui
        o = self.doc.addObject("Part::FeaturePython","Placement")
        FreeCADGui.Selection.addSelection(o)

    def testInvoluteGear(self):
        """ Support of boolean or integer values """
        try:
            import InvoluteGearFeature
        except ImportError:
            return
        InvoluteGearFeature.makeInvoluteGear('InvoluteGear')
        self.doc.recompute()
        sketch=self.doc.addObject('Sketcher::SketchObject','Sketch')
        sketch.addGeometry(Part.Line(v(0,0,0),v(10,10,0)),False)
        sketch.addConstraint(Sketcher.Constraint('Distance',0,65.285388))
        sketch.setExpression('Constraints[0]', 'InvoluteGear.NumberOfTeeth')
        self.doc.recompute()
        self.assertIn('Up-to-date',sketch.State)

    def testSketcher(self):
        """ Mixup of Label and Name (bug #2407)"""
        sketch=self.doc.addObject('Sketcher::SketchObject','Sketch')
        sheet=self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
        sheet.setAlias('A1', 'Length')
        self.doc.recompute()
        sheet.set('A1', '47,11')
        self.doc.recompute()

        index=sketch.addGeometry(Part.Line(v(0,0,0),v(10,10,0)),False)
        sketch.addConstraint(Sketcher.Constraint('Distance',index,14.0))
        self.doc.recompute()
        sketch.setExpression('Constraints[0]', u'Spreadsheet.Length')
        self.doc.recompute()
        sheet.Label="Calc"
        self.doc.recompute()
        self.assertEqual(sketch.ExpressionEngine[0][1],'Calc.Length')
        self.assertIn('Up-to-date',sketch.State)

    def testCrossDocumentLinks(self):
        """ Expressions accross files are not saved (bug #2442) """

        # Create a box
        box = self.doc.addObject('Part::Box', 'Box')

        # Create a second document with a cylinder
        doc2 = FreeCAD.newDocument()
        cylinder = doc2.addObject('Part::Cylinder', 'Cylinder')
        cylinder.setExpression('Radius', 'cube#Cube.Height')

        # Save and close first document
        self.doc.saveAs(self.TempPath + os.sep + 'cube.fcstd')
        FreeCAD.closeDocument(self.doc.Name)

        # Save and close second document
        doc2.saveAs(self.TempPath + os.sep + 'cylinder.fcstd')
        FreeCAD.closeDocument(doc2.Name)

        # Open both documents again
        self.doc = FreeCAD.openDocument(self.TempPath + os.sep + 'cube.fcstd')
        doc2 = FreeCAD.openDocument(self.TempPath + os.sep + 'cylinder.fcstd')

        # Check reference between them
        self.assertEqual(doc2.getObject('Cylinder').ExpressionEngine[0][1], 'cube#Cube.Height')

        # Close second document
        FreeCAD.closeDocument(doc2.Name)

    def tearDown(self):
        #closing doc
        FreeCAD.closeDocument(self.doc.Name)
<|endoftext|>


<commit_before>import datetime
import random

import irc.client

def test_version():
	assert 'VERSION' in vars(irc.client)
	assert isinstance(irc.client.VERSION, tuple)
	assert irc.client.VERSION, "No VERSION detected."

def test_delayed_command_order():
	"""
	delayed commands should be sorted by delay time
	"""
	null = lambda: None
	delays = [random.randint(0, 99) for x in xrange(5)]
	cmds = sorted([
		irc.client.DelayedCommand(delay, null, tuple())
		for delay in delays
	])
	assert [c.delay.seconds for c in cmds] == sorted(delays)

def test_periodic_command_fixed_delay():
	"""
	Test that we can construct a periodic command with a fixed initial
	delay.
	"""
	fd = irc.client.PeriodicCommandFixedDelay.at_time(
		at = datetime.datetime.now(),
		delay = datetime.timedelta(seconds=2),
		function = lambda: None,
		arguments = [],
		)
	assert fd.due() == True
	assert fd.next().due() == False
<commit_msg>Added a couple of tests for privmsg, one of which fails due to issue #4.<commit_after>import datetime
import random

import pytest
import mock

import irc.client

def test_version():
	assert 'VERSION' in vars(irc.client)
	assert isinstance(irc.client.VERSION, tuple)
	assert irc.client.VERSION, "No VERSION detected."

def test_delayed_command_order():
	"""
	delayed commands should be sorted by delay time
	"""
	null = lambda: None
	delays = [random.randint(0, 99) for x in xrange(5)]
	cmds = sorted([
		irc.client.DelayedCommand(delay, null, tuple())
		for delay in delays
	])
	assert [c.delay.seconds for c in cmds] == sorted(delays)

def test_periodic_command_fixed_delay():
	"""
	Test that we can construct a periodic command with a fixed initial
	delay.
	"""
	fd = irc.client.PeriodicCommandFixedDelay.at_time(
		at = datetime.datetime.now(),
		delay = datetime.timedelta(seconds=2),
		function = lambda: None,
		arguments = [],
		)
	assert fd.due() == True
	assert fd.next().due() == False

@mock.patch('irc.client.socket')
def test_privmsg_sends_msg(socket_mod):
	server = irc.client.IRC().server()
	server.connect('foo', 6667, 'bestnick')
	server.privmsg('#best-channel', 'You are great')
	socket_mod.socket.return_value.send.assert_called_with(
		'PRIVMSG #best-channel :You are great\r\n')

@mock.patch('irc.client.socket')
def test_privmsg_fails_on_embedded_carriage_returns(socket_mod):
	server = irc.client.IRC().server()
	server.connect('foo', 6667, 'bestnick')
	with pytest.raises(ValueError):
		server.privmsg('#best-channel', 'You are great\nSo are you')
<|endoftext|>


<commit_before>'''
Created on 14.06.2012

@author: moschlar
'''
from sauce.tests import TestController

# The USERS and PATHS variables are interpreted in a matrix-like style
# USERS contain valid usernames for the REMOTE_USER variable.
# The usernames should be sorted hierarchically to make configuration
# simpler
# PATHS contains a path and expected statuses for the above usernames.
# (statuses that are not specified aren't tested, of course)
# If path[0] is a tuple or list, it will be used for recursive url generation

#   PATH                    ANONYMOUS   STUDENT     TEACHERASS  TEACHER     MANAGER
USERS = (                   None,       'studenta1','teacherass','teacher','manager')
PATHS = (
    # All the static pages
    (('', ['/', '/index', '/about', '/contact', '/login',
        ('/docs', ['', '/', '/tests', '/deutsch', '/tips', '/Changelog', '/Roadmap'])
    ]),
                            None),
    # The basic event-sheet-assignment pages
    (('/events', ['', '/',
        ('/eip12', ['/',
            ('/sheets', ['', '/',
                ('/1', ['', '/',
                    ('/assignments', ['', '/', '/1'])
                ])
            ])
        ])
    ]),
                            None),
    (('/events/eip12/sheets/1/assignments/1/similarity', ['', '/', '/dendrogram', '/dendrogram.png']),
                            401,        403,        None),
    ('/user',               401,        None),
    ('/admin',              401,        403,        403,        403,        None),
    ('/events/eip12/admin', 401,        403,        403,        None),
    (('/events/eip12/lessons/1', ['', '/',
        ('/submissions', ['', '/',
            ('/sheet/1', ['', '/assignment/1'])
        ])
    ]),
                            401,        403,        None),
    (('/events/eip12/lessons/2', ['', '/',
        ('/submissions', ['', '/',
            ('/sheet/1', ['', '/assignment/1'])
        ])
    ]),
                            401,        403,        403,        None),
    # WARNING: Submission ids come from websetup/data/course.py
    # A submission of studenta1, belonging to the lesson of teacherass
    (('/submissions/25', ['', '/', '/show', '/edit', '/result']),
                            401,        None,       None,       None,       None),
    ('/submissions/25/judge',
                            401,        403,        None),
    # Team member of studenta1 submission
    (('/submissions/26', ['', '/', '/show', '/result']),
                            401,        None),
    ('/submissions/26/edit',
                            401,        403,        None),
    # A submission of studentc1, NOT belonging to the lesson of teacherass
    (('/submissions/27', ['', '/', '/show', '/edit', '/result', '/judge']),
                            401,        403,        403,        None,       None),
    )

class TestSite(TestController):

    def _generate_paths(self, base):
        a, b = base
        for c in b:
            if isinstance(c, tuple):
                for d in self._generate_paths(c):
                    yield a + d
            else:
                yield a + c

    def _test_path(self, path, user=None, status=None):
        if user:
            env = dict(REMOTE_USER=user)
        else:
            env = None
        self.app.get(path, extra_environ=env, status=status)

    def test_paths(self):
        for path in PATHS:
            p, stati = path[0], path[1:]

            if isinstance(p, tuple):
                pp = self._generate_paths(p)
            else:
                pp = (p, )

            for p in pp:
                for i, status in enumerate(stati):
                    if status is not False:
                        user = USERS[i]
                        yield self._test_path, p, user, status

<commit_msg>Badass speedup for simple site test cases!<commit_after>'''
Created on 14.06.2012

@author: moschlar
'''

from os import path
import sys

from tg import config
from paste.deploy import loadapp
from paste.script.appinstall import SetupCommand
from webtest import TestApp

from sauce.tests import teardown_db
from sauce import model

app = None

def setUpModule():
    # Loading the application:
    conf_dir = config.here
    wsgiapp = loadapp('config:test.ini#main_without_authn',
                      relative_to=conf_dir)
    global app
    app = TestApp(wsgiapp)
    # Setting it up:
    test_file = path.join(conf_dir, 'test.ini')
    cmd = SetupCommand('setup-app')
    cmd.run([test_file])

def tearDownModule():
    model.DBSession.remove()
    teardown_db()

# The USERS and PATHS variables are interpreted in a matrix-like style
# USERS contain valid usernames for the REMOTE_USER variable.
# The usernames should be sorted hierarchically to make configuration
# simpler
# PATHS contains a path and expected statuses for the above usernames.
# (statuses that are not specified aren't tested, of course)
# If path[0] is a tuple or list, it will be used for recursive url generation

#   PATH                    ANONYMOUS   STUDENT     TEACHERASS  TEACHER     MANAGER
USERS = (                   None,       'studenta1','teacherass','teacher','manager')
PATHS = (
    # All the static pages
    (('', ['/', '/index', '/about', '/contact', '/login',
        ('/docs', ['', '/', '/tests', '/deutsch', '/tips', '/Changelog', '/Roadmap'])
    ]),
                            None),
    # The basic event-sheet-assignment pages
    (('/events', ['', '/',
        ('/eip12', ['/',
            ('/sheets', ['', '/',
                ('/1', ['', '/',
                    ('/assignments', ['', '/', '/1'])
                ])
            ])
        ])
    ]),
                            None),
    (('/events/eip12/sheets/1/assignments/1/similarity', ['', '/', '/dendrogram', '/dendrogram.png']),
                            401,        403,        None),
    ('/user',               401,        None),
    ('/admin',              401,        403,        403,        403,        None),
    ('/events/eip12/admin', 401,        403,        403,        None),
    (('/events/eip12/lessons/1', ['', '/',
        ('/submissions', ['', '/',
            ('/sheet/1', ['', '/assignment/1'])
        ])
    ]),
                            401,        403,        None),
    (('/events/eip12/lessons/2', ['', '/',
        ('/submissions', ['', '/',
            ('/sheet/1', ['', '/assignment/1'])
        ])
    ]),
                            401,        403,        403,        None),
    # WARNING: Submission ids come from websetup/data/course.py
    # A submission of studenta1, belonging to the lesson of teacherass
    (('/submissions/25', ['', '/', '/show', '/edit', '/result']),
                            401,        None,       None,       None,       None),
    ('/submissions/25/judge',
                            401,        403,        None),
    # Team member of studenta1 submission
    (('/submissions/26', ['', '/', '/show', '/result']),
                            401,        None),
    ('/submissions/26/edit',
                            401,        403,        None),
    # A submission of studentc1, NOT belonging to the lesson of teacherass
    (('/submissions/27', ['', '/', '/show', '/edit', '/result', '/judge']),
                            401,        403,        403,        None,       None),
    )

class TestSite(object):

    def _generate_paths(self, base):
        a, b = base
        for c in b:
            if isinstance(c, tuple):
                for d in self._generate_paths(c):
                    yield a + d
            else:
                yield a + c

    def _test_path(self, path, user=None, status=None):
        if user:
            env = dict(REMOTE_USER=user)
        else:
            env = None
        app.get(path, extra_environ=env, status=status)

    def test_paths(self):
        for path in PATHS:
            p, stati = path[0], path[1:]

            if isinstance(p, tuple):
                pp = self._generate_paths(p)
            else:
                pp = (p, )

            for p in pp:
                for i, status in enumerate(stati):
                    if status is not False:
                        user = USERS[i]
                        yield self._test_path, p, user, status

<|endoftext|>


<commit_before>from rest_framework import serializers

from callback_request.models import CallbackRequest, CallEntry
from callback_schedule.models import CallbackManagerPhone

class CallbackSerializer(serializers.ModelSerializer):
    class Meta:
        model = CallbackRequest
        fields = ('id', 'phone', 'name', 'comment', 'immediate')

    def validate(self, data):
        immediate = data['immediate']
        if not immediate and not data.get('comment', None):
            raise serializers.ValidationError('Enter comment')

        if immediate:
            phones = CallbackManagerPhone.get_available_phones()
            if not phones.exists():
                raise serializers.ValidationError('No free managers')
        return data

class CallbackRequestAdminSerializer(CallbackSerializer):
    class Meta:
        model = CallbackRequest
        fields = ('id', 'phone', 'comment', 'client', 'created', 'name', 'completed', 'date', 'right_phone',
                  'immediate')
        read_only_fields = ('id', 'phone', 'comment', 'created', 'name', 'immediate', 'client')

class CallEntrySerializer(serializers.ModelSerializer):
    class Meta:
        model = CallEntry
        fields = '__all__'

class ManagersAvailabilitySerializer(serializers.Serializer):
    available = serializers.BooleanField()
    nearest = serializers.DateTimeField(required=False)
<commit_msg>fixed callback serializer<commit_after>from rest_framework import serializers

from callback_request.models import CallbackRequest, CallEntry
from callback_schedule.models import CallbackManagerPhone

class CallbackSerializer(serializers.ModelSerializer):
    class Meta:
        model = CallbackRequest
        fields = ('id', 'phone', 'name', 'comment', 'immediate')

    def validate(self, data):
        immediate = data['immediate']
        if not immediate and not data.get('comment', None):
            raise serializers.ValidationError('Enter comment')

        if immediate:
            phones = CallbackManagerPhone.get_available_phones()
            if not phones.exists():
                raise serializers.ValidationError('No free managers')
        return data

class CallbackRequestAdminSerializer(serializers.ModelSerializer):
    class Meta:
        model = CallbackRequest
        fields = ('id', 'phone', 'comment', 'client', 'created', 'name', 'completed', 'date', 'right_phone',
                  'immediate')
        read_only_fields = ('id', 'phone', 'comment', 'created', 'name', 'immediate', 'client')

class CallEntrySerializer(serializers.ModelSerializer):
    class Meta:
        model = CallEntry
        fields = '__all__'

class ManagersAvailabilitySerializer(serializers.Serializer):
    available = serializers.BooleanField()
    nearest = serializers.DateTimeField(required=False)
<|endoftext|>