<commit_before>import os
import ctypes
import pickle
import subprocess

def infer_ctype(var):
    if isinstance(var, int):
        return ctypes.c_int
    elif isinstance(var, float):
        return ctypes.c_double
    elif isinstance(var, bool):
        return ctypes.c_bool
    elif isinstance(var, str):
        return ctypes.c_char_p
    elif isinstance(var, list):
        # For a list, we add an extra argument for the size of the input C array.
        return infer_ctype(var[0]) * len(var), ctypes.c_int
    else:
        raise NotImplementedError("Cannot infer ctype of type(var)={:}, var={:}".format(type(var), var))

def infer_argtypes(input_tuple):
    arg_ctypes = []
    for var in input_tuple:
        ctypes = infer_ctype(var)
        if isinstance(ctypes, tuple):
            arg_ctypes.extend(ctypes)
        else:
             arg_ctypes.append(ctypes)

    return arg_ctypes

def infer_restype(output_tuple):
    if len(output_tuple) > 1:
        raise NotImplementedError("C does not support multiple return values but len(output_tuple)={:d}"
                                  .format(len(output_tuple)))
    return infer_ctype(output_tuple[0])

def ctype_input_list(input_tuple):
    input_list = []
    for k, var in enumerate(input_tuple):
        if isinstance(var, str):
            # C wants bytes, not strings.
            input_list.append(ctypes.c_char_p(bytes(var, "utf-8")))
        elif isinstance(var, list):
            # For a list, we add an extra argument for the size of the input C array.
            array_type = infer_ctype(var[0]) * len(var)
            arr = array_type(*var)
            input_list.append(arr)
            input_list.append(len(var))
        else:
            input_list.append(var)
    return input_list

def ctype_output(var, correct_output):
    if isinstance(correct_output, str):
        if not isinstance(var, bytes):
            raise TypeError("Return type is wrong! Was expecting return type char* (Python bytes). "
                            "Instead got return type {:}".format(type(var)))
        return var.decode("utf-8")
    else:
        return var

run_id = os.path.basename(__file__).split('.')[0]
input_pickle = "{:s}.input.pickle".format(run_id)
correct_pickle = "{:s}.correct.pickle".format(run_id)
code_file = "{:s}.c".format(run_id)
lib_file = "{:s}.so".format(run_id)

with open(input_pickle, mode='rb') as f:
    input_tuples = pickle.load(f)

with open(correct_pickle, mode='rb') as f:
    correct_output_tuples = pickle.load(f)

# Compile the user's C code.
# -fPIC for position-independent code, needed for shared libraries to work no matter where in memory they are loaded.
# check=True will raise a CalledProcessError for non-zero return codes (user code failed to compile.)
subprocess.run(["gcc", "-fPIC", "-shared", "-o", lib_file, code_file], check=True)

# Load the compiled shared library. We use the absolute path as the cwd is not in LD_LIBRARY_PATH so cdll won't find
# the .so file if we use a relative path or just a filename.
cwd = os.path.dirname(os.path.realpath(__file__))
lib = ctypes.cdll.LoadLibrary(os.path.join(cwd, lib_file))

for i, (input_tuple, correct_output_tuple) in enumerate(zip(input_tuples, correct_output_tuples)):
    # Use the input and output tuple to infer the type of input arguments and return value. We do this again for each
    # test case in case outputs change type or arrays change size.
    lib.$FUNCTION_NAME.argtypes = infer_argtypes(input_tuple)
    lib.$FUNCTION_NAME.restype = infer_restype(correct_output_tuple)

    ctyped_input_list = ctype_input_list(input_tuple)

    # $FUNCTION_NAME will be replaced by the name of the user's function by the CodeRunner before this script is run.
    user_output = lib.$FUNCTION_NAME(*ctyped_input_list)

    user_output = ctype_output(user_output, correct_output_tuple[0])

    output_dict = {
        'user_output': user_output if isinstance(user_output, tuple) else (user_output,),
        'runtime': 0,
        'max_mem_usage': 0
    }

    output_pickle = '{:s}.output{:d}.pickle'.format(run_id, i)
    with open(output_pickle, mode='wb') as f:
        pickle.dump(output_dict, file=f, protocol=pickle.HIGHEST_PROTOCOL)
<commit_msg>Infer C array type from numpy ndarray<commit_after>import os
import ctypes
import pickle
import subprocess

from numpy import ndarray
from numpy.ctypeslib import as_ctypes_type, as_ctypes, as_array

def infer_ctype(var):
    if isinstance(var, int):
        return ctypes.c_int

    elif isinstance(var, float):
        return ctypes.c_double

    elif isinstance(var, bool):
        return ctypes.c_bool

    elif isinstance(var, str):
        return ctypes.c_char_p

    elif isinstance(var, list):
        # For a Python list, we add an extra argument for the size of the C array.
        return infer_ctype(var[0]) * len(var), ctypes.c_int

    elif isinstance(var, ndarray):
        # For an array, we add extra arguments for the size of the input C array (one extra argument per dimension).
        arr_ctype = type(as_ctypes(var))
        types = [arr_ctype]
        for _ in range(len(var.shape)):
            types.append(ctypes.c_int)
        return types

    else:
        raise NotImplementedError("Cannot infer ctype of type(var)={:}, var={:}".format(type(var), var))

def infer_argtypes(input_tuple):
    arg_ctypes = []
    for var in input_tuple:
        types = infer_ctype(var)
        if isinstance(types, (list, tuple)):
            arg_ctypes.extend(types)
        else:
             arg_ctypes.append(types)

    return arg_ctypes

def infer_restype(output_tuple):
    if len(output_tuple) > 1:
        raise NotImplementedError("C does not support multiple return values but len(output_tuple)={:d}"
                                  .format(len(output_tuple)))
    return infer_ctype(output_tuple[0])

def ctype_input_list(input_tuple):
    input_list = []
    for k, var in enumerate(input_tuple):
        if isinstance(var, str):
            # C wants bytes, not strings.
            input_list.append(ctypes.c_char_p(bytes(var, "utf-8")))

        elif isinstance(var, list):
            # For a list, we add an extra argument for the size of the input C array.
            array_type = infer_ctype(var[0]) * len(var)
            arr = array_type(*var)
            input_list.append(arr)
            input_list.append(len(var))

        elif isinstance(var, ndarray):
            # For an array, we add extra arguments for the size of the input C array (one extra argument per dimension).
            arr = as_ctypes(var)
            input_list.append(arr)
            for s in var.shape:
                input_list.append(s)

        else:
            input_list.append(var)

    return input_list

def ctype_output(var, correct_output):
    if isinstance(correct_output, str):
        if not isinstance(var, bytes):
            raise TypeError("Return type is wrong! Was expecting return type char* (Python bytes). "
                            "Instead got return type {:}".format(type(var)))
        return var.decode("utf-8")
    else:
        return var

run_id = os.path.basename(__file__).split('.')[0]
input_pickle = "{:s}.input.pickle".format(run_id)
correct_pickle = "{:s}.correct.pickle".format(run_id)
code_file = "{:s}.c".format(run_id)
lib_file = "{:s}.so".format(run_id)

with open(input_pickle, mode='rb') as f:
    input_tuples = pickle.load(f)

with open(correct_pickle, mode='rb') as f:
    correct_output_tuples = pickle.load(f)

# Compile the user's C code.
# -fPIC for position-independent code, needed for shared libraries to work no matter where in memory they are loaded.
# check=True will raise a CalledProcessError for non-zero return codes (user code failed to compile.)
subprocess.run(["gcc", "-fPIC", "-shared", "-o", lib_file, code_file], check=True)

# Load the compiled shared library. We use the absolute path as the cwd is not in LD_LIBRARY_PATH so cdll won't find
# the .so file if we use a relative path or just a filename.
cwd = os.path.dirname(os.path.realpath(__file__))
lib = ctypes.cdll.LoadLibrary(os.path.join(cwd, lib_file))

for i, (input_tuple, correct_output_tuple) in enumerate(zip(input_tuples, correct_output_tuples)):
    # Use the input and output tuple to infer the type of input arguments and return value. We do this again for each
    # test case in case outputs change type or arrays change size.
    lib.$FUNCTION_NAME.argtypes = infer_argtypes(input_tuple)
    lib.$FUNCTION_NAME.restype = infer_restype(correct_output_tuple)

    ctyped_input_list = ctype_input_list(input_tuple)

    # $FUNCTION_NAME will be replaced by the name of the user's function by the CodeRunner before this script is run.
    user_output = lib.$FUNCTION_NAME(*ctyped_input_list)

    user_output = ctype_output(user_output, correct_output_tuple[0])

    output_dict = {
        'user_output': user_output if isinstance(user_output, tuple) else (user_output,),
        'runtime': 0,
        'max_mem_usage': 0
    }

    output_pickle = '{:s}.output{:d}.pickle'.format(run_id, i)
    with open(output_pickle, mode='wb') as f:
        pickle.dump(output_dict, file=f, protocol=pickle.HIGHEST_PROTOCOL)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# 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 >.

from datetime import datetime, timedelta

from django.conf import settings
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

from mollie.ideal.utils import get_mollie_banklist

from ..fields import ValidXMLCharField, ValidXMLTextField

class InvoiceManager(models.Manager):
    def get_queryset(self):
        """Returns a queryset of all invoices
        Test invoices are excluded in production mode
        """
        if not settings.DONATION_TEST:
            return super(InvoiceManager, self).get_queryset().exclude(test=True)
        else:
            return super(InvoiceManager, self).get_queryset()

    def stale(self):
        """Returns a queryset of invoices which have been pending
        for longer than settings.PAYPAL_INVOICE_TIMEOUT (60 minutes by default)
        """
        timeout = (datetime.now() - timedelta(minutes=getattr(settings, 'PAYPAL_INVOICE_TIMEOUT', 60)))
        qs = self.filter(status=1, time__lte=timeout)
        return qs

    def complete(self):
        """Returns a queryset of invoices which have
        a status of 'Complete'
        """
        qs = self.filter(status=3)
        return qs

class Invoice(models.Model):
    PAYPAL_INVOICE_STATUS_PENDING = 1
    PAYPAL_INVOICE_STATUS_VOID = 2
    PAYPAL_INVOICE_STATUS_COMPLETE = 3
    PAYPAL_INVOICE_STATUS_STALE = 4

    STATUS_CHOICES = (
        (PAYPAL_INVOICE_STATUS_PENDING, 'Pending'),
        (PAYPAL_INVOICE_STATUS_VOID, 'Void'),
        (PAYPAL_INVOICE_STATUS_COMPLETE, 'Complete'),
        (PAYPAL_INVOICE_STATUS_STALE, 'Stale'),
    )
    # NOTE: There are still a few places in templates where the strings 'paypal' and 'ideal' are used
    PAYMENT_ENGINE_PAYPAL = 'paypal'
    PAYMENT_ENGINE_IDEAL = 'ideal'
    PAYMENT_ENGINES = (
        (PAYMENT_ENGINE_PAYPAL, u'PayPal'),
        (PAYMENT_ENGINE_IDEAL, u'iDEAL'),
    )
    test = models.BooleanField(
        u'test donation',
        help_text=u'This flag is set if the donation was made in test mode.',
        default=False)
    engine = ValidXMLCharField(u'payment engine', choices=PAYMENT_ENGINES, max_length=10, default=PAYMENT_ENGINE_PAYPAL)
    user = models.ForeignKey(User, blank=True, null=True)
    project = models.ForeignKey('Project', related_name='invoices')
    # Common
    amount = models.PositiveIntegerField(help_text=u'Amount requested by user.')
    amount_received = models.DecimalField(
        max_digits=10, decimal_places=2, blank=True, null=True,
        help_text=u'Amount actually received after charges have been applied.'
    )
    time = models.DateTimeField(auto_now_add=True)
    name = ValidXMLCharField(max_length=75, blank=True, null=True)
    email = models.EmailField(blank=True, null=True)
    status = models.PositiveSmallIntegerField('status', choices=STATUS_CHOICES, default=1)
    http_referer = ValidXMLCharField(u'HTTP referer', max_length=255, blank=True)
    campaign_code = ValidXMLCharField(u'Campaign code', blank=True, max_length=15)
    is_anonymous = models.BooleanField(u'anonymous donation', default=False)
    # PayPal
    ipn = ValidXMLCharField(u'PayPal IPN', blank=True, null=True, max_length=75)
    # Mollie
    bank = ValidXMLCharField(u'mollie.nl bank ID', max_length=4, choices=get_mollie_banklist(), blank=True)
    transaction_id = ValidXMLCharField(u'mollie.nl transaction ID', max_length=100, blank=True)

    notes = ValidXMLTextField(verbose_name=_("Notes and comments"), blank=True, default='')

    admin_objects = models.Manager()
    objects = InvoiceManager()

    def get_favicon(self):
        pass  # TODO: @ grab favicon from HTTP_REFERER site

    @property
    def get_name(self):
        if self.user:
            return self.user.get_full_name()
        return self.name

    @property
    def get_email(self):
        if self.user:
            return self.user.email
        return self.email

    @property
    def currency(self):
        return self.project.currency

    @property
    def gateway(self):
        if self.engine == self.PAYMENT_ENGINE_PAYPAL:
            if settings.DONATION_TEST:
                return settings.PAYPAL_SANDBOX_GATEWAY
            else:
                return self.project.paymentgatewayselector.paypal_gateway.account_email
        elif self.engine == 'ideal':
            return self.project.paymentgatewayselector.mollie_gateway.partner_id

    @property
    def locale(self):
        return self.project.paymentgatewayselector.paypal_gateway.locale

    @property
    def notification_email(self):
        if getattr(settings, "DONATION_TEST", False):
            return "test@akvo.org"
        else:
            if self.engine == "paypal":
                return self.project.paymentgatewayselector.paypal_gateway.notification_email
            elif self.engine == "ideal":
                return self.project.paymentgatewayselector.mollie_gateway.notification_email

    @property
    def donation_fee(self):
        return (self.amount - self.amount_received)

    def __unicode__(self):
        return u'Invoice %(invoice_id)s (Project: %(project_name)s)' % {
            'invoice_id': self.id, 'project_name': self.project
        }

    class Meta:
        app_label = 'rsr'
        verbose_name = u'invoice'
        ordering = ['-id', ]
<commit_msg>[#796] Re-added Invoice signal<commit_after># -*- coding: utf-8 -*-

# 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 >.

from datetime import datetime, timedelta

from django.conf import settings
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

from mollie.ideal.utils import get_mollie_banklist

from paypal.standard.ipn.signals import payment_was_flagged

from ..fields import ValidXMLCharField, ValidXMLTextField

class InvoiceManager(models.Manager):
    def get_queryset(self):
        """Returns a queryset of all invoices
        Test invoices are excluded in production mode
        """
        if not settings.DONATION_TEST:
            return super(InvoiceManager, self).get_queryset().exclude(test=True)
        else:
            return super(InvoiceManager, self).get_queryset()

    def stale(self):
        """Returns a queryset of invoices which have been pending
        for longer than settings.PAYPAL_INVOICE_TIMEOUT (60 minutes by default)
        """
        timeout = (datetime.now() - timedelta(minutes=getattr(settings, 'PAYPAL_INVOICE_TIMEOUT', 60)))
        qs = self.filter(status=1, time__lte=timeout)
        return qs

    def complete(self):
        """Returns a queryset of invoices which have
        a status of 'Complete'
        """
        qs = self.filter(status=3)
        return qs

class Invoice(models.Model):
    PAYPAL_INVOICE_STATUS_PENDING = 1
    PAYPAL_INVOICE_STATUS_VOID = 2
    PAYPAL_INVOICE_STATUS_COMPLETE = 3
    PAYPAL_INVOICE_STATUS_STALE = 4

    STATUS_CHOICES = (
        (PAYPAL_INVOICE_STATUS_PENDING, 'Pending'),
        (PAYPAL_INVOICE_STATUS_VOID, 'Void'),
        (PAYPAL_INVOICE_STATUS_COMPLETE, 'Complete'),
        (PAYPAL_INVOICE_STATUS_STALE, 'Stale'),
    )
    # NOTE: There are still a few places in templates where the strings 'paypal' and 'ideal' are used
    PAYMENT_ENGINE_PAYPAL = 'paypal'
    PAYMENT_ENGINE_IDEAL = 'ideal'
    PAYMENT_ENGINES = (
        (PAYMENT_ENGINE_PAYPAL, u'PayPal'),
        (PAYMENT_ENGINE_IDEAL, u'iDEAL'),
    )
    test = models.BooleanField(
        u'test donation',
        help_text=u'This flag is set if the donation was made in test mode.',
        default=False)
    engine = ValidXMLCharField(u'payment engine', choices=PAYMENT_ENGINES, max_length=10, default=PAYMENT_ENGINE_PAYPAL)
    user = models.ForeignKey(User, blank=True, null=True)
    project = models.ForeignKey('Project', related_name='invoices')
    # Common
    amount = models.PositiveIntegerField(help_text=u'Amount requested by user.')
    amount_received = models.DecimalField(
        max_digits=10, decimal_places=2, blank=True, null=True,
        help_text=u'Amount actually received after charges have been applied.'
    )
    time = models.DateTimeField(auto_now_add=True)
    name = ValidXMLCharField(max_length=75, blank=True, null=True)
    email = models.EmailField(blank=True, null=True)
    status = models.PositiveSmallIntegerField('status', choices=STATUS_CHOICES, default=1)
    http_referer = ValidXMLCharField(u'HTTP referer', max_length=255, blank=True)
    campaign_code = ValidXMLCharField(u'Campaign code', blank=True, max_length=15)
    is_anonymous = models.BooleanField(u'anonymous donation', default=False)
    # PayPal
    ipn = ValidXMLCharField(u'PayPal IPN', blank=True, null=True, max_length=75)
    # Mollie
    bank = ValidXMLCharField(u'mollie.nl bank ID', max_length=4, choices=get_mollie_banklist(), blank=True)
    transaction_id = ValidXMLCharField(u'mollie.nl transaction ID', max_length=100, blank=True)

    notes = ValidXMLTextField(verbose_name=_("Notes and comments"), blank=True, default='')

    admin_objects = models.Manager()
    objects = InvoiceManager()

    def get_favicon(self):
        pass  # TODO: @ grab favicon from HTTP_REFERER site

    @property
    def get_name(self):
        if self.user:
            return self.user.get_full_name()
        return self.name

    @property
    def get_email(self):
        if self.user:
            return self.user.email
        return self.email

    @property
    def currency(self):
        return self.project.currency

    @property
    def gateway(self):
        if self.engine == self.PAYMENT_ENGINE_PAYPAL:
            if settings.DONATION_TEST:
                return settings.PAYPAL_SANDBOX_GATEWAY
            else:
                return self.project.paymentgatewayselector.paypal_gateway.account_email
        elif self.engine == 'ideal':
            return self.project.paymentgatewayselector.mollie_gateway.partner_id

    @property
    def locale(self):
        return self.project.paymentgatewayselector.paypal_gateway.locale

    @property
    def notification_email(self):
        if getattr(settings, "DONATION_TEST", False):
            return "test@akvo.org"
        else:
            if self.engine == "paypal":
                return self.project.paymentgatewayselector.paypal_gateway.notification_email
            elif self.engine == "ideal":
                return self.project.paymentgatewayselector.mollie_gateway.notification_email

    @property
    def donation_fee(self):
        return (self.amount - self.amount_received)

    def __unicode__(self):
        return u'Invoice %(invoice_id)s (Project: %(project_name)s)' % {
            'invoice_id': self.id, 'project_name': self.project
        }

    class Meta:
        app_label = 'rsr'
        verbose_name = u'invoice'
        ordering = ['-id', ]

# PayPal IPN listener
def process_paypal_ipn(sender, **kwargs):
    ipn = sender
    if ipn.payment_status == 'Completed':
        invoice = Invoice.objects.get(pk=int(ipn.invoice))
        invoice.amount_received = invoice.amount - ipn.mc_fee
        invoice.ipn = ipn.txn_id
        invoice.status = 3
        invoice.save()
payment_was_flagged.connect(process_paypal_ipn)<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# vim:et sts=4 sw=4
#
# ibus-sdim - The Tables engine for IBus
#
# Copyright (c) 2008-2009 Yu Yuwei <acevery@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# $Id: $
#
__all__ = (
    "tabengine",
)

def _str_percent_decode(str):
    return str.replace("%20", " ").replace("%25", "%")

import os
import ibus
#from ibus import Property
from ibus import keysyms
from ibus import modifier
from ibus import ascii
import re

from gettext import dgettext
_  = lambda a : dgettext ("ibus-sdim", a)
N_ = lambda a : a

import dbus
from socket import *

class KeyEvent:
    all_mods = [
        '', #no modifier
        'A', # alt
        'AC',# alt ctrl
        'ACS', #alt ctrl shift
        'AS',
        'C',
        'CS',
        'S'
        ]
    def __init__(self, keyval, is_press, state):
        self.code = keyval
        self.mask = state
        self.name = ''
        if not is_press:
            self.mask |= modifier.RELEASE_MASK
            return

        try:
            self.name = chr(self.code)
            if self.name == ' ':
                self.name = 'space'
            else:
                self.mask &= ~modifier.SHIFT_MASK
        except:
            self.name = keysyms.keycode_to_name(self.code).lower()

        if self.name in ("control_l",
                         "control_r",
                         "alt_l",
                         "alt_r",
                         "shift_l",
                         "shift_r",
                         ):
            self.name = ""
            return

        mods = ''
        if self.mask & modifier.ALT_MASK:
            mods += 'A'
        if self.mask & modifier.CONTROL_MASK:
            mods += 'C'
        if self.mask & modifier.SHIFT_MASK:
            mods += 'S'

        if mods != '':
            self.name = mods + ' ' + self.name
    def __str__(self):
        return self.name

class tabengine (ibus.EngineBase):
    '''The IM Engine for Tables'''

    _page_size = 10
    def do_connect(self):
        if self.sock:
            self.do_disconnect()
        self.sock = socket(AF_INET, SOCK_STREAM)
        self.sock.connect(("localhost", 12345))
        self.sock = self.sock.makefile("rwb", 0)

    def do_disconnect(self):
        if self.sock:
            self.sock.close()
            self.sock = None

    def __init__ (self, bus, obj_path):
        print 'obj_path is', obj_path
        super(tabengine,self).__init__ (bus,obj_path)
        self._bus = bus
        self.sock = None

        self.do_connect()

        self.clear_data()
        self._lookup_table = ibus.LookupTable (tabengine._page_size)

        self._name = 'sdim'
        print 'name is', self._name
        self._config_section = "engine/%s" % self._name

        # config module
        self._config = self._bus.get_config ()
        self._on = True
        self.reset ()

    def clear_data(self):
        self._preedit_str = ''
        self._cands = []
        self._aux_str = ''
        self._commit_str = ''
        self._cands_str = ''
        self._cand_idx = '0'
        self._active = ''

    def reset (self):
        self._update_ui ()

    def do_destroy(self):
        self.reset ()
        self.focus_out ()
        self.do_disconnect()
        super(tabengine,self).do_destroy()

    def _update_preedit (self):
        '''Update Preedit String in UI'''
        _str = self._preedit_str
        if _str == '':
            super(tabengine, self).update_preedit_text(ibus.Text('',None), 0, False)
        else:
            attrs = ibus.AttrList()
            attrs.append( ibus.AttributeForeground(0x1b3f03,0,len(_str)) )
            # because ibus now can only insert preedit into txt, so...
            attrs = ibus.AttrList()
            attrs.append(ibus.AttributeUnderline(ibus.ATTR_UNDERLINE_SINGLE, 0, len(_str)))

            super(tabengine, self).update_preedit_text(ibus.Text(_str.decode('utf-8'), attrs), len(_str), True)

    def _update_aux (self):
        '''Update Aux String in UI'''
        _aux = self._aux_str
        if _aux:
            attrs = ibus.AttrList([ ibus.AttributeForeground(0x9515b5, 0, len(_aux)) ])
            super(tabengine, self).update_auxiliary_text(ibus.Text(_aux.decode('utf-8'), attrs), True)
        else:
            self.hide_auxiliary_text()

    def _update_lookup_table (self):
        '''Update Lookup Sdim in UI'''
        if self._cands_str == '':
            self.hide_lookup_table()
            return

        _cands = self._cands_str.split()
        _cands = [_str_percent_decode(str) for str in _cands]

        self._lookup_table.clean()

        for cand in _cands:
            self._lookup_table.append_candidate(ibus.Text(cand.decode('utf-8'), None))

        index = int(self._cand_idx) % 10
        self._lookup_table.set_cursor_pos_in_current_page(index)
        self._lookup_table.show_cursor(True)
        self.update_lookup_table ( self._lookup_table, True, True )

    def _update_ui (self):
        '''Update User Interface'''
        self._update_lookup_table ()
        self._update_preedit ()
        self._update_aux ()
        self.commit_string()

    def commit_string (self):
        if self._commit_str == '':
            return
        commit = self._commit_str
        self._commit_str = ''
        super(tabengine,self).commit_text(ibus.Text(commit.decode('utf-8')))

    def process_key_event(self, keyval, keycode, state):
        '''Process Key Events
        Key Events include Key Press and Key Release,
        modifier means Key Pressed
        '''
        key = KeyEvent(keyval, state & modifier.RELEASE_MASK == 0, state)
        # ignore NumLock mask
        key.mask &= ~modifier.MOD2_MASK

        result = self._process_key_event (key)
        return result

    def _process_key_event (self, key):
        '''Internal method to process key event'''
        key = str(key)
        if key == '':
            return False
        if self._preedit_str == '' and len(key) != 1:
            return False
        self._really_process_key(key)
        self._update_ui()
        return True

    def _really_process_key (self, key):
        try:
            self.sock.write("keyed " + key + "\n")
        except:
            self.do_connect()
            return
        self.clear_data()
        while True:
            try:
                line = self.sock.readline()
            except:
                self.do_connect()
                self._aux_str = "Error with sock connection"
                break
            if not line:
                break
            line = line[:-1]

            if line.find('commit: ') == 0:
                self._commit_str = line[len('commit: '):]

            elif line.find('hint: ') == 0:
                self._aux_str = line[len('hint: '):]

            elif line.find('comp: ') == 0:
                self._preedit_str = line[len('comp: '):]

            elif line.find('cands: ') == 0:
                self._cands_str = line[len('cands: '):]

            elif line.find('cand_index: ') == 0:
                self._cand_idx = line[len('cand_index: '):]

            elif line.find('active: ') == 0:
                self._active = line[len('active: '):]

            elif line == "end:":
                break

            else:
                self._aux_str = line

    def focus_in (self):
        if self._on:
            self._update_ui ()

    def focus_out (self):
        super(tabengine,self).focus_out()
        pass

    def enable (self):
        self._on = True
        if not self.sock:
            self.do_connect()
        self.focus_in()

    def disable (self):
        self.reset()
        self.do_disconnect()
        self._on = False

    # for further implementation :)
    @classmethod
    def CONFIG_VALUE_CHANGED(cls, bus, section, name, value):
        config = bus.get_config()
        if section != self._config_section:
            return

    @classmethod
    def CONFIG_RELOADED(cls, bus):
        config = bus.get_config()
        if section != self._config_section:
            return
<commit_msg>sdim: change port so that monkeyrunner won't clash with us bin/cgi-bin/upload-logs.cgi: add session management bin/git-push-mac: push for review bin/overide/adb: fix compatibility bin/watch-and-sync: sync files to remote automatically<commit_after># -*- coding: utf-8 -*-
# vim:et sts=4 sw=4
#
# ibus-sdim - The Tables engine for IBus
#
# Copyright (c) 2008-2009 Yu Yuwei <acevery@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# $Id: $
#
__all__ = (
    "tabengine",
)

def _str_percent_decode(str):
    return str.replace("%20", " ").replace("%25", "%")

import os
import ibus
#from ibus import Property
from ibus import keysyms
from ibus import modifier
from ibus import ascii
import re

from gettext import dgettext
_  = lambda a : dgettext ("ibus-sdim", a)
N_ = lambda a : a

import dbus
from socket import *

class KeyEvent:
    all_mods = [
        '', #no modifier
        'A', # alt
        'AC',# alt ctrl
        'ACS', #alt ctrl shift
        'AS',
        'C',
        'CS',
        'S'
        ]
    def __init__(self, keyval, is_press, state):
        self.code = keyval
        self.mask = state
        self.name = ''
        if not is_press:
            self.mask |= modifier.RELEASE_MASK
            return

        try:
            self.name = chr(self.code)
            if self.name == ' ':
                self.name = 'space'
            else:
                self.mask &= ~modifier.SHIFT_MASK
        except:
            self.name = keysyms.keycode_to_name(self.code).lower()

        if self.name in ("control_l",
                         "control_r",
                         "alt_l",
                         "alt_r",
                         "shift_l",
                         "shift_r",
                         ):
            self.name = ""
            return

        mods = ''
        if self.mask & modifier.ALT_MASK:
            mods += 'A'
        if self.mask & modifier.CONTROL_MASK:
            mods += 'C'
        if self.mask & modifier.SHIFT_MASK:
            mods += 'S'

        if mods != '':
            self.name = mods + ' ' + self.name
    def __str__(self):
        return self.name

class tabengine (ibus.EngineBase):
    '''The IM Engine for Tables'''

    _page_size = 10
    def do_connect(self):
        if self.sock:
            self.do_disconnect()
        self.sock = socket(AF_INET, SOCK_STREAM)
        self.sock.connect(("localhost", 31415))
        self.sock = self.sock.makefile("rwb", 0)

    def do_disconnect(self):
        if self.sock:
            self.sock.close()
            self.sock = None

    def __init__ (self, bus, obj_path):
        print 'obj_path is', obj_path
        super(tabengine,self).__init__ (bus,obj_path)
        self._bus = bus
        self.sock = None

        self.do_connect()

        self.clear_data()
        self._lookup_table = ibus.LookupTable (tabengine._page_size)

        self._name = 'sdim'
        print 'name is', self._name
        self._config_section = "engine/%s" % self._name

        # config module
        self._config = self._bus.get_config ()
        self._on = True
        self.reset ()

    def clear_data(self):
        self._preedit_str = ''
        self._cands = []
        self._aux_str = ''
        self._commit_str = ''
        self._cands_str = ''
        self._cand_idx = '0'
        self._active = ''

    def reset (self):
        self._update_ui ()

    def do_destroy(self):
        self.reset ()
        self.focus_out ()
        self.do_disconnect()
        super(tabengine,self).do_destroy()

    def _update_preedit (self):
        '''Update Preedit String in UI'''
        _str = self._preedit_str
        if _str == '':
            super(tabengine, self).update_preedit_text(ibus.Text('',None), 0, False)
        else:
            attrs = ibus.AttrList()
            attrs.append( ibus.AttributeForeground(0x1b3f03,0,len(_str)) )
            # because ibus now can only insert preedit into txt, so...
            attrs = ibus.AttrList()
            attrs.append(ibus.AttributeUnderline(ibus.ATTR_UNDERLINE_SINGLE, 0, len(_str)))

            super(tabengine, self).update_preedit_text(ibus.Text(_str.decode('utf-8'), attrs), len(_str), True)

    def _update_aux (self):
        '''Update Aux String in UI'''
        _aux = self._aux_str
        if _aux:
            attrs = ibus.AttrList([ ibus.AttributeForeground(0x9515b5, 0, len(_aux)) ])
            super(tabengine, self).update_auxiliary_text(ibus.Text(_aux.decode('utf-8'), attrs), True)
        else:
            self.hide_auxiliary_text()

    def _update_lookup_table (self):
        '''Update Lookup Sdim in UI'''
        if self._cands_str == '':
            self.hide_lookup_table()
            return

        _cands = self._cands_str.split()
        _cands = [_str_percent_decode(str) for str in _cands]

        self._lookup_table.clean()

        for cand in _cands:
            self._lookup_table.append_candidate(ibus.Text(cand.decode('utf-8'), None))

        index = int(self._cand_idx) % 10
        self._lookup_table.set_cursor_pos_in_current_page(index)
        self._lookup_table.show_cursor(True)
        self.update_lookup_table ( self._lookup_table, True, True )

    def _update_ui (self):
        '''Update User Interface'''
        self._update_lookup_table ()
        self._update_preedit ()
        self._update_aux ()
        self.commit_string()

    def commit_string (self):
        if self._commit_str == '':
            return
        commit = self._commit_str
        self._commit_str = ''
        super(tabengine,self).commit_text(ibus.Text(commit.decode('utf-8')))

    def process_key_event(self, keyval, keycode, state):
        '''Process Key Events
        Key Events include Key Press and Key Release,
        modifier means Key Pressed
        '''
        key = KeyEvent(keyval, state & modifier.RELEASE_MASK == 0, state)
        # ignore NumLock mask
        key.mask &= ~modifier.MOD2_MASK

        result = self._process_key_event (key)
        return result

    def _process_key_event (self, key):
        '''Internal method to process key event'''
        key = str(key)
        if key == '':
            return False
        if self._preedit_str == '' and len(key) != 1:
            return False
        self._really_process_key(key)
        self._update_ui()
        return True

    def _really_process_key (self, key):
        try:
            self.sock.write("keyed " + key + "\n")
        except:
            self.do_connect()
            return
        self.clear_data()
        while True:
            try:
                line = self.sock.readline()
            except:
                self.do_connect()
                self._aux_str = "Error with sock connection"
                break
            if not line:
                break
            line = line[:-1]

            if line.find('commit: ') == 0:
                self._commit_str = line[len('commit: '):]

            elif line.find('hint: ') == 0:
                self._aux_str = line[len('hint: '):]

            elif line.find('comp: ') == 0:
                self._preedit_str = line[len('comp: '):]

            elif line.find('cands: ') == 0:
                self._cands_str = line[len('cands: '):]

            elif line.find('cand_index: ') == 0:
                self._cand_idx = line[len('cand_index: '):]

            elif line.find('active: ') == 0:
                self._active = line[len('active: '):]

            elif line == "end:":
                break

            else:
                self._aux_str = line

    def focus_in (self):
        if self._on:
            self._update_ui ()

    def focus_out (self):
        super(tabengine,self).focus_out()
        pass

    def enable (self):
        self._on = True
        if not self.sock:
            self.do_connect()
        self.focus_in()

    def disable (self):
        self.reset()
        self.do_disconnect()
        self._on = False

    # for further implementation :)
    @classmethod
    def CONFIG_VALUE_CHANGED(cls, bus, section, name, value):
        config = bus.get_config()
        if section != self._config_section:
            return

    @classmethod
    def CONFIG_RELOADED(cls, bus):
        config = bus.get_config()
        if section != self._config_section:
            return
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import os
from collections import defaultdict
from flask.ext.script import Command, Option
from alchemist import db
from alchemist.db.model import _component_of
from pkgutil import get_importer
from termcolor import colored
from sqlalchemy import event
from six import print_
from sqlalchemy.orm import Mapper

class Load(Command):
    """Loads the passed named fixture or file.
    """

    name = 'load'

    # The idea is to eventually extend this command to support:

    # @code
    # $ alchemist load <file.(json|yml|py)> [--database <database>]
    # $ alchemist load <name> [--database <database>]
    # @endcode

    # Where the second form would traverse each package and look for a
    # <name>.(json|yml|py) in the package or in a fixtures folder of the
    # package and load each one.

    option_list = [Option(dest='filename')]

    def run(self, filename):
        # Resolve the absoulte path.
        path = os.path.abspath(filename)

        # Resolve the module name.
        name = os.path.basename(os.path.splitext(path)[0])

        # Create a after_* hook so that we can record what happens.
        models = defaultdict(int)

        @event.listens_for(Mapper, 'after_insert')
        def hook(mapper, connection, target):
            target_cls = type(target)
            component = _component_of(target_cls.__module__)
            name = '%s.%s' % (component, target_cls.__name__)
            models[name] += 1

        try:
            # Import and execute the file.
            imp = get_importer(os.path.dirname(filename))
            imp.find_module(name).load_module(name)

            # Commit the session.
            db.session.commit()

        except:
            # Something happened; rollback the session.
            db.session.rollback()

            # Re-raise so the console gets the traceback.
            raise

        # Get sizes for logging.
        max_count = len(str(max(models.values())))

        # Let the user know.
        for name, count in models.items():
            msg = ('{:>%s} {}' % (max_count)).format(count, name)
            print_(colored(' *', 'white', attrs=['dark']),
                   colored('insert', 'cyan'),
                   colored(msg, 'white'),
                   colored('default', 'white', attrs=['dark']))
<commit_msg>Stop 'load' from crashing when items loaded are 0<commit_after># -*- coding: utf-8 -*-
import os
from collections import defaultdict
from flask.ext.script import Command, Option
from alchemist import db
from alchemist.db.model import _component_of
from pkgutil import get_importer
from termcolor import colored
from sqlalchemy import event
from six import print_
from sqlalchemy.orm import Mapper

class Load(Command):
    """Loads the passed named fixture or file.
    """

    name = 'load'

    # The idea is to eventually extend this command to support:

    # @code
    # $ alchemist load <file.(json|yml|py)> [--database <database>]
    # $ alchemist load <name> [--database <database>]
    # @endcode

    # Where the second form would traverse each package and look for a
    # <name>.(json|yml|py) in the package or in a fixtures folder of the
    # package and load each one.

    option_list = [Option(dest='filename')]

    def run(self, filename):
        # Resolve the absoulte path.
        path = os.path.abspath(filename)

        # Resolve the module name.
        name = os.path.basename(os.path.splitext(path)[0])

        # Create a after_* hook so that we can record what happens.
        models = defaultdict(int)

        @event.listens_for(Mapper, 'after_insert')
        def hook(mapper, connection, target):
            target_cls = type(target)
            component = _component_of(target_cls.__module__)
            name = '%s.%s' % (component, target_cls.__name__)
            models[name] += 1

        try:
            # Import and execute the file.
            imp = get_importer(os.path.dirname(filename))
            imp.find_module(name).load_module(name)

            # Commit the session.
            db.session.commit()

        except:
            # Something happened; rollback the session.
            db.session.rollback()

            # Re-raise so the console gets the traceback.
            raise

        # Get sizes for logging.
        if models:
            max_count = len(str(max(models.values())))
        else:
            max_count = 0

        # Let the user know.
        for name, count in models.items():
            msg = ('{:>%s} {}' % (max_count)).format(count, name)
            print_(colored(' *', 'white', attrs=['dark']),
                   colored('insert', 'cyan'),
                   colored(msg, 'white'),
                   colored('default', 'white', attrs=['dark']))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import, division
from flask.ext.script import Command, Option
from alchemist import app, utils
from alchemist.app import _get_application_from_name as _get_app
import pytest
from os import path
from alchemist.conf import settings
from importlib import import_module
from subprocess import Popen

class Test(Command):

    name = 'test'

    option_list = [
        Option(dest='names', nargs='*',
               help='The name of the component (or project) to test.'),
    ]

    capture_all_args = True

    def run(self, *args, **kwargs):

        names = kwargs.pop('names')
        for name in (names or settings['COMPONENTS']):

            # Get the tests module or package location.

            module = import_module(name)
            tests = path.join(path.dirname(module.__file__), 'tests')
            if not path.exists(tests):
                tests += '.py'
                if not path.exists(tests):
                    continue

            # Indicate progress.

            utils.print_('*', 'test', name, path.relpath(path.abspath(tests)))

            # Execute 'py.test' with the remaining arguments.

            if name == 'alchemist':
                cwd = path.dirname(tests)
                tests = path.relpath(tests, cwd)
                cmd = ['py.test', '-p', 'no:alchemist'] + [tests] + list(*args)
                process = Popen(cmd, cwd=cwd)
                process.wait()

            else:
                pytest.main([tests] + list(*args))
<commit_msg>Don't test alchemist in the test runner.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import, division
from flask.ext.script import Command, Option
from alchemist import app, utils
from alchemist.app import _get_application_from_name as _get_app
import pytest
from os import path
from alchemist.conf import settings
from importlib import import_module
from subprocess import Popen

class Test(Command):

    name = 'test'

    option_list = [
        Option(dest='names', nargs='*',
               help='The name of the component (or project) to test.'),
    ]

    capture_all_args = True

    def run(self, *args, **kwargs):

        collected = []
        names = kwargs.pop('names')
        for name in (names or settings['COMPONENTS']):

            # Get the tests module or package location.

            module = import_module(name)
            tests = path.join(path.dirname(module.__file__), 'tests')
            if not path.exists(tests):
                tests += '.py'
                if not path.exists(tests):
                    continue

            # Collect all test components that are not alchemist.

            if name != 'alchemist':
                collected.append(tests)

        # Run collected test modules.
        pytest.main(collected + list(*args))
<|endoftext|>


<commit_before># Tai Sakuma <tai.sakuma@cern.ch>

##____________________________________________________________________________||
class GenericKeyComposerB(object):
    """This class is a faster of GenericKeyComposer.

    This class can be used with BEvents.

    (this docstring is under development.)

    """
    def __init__(self, branchNames, binnings, indices = None):
        self.branchNames = branchNames
        self.binnings = binnings
        self.indices = indices if indices is not None else [None]*len(self.branchNames)

    def begin(self, event):
        self._zip = self._zipArrays(event)

    def __call__(self, event):
        if self._zip is None: return ()
        ret = [ ]
        for branche, binning, index in self._zip:
            if index is not None:
                if len(branche) <= index: return ()
                var = branche[index]
            else:
                var = branche[0]
            var_bin = binning(var)
            if var_bin is None: return ()
            ret.append(var_bin)
        return (tuple(ret), )

    def _zipArrays(self, event):
        self.branches = [ ]
        for varname in self.branchNames:
            try:
                branch = getattr(event, varname)
            except AttributeError, e:
                import logging
                logging.warning(e)
                return None
            self.branches.append(branch)
        return zip(self.branches, self.binnings, self.indices)

##____________________________________________________________________________||
class GenericKeyComposerBFactory(object):
    def __init__(self, branchNames, binnings, indices = None):
        self.branchNames = branchNames
        self.binnings = binnings
        self.indices = indices
    def __call__(self):
        return GenericKeyComposerB(branchNames = self.branchNames, binnings = self.binnings, indices = self.indices)

##____________________________________________________________________________||
<commit_msg>update the docstring in GenericKeyComposerB<commit_after># Tai Sakuma <tai.sakuma@cern.ch>

##____________________________________________________________________________||
class GenericKeyComposerB(object):
    """This class is a faster version of GenericKeyComposer.

    This class can be used with BEvents.

    (this docstring is under development.)

    """
    def __init__(self, branchNames, binnings, indices = None):
        self.branchNames = branchNames
        self.binnings = binnings
        self.indices = indices if indices is not None else [None]*len(self.branchNames)

    def begin(self, event):
        self._zip = self._zipArrays(event)

    def __call__(self, event):
        if self._zip is None: return ()
        ret = [ ]
        for branche, binning, index in self._zip:
            if index is not None:
                if len(branche) <= index: return ()
                var = branche[index]
            else:
                var = branche[0]
            var_bin = binning(var)
            if var_bin is None: return ()
            ret.append(var_bin)
        return (tuple(ret), )

    def _zipArrays(self, event):
        self.branches = [ ]
        for varname in self.branchNames:
            try:
                branch = getattr(event, varname)
            except AttributeError, e:
                import logging
                logging.warning(e)
                return None
            self.branches.append(branch)
        return zip(self.branches, self.binnings, self.indices)

##____________________________________________________________________________||
class GenericKeyComposerBFactory(object):
    def __init__(self, branchNames, binnings, indices = None):
        self.branchNames = branchNames
        self.binnings = binnings
        self.indices = indices
    def __call__(self):
        return GenericKeyComposerB(branchNames = self.branchNames, binnings = self.binnings, indices = self.indices)

##____________________________________________________________________________||
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

"""UnitTests for ap_graph."""

import unittest
from airwaveapiclient import APGraph
from airwaveapiclient import APList
from airwaveapiclient.tests import test_utils
import os

class APGraphUnitTests(unittest.TestCase):

    """Class APGraphUnitTests.

    Unit test for APGraph.

    """

    def setUp(self):
        """Setup."""
        self.url = u'https://192.168.1.1/'
        self.ap_list_file = 'test_ap_list.xml'
        self.here = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(self.here, self.ap_list_file)
        self.ap_list = test_utils.read_file(path)
        self.objs = APList(self.ap_list)

    def tearDown(self):
        """Tear down."""

    def test_init(self):
        """Test init."""
        ap_graph = APGraph(self.url, self.objs[0])
        self.assertEqual(ap_graph.url, u'https://192.168.1.1/')

    def test_client_count_802dot11bgn(self):
        """Test for client_count_802dot11bgn."""
        ap_graph = APGraph(self.url, self.objs[0])
        graph_url = ap_graph.client_count_802dot11bgn()
        _graph_url = ('https://192.168.1.1/nf/rrd_graph?'
                      'end=0s&'
                      'id=1&'
                      'radio_index=1&'
                      'start=-7200s&'
                      'type=ap_client_count')
        self.assertEqual(graph_url, _graph_url)
<commit_msg>Change APGraph init test.<commit_after># -*- coding: utf-8 -*-

"""UnitTests for ap_graph."""

import unittest
from airwaveapiclient import APGraph
from airwaveapiclient import APList
from airwaveapiclient.tests import test_utils
import os

class APGraphUnitTests(unittest.TestCase):

    """Class APGraphUnitTests.

    Unit test for APGraph.

    """

    def setUp(self):
        """Setup."""
        self.url = u'https://192.168.1.1/'
        self.ap_list_file = 'test_ap_list.xml'
        self.here = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(self.here, self.ap_list_file)
        self.ap_list = test_utils.read_file(path)
        self.objs = APList(self.ap_list)

    def tearDown(self):
        """Tear down."""

    def test_init(self):
        """Test init."""
        ap_graph = APGraph(self.url, self.objs[0])
        self.assertEqual(ap_graph.url, u'https://192.168.1.1/')
        self.assertEqual(ap_graph.path, u'/nf/rrd_graph')
        self.assertEqual(ap_graph.default_start_time, -7200)
        self.assertEqual(ap_graph.default_end_time, 0)

        ap_graph = APGraph(self.url, self.objs[1])
        self.assertEqual(ap_graph.url, u'https://192.168.1.1/')
        self.assertEqual(ap_graph.path, u'/nf/rrd_graph')
        self.assertEqual(ap_graph.default_start_time, -7200)
        self.assertEqual(ap_graph.default_end_time, 0)

        ap_graph = APGraph(self.url, self.objs[2])
        self.assertEqual(ap_graph.url, u'https://192.168.1.1/')
        self.assertEqual(ap_graph.path, u'/nf/rrd_graph')
        self.assertEqual(ap_graph.default_start_time, -7200)
        self.assertEqual(ap_graph.default_end_time, 0)

    def test_client_count_802dot11bgn(self):
        """Test for client_count_802dot11bgn."""
        ap_graph = APGraph(self.url, self.objs[0])
        graph_url = ap_graph.client_count_802dot11bgn()
        _graph_url = ('https://192.168.1.1/nf/rrd_graph?'
                      'end=0s&'
                      'id=1&'
                      'radio_index=1&'
                      'start=-7200s&'
                      'type=ap_client_count')
        self.assertEqual(graph_url, _graph_url)
<|endoftext|>


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

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

"""
    Plugin Lookup
"""

import json
from logging import getLogger
from bottle import request, response

logger = getLogger(__name__)

# Will be valued by the plugin loader
webui = None

def lookup():  # pragma: no cover - not yet implemented!
    """
    TODO:
    Empty ... not yet implemented!
    """
    datamgr = request.environ['beaker.session']['datamanager']

    query = request.query.get('query', '')

    logger.warning("lookup: %s", query)

    elements = datamgr.get_livestate(search={'where': {'name': {"$regex": ".*" + query + ".*"}}})
    names = [e.name for e in elements]
    logger.warning("lookup: %s", names)

    return json.dumps(names)

pages = {
    lookup: {
        'name': 'GetLookup',
        'route': '/lookup',
        'method': 'GET'
    }
}
<commit_msg>Fix typo<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

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

"""
    Plugin Lookup
"""

import json
from logging import getLogger
from bottle import request, response

logger = getLogger(__name__)

# Will be valued by the plugin loader
webui = None

def lookup():  # pragma: no cover - not yet implemented!
    """
    Search in the livestate for an element name
    """
    datamgr = request.environ['beaker.session']['datamanager']

    query = request.query.get('query', '')

    logger.warning("lookup: %s", query)

    elements = datamgr.get_livestate(search={'where': {'name': {"$regex": ".*" + query + ".*"}}})
    names = [e.name for e in elements]
    logger.warning("lookup: %s", names)

    return json.dumps(names)

pages = {
    lookup: {
        'name': 'GetLookup',
        'route': '/lookup',
        'method': 'GET'
    }
}
<|endoftext|>


<commit_before># Tai Sakuma <tai.sakuma@gmail.com>
import os
import shutil
import datetime
import tempfile
import imp
import logging
import tarfile
import gzip
import json
import re

try:
    import cPickle as pickle
except:
    import pickle

import alphatwirl

from alphatwirl.misc.deprecation import atrenamed_class_method_option

##__________________________________________________________________||
class WorkingArea(object):
    """
        Args:
        topdir (str): a path to a directory in which a new directory will be created
        python_modules (list): names of python modules to be shipped to worker nodes

    """

    @atrenamed_class_method_option(old='dir', new='topdir')
    def __init__(self, topdir, python_modules):
        self.topdir = topdir
        self.python_modules = python_modules
        self.path = None
        self.last_package_index = None

    def __repr__(self):
        return '{}(topdir={!r}, python_modules={!r}, path={!r}, last_package_index={!r})'.format(
            self.__class__.__name__,
            self.topdir, self.python_modules, self.path, self.last_package_index
        )

    def open(self):
        self.path = self._prepare_dir(self.topdir)
        self._copy_run_py(area_path=self.path)
        self._save_logging_levels(area_path=self.path)
        self._put_python_modules(modules=self.python_modules, area_path=self.path)
        self.last_package_index = -1 # so it starts from 0

    def put_package(self, package):

        self.last_package_index += 1
        package_index = self.last_package_index

        package_path = self.package_path(package_index)
        # relative to self.path, e.g., 'task_00009.p.gz'

        package_fullpath = os.path.join(self.path, package_path)
        # e.g., '{path}/tpd_20161129_122841_HnpcmF/task_00009.p.gz'

        with gzip.open(package_fullpath, 'wb') as f:
           pickle.dump(package, f, protocol=pickle.HIGHEST_PROTOCOL)
           f.close()

        return package_index

    def package_path(self, package_index):
        return 'task_{:05d}.p.gz'.format(package_index)

    def collect_result(self, package_index):

        dirname = 'task_{:05d}'.format(package_index)
        # e.g., 'task_00009'

        result_path = os.path.join(self.path, 'results', dirname, 'result.p.gz')
        # e.g., '{path}/tpd_20161129_122841_HnpcmF/results/task_00009/result.p.gz'

        try:
           with gzip.open(result_path, 'rb') as f:
              result = pickle.load(f)
        except (IOError, EOFError) as e:
           logger = logging.getLogger(__name__)
           logger.warning(e)
           return None

        return result

    def close(self):
        self.path = None
        self.last_package_index = None

    def _prepare_dir(self, dir):

        alphatwirl.mkdir_p(dir)

        prefix = 'tpd_{:%Y%m%d_%H%M%S}_'.format(datetime.datetime.now())
        # e.g., 'tpd_20161129_122841_'

        path = tempfile.mkdtemp(prefix=prefix, dir=dir)
        # e.g., '{path}/tpd_20161129_122841_HnpcmF'

        return path

    def _copy_run_py(self, area_path):
        thisdir = os.path.dirname(__file__)
        src = os.path.join(thisdir, 'run.py')
        shutil.copy(src, area_path)

    def _save_logging_levels(self, area_path):
        logger_names = logging.Logger.manager.loggerDict.keys()
        loglevel_dict = {l: logging.getLogger(l).getEffectiveLevel() for l in logger_names}

        json_str = json.dumps(loglevel_dict, indent=4, sort_keys=True)
        json_str = re.sub(r' *\n', '\n', json_str, flags=re.MULTILINE)
        json_str += "\n"
        json_bytes = json_str.encode('utf-8')
        path = os.path.join(area_path, 'logging_levels.json.gz')
        with gzip.open(path, "w") as f:
            f.write(json_bytes)

    def _put_python_modules(self, modules, area_path):

        if not modules: return

        tar = tarfile.open(os.path.join(area_path, 'python_modules.tar.gz'), 'w:gz')

        def tar_filter(tarinfo):
            exclude_extensions = ('.pyc', )
            exclude_names = ('.git', )
            if os.path.splitext(tarinfo.name)[1] in exclude_extensions: return None
            if os.path.basename(tarinfo.name) in exclude_names: return None
            return tarinfo

        for module in modules:
            imp_tuple = imp.find_module(module)
            path = imp_tuple[1]
            arcname = os.path.join('python_modules', module + imp_tuple[2][0])
            tar.add(path, arcname=arcname, filter=tar_filter)
        tar.close()

##__________________________________________________________________||
<commit_msg>update __repr__() in WorkingArea<commit_after># Tai Sakuma <tai.sakuma@gmail.com>
import os
import shutil
import datetime
import tempfile
import imp
import logging
import tarfile
import gzip
import json
import re

try:
    import cPickle as pickle
except:
    import pickle

import alphatwirl

from alphatwirl.misc.deprecation import atrenamed_class_method_option

##__________________________________________________________________||
class WorkingArea(object):
    """
        Args:
        topdir (str): a path to a directory in which a new directory will be created
        python_modules (list): names of python modules to be shipped to worker nodes

    """

    @atrenamed_class_method_option(old='dir', new='topdir')
    def __init__(self, topdir, python_modules):
        self.topdir = topdir
        self.python_modules = python_modules
        self.path = None
        self.last_package_index = None

    def __repr__(self):
        name_value_pairs = (
            ('topdir', self.topdir),
            ('python_modules', self.python_modules),
        )
        return '{}({})'.format(
            self.__class__.__name__,
            ', '.join(['{}={!r}'.format(n, v) for n, v in name_value_pairs]),
        )

    def open(self):
        self.path = self._prepare_dir(self.topdir)
        self._copy_run_py(area_path=self.path)
        self._save_logging_levels(area_path=self.path)
        self._put_python_modules(modules=self.python_modules, area_path=self.path)
        self.last_package_index = -1 # so it starts from 0

    def put_package(self, package):

        self.last_package_index += 1
        package_index = self.last_package_index

        package_path = self.package_path(package_index)
        # relative to self.path, e.g., 'task_00009.p.gz'

        package_fullpath = os.path.join(self.path, package_path)
        # e.g., '{path}/tpd_20161129_122841_HnpcmF/task_00009.p.gz'

        with gzip.open(package_fullpath, 'wb') as f:
           pickle.dump(package, f, protocol=pickle.HIGHEST_PROTOCOL)
           f.close()

        return package_index

    def package_path(self, package_index):
        return 'task_{:05d}.p.gz'.format(package_index)

    def collect_result(self, package_index):

        dirname = 'task_{:05d}'.format(package_index)
        # e.g., 'task_00009'

        result_path = os.path.join(self.path, 'results', dirname, 'result.p.gz')
        # e.g., '{path}/tpd_20161129_122841_HnpcmF/results/task_00009/result.p.gz'

        try:
           with gzip.open(result_path, 'rb') as f:
              result = pickle.load(f)
        except (IOError, EOFError) as e:
           logger = logging.getLogger(__name__)
           logger.warning(e)
           return None

        return result

    def close(self):
        self.path = None
        self.last_package_index = None

    def _prepare_dir(self, dir):

        alphatwirl.mkdir_p(dir)

        prefix = 'tpd_{:%Y%m%d_%H%M%S}_'.format(datetime.datetime.now())
        # e.g., 'tpd_20161129_122841_'

        path = tempfile.mkdtemp(prefix=prefix, dir=dir)
        # e.g., '{path}/tpd_20161129_122841_HnpcmF'

        return path

    def _copy_run_py(self, area_path):
        thisdir = os.path.dirname(__file__)
        src = os.path.join(thisdir, 'run.py')
        shutil.copy(src, area_path)

    def _save_logging_levels(self, area_path):
        logger_names = logging.Logger.manager.loggerDict.keys()
        loglevel_dict = {l: logging.getLogger(l).getEffectiveLevel() for l in logger_names}

        json_str = json.dumps(loglevel_dict, indent=4, sort_keys=True)
        json_str = re.sub(r' *\n', '\n', json_str, flags=re.MULTILINE)
        json_str += "\n"
        json_bytes = json_str.encode('utf-8')
        path = os.path.join(area_path, 'logging_levels.json.gz')
        with gzip.open(path, "w") as f:
            f.write(json_bytes)

    def _put_python_modules(self, modules, area_path):

        if not modules: return

        tar = tarfile.open(os.path.join(area_path, 'python_modules.tar.gz'), 'w:gz')

        def tar_filter(tarinfo):
            exclude_extensions = ('.pyc', )
            exclude_names = ('.git', )
            if os.path.splitext(tarinfo.name)[1] in exclude_extensions: return None
            if os.path.basename(tarinfo.name) in exclude_names: return None
            return tarinfo

        for module in modules:
            imp_tuple = imp.find_module(module)
            path = imp_tuple[1]
            arcname = os.path.join('python_modules', module + imp_tuple[2][0])
            tar.add(path, arcname=arcname, filter=tar_filter)
        tar.close()

##__________________________________________________________________||
<|endoftext|>


<commit_before># Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import unittest

import httplib2

from gcloud import _helpers
from gcloud.environment_vars import PUBSUB_EMULATOR
from gcloud.environment_vars import TESTS_PROJECT
from gcloud import pubsub

from retry import RetryInstanceState
from retry import RetryResult
from system_test_utils import EmulatorCreds
from system_test_utils import unique_resource_id

DEFAULT_TOPIC_NAME = 'subscribe-me' + unique_resource_id('-')

class Config(object):
    """Run-time configuration to be modified at set-up.

    This is a mutable stand-in to allow test set-up to modify
    global state.
    """
    CLIENT = None

def setUpModule():
    _helpers.PROJECT = TESTS_PROJECT
    if os.getenv(PUBSUB_EMULATOR) is None:
        Config.CLIENT = pubsub.Client()
    else:
        credentials = EmulatorCreds()
        http = httplib2.Http()  # Un-authorized.
        Config.CLIENT = pubsub.Client(credentials=credentials,
                                      http=http)

class TestPubsub(unittest.TestCase):

    def setUp(self):
        self.to_delete = []

    def tearDown(self):
        for doomed in self.to_delete:
            doomed.delete()

    def test_create_topic(self):
        topic_name = 'a-new-topic' + unique_resource_id('-')
        topic = Config.CLIENT.topic(topic_name)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        self.assertTrue(topic.exists())
        self.assertEqual(topic.name, topic_name)

    def test_list_topics(self):
        before, _ = Config.CLIENT.list_topics()
        topics_to_create = [
            'new' + unique_resource_id(),
            'newer' + unique_resource_id(),
            'newest' + unique_resource_id(),
        ]
        for topic_name in topics_to_create:
            topic = Config.CLIENT.topic(topic_name)
            topic.create()
            self.to_delete.append(topic)

        # Retrieve the topics.
        def _all_created(result):
            return len(result[0]) == len(before) + len(topics_to_create)

        retry = RetryResult(_all_created)
        after, _ = retry(Config.CLIENT.list_topics)()

        created = [topic for topic in after
                   if topic.name in topics_to_create and
                   topic.project == Config.CLIENT.project]
        self.assertEqual(len(created), len(topics_to_create))

    def test_create_subscription_defaults(self):
        topic = Config.CLIENT.topic(DEFAULT_TOPIC_NAME)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        SUBSCRIPTION_NAME = 'subscribing-now' + unique_resource_id('-')
        subscription = topic.subscription(SUBSCRIPTION_NAME)
        self.assertFalse(subscription.exists())
        subscription.create()
        self.to_delete.append(subscription)
        self.assertTrue(subscription.exists())
        self.assertEqual(subscription.name, SUBSCRIPTION_NAME)
        self.assertTrue(subscription.topic is topic)

    def test_create_subscription_w_ack_deadline(self):
        topic = Config.CLIENT.topic(DEFAULT_TOPIC_NAME)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        SUBSCRIPTION_NAME = 'subscribing-now' + unique_resource_id()
        subscription = topic.subscription(SUBSCRIPTION_NAME, ack_deadline=120)
        self.assertFalse(subscription.exists())
        subscription.create()
        self.to_delete.append(subscription)
        self.assertTrue(subscription.exists())
        self.assertEqual(subscription.name, SUBSCRIPTION_NAME)
        self.assertEqual(subscription.ack_deadline, 120)
        self.assertTrue(subscription.topic is topic)

    def test_list_subscriptions(self):
        topic = Config.CLIENT.topic(DEFAULT_TOPIC_NAME)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        empty, _ = topic.list_subscriptions()
        self.assertEqual(len(empty), 0)
        subscriptions_to_create = [
            'new' + unique_resource_id(),
            'newer' + unique_resource_id(),
            'newest' + unique_resource_id(),
        ]
        for subscription_name in subscriptions_to_create:
            subscription = topic.subscription(subscription_name)
            subscription.create()
            self.to_delete.append(subscription)

        # Retrieve the subscriptions.
        def _all_created(result):
            return len(result[0]) == len(subscriptions_to_create)

        retry = RetryResult(_all_created)
        all_subscriptions, _ = retry(topic.list_subscriptions)()

        created = [subscription for subscription in all_subscriptions
                   if subscription.name in subscriptions_to_create]
        self.assertEqual(len(created), len(subscriptions_to_create))

    def test_message_pull_mode_e2e(self):
        topic = Config.CLIENT.topic(DEFAULT_TOPIC_NAME,
                                    timestamp_messages=True)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        SUBSCRIPTION_NAME = 'subscribing-now' + unique_resource_id('-')
        subscription = topic.subscription(SUBSCRIPTION_NAME)
        self.assertFalse(subscription.exists())
        subscription.create()
        self.to_delete.append(subscription)

        MESSAGE_1 = b'MESSAGE ONE'
        MESSAGE_2 = b'MESSAGE ONE'
        EXTRA_1 = 'EXTRA 1'
        EXTRA_2 = 'EXTRA 2'
        topic.publish(MESSAGE_1, extra=EXTRA_1)
        topic.publish(MESSAGE_2, extra=EXTRA_2)

        received = subscription.pull(max_messages=2)
        ack_ids = [recv[0] for recv in received]
        subscription.acknowledge(ack_ids)
        messages = [recv[1] for recv in received]

        def _by_timestamp(message):
            return message.timestamp

        message1, message2 = sorted(messages, key=_by_timestamp)
        self.assertEqual(message1.data, MESSAGE_1)
        self.assertEqual(message1.attributes['extra'], EXTRA_1)
        self.assertEqual(message2.data, MESSAGE_2)
        self.assertEqual(message2.attributes['extra'], EXTRA_2)

    def _maybe_emulator_skip(self):
        # NOTE: We check at run-time rather than using the @unittest.skipIf
        #       decorator. This matches the philosophy behind using
        #       setUpModule to determine the environment at run-time
        #       rather than at import time.
        if os.getenv(PUBSUB_EMULATOR) is not None:
            self.skipTest('IAM not supported by Pub/Sub emulator')

    def test_topic_iam_policy(self):
        from gcloud.pubsub.iam import PUBSUB_TOPICS_GET_IAM_POLICY
        self._maybe_emulator_skip()
        topic_name = 'test-topic-iam-policy-topic' + unique_resource_id('-')
        topic = Config.CLIENT.topic(topic_name)
        topic.create()

        # Retry / backoff up to 7 seconds (1 + 2 + 4)
        retry = RetryResult(lambda result: result, max_tries=4)
        retry(topic.exists)()

        self.assertTrue(topic.exists())
        self.to_delete.append(topic)

        if topic.check_iam_permissions([PUBSUB_TOPICS_GET_IAM_POLICY]):
            policy = topic.get_iam_policy()
            policy.viewers.add(policy.user('jjg@google.com'))
            new_policy = topic.set_iam_policy(policy)
            self.assertEqual(new_policy.viewers, policy.viewers)

    def test_subscription_iam_policy(self):
        from gcloud.pubsub.iam import PUBSUB_SUBSCRIPTIONS_GET_IAM_POLICY
        self._maybe_emulator_skip()
        topic_name = 'test-sub-iam-policy-topic' + unique_resource_id('-')
        topic = Config.CLIENT.topic(topic_name)
        topic.create()

        # Retry / backoff up to 7 seconds (1 + 2 + 4)
        retry = RetryResult(lambda result: result, max_tries=4)
        retry(topic.exists)()

        self.assertTrue(topic.exists())
        self.to_delete.append(topic)
        SUB_NAME = 'test-sub-iam-policy-sub' + unique_resource_id('-')
        subscription = topic.subscription(SUB_NAME)
        subscription.create()

        # Retry / backoff up to 7 seconds (1 + 2 + 4)
        retry = RetryResult(lambda result: result, max_tries=4)
        retry(subscription.exists)()

        self.assertTrue(subscription.exists())
        self.to_delete.insert(0, subscription)

        if subscription.check_iam_permissions(
                [PUBSUB_SUBSCRIPTIONS_GET_IAM_POLICY]):
            policy = subscription.get_iam_policy()
            policy.viewers.add(policy.user('jjg@google.com'))
            new_policy = subscription.set_iam_policy(policy)
            self.assertEqual(new_policy.viewers, policy.viewers)

    def test_fetch_delete_subscription_w_deleted_topic(self):
        TO_DELETE = 'delete-me' + unique_resource_id('-')
        ORPHANED = 'orphaned' + unique_resource_id('-')
        topic = Config.CLIENT.topic(TO_DELETE)
        topic.create()
        subscription = topic.subscription(ORPHANED)
        subscription.create()
        topic.delete()

        all_subs = []
        token = None
        while True:
            subs, token = Config.CLIENT.list_subscriptions(page_token=token)
            all_subs.extend(subs)
            if token is None:
                break

        created = [subscription for subscription in all_subs
                   if subscription.name == ORPHANED]
        self.assertEqual(len(created), 1)
        orphaned = created[0]

        def _no_topic(instance):
            return instance.topic is None

        retry = RetryInstanceState(_no_topic)
        retry(orphaned.reload)()

        self.assertTrue(orphaned.topic is None)
        orphaned.delete()
<commit_msg>Wait longer for orphaned subscription to have topic==None.<commit_after># Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import unittest

import httplib2

from gcloud import _helpers
from gcloud.environment_vars import PUBSUB_EMULATOR
from gcloud.environment_vars import TESTS_PROJECT
from gcloud import pubsub

from retry import RetryInstanceState
from retry import RetryResult
from system_test_utils import EmulatorCreds
from system_test_utils import unique_resource_id

DEFAULT_TOPIC_NAME = 'subscribe-me' + unique_resource_id('-')

class Config(object):
    """Run-time configuration to be modified at set-up.

    This is a mutable stand-in to allow test set-up to modify
    global state.
    """
    CLIENT = None

def setUpModule():
    _helpers.PROJECT = TESTS_PROJECT
    if os.getenv(PUBSUB_EMULATOR) is None:
        Config.CLIENT = pubsub.Client()
    else:
        credentials = EmulatorCreds()
        http = httplib2.Http()  # Un-authorized.
        Config.CLIENT = pubsub.Client(credentials=credentials,
                                      http=http)

class TestPubsub(unittest.TestCase):

    def setUp(self):
        self.to_delete = []

    def tearDown(self):
        for doomed in self.to_delete:
            doomed.delete()

    def test_create_topic(self):
        topic_name = 'a-new-topic' + unique_resource_id('-')
        topic = Config.CLIENT.topic(topic_name)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        self.assertTrue(topic.exists())
        self.assertEqual(topic.name, topic_name)

    def test_list_topics(self):
        before, _ = Config.CLIENT.list_topics()
        topics_to_create = [
            'new' + unique_resource_id(),
            'newer' + unique_resource_id(),
            'newest' + unique_resource_id(),
        ]
        for topic_name in topics_to_create:
            topic = Config.CLIENT.topic(topic_name)
            topic.create()
            self.to_delete.append(topic)

        # Retrieve the topics.
        def _all_created(result):
            return len(result[0]) == len(before) + len(topics_to_create)

        retry = RetryResult(_all_created)
        after, _ = retry(Config.CLIENT.list_topics)()

        created = [topic for topic in after
                   if topic.name in topics_to_create and
                   topic.project == Config.CLIENT.project]
        self.assertEqual(len(created), len(topics_to_create))

    def test_create_subscription_defaults(self):
        topic = Config.CLIENT.topic(DEFAULT_TOPIC_NAME)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        SUBSCRIPTION_NAME = 'subscribing-now' + unique_resource_id('-')
        subscription = topic.subscription(SUBSCRIPTION_NAME)
        self.assertFalse(subscription.exists())
        subscription.create()
        self.to_delete.append(subscription)
        self.assertTrue(subscription.exists())
        self.assertEqual(subscription.name, SUBSCRIPTION_NAME)
        self.assertTrue(subscription.topic is topic)

    def test_create_subscription_w_ack_deadline(self):
        topic = Config.CLIENT.topic(DEFAULT_TOPIC_NAME)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        SUBSCRIPTION_NAME = 'subscribing-now' + unique_resource_id()
        subscription = topic.subscription(SUBSCRIPTION_NAME, ack_deadline=120)
        self.assertFalse(subscription.exists())
        subscription.create()
        self.to_delete.append(subscription)
        self.assertTrue(subscription.exists())
        self.assertEqual(subscription.name, SUBSCRIPTION_NAME)
        self.assertEqual(subscription.ack_deadline, 120)
        self.assertTrue(subscription.topic is topic)

    def test_list_subscriptions(self):
        topic = Config.CLIENT.topic(DEFAULT_TOPIC_NAME)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        empty, _ = topic.list_subscriptions()
        self.assertEqual(len(empty), 0)
        subscriptions_to_create = [
            'new' + unique_resource_id(),
            'newer' + unique_resource_id(),
            'newest' + unique_resource_id(),
        ]
        for subscription_name in subscriptions_to_create:
            subscription = topic.subscription(subscription_name)
            subscription.create()
            self.to_delete.append(subscription)

        # Retrieve the subscriptions.
        def _all_created(result):
            return len(result[0]) == len(subscriptions_to_create)

        retry = RetryResult(_all_created)
        all_subscriptions, _ = retry(topic.list_subscriptions)()

        created = [subscription for subscription in all_subscriptions
                   if subscription.name in subscriptions_to_create]
        self.assertEqual(len(created), len(subscriptions_to_create))

    def test_message_pull_mode_e2e(self):
        topic = Config.CLIENT.topic(DEFAULT_TOPIC_NAME,
                                    timestamp_messages=True)
        self.assertFalse(topic.exists())
        topic.create()
        self.to_delete.append(topic)
        SUBSCRIPTION_NAME = 'subscribing-now' + unique_resource_id('-')
        subscription = topic.subscription(SUBSCRIPTION_NAME)
        self.assertFalse(subscription.exists())
        subscription.create()
        self.to_delete.append(subscription)

        MESSAGE_1 = b'MESSAGE ONE'
        MESSAGE_2 = b'MESSAGE ONE'
        EXTRA_1 = 'EXTRA 1'
        EXTRA_2 = 'EXTRA 2'
        topic.publish(MESSAGE_1, extra=EXTRA_1)
        topic.publish(MESSAGE_2, extra=EXTRA_2)

        received = subscription.pull(max_messages=2)
        ack_ids = [recv[0] for recv in received]
        subscription.acknowledge(ack_ids)
        messages = [recv[1] for recv in received]

        def _by_timestamp(message):
            return message.timestamp

        message1, message2 = sorted(messages, key=_by_timestamp)
        self.assertEqual(message1.data, MESSAGE_1)
        self.assertEqual(message1.attributes['extra'], EXTRA_1)
        self.assertEqual(message2.data, MESSAGE_2)
        self.assertEqual(message2.attributes['extra'], EXTRA_2)

    def _maybe_emulator_skip(self):
        # NOTE: We check at run-time rather than using the @unittest.skipIf
        #       decorator. This matches the philosophy behind using
        #       setUpModule to determine the environment at run-time
        #       rather than at import time.
        if os.getenv(PUBSUB_EMULATOR) is not None:
            self.skipTest('IAM not supported by Pub/Sub emulator')

    def test_topic_iam_policy(self):
        from gcloud.pubsub.iam import PUBSUB_TOPICS_GET_IAM_POLICY
        self._maybe_emulator_skip()
        topic_name = 'test-topic-iam-policy-topic' + unique_resource_id('-')
        topic = Config.CLIENT.topic(topic_name)
        topic.create()

        # Retry / backoff up to 7 seconds (1 + 2 + 4)
        retry = RetryResult(lambda result: result, max_tries=4)
        retry(topic.exists)()

        self.assertTrue(topic.exists())
        self.to_delete.append(topic)

        if topic.check_iam_permissions([PUBSUB_TOPICS_GET_IAM_POLICY]):
            policy = topic.get_iam_policy()
            policy.viewers.add(policy.user('jjg@google.com'))
            new_policy = topic.set_iam_policy(policy)
            self.assertEqual(new_policy.viewers, policy.viewers)

    def test_subscription_iam_policy(self):
        from gcloud.pubsub.iam import PUBSUB_SUBSCRIPTIONS_GET_IAM_POLICY
        self._maybe_emulator_skip()
        topic_name = 'test-sub-iam-policy-topic' + unique_resource_id('-')
        topic = Config.CLIENT.topic(topic_name)
        topic.create()

        # Retry / backoff up to 7 seconds (1 + 2 + 4)
        retry = RetryResult(lambda result: result, max_tries=4)
        retry(topic.exists)()

        self.assertTrue(topic.exists())
        self.to_delete.append(topic)
        SUB_NAME = 'test-sub-iam-policy-sub' + unique_resource_id('-')
        subscription = topic.subscription(SUB_NAME)
        subscription.create()

        # Retry / backoff up to 7 seconds (1 + 2 + 4)
        retry = RetryResult(lambda result: result, max_tries=4)
        retry(subscription.exists)()

        self.assertTrue(subscription.exists())
        self.to_delete.insert(0, subscription)

        if subscription.check_iam_permissions(
                [PUBSUB_SUBSCRIPTIONS_GET_IAM_POLICY]):
            policy = subscription.get_iam_policy()
            policy.viewers.add(policy.user('jjg@google.com'))
            new_policy = subscription.set_iam_policy(policy)
            self.assertEqual(new_policy.viewers, policy.viewers)

    def test_fetch_delete_subscription_w_deleted_topic(self):
        TO_DELETE = 'delete-me' + unique_resource_id('-')
        ORPHANED = 'orphaned' + unique_resource_id('-')
        topic = Config.CLIENT.topic(TO_DELETE)
        topic.create()
        subscription = topic.subscription(ORPHANED)
        subscription.create()
        topic.delete()

        all_subs = []
        token = None
        while True:
            subs, token = Config.CLIENT.list_subscriptions(page_token=token)
            all_subs.extend(subs)
            if token is None:
                break

        created = [subscription for subscription in all_subs
                   if subscription.name == ORPHANED]
        self.assertEqual(len(created), 1)
        orphaned = created[0]

        def _no_topic(instance):
            return instance.topic is None

        retry = RetryInstanceState(_no_topic, max_tries=6)
        retry(orphaned.reload)()

        self.assertTrue(orphaned.topic is None)
        orphaned.delete()
<|endoftext|>


<commit_before>import unittest
"""
Given an array of positive and negative numbers in random order, rearrange the array elements
so that positive and negative numbers are placed alternatively. If one type of numbers is more
than half, the extra ones appear at end of array.
Input: -1 2 -3 4 5 6 -7 8 9
Output: 9 -7 8 -3 5 -1 2 4 6
"""

"""
Approach:
1. Get all the positive numbers on one side.
2. Get all the negative numbers on other side.
3. Now swap every alternate negative number with next positive number.
"""

def rearrage_positive_and_negative(list_of_numbers):

    # Get all negative numbers before all positive numbers
    i = -1
    for j in range(len(list_of_numbers)):
        if list_of_numbers[j] < 0:
            i += 1
            list_of_numbers[i], list_of_numbers[j] = list_of_numbers[j], list_of_numbers[i]

    pos_index = i + 1
    neg_index = 0

    while pos_index < len(list_of_numbers) and neg_index < pos_index and list_of_numbers[neg_index] < 0:
        list_of_numbers[pos_index], list_of_numbers[neg_index] = list_of_numbers[neg_index],\
                                                                 list_of_numbers[pos_index]
        pos_index += 1
        neg_index += 2

    return list_of_numbers

class TestRearrangement(unittest.TestCase):

    def test_rearrangement(self):
        result = [4, -3, 5, -1, 6, -7, 2, 8, 9]
        self.assertEqual(rearrage_positive_and_negative([-1, 2, -3, 4, 5, 6, -7, 8, 9]), result)
<commit_msg>Simplified condition<commit_after>import unittest
"""
Given an array of positive and negative numbers in random order, rearrange the array elements
so that positive and negative numbers are placed alternatively. If one type of numbers is more
than half, the extra ones appear at end of array.
Input: -1 2 -3 4 5 6 -7 8 9
Output: 9 -7 8 -3 5 -1 2 4 6
"""

"""
Approach:
1. Get all the positive numbers on one side.
2. Get all the negative numbers on other side.
3. Now swap every alternate negative number with next positive number.
"""

def rearrage_positive_and_negative(list_of_numbers):

    # Get all negative numbers before all positive numbers
    i = -1
    for j in range(len(list_of_numbers)):
        if list_of_numbers[j] < 0:
            i += 1
            list_of_numbers[i], list_of_numbers[j] = list_of_numbers[j], list_of_numbers[i]

    pos_index = i + 1
    neg_index = 0

    while neg_index < pos_index < len(list_of_numbers) and list_of_numbers[neg_index] < 0:
        list_of_numbers[pos_index], list_of_numbers[neg_index] = list_of_numbers[neg_index],\
                                                                 list_of_numbers[pos_index]
        pos_index += 1
        neg_index += 2

    return list_of_numbers

class TestRearrangement(unittest.TestCase):

    def test_rearrangement(self):
        result = [4, -3, 5, -1, 6, -7, 2, 8, 9]
        self.assertEqual(rearrage_positive_and_negative([-1, 2, -3, 4, 5, 6, -7, 8, 9]), result)
<|endoftext|>


<commit_before>
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Altai API Service
# Copyright (C) 2012 Grid Dynamics Consulting Services, Inc
# All Rights Reserved
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.

import uuid

from flask import Blueprint, abort, url_for, g

from openstackclient_base import exceptions as osc_exc
from altai_api import exceptions as exc

from altai_api.utils.misc import from_mb, from_gb, to_mb, to_gb
from altai_api.utils import *

from altai_api.utils.decorators import root_endpoint

from altai_api.schema import Schema
from altai_api.schema import types as st

instance_types = Blueprint('instance_types', __name__)

def _instance_type_from_nova(flavor):
    return {
        u'id': flavor.id,
        u'href': url_for('instance_types.get_instance_type',
                         instance_type_id=flavor.id),
        u'name': flavor.name,
        u'cpus': flavor.vcpus,
        u'ram': from_mb(flavor.ram),
        u'root-size': from_gb(flavor.disk),
        u'ephemeral-size': from_gb(flavor.ephemeral)
    }

def _instance_type_for_nova(data):
    return  {
        u'name': data['name'],
        u'ram': to_mb(data['ram']),
        u'vcpus': data['cpus'],
        u'disk': to_gb(data['root-size']),
        u'ephemeral': to_gb(data['ephemeral-size'])
    }

_SCHEMA = Schema((
    st.String('id'),
    st.String('name'),
    st.Int('cpus'),
    st.Int('ram'),
    st.Int('root-size'),
    st.Int('ephemeral-size')),

    required=('name', 'cpus', 'ram', 'root-size', 'ephemeral-size')
)

@instance_types.route('/', methods=('GET',))
@root_endpoint('instance-types')
def list_instance_types():
    parse_collection_request(_SCHEMA)
    all_flavors = g.client_set.compute.flavors.list()
    result = [_instance_type_from_nova(flavor)
              for flavor in all_flavors]
    return make_collection_response(u'instance-types', result)

@instance_types.route('/<instance_type_id>', methods=('GET',))
def get_instance_type(instance_type_id):
    # the following great code returns deleted flavors...
    #try:
    #    flavor = g.client_set.compute.flavors.get(instance_type_id)
    #except osc_exc.NotFound:
    #    abort(404)

    all_flavors = g.client_set.compute.flavors.list()
    for flavor in all_flavors:
        if flavor.id == instance_type_id:
            return make_json_response(_instance_type_from_nova(flavor))
    abort(404)

@instance_types.route('/', methods=('POST',))
def create_instance_type():
    data = parse_request_data(required=_SCHEMA.required)
    args = _instance_type_for_nova(data)
    args['flavorid'] = uuid.uuid4().int
    try:
        flavor = g.client_set.compute.flavors.create(**args)
    except osc_exc.HttpException, e:
        raise exc.InvalidRequest(str(e))
    set_audit_resource_id(flavor)
    return make_json_response(_instance_type_from_nova(flavor))

@instance_types.route('/<instance_type_id>', methods=('DELETE',))
def delete_instance_type(instance_type_id):
    set_audit_resource_id(instance_type_id)
    try:
        g.client_set.compute.flavors.delete(instance_type_id)
    except osc_exc.NotFound:
        abort(404)
    return make_json_response(None, 204)

<commit_msg>Access to instance types collection for non-administrators<commit_after>
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Altai API Service
# Copyright (C) 2012 Grid Dynamics Consulting Services, Inc
# All Rights Reserved
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.

import uuid

from flask import Blueprint, abort, url_for, g

from openstackclient_base import exceptions as osc_exc
from altai_api import exceptions as exc

from altai_api.auth import bound_client_set
from altai_api.utils import *
from altai_api.utils.misc import from_mb, from_gb, to_mb, to_gb

from altai_api.utils.decorators import root_endpoint, user_endpoint

from altai_api.schema import Schema
from altai_api.schema import types as st

instance_types = Blueprint('instance_types', __name__)

def _instance_type_from_nova(flavor):
    return {
        u'id': flavor.id,
        u'href': url_for('instance_types.get_instance_type',
                         instance_type_id=flavor.id),
        u'name': flavor.name,
        u'cpus': flavor.vcpus,
        u'ram': from_mb(flavor.ram),
        u'root-size': from_gb(flavor.disk),
        u'ephemeral-size': from_gb(flavor.ephemeral)
    }

def _instance_type_for_nova(data):
    return  {
        u'name': data['name'],
        u'ram': to_mb(data['ram']),
        u'vcpus': data['cpus'],
        u'disk': to_gb(data['root-size']),
        u'ephemeral': to_gb(data['ephemeral-size'])
    }

_SCHEMA = Schema((
    st.String('id'),
    st.String('name'),
    st.Int('cpus'),
    st.Int('ram'),
    st.Int('root-size'),
    st.Int('ephemeral-size')),

    required=('name', 'cpus', 'ram', 'root-size', 'ephemeral-size')
)

@instance_types.route('/', methods=('GET',))
@root_endpoint('instance-types')
@user_endpoint
def list_instance_types():
    parse_collection_request(_SCHEMA)
    all_flavors = bound_client_set().compute.flavors.list()
    result = [_instance_type_from_nova(flavor)
              for flavor in all_flavors]
    return make_collection_response(u'instance-types', result)

@instance_types.route('/<instance_type_id>', methods=('GET',))
@user_endpoint
def get_instance_type(instance_type_id):
    # the following great code returns deleted flavors...
    #try:
    #    flavor = g.client_set.compute.flavors.get(instance_type_id)
    #except osc_exc.NotFound:
    #    abort(404)

    all_flavors = bound_client_set().compute.flavors.list()
    for flavor in all_flavors:
        if flavor.id == instance_type_id:
            return make_json_response(_instance_type_from_nova(flavor))
    abort(404)

@instance_types.route('/', methods=('POST',))
def create_instance_type():
    data = parse_request_data(required=_SCHEMA.required)
    args = _instance_type_for_nova(data)
    args['flavorid'] = uuid.uuid4().int
    try:
        flavor = g.client_set.compute.flavors.create(**args)
    except osc_exc.HttpException, e:
        raise exc.InvalidRequest(str(e))
    set_audit_resource_id(flavor)
    return make_json_response(_instance_type_from_nova(flavor))

@instance_types.route('/<instance_type_id>', methods=('DELETE',))
def delete_instance_type(instance_type_id):
    set_audit_resource_id(instance_type_id)
    try:
        g.client_set.compute.flavors.delete(instance_type_id)
    except osc_exc.NotFound:
        abort(404)
    return make_json_response(None, 204)

<|endoftext|>