<commit_before># TODO: Use dictionary in name
import strictyaml
from os import environ
from os.path import dirname, join

from .constants import (
    HOST,
    L,
    VARIABLE_ID_PATTERN,
    VARIABLE_TEXT_PATTERN,
    VIEWS)
from .exceptions import CrossComputeConfigurationError

def get_crosscompute_host():
    return get_environment_variable('CROSSCOMPUTE_HOST', HOST)

def get_crosscompute_token():
    return get_environment_variable('CROSSCOMPUTE_TOKEN', required=True)

def get_environment_variable(name, default=None, required=False):
    # TODO: Consider renaming to get_environment_value
    try:
        value = environ[name]
    except KeyError:
        if required:
            exit(f'{name} is required in the environment')
        value = default
    return value

def load_tool_configuration(path):
    text = open(path, 'rt').read()
    dictionary = strictyaml.load(text).data
    if not isinstance(dictionary, dict):
        raise CrossComputeConfigurationError({'configuration': 'is invalid'})
    try:
        protocol = dictionary.pop('protocol')
    except KeyError:
        raise CrossComputeConfigurationError({'protocol': 'is required'})
    try:
        normalize_tool_configuration = {
            '0.8.3': normalize_tool_configuration_from_protocol_0_8_3,
        }[protocol]
    except KeyError:
        raise CrossComputeConfigurationError({'protocol': 'is invalid'})
    folder = dirname(path)
    return normalize_tool_configuration(dictionary, folder)

def normalize_tool_configuration_from_protocol_0_8_3(dictionary, folder):
    d = {}
    if 'id' in dictionary:
        d['id'] = dictionary['id']
    if 'slug' in dictionary:
        d['slug'] = dictionary['slug']
    try:
        d['name'] = dictionary['name']
        d['version'] = dictionary['version']
    except KeyError as e:
        raise CrossComputeConfigurationError({e.args[0]: 'is required'})
    d['input'] = normalize_put_configuration('input', dictionary, folder)
    d['output'] = normalize_put_configuration('output', dictionary, folder)
    d['tests'] = normalize_tests_configuration('tests', dictionary)
    d['script'] = normalize_script_configuration('script', dictionary)
    d['environment'] = normalize_environment_configuration(
        'environment', dictionary)
    return d

def normalize_put_configuration(key, dictionary, folder=None):
    d = {}

    try:
        put_dictionary = dictionary[key]
    except KeyError:
        L.warning(f'missing {key} configuration')
        return d

    try:
        variable_dictionaries = put_dictionary['variables']
    except KeyError:
        L.warning(f'missing {key} variables configuration')
        variable_dictionaries = []
    variable_dictionaries = normalize_variable_dictionaries(
        variable_dictionaries)
    if variable_dictionaries:
        d['variables'] = variable_dictionaries

    try:
        template_dictionaries = put_dictionary['templates']
    except KeyError:
        L.warning(f'missing {key} templates configuration')
        template_dictionaries = []
    template_dictionaries = normalize_template_dictionaries(
        template_dictionaries, variable_dictionaries, folder)
    if template_dictionaries:
        d['templates'] = template_dictionaries

    return d

def normalize_tests_configuration(key, dictionary):
    try:
        raw_test_dictionaries = dictionary[key]
    except KeyError:
        L.warning(f'missing {key} configuration')
    return normalize_test_dictionaries(raw_test_dictionaries)

def normalize_script_configuration(key, dictionary):
    try:
        raw_script_dictionary = dictionary[key]
    except KeyError:
        L.warning(f'missing {key} configuration')
    return normalize_script_dictionary(raw_script_dictionary)

def normalize_environment_configuration(key, dictionary):
    try:
        raw_environment_dictionary = dictionary[key]
    except KeyError:
        L.warning(f'missing {key} configuration')
    return normalize_environment_dictionary(raw_environment_dictionary)

def normalize_variable_dictionaries(raw_variable_dictionaries):
    variable_dictionaries = []
    for raw_variable_dictionary in raw_variable_dictionaries:
        try:
            variable_dictionaries.append({
                'id': raw_variable_dictionary['id'],
                'name': raw_variable_dictionary['name'],
                'view': raw_variable_dictionary['view'],
                'path': raw_variable_dictionary['path'],
            })
        except KeyError as e:
            raise CrossComputeConfigurationError({
                e.args[0]: 'is required for each variable'})
    return variable_dictionaries

def normalize_template_dictionaries(
        raw_template_dictionaries, variable_dictionaries, folder=None):
    template_dictionaries = []
    for raw_template_dictionary in raw_template_dictionaries:
        try:
            template_id = raw_template_dictionary['id']
            template_name = raw_template_dictionary['name']
        except KeyError as e:
            raise CrossComputeConfigurationError({
                e.args[0]: 'is required for each template'})
        template_blocks = normalize_blocks_configuration(
            'blocks', raw_template_dictionary, variable_dictionaries, folder)
        if not template_blocks:
            continue
        template_dictionaries.append({
            'id': template_id,
            'name': template_name,
            'blocks': template_blocks,
        })
    if not template_dictionaries:
        template_dictionaries.append({
            'id': 'generated',
            'name': 'Generated',
            'blocks': [{'id': _['id']} for _ in variable_dictionaries],
        })
    return template_dictionaries

def normalize_blocks_configuration(key, dictionary, variables, folder=None):
    blocks = []
    if 'blocks' in dictionary:
        blocks = dictionary['blocks']
    elif 'path' in dictionary and folder is not None:
        template_path = join(folder, dictionary['path'])
        blocks = load_blocks(template_path)
    return normalize_blocks(blocks, variables)

def load_blocks(template_path):
    try:
        template_text = open(template_path, 'rt').read()
    except IOError:
        raise CrossComputeConfigurationError({
            'path': f'is invalid for template {template_path}'})
    return parse_blocks(template_text)

def parse_blocks(template_text):
    blocks = []
    for text in VARIABLE_TEXT_PATTERN.split(template_text):
        text = text.strip()
        if not text:
            continue
        match = VARIABLE_ID_PATTERN.match(text)
        if match:
            variable_id = match.group(1)
            blocks.append({'id': variable_id})
            continue
        blocks.append({'view': 'markdown', 'data': {'value': text}})
    return blocks

def normalize_blocks(blocks, variables):
    ds = []
    for block in blocks:
        if 'id' in block:
            ds.append({'id': block['id']})
            continue
        d = {}
        try:
            raw_view = block['view']
            raw_data = block['data']
        except KeyError as e:
            raise CrossComputeConfigurationError({
                e.args[0]: 'is required for each block that lacks an id'})
        view = normalize_view(raw_view)
        data = normalize_data(raw_data, view)
        d['view'] = view
        d['data'] = data
        ds.append(d)
    return ds

def normalize_test_dictionaries(raw_test_dictionaries):
    return [{
        'id': _['id'],
        'name': _['name'],
        'path': _['path'],
    } for _ in raw_test_dictionaries]

def normalize_script_dictionary(raw_script_dictionary):
    uri = raw_script_dictionary['uri']
    folder = raw_script_dictionary['folder']
    command = raw_script_dictionary['command']
    return {
        'uri': uri,
        'folder': folder,
        'command': command,
    }

def normalize_environment_dictionary(raw_environment_dictionary):
    image = raw_environment_dictionary['image']
    processor = raw_environment_dictionary['processor']
    memory = raw_environment_dictionary['memory']
    return {
        'image': image,
        'processor': processor,
        'memory': memory,
    }

def normalize_view(raw_view):
    if not isinstance(raw_view, str):
        raise CrossComputeConfigurationError({'view': 'must be a string'})

    view = raw_view.lower()
    if view not in VIEWS:
        raise CrossComputeConfigurationError({
            'view': 'must be one of ' + ' '.join(VIEWS)})
    return view

def normalize_data(raw_data, view):
    # TODO: Consider renaming raw_data to raw_data_dictionary
    if not isinstance(raw_data, dict):
        raise CrossComputeConfigurationError({
            'data': 'must be a dictionary'})
    if 'value' not in raw_data and 'file' not in raw_data:
        raise CrossComputeConfigurationError({
            'data': 'must contain either a file or value'})

    data = {}
    if 'value' in raw_data:
        data['value'] = normalize_value(raw_data['value'], view)
    elif 'file' in raw_data:
        data['file'] = normalize_file(raw_data['file'])
    return data

def normalize_value(raw_value, view=None):
    # TODO: Normalize value based on view
    return raw_value

def normalize_file(raw_file):
    # TODO: Normalize file
    return raw_file

def get_resource_url(host, resource_name, resource_id=None):
    url = host + '/' + resource_name
    if resource_id:
        url += '/' + resource_id
    return url + '.json'
<commit_msg>Fix issue<commit_after># TODO: Use dictionary in name
import strictyaml
from os import environ
from os.path import dirname, join

from .constants import (
    HOST,
    L,
    VARIABLE_ID_PATTERN,
    VARIABLE_TEXT_PATTERN,
    VIEWS)
from .exceptions import CrossComputeConfigurationError

def get_crosscompute_host():
    return get_environment_variable('CROSSCOMPUTE_HOST', HOST)

def get_crosscompute_token():
    return get_environment_variable('CROSSCOMPUTE_TOKEN', required=True)

def get_environment_variable(name, default=None, required=False):
    # TODO: Consider renaming to get_environment_value
    try:
        value = environ[name]
    except KeyError:
        if required:
            exit(f'{name} is required in the environment')
        value = default
    return value

def load_tool_configuration(path):
    text = open(path, 'rt').read()
    dictionary = strictyaml.load(text).data
    if not isinstance(dictionary, dict):
        raise CrossComputeConfigurationError({'configuration': 'is invalid'})
    try:
        protocol = dictionary.pop('protocol')
    except KeyError:
        raise CrossComputeConfigurationError({'protocol': 'is required'})
    try:
        normalize_tool_configuration = {
            '0.8.3': normalize_tool_configuration_from_protocol_0_8_3,
        }[protocol]
    except KeyError:
        raise CrossComputeConfigurationError({'protocol': 'is invalid'})
    folder = dirname(path)
    return normalize_tool_configuration(dictionary, folder)

def normalize_tool_configuration_from_protocol_0_8_3(dictionary, folder):
    d = {}
    if 'id' in dictionary:
        d['id'] = dictionary['id']
    if 'slug' in dictionary:
        d['slug'] = dictionary['slug']
    try:
        d['name'] = dictionary['name']
        d['version'] = dictionary['version']
    except KeyError as e:
        raise CrossComputeConfigurationError({e.args[0]: 'is required'})
    d['input'] = normalize_put_configuration('input', dictionary, folder)
    d['output'] = normalize_put_configuration('output', dictionary, folder)
    d['tests'] = normalize_tests_configuration('tests', dictionary)
    d['script'] = normalize_script_configuration('script', dictionary)
    d['environment'] = normalize_environment_configuration(
        'environment', dictionary)
    return d

def normalize_put_configuration(key, dictionary, folder=None):
    d = {}

    try:
        put_dictionary = dictionary[key]
    except KeyError:
        L.warning(f'missing {key} configuration')
        return d

    try:
        variable_dictionaries = put_dictionary['variables']
    except KeyError:
        L.warning(f'missing {key} variables configuration')
        variable_dictionaries = []
    variable_dictionaries = normalize_variable_dictionaries(
        variable_dictionaries)
    if variable_dictionaries:
        d['variables'] = variable_dictionaries

    try:
        template_dictionaries = put_dictionary['templates']
    except KeyError:
        L.warning(f'missing {key} templates configuration')
        template_dictionaries = []
    template_dictionaries = normalize_template_dictionaries(
        template_dictionaries, variable_dictionaries, folder)
    if template_dictionaries:
        d['templates'] = template_dictionaries

    return d

def normalize_tests_configuration(key, dictionary):
    try:
        raw_test_dictionaries = dictionary[key]
    except KeyError:
        L.warning(f'missing {key} configuration')
        raw_test_dictionaries = []
    return normalize_test_dictionaries(raw_test_dictionaries)

def normalize_script_configuration(key, dictionary):
    try:
        raw_script_dictionary = dictionary[key]
    except KeyError:
        L.warning(f'missing {key} configuration')
        raw_script_dictionary = {}
    return normalize_script_dictionary(raw_script_dictionary)

def normalize_environment_configuration(key, dictionary):
    try:
        raw_environment_dictionary = dictionary[key]
    except KeyError:
        L.warning(f'missing {key} configuration')
        raw_environment_dictionary = {}
    return normalize_environment_dictionary(raw_environment_dictionary)

def normalize_variable_dictionaries(raw_variable_dictionaries):
    variable_dictionaries = []
    for raw_variable_dictionary in raw_variable_dictionaries:
        try:
            variable_dictionaries.append({
                'id': raw_variable_dictionary['id'],
                'name': raw_variable_dictionary['name'],
                'view': raw_variable_dictionary['view'],
                'path': raw_variable_dictionary['path'],
            })
        except KeyError as e:
            raise CrossComputeConfigurationError({
                e.args[0]: 'is required for each variable'})
    return variable_dictionaries

def normalize_template_dictionaries(
        raw_template_dictionaries, variable_dictionaries, folder=None):
    template_dictionaries = []
    for raw_template_dictionary in raw_template_dictionaries:
        try:
            template_id = raw_template_dictionary['id']
            template_name = raw_template_dictionary['name']
        except KeyError as e:
            raise CrossComputeConfigurationError({
                e.args[0]: 'is required for each template'})
        template_blocks = normalize_blocks_configuration(
            'blocks', raw_template_dictionary, variable_dictionaries, folder)
        if not template_blocks:
            continue
        template_dictionaries.append({
            'id': template_id,
            'name': template_name,
            'blocks': template_blocks,
        })
    if not template_dictionaries:
        template_dictionaries.append({
            'id': 'generated',
            'name': 'Generated',
            'blocks': [{'id': _['id']} for _ in variable_dictionaries],
        })
    return template_dictionaries

def normalize_blocks_configuration(key, dictionary, variables, folder=None):
    blocks = []
    if 'blocks' in dictionary:
        blocks = dictionary['blocks']
    elif 'path' in dictionary and folder is not None:
        template_path = join(folder, dictionary['path'])
        blocks = load_blocks(template_path)
    return normalize_blocks(blocks, variables)

def load_blocks(template_path):
    try:
        template_text = open(template_path, 'rt').read()
    except IOError:
        raise CrossComputeConfigurationError({
            'path': f'is invalid for template {template_path}'})
    return parse_blocks(template_text)

def parse_blocks(template_text):
    blocks = []
    for text in VARIABLE_TEXT_PATTERN.split(template_text):
        text = text.strip()
        if not text:
            continue
        match = VARIABLE_ID_PATTERN.match(text)
        if match:
            variable_id = match.group(1)
            blocks.append({'id': variable_id})
            continue
        blocks.append({'view': 'markdown', 'data': {'value': text}})
    return blocks

def normalize_blocks(blocks, variables):
    ds = []
    for block in blocks:
        if 'id' in block:
            ds.append({'id': block['id']})
            continue
        d = {}
        try:
            raw_view = block['view']
            raw_data = block['data']
        except KeyError as e:
            raise CrossComputeConfigurationError({
                e.args[0]: 'is required for each block that lacks an id'})
        view = normalize_view(raw_view)
        data = normalize_data(raw_data, view)
        d['view'] = view
        d['data'] = data
        ds.append(d)
    return ds

def normalize_test_dictionaries(raw_test_dictionaries):
    return [{
        'id': _['id'],
        'name': _['name'],
        'path': _['path'],
    } for _ in raw_test_dictionaries]

def normalize_script_dictionary(raw_script_dictionary):
    uri = raw_script_dictionary['uri']
    folder = raw_script_dictionary['folder']
    command = raw_script_dictionary['command']
    return {
        'uri': uri,
        'folder': folder,
        'command': command,
    }

def normalize_environment_dictionary(raw_environment_dictionary):
    image = raw_environment_dictionary['image']
    processor = raw_environment_dictionary['processor']
    memory = raw_environment_dictionary['memory']
    return {
        'image': image,
        'processor': processor,
        'memory': memory,
    }

def normalize_view(raw_view):
    if not isinstance(raw_view, str):
        raise CrossComputeConfigurationError({'view': 'must be a string'})

    view = raw_view.lower()
    if view not in VIEWS:
        raise CrossComputeConfigurationError({
            'view': 'must be one of ' + ' '.join(VIEWS)})
    return view

def normalize_data(raw_data, view):
    # TODO: Consider renaming raw_data to raw_data_dictionary
    if not isinstance(raw_data, dict):
        raise CrossComputeConfigurationError({
            'data': 'must be a dictionary'})
    if 'value' not in raw_data and 'file' not in raw_data:
        raise CrossComputeConfigurationError({
            'data': 'must contain either a file or value'})

    data = {}
    if 'value' in raw_data:
        data['value'] = normalize_value(raw_data['value'], view)
    elif 'file' in raw_data:
        data['file'] = normalize_file(raw_data['file'])
    return data

def normalize_value(raw_value, view=None):
    # TODO: Normalize value based on view
    return raw_value

def normalize_file(raw_file):
    # TODO: Normalize file
    return raw_file

def get_resource_url(host, resource_name, resource_id=None):
    url = host + '/' + resource_name
    if resource_id:
        url += '/' + resource_id
    return url + '.json'
<|endoftext|>


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

'''
basic operation for a game(like a user does)
'''

import os

from airtest import base
from appium import webdriver

DEBUG = os.getenv("DEBUG")=="true"
log = base.getLogger('ios')

def getMem(serialno, package):
    '''
    @param package(string): android package name
    @return float: the memory, unit MB
    '''
    pass

def getCpu(serialno, package):
    '''
    @param package(string): android package name
    @return float: the cpu usage
    '''
    pass

# from zope.interface.declarations import implementer
# from airtest import interface

#@implementer(interface.IDevice)
class Device(object):
    def __init__(self, serialno=None):
        self.url = 'http://127.0.0.1:4723/wd/hub'
        self.driver = webdriver.Remote(
            command_executor=self.url,
            desired_capabilities={
                'platformName': 'iOS'
            }
        )

    def snapshot(self, filename):
        ''' save screen snapshot '''
        log.debug('start take snapshot')
        self.driver.save_screenshot(filename)
        log.debug('finish take snapshot and save to '+filename)

    def touch(self, x, y, eventType=None):
        '''
        touch screen at (x, y)
        multi finger operation not provided yet
        '''
        eventType=adbclient.DOWN_AND_UP
        log.debug('touch position %s', (x, y))
        self.driver.tap([(x, y)])

    def drag((x1, y1), (x2, y2), duration=0):
        '''
        Simulate drag from (x1, y1) to (x2, y2)
        multi finger operation not provided yet
        '''
        log.debug('drag from (%s, %s) to (%s, %s)' % (x1, y1, x2, y2))
        self.driver.swipe(x1, y1, x2, y2, duration)

    def shape(self):
        '''
        Get screen width and height
        '''
        screen_size = self.driver.get_window_size()
        width, height = screen_size["width"], screen_size["height"]
        return (width, height)

    def type(self, text):
        '''
        Input some text

        @param text: string (text want to type)
        '''
        print "not provided yet on ios"

    def getMem(self, appname):
        print "not provided yet on ios"
        return getMem(self._serialno, appname)

    def getCpu(self, appname):
        print "not provided yet on ios"
        return getCpu(self._serialno, appname)

<commit_msg>update ios.py for devsuit<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
basic operation for a game(like a user does)
'''

import os

from airtest import base
from appium import webdriver
from PIL import Image

DEBUG = os.getenv("DEBUG")=="true"
log = base.getLogger('ios')

def getMem(serialno, package):
    '''
    @param package(string): android package name
    @return float: the memory, unit MB
    '''
    pass

def getCpu(serialno, package):
    '''
    @param package(string): android package name
    @return float: the cpu usage
    '''
    pass

# from zope.interface.declarations import implementer
# from airtest import interface

#@implementer(interface.IDevice)
class Device(object):
    def __init__(self, serialno=None):
        self.url = 'http://127.0.0.1:4723/wd/hub'
        self.driver = webdriver.Remote(
            command_executor=self.url,
            desired_capabilities={
                'platformName': 'iOS'
            }
        )
        self._getShapeReal()
        self._getShapeInput()

    def snapshot(self, filename):
        ''' save screen snapshot '''
        log.debug('start take snapshot')
        self.driver.save_screenshot(filename)
        log.debug('finish take snapshot and save to '+filename)
        return filename

    def _cvtXY(self, x, y):
        """convert x,y from device real resolution to action input resolution"""
        x_input = x * self.width / self.width_real
        y_input = y * self.height / self.height_real
        log.debug("cvt %s,%s to %s,%s" % (x, y, x_input, y_input))
        return (int(x_input), int(y_input))

    def touch(self, x, y):
        '''
        touch screen at (x, y)
        multi finger operation not provided yet
        '''
        x, y = self._cvtXY(x, y)
        log.debug('touch position %s', (x, y))
        self.driver.tap([(x, y)])

    def drag(self, (x1, y1), (x2, y2), duration=0):
        '''
        Simulate drag from (x1, y1) to (x2, y2)
        multi finger operation not provided yet
        '''
        x1, y1 = self._cvtXY(x1, y1)
        x2, y2 = self._cvtXY(x2, y2)
        log.debug('drag from (%s, %s) to (%s, %s)' % (x1, y1, x2, y2))
        self.driver.swipe(x1, y1, x2, y2, duration)

    def _getShapeReal(self):
        '''
        Get screen real resolution
        '''
        screen_shot = self.snapshot("screen_shot.png")
        img = Image.open(screen_shot)
        self.width_real, self.height_real = img.size
        log.debug('IosDevice real resolution: width:{width}, height:{height}'.format(
            width=self.width_real, height=self.height_real))
        return img.size

    def _getShapeInput(self):
        '''
        Get screen shape for x, y Input
        '''
        screen_size = self.driver.get_window_size()
        self.width, self.height = screen_size["width"], screen_size["height"]
        log.debug('IosDevice input resolution: width:{width}, height:{height}'.format(
            width=self.width, height=self.height))
        return (self.width, self.height)

    def shape(self):
        '''
        Get screen width and height
        '''
        return (self.width_real, self.height_real)

    def type(self, text):
        '''
        Input some text

        @param text: string (text want to type)
        '''
        print "not provided yet on ios"

    def getMem(self, appname):
        print "not provided yet on ios"
        return getMem(self._serialno, appname)

    def getCpu(self, appname):
        print "not provided yet on ios"
        return getCpu(self._serialno, appname)

if __name__ == '__main__':
    d = Device()
    d.snapshot("test.png")
    d.touch(180, 720)
    print d.shape()
    width, height = d.shape()
    d.drag((width * 0.5, height * 0.5), (width * 0.1, height * 0.5))

<|endoftext|>


<commit_before>import pandas as pd
import numpy as np
from ..siu import create_sym_call, Symbolic
from functools import singledispatch

# TODO: move into siu
def register_symbolic(f):
    @f.register(Symbolic)
    def _dispatch_symbol(__data, *args, **kwargs):
        return create_sym_call(f, __data.source, *args, **kwargs)

    return f

@register_symbolic
@singledispatch
def fct_reorder(fct, x, func = np.median):
    s = pd.Series(x.values, index = fct)

    # for each cat, calc agg func, make values of ordered the codes
    ordered = s.groupby(level = 0).agg(func).sort_values()
    ordered[:] = np.arange(len(ordered))
    codes = ordered[s.index.values]
    return pd.Categorical.from_codes(codes, list(ordered.index))

@register_symbolic
@singledispatch
def fct_recode(fct, **kwargs):
    if not isinstance(fct, pd.Categorical):
        fct = pd.Categorical(fct)

    rev_kwargs = {v:k for k,v in kwargs.items()}
    fct.rename_categories(rev_kwargs)

@register_symbolic
@singledispatch
def fct_collapse(fct, recat, group_other = None):
    if not isinstance(fct, pd.Categorical):
        fct = pd.Categorical(fct)

    # each existing cat will map to a new one ----
    # need to know existing to new cat
    # need to know new cat to new code
    cat_to_new = {k: None for k in fct.categories}
    new_cat_set = {k: True for k in fct.categories}
    for new_name, v in recat.items():
        v = [v] if not np.ndim(v) else v
        for old_name in v:
            if cat_to_new[old_name] is not None:
                raise Exception("category %s was already re-assigned"%old_name)
            cat_to_new[old_name] = new_name
            del new_cat_set[old_name]
            new_cat_set[new_name] = True    # add new cat

    # collapse all unspecified cats to group_other if specified ----
    for k, v in cat_to_new.items():
        if v is None:
            if group_other is not None:
                new_cat_set[group_other] = True
                cat_to_new[k] = group_other
                del new_cat_set[k]
            else:
                cat_to_new[k] = k

    # map from old cat to new code ----
    # calculate new codes
    new_cat_set = {k: ii for ii, k in enumerate(new_cat_set)}
    # map old cats to them
    remap_code = {old: new_cat_set[new] for old, new in cat_to_new.items()}

    new_codes = fct.map(remap_code)
    new_cats = list(new_cat_set.keys())
    return pd.Categorical.from_codes(new_codes, new_cats)

@register_symbolic
@singledispatch
def fct_lump(fct, n = None, prop = None, w = None, other_level = "Other", ties = None):
    if ties is not None:
        raise NotImplementedError("ties is not implemented")

    if n is None and prop is None:
        raise NotImplementedError("Either n or prop must be specified")

    if prop is not None:
        raise NotImplementedError("prop arg is not implemented")

    keep_cats = _fct_lump_n_cats(fct, n, w, other_level, ties)
    return fct_collapse(fct, {k:k for k in keep_cats}, group_other = other_level)

def _fct_lump_n_cats(fct, n, w, other_level, ties):
    # TODO: currently always selects n, even if ties
    ascending = n < 0
    arr = w if w is not None else 1
    ser = pd.Series(arr, index = fct)
    sorted_arr = ser.groupby(level = 0).sum().sort_values(ascending = ascending)
    return sorted_arr.iloc[:abs(n)].index.values

<commit_msg>make fct_reorder more robust<commit_after>import pandas as pd
import numpy as np
from ..siu import create_sym_call, Symbolic
from functools import singledispatch

# TODO: move into siu
def register_symbolic(f):
    @f.register(Symbolic)
    def _dispatch_symbol(__data, *args, **kwargs):
        return create_sym_call(f, __data.source, *args, **kwargs)

    return f

@register_symbolic
@singledispatch
def fct_reorder(fct, x, func = np.median):
    x_vals = x.values if isinstance(x, pd.Series) else x
    s = pd.Series(x_vals, index = fct)

    # for each cat, calc agg func, make values of ordered the codes
    ordered = s.groupby(level = 0).agg(func).sort_values()
    ordered[:] = np.arange(len(ordered))
    codes = ordered[s.index.values]
    return pd.Categorical.from_codes(codes, list(ordered.index))

@register_symbolic
@singledispatch
def fct_recode(fct, **kwargs):
    if not isinstance(fct, pd.Categorical):
        fct = pd.Categorical(fct)

    rev_kwargs = {v:k for k,v in kwargs.items()}
    fct.rename_categories(rev_kwargs)

@register_symbolic
@singledispatch
def fct_collapse(fct, recat, group_other = None):
    if not isinstance(fct, pd.Categorical):
        fct = pd.Categorical(fct)

    # each existing cat will map to a new one ----
    # need to know existing to new cat
    # need to know new cat to new code
    cat_to_new = {k: None for k in fct.categories}
    new_cat_set = {k: True for k in fct.categories}
    for new_name, v in recat.items():
        v = [v] if not np.ndim(v) else v
        for old_name in v:
            if cat_to_new[old_name] is not None:
                raise Exception("category %s was already re-assigned"%old_name)
            cat_to_new[old_name] = new_name
            del new_cat_set[old_name]
            new_cat_set[new_name] = True    # add new cat

    # collapse all unspecified cats to group_other if specified ----
    for k, v in cat_to_new.items():
        if v is None:
            if group_other is not None:
                new_cat_set[group_other] = True
                cat_to_new[k] = group_other
                del new_cat_set[k]
            else:
                cat_to_new[k] = k

    # map from old cat to new code ----
    # calculate new codes
    new_cat_set = {k: ii for ii, k in enumerate(new_cat_set)}
    # map old cats to them
    remap_code = {old: new_cat_set[new] for old, new in cat_to_new.items()}

    new_codes = fct.map(remap_code)
    new_cats = list(new_cat_set.keys())
    return pd.Categorical.from_codes(new_codes, new_cats)

@register_symbolic
@singledispatch
def fct_lump(fct, n = None, prop = None, w = None, other_level = "Other", ties = None):
    if ties is not None:
        raise NotImplementedError("ties is not implemented")

    if n is None and prop is None:
        raise NotImplementedError("Either n or prop must be specified")

    if prop is not None:
        raise NotImplementedError("prop arg is not implemented")

    keep_cats = _fct_lump_n_cats(fct, n, w, other_level, ties)
    return fct_collapse(fct, {k:k for k in keep_cats}, group_other = other_level)

def _fct_lump_n_cats(fct, n, w, other_level, ties):
    # TODO: currently always selects n, even if ties
    ascending = n < 0
    arr = w if w is not None else 1
    ser = pd.Series(arr, index = fct)
    sorted_arr = ser.groupby(level = 0).sum().sort_values(ascending = ascending)
    return sorted_arr.iloc[:abs(n)].index.values

<|endoftext|>


<commit_before>import changeling.models

class ChangeAPI(object):
    def __init__(self, storage):
        self.storage = storage

    def list(self):
        for change_data in self.storage.list_changes():
            yield changeling.models.Change.from_dict(change_data)

    def get(self, change_id):
        change_data = self.storage.get_change(change_id)
        return changeling.models.Change.from_dict(change_data)

    def new(self, data):
        change = changeling.models.Change.from_dict(data)
        self.save(change)
        return change

    def save(self, change):
        self.storage.save_change(change.to_dict())

    def delete(self, change):
        self.storage.delete_change(change.id)

    @staticmethod
    def schema():
        return changeling.models.Change.schema
<commit_msg>Ignore entities in S3 that fail to validate properly<commit_after>import changeling.models

class ChangeAPI(object):
    def __init__(self, storage):
        self.storage = storage

    def list(self):
        for change_data in self.storage.list_changes():
            try:
                yield changeling.models.Change.from_dict(change_data)
            except changeling.exception.ValidationError:
                #TODO(bcwaldon): Log the offending object so it can
                # be removed from the datastore
                continue

    def get(self, change_id):
        change_data = self.storage.get_change(change_id)
        return changeling.models.Change.from_dict(change_data)

    def new(self, data):
        change = changeling.models.Change.from_dict(data)
        self.save(change)
        return change

    def save(self, change):
        self.storage.save_change(change.to_dict())

    def delete(self, change):
        self.storage.delete_change(change.id)

    @staticmethod
    def schema():
        return changeling.models.Change.schema
<|endoftext|>


<commit_before>import logging
import sys
import time

import pyscreenshot
from entrypoint2 import entrypoint
from pyscreenshot.util import proc

def run(force_backend, n, childprocess, bbox=None):
    sys.stdout.write("%-20s\t" % force_backend)
    sys.stdout.flush()  # before any crash

    if force_backend == "default":
        force_backend = None
    try:
        start = time.time()
        for _ in range(n):
            pyscreenshot.grab(
                backend=force_backend, childprocess=childprocess, bbox=bbox
            )
        end = time.time()
        dt = end - start
        s = "%-4.2g sec\t" % dt
        s += "(%5d ms per call)" % (1000.0 * dt / n)
        sys.stdout.write(s)
    finally:
        print("")

def run_all(n, childprocess_param, virtual_only=True, bbox=None):
    debug = True
    print("")
    print("n=%s" % n)
    print("------------------------------------------------------")

    if bbox:
        x1, y1, x2, y2 = map(str, bbox)
        bbox = ":".join(map(str, (x1, y1, x2, y2)))
        bboxpar = ["--bbox", bbox]
    else:
        bboxpar = []
    if debug:
        debugpar = ["--debug"]
    else:
        debugpar = []
    for x in ["default"] + pyscreenshot.backends():
        backendpar = ["--backend", x]
        if virtual_only and x == "gnome-screenshot":  # TODO: remove
            continue
        p = proc(
            "pyscreenshot.check.speedtest",
            ["--childprocess", childprocess_param] + bboxpar + debugpar + backendpar,
        )
        print(p.stdout)

@entrypoint
def speedtest(virtual_display=False, backend="", childprocess="", bbox=""):
    """Performance test of all back-ends.

    :param virtual_display: run with Xvfb
    :param bbox: bounding box coordinates x1:y1:x2:y2
    :param backend: back-end can be forced if set (example:default, scrot, wx,..),
                    otherwise all back-ends are tested
    :param childprocess: pyscreenshot parameter childprocess (0/1)
    """
    childprocess_param = childprocess
    if childprocess == "":
        childprocess = None
    elif childprocess == "0":
        childprocess = False
    elif childprocess == "1":
        childprocess = True
    else:
        raise ValueError("invalid childprocess value")

    n = 10
    if bbox:
        x1, y1, x2, y2 = map(int, bbox.split(":"))
        bbox = x1, y1, x2, y2
    else:
        bbox = None

    def f(virtual_only):
        if backend:
            try:
                run(backend, n, childprocess, bbox=bbox)
            except pyscreenshot.FailedBackendError:
                pass
        else:
            run_all(n, childprocess_param, virtual_only=virtual_only, bbox=bbox)

    if virtual_display:
        from pyvirtualdisplay import Display

        with Display(visible=0):
            f(virtual_only=True)
    else:
        f(virtual_only=False)
<commit_msg>speedtest: skip non X backends<commit_after>import logging
import sys
import time

import pyscreenshot
from entrypoint2 import entrypoint
from pyscreenshot.plugins.gnome_dbus import GnomeDBusWrapper
from pyscreenshot.plugins.gnome_screenshot import GnomeScreenshotWrapper
from pyscreenshot.plugins.kwin_dbus import KwinDBusWrapper
from pyscreenshot.util import proc

def run(force_backend, n, childprocess, bbox=None):
    sys.stdout.write("%-20s\t" % force_backend)
    sys.stdout.flush()  # before any crash

    if force_backend == "default":
        force_backend = None
    try:
        start = time.time()
        for _ in range(n):
            pyscreenshot.grab(
                backend=force_backend, childprocess=childprocess, bbox=bbox
            )
        end = time.time()
        dt = end - start
        s = "%-4.2g sec\t" % dt
        s += "(%5d ms per call)" % (1000.0 * dt / n)
        sys.stdout.write(s)
    finally:
        print("")

novirt = [GnomeDBusWrapper.name, KwinDBusWrapper.name, GnomeScreenshotWrapper.name]

def run_all(n, childprocess_param, virtual_only=True, bbox=None):
    debug = True
    print("")
    print("n=%s" % n)
    print("------------------------------------------------------")

    if bbox:
        x1, y1, x2, y2 = map(str, bbox)
        bbox = ":".join(map(str, (x1, y1, x2, y2)))
        bboxpar = ["--bbox", bbox]
    else:
        bboxpar = []
    if debug:
        debugpar = ["--debug"]
    else:
        debugpar = []
    for x in ["default"] + pyscreenshot.backends():
        backendpar = ["--backend", x]
        # skip non X backends
        if virtual_only and x in novirt:
            continue
        p = proc(
            "pyscreenshot.check.speedtest",
            ["--childprocess", childprocess_param] + bboxpar + debugpar + backendpar,
        )
        print(p.stdout)

@entrypoint
def speedtest(virtual_display=False, backend="", childprocess="", bbox=""):
    """Performance test of all back-ends.

    :param virtual_display: run with Xvfb
    :param bbox: bounding box coordinates x1:y1:x2:y2
    :param backend: back-end can be forced if set (example:default, scrot, wx,..),
                    otherwise all back-ends are tested
    :param childprocess: pyscreenshot parameter childprocess (0/1)
    """
    childprocess_param = childprocess
    if childprocess == "":
        childprocess = None
    elif childprocess == "0":
        childprocess = False
    elif childprocess == "1":
        childprocess = True
    else:
        raise ValueError("invalid childprocess value")

    n = 10
    if bbox:
        x1, y1, x2, y2 = map(int, bbox.split(":"))
        bbox = x1, y1, x2, y2
    else:
        bbox = None

    def f(virtual_only):
        if backend:
            try:
                run(backend, n, childprocess, bbox=bbox)
            except pyscreenshot.FailedBackendError:
                pass
        else:
            run_all(n, childprocess_param, virtual_only=virtual_only, bbox=bbox)

    if virtual_display:
        from pyvirtualdisplay import Display

        with Display(visible=0):
            f(virtual_only=True)
    else:
        f(virtual_only=False)
<|endoftext|>


<commit_before>import fnmatch
import hexchat

__module_name__ = 'BanSearch'
__module_author__ = 'TingPing'
__module_version__ = '1'
__module_description__ = 'Search for bans/quiets matching a user'

banhook = 0
quiethook = 0
endbanhook = 0
endquiethook = 0

banlist = []
quietlist = []

def print_result(mask, matchlist, matchnum, _type):
	if matchnum:
		print('\00318{}\017 had \00320{}\017 {} matches:'.format(mask, matchnum, _type))
		for match in matchlist:
			print('\t\t\t{}'.format(match))
	else:
		print('No {} matches for \00318{}\017 were found.'.format(_type, mask))

def match_mask(mask, searchmask):
	if searchmask is None:
		return False
	# The fnmatch function works for * and ?, to be replaced by a more irc friendly version...
	return fnmatch.fnmatch(mask, searchmask)

def match_extban(mask, host, account, realname, usermask):
	try:
		extban, banmask = mask.split(':')
	except ValueError:
		extban = mask
		banmask = '*'

	if '~' in extban:
		invert = True
	else:
		invert = False

	# From http://freenode.net/using_the_network.shtml
	if 'a' in extban:
		ret = match_mask (account, banmask)
	elif 'r' in extban:
		ret = match_mask (realname, banmask)
	elif 'x' in extban:
		ret = match_mask ('{}#{}'.format(host, realname), banmask)
	else:
		return False

	if invert:
		return not ret
	else:
		return ret

def get_user_info(nick):
	invalid_chars = ['*', '?', '$', '@', '!']
	if any(char in nick for char in invalid_chars):
		return (None, None, None) # It's a mask not a nick.

	for user in hexchat.get_list('users'):
		if user.nick == nick:
			host = user.host
			account = user.account
			realname = user.realname
			return (host, account, realname)

	return (None, None, None)

def banlist_cb(word, word_eol, userdata):
	global banlist
	banlist.append(word[4])
	return hexchat.EAT_HEXCHAT

def endbanlist_cb(word, word_eol, usermask):
	global banhook
	global endbanhook
	global banlist
	matchlist = []
	matchnum = 0

	# Cleanup hooks
	hexchat.unhook(banhook)
	banhook = 0
	hexchat.unhook(endbanhook)
	endbanhook = 0

	host, account, realname = get_user_info (usermask)

	if banlist:
		for mask in banlist:
			if mask[0] == '$' and host: # If extban we need user info
				if match_extban (mask, host, account, realname, usermask):
					matchlist.append(mask)
					matchnum = matchnum + 1
			elif match_mask (mask, usermask):
				matchlist.append(mask)
				matchnum = matchnum + 1

		banlist = []

	print_result (usermask, matchlist, matchnum, 'Ban')

	return hexchat.EAT_HEXCHAT

def quietlist_cb(word, word_eol, userdata):
	global quietlist
	quietlist.append(word[5])
	return hexchat.EAT_HEXCHAT

def endquietlist_cb(word, word_eol, usermask):
	global quiethook
	global endquiethook
	global quietlist
	matchlist = []
	matchnum = 0

	# Cleanup hooks
	hexchat.unhook(quiethook)
	quiethook = 0
	hexchat.unhook(endquiethook)
	endquiethook = 0

	host, account, realname = get_user_info (usermask)

	if quietlist:
		for mask in quietlist:
			if mask[0] == '$' and host: # If extban we need user info
				if match_extban (mask, host, account, realname, usermask):
					matchlist.append(mask)
					matchnum = matchnum + 1
			elif match_mask (mask, usermask):
				matchlist.append(mask)
				matchnum = matchnum + 1

		quietlist = []

	print_result (usermask, matchlist, matchnum, 'Quiet')

	return hexchat.EAT_HEXCHAT

def search_cb(word, word_eol, userdata):
	global banhook
	global quiethook
	global endbanhook
	global endquiethook

	if len(word) == 2:
		hooks = [quiethook, banhook, endquiethook, endbanhook]
		if not any(hook for hook in hooks):
			banhook = hexchat.hook_server ('367', banlist_cb)
			quiethook = hexchat.hook_server ('728', quietlist_cb)
			endbanhook = hexchat.hook_server ('368', endbanlist_cb, word[1])
			endquiethook = hexchat.hook_server ('729', endquietlist_cb, word[1])

			hexchat.command('ban')
			hexchat.command('quiet')
		else:
			print('A ban search is already in progress.')

	else:
		hexchat.command('help bansearch')

	return hexchat.EAT_ALL

hexchat.hook_command('bansearch', search_cb, help='BANSEARCH <mask|nick>')
hexchat.prnt(__module_name__ + ' version ' + __module_version__ + ' loaded.')
<commit_msg>Update bansearch.py<commit_after>import hexchat
import re

__module_name__ = 'BanSearch'
__module_author__ = 'TingPing'
__module_version__ = '1'
__module_description__ = 'Search for bans/quiets matching a user'

banhook = 0
quiethook = 0
endbanhook = 0
endquiethook = 0

banlist = []
quietlist = []

patterns = {'?': ".", '*': ".*", # translate wildcards to regex
            '{': r"\[", '}': r"\]", '|': r"\\", # IRC nick substitutions
            '[': r"\[", ']': r"\]", '$': r"\$"} # regex escapes

def print_result(mask, matchlist, matchnum, _type):
	if matchnum:
		print('\00318{}\017 had \00320{}\017 {} matches:'.format(mask, matchnum, _type))
		for match in matchlist:
			print('\t\t\t{}'.format(match))
	else:
		print('No {} matches for \00318{}\017 were found.'.format(_type, mask))

def match_mask(mask, searchmask):
	if searchmask is None:
		return False
	for match, repl in patterns.items():
		searchmask = searchmask.replace(match, repl)
	pattern = re.compile(searchmask.lower())
	return pattern.match(mask.lower())

def match_extban(mask, host, account, realname, usermask):
	try:
		extban, banmask = mask.split(':')
	except ValueError:
		extban = mask
		banmask = '*'

	if '~' in extban:
		invert = True
	else:
		invert = False

	# From http://freenode.net/using_the_network.shtml
	if 'a' in extban:
		ret = match_mask (account, banmask)
	elif 'r' in extban:
		ret = match_mask (realname, banmask)
	elif 'x' in extban:
		ret = match_mask ('{}#{}'.format(host, realname), banmask)
	else:
		return False

	if invert:
		return not ret
	else:
		return ret

def get_user_info(nick):
	invalid_chars = ['*', '?', '$', '@', '!']
	if any(char in nick for char in invalid_chars):
		return (None, None, None) # It's a mask not a nick.

	for user in hexchat.get_list('users'):
		if user.nick == nick:
			host = user.host
			account = user.account
			realname = user.realname
			return (host, account, realname)

	return (None, None, None)

def banlist_cb(word, word_eol, userdata):
	global banlist
	banlist.append(word[4])
	return hexchat.EAT_HEXCHAT

def endbanlist_cb(word, word_eol, usermask):
	global banhook
	global endbanhook
	global banlist
	matchlist = []
	matchnum = 0

	# Cleanup hooks
	hexchat.unhook(banhook)
	banhook = 0
	hexchat.unhook(endbanhook)
	endbanhook = 0

	host, account, realname = get_user_info (usermask)

	if banlist:
		for mask in banlist:
			if mask[0] == '$' and host: # If extban we need user info
				if match_extban (mask, host, account, realname, usermask):
					matchlist.append(mask)
					matchnum = matchnum + 1
			elif match_mask (mask, usermask):
				matchlist.append(mask)
				matchnum = matchnum + 1

		banlist = []

	print_result (usermask, matchlist, matchnum, 'Ban')

	return hexchat.EAT_HEXCHAT

def quietlist_cb(word, word_eol, userdata):
	global quietlist
	quietlist.append(word[5])
	return hexchat.EAT_HEXCHAT

def endquietlist_cb(word, word_eol, usermask):
	global quiethook
	global endquiethook
	global quietlist
	matchlist = []
	matchnum = 0

	# Cleanup hooks
	hexchat.unhook(quiethook)
	quiethook = 0
	hexchat.unhook(endquiethook)
	endquiethook = 0

	host, account, realname = get_user_info (usermask)

	if quietlist:
		for mask in quietlist:
			if mask[0] == '$' and host: # If extban we need user info
				if match_extban (mask, host, account, realname, usermask):
					matchlist.append(mask)
					matchnum = matchnum + 1
			elif match_mask (mask, usermask):
				matchlist.append(mask)
				matchnum = matchnum + 1

		quietlist = []

	print_result (usermask, matchlist, matchnum, 'Quiet')

	return hexchat.EAT_HEXCHAT

def search_cb(word, word_eol, userdata):
	global banhook
	global quiethook
	global endbanhook
	global endquiethook

	if len(word) == 2:
		hooks = [quiethook, banhook, endquiethook, endbanhook]
		if not any(hook for hook in hooks):
			banhook = hexchat.hook_server ('367', banlist_cb)
			quiethook = hexchat.hook_server ('728', quietlist_cb)
			endbanhook = hexchat.hook_server ('368', endbanlist_cb, word[1])
			endquiethook = hexchat.hook_server ('729', endquietlist_cb, word[1])

			hexchat.command('ban')
			hexchat.command('quiet')
		else:
			print('A ban search is already in progress.')

	else:
		hexchat.command('help bansearch')

	return hexchat.EAT_ALL

hexchat.hook_command('bansearch', search_cb, help='BANSEARCH <mask|nick>')
hexchat.prnt(__module_name__ + ' version ' + __module_version__ + ' loaded.')

<|endoftext|>


<commit_before># Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

# Django settings for akvo project.
# settings_base.py contains non-server specific settings and
# is imported into settings.py.

# Alphabetically ordered (more or less...)

# Accounts not activated in a week get purged. Used by registration app.
ACCOUNT_ACTIVATION_DAYS = 7

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/rsr/media/admin/'

ADMINS = (
     ('Gabriel von Heijne', 'gabriel@akvo.org'),
)

SUPPORT_EMAIL = ADMINS[0][1]

#Additional information about a User
AUTH_PROFILE_MODULE = 'rsr.userprofile'

#Memcached settings
#CACHE_MIDDLEWARE_SECONDS = 300 # 5 minutes
#CACHE_MIDDLEWARE_KEY_PREFIX = 'rsr' #not needed but mandatory setting
#CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True # Only non-logged in

#Use memcached for sessions with write-through to DB
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'

#DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
#DATABASE_NAME = '/var/dev/akvo/data/akvo.sqlite' # Or path to database file if using sqlite3.
#DATABASE_USER = ''             # Not used with sqlite3.
#DATABASE_PASSWORD = ''         # Not used with sqlite3.
#DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
#DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

#DEBUG = True

#DEFAULT_FROM_EMAIL = 'noreply@akvo.og'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.humanize',
    'django.contrib.webdesign',
    #'feedjack', #maybe later...
    # not used here, used for GraphViz on the Mac 'extensions', #django-command-extensions see http://code.google.com/p/django-command-extensions
    'akvo.rsr',
    'template_utils', #see http://code.google.com/p/django-template-utils/
    'registration', #see http://code.google.com/p/django-registration/
    #'django_granular_permissions',
    #'akvo.status',
    'paypal.standard.ipn',
    'sorl.thumbnail',
    'rosetta',
    'django_counter',
    'mollie.ideal',
    'django_sorting',
    'pagination',
    'oembed',
    'django_markup',
)

#INTERNAL_IPS = (
#    '87.227.60.54', '125.27.250.14', '192.168.38.68',
#)

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

gettext = lambda s: s

LANGUAGES = (
    ('de', gettext('German')),
    ('en', gettext('English')),
    ('nl', gettext('Dutch')),
    #('es', gettext('Spanish')),
    #('fr', gettext('French')),
    #('sv', gettext('Swedish')),
)

LOGIN_URL = '/rsr/signin/'
LOGIN_REDIRECT_URL = '/rsr/'

MANAGERS = ADMINS

#MEDIA_ROOT = '/var/dev/akvo/mediaroot/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
#MEDIA_URL = 'http://dev.akvo.org:8080/rsr/media/'

MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GZipMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.http.ConditionalGetMiddleware',
    'django_sorting.middleware.SortingMiddleware',
    'pagination.middleware.PaginationMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
)

# PAUL
# PAYPAL_RECEIVER_EMAIL = 'noreply@akvo.org'

# Is this the pvw-rsr?
PVW_RSR = False

ROOT_URLCONF = 'akvo.urls'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '^qd@tv7*d96zt(6c%ouy-hql0(+5ess^kpqsqwtbn+=)13a^+^'

SEND_BROKEN_LINK_EMAILS = True

SITE_ID = 1

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "akvo.rsr.context_processors.extra_context",
)

#TEMPLATE_DEBUG = DEBUG

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
#     'django.template.loaders.eggs.load_template_source',
)

import os.path
if PVW_RSR:
    TEMPLATE_LEAF_DIR = 'pvw'
else:
    TEMPLATE_LEAF_DIR = 'akvo'

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates/%s-take2' % TEMPLATE_LEAF_DIR).replace('\\','/'),
    os.path.join(os.path.dirname(__file__), 'templates/%s' % TEMPLATE_LEAF_DIR).replace('\\','/'),
    os.path.join(os.path.dirname(__file__), 'templates/core').replace('\\','/'),
)

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be avilable on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/Stockholm'

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
USE_L10N = True

<commit_msg>Disable all languages but English<commit_after># Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

# Django settings for akvo project.
# settings_base.py contains non-server specific settings and
# is imported into settings.py.

# Alphabetically ordered (more or less...)

# Accounts not activated in a week get purged. Used by registration app.
ACCOUNT_ACTIVATION_DAYS = 7

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/rsr/media/admin/'

ADMINS = (
     ('Gabriel von Heijne', 'gabriel@akvo.org'),
)

SUPPORT_EMAIL = ADMINS[0][1]

#Additional information about a User
AUTH_PROFILE_MODULE = 'rsr.userprofile'

#Memcached settings
#CACHE_MIDDLEWARE_SECONDS = 300 # 5 minutes
#CACHE_MIDDLEWARE_KEY_PREFIX = 'rsr' #not needed but mandatory setting
#CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True # Only non-logged in

#Use memcached for sessions with write-through to DB
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'

#DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
#DATABASE_NAME = '/var/dev/akvo/data/akvo.sqlite' # Or path to database file if using sqlite3.
#DATABASE_USER = ''             # Not used with sqlite3.
#DATABASE_PASSWORD = ''         # Not used with sqlite3.
#DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
#DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

#DEBUG = True

#DEFAULT_FROM_EMAIL = 'noreply@akvo.og'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.humanize',
    'django.contrib.webdesign',
    #'feedjack', #maybe later...
    # not used here, used for GraphViz on the Mac 'extensions', #django-command-extensions see http://code.google.com/p/django-command-extensions
    'akvo.rsr',
    'template_utils', #see http://code.google.com/p/django-template-utils/
    'registration', #see http://code.google.com/p/django-registration/
    #'django_granular_permissions',
    #'akvo.status',
    'paypal.standard.ipn',
    'sorl.thumbnail',
    'rosetta',
    'django_counter',
    'mollie.ideal',
    'django_sorting',
    'pagination',
    'oembed',
    'django_markup',
)

#INTERNAL_IPS = (
#    '87.227.60.54', '125.27.250.14', '192.168.38.68',
#)

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

gettext = lambda s: s

LANGUAGES = (
    #('de', gettext('German')),
    ('en', gettext('English')),
    #('nl', gettext('Dutch')),
    #('es', gettext('Spanish')),
    #('fr', gettext('French')),
    #('sv', gettext('Swedish')),
)

LOGIN_URL = '/rsr/signin/'
LOGIN_REDIRECT_URL = '/rsr/'

MANAGERS = ADMINS

#MEDIA_ROOT = '/var/dev/akvo/mediaroot/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
#MEDIA_URL = 'http://dev.akvo.org:8080/rsr/media/'

MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GZipMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.http.ConditionalGetMiddleware',
    'django_sorting.middleware.SortingMiddleware',
    'pagination.middleware.PaginationMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
)

# PAUL
# PAYPAL_RECEIVER_EMAIL = 'noreply@akvo.org'

# Is this the pvw-rsr?
PVW_RSR = False

ROOT_URLCONF = 'akvo.urls'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '^qd@tv7*d96zt(6c%ouy-hql0(+5ess^kpqsqwtbn+=)13a^+^'

SEND_BROKEN_LINK_EMAILS = True

SITE_ID = 1

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "akvo.rsr.context_processors.extra_context",
)

#TEMPLATE_DEBUG = DEBUG

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
#     'django.template.loaders.eggs.load_template_source',
)

import os.path
if PVW_RSR:
    TEMPLATE_LEAF_DIR = 'pvw'
else:
    TEMPLATE_LEAF_DIR = 'akvo'

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates/%s-take2' % TEMPLATE_LEAF_DIR).replace('\\','/'),
    os.path.join(os.path.dirname(__file__), 'templates/%s' % TEMPLATE_LEAF_DIR).replace('\\','/'),
    os.path.join(os.path.dirname(__file__), 'templates/core').replace('\\','/'),
)

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be avilable on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/Stockholm'

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
USE_L10N = True

<|endoftext|>


<commit_before>import types, time
from pysnmp.proto import rfc1157, rfc1905, api
from pysnmp.proto import error
from pysnmp.proto.proxy import rfc2576

def getVersionSpecifics(snmpVersion):
    if snmpVersion == 0:
        pduVersion = 0
    else:
        pduVersion = 1
    return pduVersion, api.protoModules[pduVersion]

# XXX move to rfc3413/config
def getTargetInfo(snmpEngine, snmpTargetAddrName):
    mibInstrumController = snmpEngine.msgAndPduDsp.mibInstrumController
    # Transport endpoint
    snmpTargetAddrEntry, = mibInstrumController.mibBuilder.importSymbols(
        'SNMP-TARGET-MIB', 'snmpTargetAddrEntry'
        )
    tblIdx = snmpTargetAddrEntry.getInstIdFromIndices(
        snmpTargetAddrName
        )
    snmpTargetAddrTDomain = snmpTargetAddrEntry.getNode(
        snmpTargetAddrEntry.name + (2,) + tblIdx
        )
    snmpTargetAddrTAddress = snmpTargetAddrEntry.getNode(
        snmpTargetAddrEntry.name + (3,) + tblIdx
        )
    snmpTargetAddrTimeout = snmpTargetAddrEntry.getNode(
        snmpTargetAddrEntry.name + (4,) + tblIdx
        )
    snmpTargetAddrRetryCount = snmpTargetAddrEntry.getNode(
        snmpTargetAddrEntry.name + (5,) + tblIdx
        )
    snmpTargetAddrParams = snmpTargetAddrEntry.getNode(
        snmpTargetAddrEntry.name + (7,) + tblIdx
        )

    # Target params
    snmpTargetParamsEntry, = mibInstrumController.mibBuilder.importSymbols(
        'SNMP-TARGET-MIB', 'snmpTargetParamsEntry'
        )
    tblIdx = snmpTargetParamsEntry.getInstIdFromIndices(
        snmpTargetAddrParams.syntax
        )
    snmpTargetParamsMPModel = snmpTargetParamsEntry.getNode(
        snmpTargetParamsEntry.name + (2,) + tblIdx
        )
    snmpTargetParamsSecurityModel = snmpTargetParamsEntry.getNode(
        snmpTargetParamsEntry.name + (3,) + tblIdx
        )
    snmpTargetParamsSecurityName = snmpTargetParamsEntry.getNode(
        snmpTargetParamsEntry.name + (4,) + tblIdx
        )
    snmpTargetParamsSecurityLevel = snmpTargetParamsEntry.getNode(
        snmpTargetParamsEntry.name + (5,) + tblIdx
        )

    return ( snmpTargetAddrTDomain.syntax,
             tuple(snmpTargetAddrTAddress.syntax),
             snmpTargetAddrTimeout.syntax,
             snmpTargetAddrRetryCount.syntax,
             snmpTargetParamsMPModel.syntax,
             snmpTargetParamsSecurityModel.syntax,
             snmpTargetParamsSecurityName.syntax,
             snmpTargetParamsSecurityLevel.syntax )

class CommandGeneratorBase:
    def __init__(self):
        self.__pendingReqs = {}
        self._sendRequestHandleSource = 0L

    def processResponsePdu(
        self,
        snmpEngine,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        statusInformation,
        sendPduHandle,
        (cbFun, cbCtx)
        ):
        # 3.1.1
        ( origTransportDomain,
          origTransportAddress,
          origMessageProcessingModel,
          origSecurityModel,
          origSecurityName,
          origSecurityLevel,
          origContextEngineId,
          origContextName,
          origPduVersion,
          origPdu,
          origTimeout,
          origRetryCount,
          origRetries,
          origSendRequestHandle
          ) = self.__pendingReqs[sendPduHandle]
        del self.__pendingReqs[sendPduHandle]

        snmpEngine.transportDispatcher.jobFinished(id(self))

        # 3.1.3
        if statusInformation:
            if origRetries == origRetryCount:
                cbFun(origSendRequestHandle,
                      statusInformation['errorIndication'], 0, 0, (),
                      cbCtx)
                return
            self._sendPdu(
                snmpEngine,
                origTransportDomain,
                origTransportAddress,
                origMessageProcessingModel,
                origSecurityModel,
                origSecurityName,
                origSecurityLevel,
                origContextEngineId,
                origContextName,
                origPduVersion,
                origPdu,
                origTimeout,
                origRetryCount,
                origRetries,
                origSendRequestHandle,
                (self.processResponsePdu, (cbFun, cbCtx))
                )
            return

        if origMessageProcessingModel != messageProcessingModel or \
           origSecurityModel != securityModel or \
           origSecurityName != origSecurityName or \
           origContextEngineId and origContextEngineId != contextEngineId or \
           origContextName and origContextName != contextName or \
           origPduVersion != pduVersion:
            return

        pMod = api.protoModules[pduVersion]

        # 3.1.2
        if pMod.apiPDU.getRequestID(PDU) != pMod.apiPDU.getRequestID(origPdu):
            return

        # User-side API assumes SMIv2
        if messageProcessingModel == 0:
            PDU = rfc2576.v1ToV2(PDU)

        self._handleResponse(
            snmpEngine,
            origTransportDomain,
            origTransportAddress,
            origMessageProcessingModel,
            origSecurityModel,
            origSecurityName,
            origSecurityLevel,
            origContextEngineId,
            origContextName,
            origPduVersion,
            origPdu,
            origTimeout,
            origRetryCount,
            pMod,
            PDU,
            origSendRequestHandle,
            (cbFun, cbCtx),
            )

    def sendReq(
        self,
        snmpEngine,
        addrName,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        raise error.ProtocolError('Method not implemented')

    def _sendPdu(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        reqPDU,
        timeout,
        retryCount,
        retries,
        sendRequestHandle,
        (processResponsePdu, cbCtx)
        ):
        # 3.1
        sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            (processResponsePdu, timeout/1000 + time.time(), cbCtx)
            )

        snmpEngine.transportDispatcher.jobStarted(id(self))

        self.__pendingReqs[sendPduHandle] = (
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            retries + 1,
            sendRequestHandle,
            )

class GetCommandGenerator(CommandGeneratorBase):
    def sendReq(
        self,
        snmpEngine,
        addrName,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        ( transportDomain,
          transportAddress,
          timeout,
          retryCount,
          messageProcessingModel,
          securityModel,
          securityName,
          securityLevel ) = getTargetInfo(snmpEngine, addrName)

        pduVersion, pMod = getVersionSpecifics(messageProcessingModel)

        reqPDU = pMod.GetRequestPDU()
        pMod.apiPDU.setDefaults(reqPDU)

        pMod.apiPDU.setVarBinds(reqPDU, varBinds)

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

        return self._sendRequestHandleSource

    def _handleResponse(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        timeout,
        retryCount,
        pMod,
        rspPDU,
        sendRequestHandle,
        (cbFun, cbCtx)
        ):
        cbFun(sendRequestHandle,
              None,
              pMod.apiPDU.getErrorStatus(rspPDU),
              pMod.apiPDU.getErrorIndex(rspPDU),
              pMod.apiPDU.getVarBinds(rspPDU),
              cbCtx)

class SetCommandGenerator(CommandGeneratorBase):
    def sendReq(
        self,
        snmpEngine,
        addrName,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        ( transportDomain,
          transportAddress,
          timeout,
          retryCount,
          messageProcessingModel,
          securityModel,
          securityName,
          securityLevel ) = getTargetInfo(snmpEngine, addrName)

        pduVersion, pMod = getVersionSpecifics(messageProcessingModel)

        reqPDU = pMod.SetRequestPDU()
        pMod.apiPDU.setDefaults(reqPDU)

        pMod.apiPDU.setVarBinds(reqPDU, varBinds)

        # User-side API assumes SMIv2
        if messageProcessingModel == 0:
            reqPDU = rfc2576.v2ToV1(reqPDU)

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

        return self._sendRequestHandleSource

    def _handleResponse(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        timeout,
        retryCount,
        pMod,
        rspPDU,
        sendRequestHandle,
        (cbFun, cbCtx)
        ):
        cbFun(sendRequestHandle,
              None,
              pMod.apiPDU.getErrorStatus(rspPDU),
              pMod.apiPDU.getErrorIndex(rspPDU),
              pMod.apiPDU.getVarBinds(rspPDU),
              cbCtx)

class NextCommandGenerator(CommandGeneratorBase):
    def sendReq(
        self,
        snmpEngine,
        addrName,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        ( transportDomain,
          transportAddress,
          timeout,
          retryCount,
          messageProcessingModel,
          securityModel,
          securityName,
          securityLevel ) = getTargetInfo(snmpEngine, addrName)

        pduVersion, pMod = getVersionSpecifics(messageProcessingModel)

        reqPDU = pMod.GetNextRequestPDU()
        pMod.apiPDU.setDefaults(reqPDU)

        pMod.apiPDU.setVarBinds(reqPDU, varBinds)

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

        return self._sendRequestHandleSource

    def _handleResponse(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        timeout,
        retryCount,
        pMod,
        rspPDU,
        sendRequestHandle,
        (cbFun, cbCtx)
        ):
        varBindTable = pMod.apiPDU.getVarBindTable(PDU, rspPDU)

        if not cbFun(sendRequestHandle, None,
                     pMod.apiPDU.getErrorStatus(rspPDU),
                     pMod.apiPDU.getErrorIndex(rspPDU),
                     varBindTable, cbCtx):
            return  # app says enough

        pMod.apiPDU.setRequestID(PDU, pMod.getNextRequestID())
        pMod.apiPDU.setVarBinds(
            PDU, map(lambda (x,y),n=pMod.Null(''): (x,n), varBindTable[-1])
            )

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            PDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

class BulkCommandGenerator(CommandGeneratorBase):
    def sendReq(
        self,
        snmpEngine,
        addrName,
        nonRepeaters,
        maxRepetitions,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        ( transportDomain,
          transportAddress,
          timeout,
          retryCount,
          messageProcessingModel,
          securityModel,
          securityName,
          securityLevel ) = getTargetInfo(snmpEngine, addrName)

        pduVersion, pMod = getVersionSpecifics(messageProcessingModel)

        if not hasattr(pMod, 'GetBulkRequestPDU'):
            raise error.ProtocolError('BULK PDU not implemented at %s' % pMod)
        reqPDU = pMod.GetBulkRequestPDU()

        pMod.apiBulkPDU.setNonRepeaters(reqPDU, nonRepeaters)
        pMod.apiBulkPDU.setMaxRepetitions(reqPDU, maxRepetitions)

        pMod.apiBulkPDU.setDefaults(reqPDU)

        pMod.apiBulkPDU.setVarBinds(reqPDU, varBinds)

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

        return self._sendRequestHandleSource

    def _handleResponse(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        timeout,
        retryCount,
        pMod,
        rspPDU,
        sendRequestHandle,
        (cbFun, cbCtx)
        ):
        varBindTable = pMod.apiBulkPDU.getVarBindTable(PDU, rspPDU)

        if not cbFun(sendRequestHandle, None,
                     pMod.apiBulkPDU.getErrorStatus(rspPDU),
                     pMod.apiBulkPDU.getErrorIndex(rspPDU),
                     varBindTable, cbCtx):
            return # app says enough

        pMod.apiBulkPDU.setRequestID(PDU, pMod.getNextRequestID())
        pMod.apiBulkPDU.setVarBinds(
            PDU, map(lambda (x,y),n=pMod.Null(''): (x,n), varBindTable[-1])
            )

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            PDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )
<commit_msg>getTargetInfo() moved to config module<commit_after>import types, time
from pysnmp.proto import rfc1157, rfc1905, api
from pysnmp.entity.rfc3413 import config
from pysnmp.proto.proxy import rfc2576
from pysnmp.proto import error

def getVersionSpecifics(snmpVersion):
    if snmpVersion == 0:
        pduVersion = 0
    else:
        pduVersion = 1
    return pduVersion, api.protoModules[pduVersion]

class CommandGeneratorBase:
    def __init__(self):
        self.__pendingReqs = {}
        self._sendRequestHandleSource = 0L

    def processResponsePdu(
        self,
        snmpEngine,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        statusInformation,
        sendPduHandle,
        (cbFun, cbCtx)
        ):
        # 3.1.1
        ( origTransportDomain,
          origTransportAddress,
          origMessageProcessingModel,
          origSecurityModel,
          origSecurityName,
          origSecurityLevel,
          origContextEngineId,
          origContextName,
          origPduVersion,
          origPdu,
          origTimeout,
          origRetryCount,
          origRetries,
          origSendRequestHandle
          ) = self.__pendingReqs[sendPduHandle]
        del self.__pendingReqs[sendPduHandle]

        snmpEngine.transportDispatcher.jobFinished(id(self))

        # 3.1.3
        if statusInformation:
            if origRetries == origRetryCount:
                cbFun(origSendRequestHandle,
                      statusInformation['errorIndication'], 0, 0, (),
                      cbCtx)
                return
            self._sendPdu(
                snmpEngine,
                origTransportDomain,
                origTransportAddress,
                origMessageProcessingModel,
                origSecurityModel,
                origSecurityName,
                origSecurityLevel,
                origContextEngineId,
                origContextName,
                origPduVersion,
                origPdu,
                origTimeout,
                origRetryCount,
                origRetries,
                origSendRequestHandle,
                (self.processResponsePdu, (cbFun, cbCtx))
                )
            return

        if origMessageProcessingModel != messageProcessingModel or \
           origSecurityModel != securityModel or \
           origSecurityName != origSecurityName or \
           origContextEngineId and origContextEngineId != contextEngineId or \
           origContextName and origContextName != contextName or \
           origPduVersion != pduVersion:
            return

        pMod = api.protoModules[pduVersion]

        # 3.1.2
        if pMod.apiPDU.getRequestID(PDU) != pMod.apiPDU.getRequestID(origPdu):
            return

        # User-side API assumes SMIv2
        if messageProcessingModel == 0:
            PDU = rfc2576.v1ToV2(PDU)

        self._handleResponse(
            snmpEngine,
            origTransportDomain,
            origTransportAddress,
            origMessageProcessingModel,
            origSecurityModel,
            origSecurityName,
            origSecurityLevel,
            origContextEngineId,
            origContextName,
            origPduVersion,
            origPdu,
            origTimeout,
            origRetryCount,
            pMod,
            PDU,
            origSendRequestHandle,
            (cbFun, cbCtx),
            )

    def sendReq(
        self,
        snmpEngine,
        addrName,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        raise error.ProtocolError('Method not implemented')

    def _sendPdu(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        reqPDU,
        timeout,
        retryCount,
        retries,
        sendRequestHandle,
        (processResponsePdu, cbCtx)
        ):
        # 3.1
        sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            (processResponsePdu, timeout/1000 + time.time(), cbCtx)
            )

        snmpEngine.transportDispatcher.jobStarted(id(self))

        self.__pendingReqs[sendPduHandle] = (
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            retries + 1,
            sendRequestHandle,
            )

class GetCommandGenerator(CommandGeneratorBase):
    def sendReq(
        self,
        snmpEngine,
        addrName,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        ( transportDomain,
          transportAddress,
          timeout,
          retryCount,
          messageProcessingModel,
          securityModel,
          securityName,
          securityLevel ) = config.getTargetInfo(snmpEngine, addrName)

        pduVersion, pMod = getVersionSpecifics(messageProcessingModel)

        reqPDU = pMod.GetRequestPDU()
        pMod.apiPDU.setDefaults(reqPDU)

        pMod.apiPDU.setVarBinds(reqPDU, varBinds)

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

        return self._sendRequestHandleSource

    def _handleResponse(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        timeout,
        retryCount,
        pMod,
        rspPDU,
        sendRequestHandle,
        (cbFun, cbCtx)
        ):
        cbFun(sendRequestHandle,
              None,
              pMod.apiPDU.getErrorStatus(rspPDU),
              pMod.apiPDU.getErrorIndex(rspPDU),
              pMod.apiPDU.getVarBinds(rspPDU),
              cbCtx)

class SetCommandGenerator(CommandGeneratorBase):
    def sendReq(
        self,
        snmpEngine,
        addrName,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        ( transportDomain,
          transportAddress,
          timeout,
          retryCount,
          messageProcessingModel,
          securityModel,
          securityName,
          securityLevel ) = config.getTargetInfo(snmpEngine, addrName)

        pduVersion, pMod = getVersionSpecifics(messageProcessingModel)

        reqPDU = pMod.SetRequestPDU()
        pMod.apiPDU.setDefaults(reqPDU)

        pMod.apiPDU.setVarBinds(reqPDU, varBinds)

        # User-side API assumes SMIv2
        if messageProcessingModel == 0:
            reqPDU = rfc2576.v2ToV1(reqPDU)

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

        return self._sendRequestHandleSource

    def _handleResponse(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        timeout,
        retryCount,
        pMod,
        rspPDU,
        sendRequestHandle,
        (cbFun, cbCtx)
        ):
        cbFun(sendRequestHandle,
              None,
              pMod.apiPDU.getErrorStatus(rspPDU),
              pMod.apiPDU.getErrorIndex(rspPDU),
              pMod.apiPDU.getVarBinds(rspPDU),
              cbCtx)

class NextCommandGenerator(CommandGeneratorBase):
    def sendReq(
        self,
        snmpEngine,
        addrName,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        ( transportDomain,
          transportAddress,
          timeout,
          retryCount,
          messageProcessingModel,
          securityModel,
          securityName,
          securityLevel ) = config.getTargetInfo(snmpEngine, addrName)

        pduVersion, pMod = getVersionSpecifics(messageProcessingModel)

        reqPDU = pMod.GetNextRequestPDU()
        pMod.apiPDU.setDefaults(reqPDU)

        pMod.apiPDU.setVarBinds(reqPDU, varBinds)

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

        return self._sendRequestHandleSource

    def _handleResponse(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        timeout,
        retryCount,
        pMod,
        rspPDU,
        sendRequestHandle,
        (cbFun, cbCtx)
        ):
        varBindTable = pMod.apiPDU.getVarBindTable(PDU, rspPDU)

        if not cbFun(sendRequestHandle, None,
                     pMod.apiPDU.getErrorStatus(rspPDU),
                     pMod.apiPDU.getErrorIndex(rspPDU),
                     varBindTable, cbCtx):
            return  # app says enough

        pMod.apiPDU.setRequestID(PDU, pMod.getNextRequestID())
        pMod.apiPDU.setVarBinds(
            PDU, map(lambda (x,y),n=pMod.Null(''): (x,n), varBindTable[-1])
            )

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            PDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

class BulkCommandGenerator(CommandGeneratorBase):
    def sendReq(
        self,
        snmpEngine,
        addrName,
        nonRepeaters,
        maxRepetitions,
        varBinds,
        cbFun,
        cbCtx=None,
        contextEngineId=None,
        contextName=''
        ):
        ( transportDomain,
          transportAddress,
          timeout,
          retryCount,
          messageProcessingModel,
          securityModel,
          securityName,
          securityLevel ) = config.getTargetInfo(snmpEngine, addrName)

        pduVersion, pMod = getVersionSpecifics(messageProcessingModel)

        if not hasattr(pMod, 'GetBulkRequestPDU'):
            raise error.ProtocolError('BULK PDU not implemented at %s' % pMod)
        reqPDU = pMod.GetBulkRequestPDU()

        pMod.apiBulkPDU.setNonRepeaters(reqPDU, nonRepeaters)
        pMod.apiBulkPDU.setMaxRepetitions(reqPDU, maxRepetitions)

        pMod.apiBulkPDU.setDefaults(reqPDU)

        pMod.apiBulkPDU.setVarBinds(reqPDU, varBinds)

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            reqPDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )

        return self._sendRequestHandleSource

    def _handleResponse(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        PDU,
        timeout,
        retryCount,
        pMod,
        rspPDU,
        sendRequestHandle,
        (cbFun, cbCtx)
        ):
        varBindTable = pMod.apiBulkPDU.getVarBindTable(PDU, rspPDU)

        if not cbFun(sendRequestHandle, None,
                     pMod.apiBulkPDU.getErrorStatus(rspPDU),
                     pMod.apiBulkPDU.getErrorIndex(rspPDU),
                     varBindTable, cbCtx):
            return # app says enough

        pMod.apiBulkPDU.setRequestID(PDU, pMod.getNextRequestID())
        pMod.apiBulkPDU.setVarBinds(
            PDU, map(lambda (x,y),n=pMod.Null(''): (x,n), varBindTable[-1])
            )

        self._sendRequestHandleSource = self._sendRequestHandleSource + 1

        self._sendPdu(
            snmpEngine,
            transportDomain,
            transportAddress,
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineId,
            contextName,
            pduVersion,
            PDU,
            timeout,
            retryCount,
            0,
            self._sendRequestHandleSource,
            (self.processResponsePdu, (cbFun, cbCtx))
            )
<|endoftext|>