<commit_before># -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (C) 2015-2018 GEM Foundation
#
# OpenQuake 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.
#
# OpenQuake 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 OpenQuake. If not, see <http://www.gnu.org/licenses/>.
import logging

from openquake.baselib import parallel
from openquake.hazardlib.calc.hazard_curve import classical
from openquake.commonlib import source

from openquake.calculators import base
from openquake.calculators.classical import ClassicalCalculator, PSHACalculator
from openquake.calculators.ucerf_base import UcerfFilter
# FIXME: the counting of effective ruptures has to be revised

@base.calculators.add('ucerf_psha')
class UcerfPSHACalculator(PSHACalculator):
    """
    UCERF classical calculator.
    """
    def pre_execute(self):
        super().pre_execute()
        self.src_filter = UcerfFilter(
            self.sitecol, self.oqparam.maximum_distance)
        for sm in self.csm.source_models:  # one branch at the time
            [grp] = sm.src_groups
            ucerf = grp.sources[0].orig
            logging.info('Getting background sources from %s', ucerf.source_id)
            grp.sources.extend(
                ucerf.get_background_sources(self.src_filter))
            for src in grp:
                self.csm.infos[src.source_id] = source.SourceInfo(src)
                grp.tot_ruptures += src.num_ruptures

    def execute(self):
        """
        Run in parallel `core_task(sources, sitecol, monitor)`, by
        parallelizing on the sources according to their weight and
        tectonic region type.
        """
        monitor = self.monitor(self.core_task.__name__)
        oq = self.oqparam
        acc = self.zerodict()
        self.nsites = []  # used in agg_dicts
        param = dict(imtls=oq.imtls, truncation_level=oq.truncation_level,
                     filter_distance=oq.filter_distance)
        for sm in self.csm.source_models:  # one branch at the time
            [grp] = sm.src_groups
            gsims = self.csm.info.get_gsims(sm.ordinal)
            acc = parallel.Starmap.apply(
                classical, (grp, self.src_filter, gsims, param, monitor),
                concurrent_tasks=oq.concurrent_tasks * 2,  # for slow tasks
            ).reduce(self.agg_dicts, acc)

        with self.monitor('store source_info', autoflush=True):
            self.store_source_info(self.csm.infos, acc)
        return acc  # {grp_id: pmap}

@base.calculators.add('ucerf_classical')
class UCERFClassicalCalculator(ClassicalCalculator):
    pre_calculator = 'ucerf_psha'
<commit_msg>Fixed weight [skip hazardlib]<commit_after># -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (C) 2015-2018 GEM Foundation
#
# OpenQuake 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.
#
# OpenQuake 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 OpenQuake. If not, see <http://www.gnu.org/licenses/>.
import logging
import operator

from openquake.baselib import parallel
from openquake.hazardlib.calc.hazard_curve import classical
from openquake.commonlib import source

from openquake.calculators import base
from openquake.calculators.classical import ClassicalCalculator, PSHACalculator
from openquake.calculators.ucerf_base import UcerfFilter
# FIXME: the counting of effective ruptures has to be revised

@base.calculators.add('ucerf_psha')
class UcerfPSHACalculator(PSHACalculator):
    """
    UCERF classical calculator.
    """
    def pre_execute(self):
        super().pre_execute()
        self.src_filter = UcerfFilter(
            self.sitecol, self.oqparam.maximum_distance)
        for sm in self.csm.source_models:  # one branch at the time
            [grp] = sm.src_groups
            ucerf = grp.sources[0].orig
            logging.info('Getting background sources from %s', ucerf.source_id)
            grp.sources.extend(
                ucerf.get_background_sources(self.src_filter))
            for src in grp:
                self.csm.infos[src.source_id] = source.SourceInfo(src)
                grp.tot_ruptures += src.num_ruptures

    def execute(self):
        """
        Run in parallel `core_task(sources, sitecol, monitor)`, by
        parallelizing on the sources according to their weight and
        tectonic region type.
        """
        monitor = self.monitor(self.core_task.__name__)
        oq = self.oqparam
        acc = self.zerodict()
        self.nsites = []  # used in agg_dicts
        param = dict(imtls=oq.imtls, truncation_level=oq.truncation_level,
                     filter_distance=oq.filter_distance)
        for sm in self.csm.source_models:  # one branch at the time
            [grp] = sm.src_groups
            gsims = self.csm.info.get_gsims(sm.ordinal)
            acc = parallel.Starmap.apply(
                classical, (grp, self.src_filter, gsims, param, monitor),
                weight=operator.attrgetter('weight'),
                concurrent_tasks=oq.concurrent_tasks * 2,  # for slow tasks
            ).reduce(self.agg_dicts, acc)

        with self.monitor('store source_info', autoflush=True):
            self.store_source_info(self.csm.infos, acc)
        return acc  # {grp_id: pmap}

@base.calculators.add('ucerf_classical')
class UCERFClassicalCalculator(ClassicalCalculator):
    pre_calculator = 'ucerf_psha'
<|endoftext|>


<commit_before>import pygame

SCREEN_SIZE = (640, 480)

class Player(pygame.sprite.Sprite):
    SPEED = 200
    def __init__(self, *groups):
        super().__init__(*groups)
        self.image = pygame.image.load('frog.gif')
        self.rect = pygame.rect.Rect((320, 240), self.image.get_size())

    def update(self, dt, game):
        # last position
        last = self.rect.copy()

        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.rect.x -= self.SPEED * dt
        if key[pygame.K_RIGHT]:
            self.rect.x += self.SPEED * dt
        if key[pygame.K_UP]:
            self.rect.y -= self.SPEED * dt
        if key[pygame.K_DOWN]:
            self.rect.y += self.SPEED * dt

        for cell in pygame.sprite.spritecollide(self, game.walls, False):
            self.rect = last

class Game():
    def main(self, screen):
        clock = pygame.time.Clock()

        background = pygame.image.load('Castle.gif')
        background = pygame.transform.scale(background, SCREEN_SIZE)
        sprites = pygame.sprite.Group()
        self.player = Player(sprites)
        self.walls = pygame.sprite.Group()
        block = pygame.image.load('Chest.gif')
        for x in range(0, 640, 32):
            for y in range(0, 480, 32):
                if x in (0, 640-32) or y in (0, 480-32):
                    wall = pygame.sprite.Sprite(self.walls)
                    wall.image = block
                    wall.rect = pygame.rect.Rect((x,y), block.get_size())

        while True:
            dt = clock.tick(30) / 1000

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    return

            sprites.update(dt, self)
            screen.blit(background, (0,0))
            sprites.draw(screen)
            pygame.display.flip()

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)
    game = Game()
    game.main(screen)<commit_msg>Make left and right speeds go the same.<commit_after>import pygame

SCREEN_SIZE = (640, 480)

class Player(pygame.sprite.Sprite):
    SPEED = 200
    def __init__(self, *groups):
        super().__init__(*groups)
        self.image = pygame.image.load('frog.gif')
        self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
        self.resting = False
        self.dy = 0

    def update(self, dt, game):
        # last position
        last = self.rect.copy()

        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.rect.x -= self.SPEED * dt - 1
        if key[pygame.K_RIGHT]:
            self.rect.x += self.SPEED * dt
        if key[pygame.K_UP]:
            self.rect.y -= self.SPEED * dt - 1
        if key[pygame.K_DOWN]:
            self.rect.y += self.SPEED * dt

        new = self.rect
        for cell in pygame.sprite.spritecollide(self, game.walls, False):
            cell = cell.rect
            if last.right <= cell.left and new.right > cell.left:
                new.right = cell.left
            if last.left >= cell.right and new.left < cell.right:
                new.left = cell.right
            if last.bottom <= cell.top and new.bottom > cell.top:
                new.bottom = cell.top
            if last.top >= cell.bottom and new.top < cell.bottom:
                new.top = cell.bottom

class Game():
    def main(self, screen):
        clock = pygame.time.Clock()

        background = pygame.image.load('Castle.gif')
        background = pygame.transform.scale(background, SCREEN_SIZE)
        sprites = pygame.sprite.Group()
        self.player = Player(sprites)
        self.walls = pygame.sprite.Group()
        block = pygame.image.load('Chest.gif')
        for x in range(0, 640, 32):
            for y in range(0, 480, 32):
                if x in (0, 640-32) or y in (0, 480-32):
                    wall = pygame.sprite.Sprite(self.walls)
                    wall.image = block
                    wall.rect = pygame.rect.Rect((x,y), block.get_size())

        while True:
            dt = clock.tick(30) / 1000

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    return

            sprites.update(dt, self)
            screen.blit(background, (0,0))
            sprites.draw(screen)
            pygame.display.flip()

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)
    game = Game()
    game.main(screen)<|endoftext|>


<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

import six

import nixnet
from nixnet import constants
from nixnet.system import system
from nixnet import types

def main():
    with system.System() as my_system:
        database_alias = 'custom_database'
        database_filepath = os.path.join(os.path.dirname(__file__), 'databases\custom_database.dbc')
        default_baud_rate = 500000
        my_system.databases.add_alias(database_alias, database_filepath, default_baud_rate)

    database_name = 'custom_database'
    cluster_name = 'CAN_Cluster'
    output_frame = 'CANEventFrame1'
    interface = 'CAN1'

    with nixnet.FrameOutQueuedSession(
            interface,
            database_name,
            cluster_name,
            output_frame) as output_session:
        terminated_cable = six.moves.input('Are you using a terminated cable (Y or N)? ')
        if terminated_cable.lower() == "y":
            output_session.intf.can_term = constants.CanTerm.OFF
        elif terminated_cable.lower() == "n":
            output_session.intf.can_term = constants.CanTerm.ON
        else:
            print("Unrecognised input ({}), assuming 'n'".format(terminated_cable))
            output_session.intf.can_term = constants.CanTerm.ON

        user_value = six.moves.input('Enter payload [int, int]: ')
        try:
            payload_list = [int(x.strip()) for x in user_value.split(",")]
        except ValueError:
            payload_list = [2, 4, 8, 16]
            print('Unrecognized input ({}). Setting data buffer to {}'.format(user_value, payload_list))

        id = types.CanIdentifier(0)
        payload = bytearray(payload_list)
        frame = types.CanFrame(id, constants.FrameType.CAN_DATA, payload)

        print("Writing CAN frames using {} alias:".format(database_name))

        i = 0
        while i < 3:
            for index, byte in enumerate(payload):
                payload[index] = byte + i

            frame.payload = payload
            output_session.frames.write([frame])
            print('Sent frame with ID: {} payload: {}'.format(frame.identifier, list(frame.payload)))
            i += 1

    with system.System() as my_system:
        del my_system.databases[database_name]

if __name__ == '__main__':
    main()
<commit_msg>Replaced backslash to ensure that the example can run on Linux<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

import six

import nixnet
from nixnet import constants
from nixnet.system import system
from nixnet import types

def main():
    with system.System() as my_system:
        database_alias = 'custom_database'
        database_filepath = os.path.join(os.path.dirname(__file__), 'databases', 'custom_database.dbc')
        default_baud_rate = 500000
        my_system.databases.add_alias(database_alias, database_filepath, default_baud_rate)

    database_name = 'custom_database'
    cluster_name = 'CAN_Cluster'
    output_frame = 'CANEventFrame1'
    interface = 'CAN1'

    with nixnet.FrameOutQueuedSession(
            interface,
            database_name,
            cluster_name,
            output_frame) as output_session:
        terminated_cable = six.moves.input('Are you using a terminated cable (Y or N)? ')
        if terminated_cable.lower() == "y":
            output_session.intf.can_term = constants.CanTerm.OFF
        elif terminated_cable.lower() == "n":
            output_session.intf.can_term = constants.CanTerm.ON
        else:
            print("Unrecognised input ({}), assuming 'n'".format(terminated_cable))
            output_session.intf.can_term = constants.CanTerm.ON

        user_value = six.moves.input('Enter payload [int, int]: ')
        try:
            payload_list = [int(x.strip()) for x in user_value.split(",")]
        except ValueError:
            payload_list = [2, 4, 8, 16]
            print('Unrecognized input ({}). Setting data buffer to {}'.format(user_value, payload_list))

        id = types.CanIdentifier(0)
        payload = bytearray(payload_list)
        frame = types.CanFrame(id, constants.FrameType.CAN_DATA, payload)

        print("Writing CAN frames using {} alias:".format(database_name))

        i = 0
        while i < 3:
            for index, byte in enumerate(payload):
                payload[index] = byte + i

            frame.payload = payload
            output_session.frames.write([frame])
            print('Sent frame with ID: {} payload: {}'.format(frame.identifier, list(frame.payload)))
            i += 1

    with system.System() as my_system:
        del my_system.databases[database_name]

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


<commit_before><commit_msg>test updates<commit_after><|endoftext|>


<commit_before>"""
Django settings for studentsdb project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from django.conf import global_settings

#################################################################
#                                                               #
#  Rename file local_settings_template.py to local_settings.py  #
#         and enter your values to his settings fields          #
#                                                               #
#################################################################
#                                                               #

try:
    from local_settings import *
except ImportError:
    pass

#                                                               #
#################################################################

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# DEBUG = False

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []
# ALLOWED_HOSTS = [PORTAL_DOMAIN]

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
    'students',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    "django.core.context_processors.request",
    "studentsdb.context_processors.students_proc",
    "students.context_processors.groups_processor",
)

ROOT_URLCONF = 'studentsdb.urls'

WSGI_APPLICATION = 'studentsdb.wsgi.application'

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'uk'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media')

# Crispy forms settings

CRISPY_TEMPLATE_PACK = 'bootstrap3'

# Logging

LOG_FILE = os.path.join(BASE_DIR, 'studentsdb.log')

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s: %(message)s'
        },
        'simple': {
            'format': '%(levelname)s: %(message)s'
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
            'formatter': 'simple'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': LOG_FILE,
            'formatter': 'verbose'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['null'],
            'propagate': True,
            'level': 'INFO'
        },
        'django.request': {
            'handlers': ['file'
                # , 'mail_admins'
                         ],
            'level': 'ERROR',
            'propagate': False,
        },
        'students.signals': {
            'handlers': ['console', 'file'
                # , 'mail_admins'
                         ],
            'level': 'INFO'
        },
        'students.views.contact_admin': {
            'handlers': ['console', 'file'
                # , 'mail_admins'
                         ],
            'level': 'INFO'
        }
    }
}
<commit_msg>LogEntry<commit_after>"""
Django settings for studentsdb project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from django.conf import global_settings

#################################################################
#                                                               #
#  Rename file local_settings_template.py to local_settings.py  #
#         and enter your values to his settings fields          #
#                                                               #
#################################################################
#                                                               #

try:
    from local_settings import *
except ImportError:
    pass

#                                                               #
#################################################################

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# DEBUG = False

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []
# ALLOWED_HOSTS = [PORTAL_DOMAIN]

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
    'students',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    "django.core.context_processors.request",
    "studentsdb.context_processors.students_proc",
    "students.context_processors.groups_processor",
)

ROOT_URLCONF = 'studentsdb.urls'

WSGI_APPLICATION = 'studentsdb.wsgi.application'

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'uk'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media')

# Crispy forms settings

CRISPY_TEMPLATE_PACK = 'bootstrap3'

# Logging

LOG_FILE = os.path.join(BASE_DIR, 'studentsdb.log')

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s: %(message)s'
        },
        'simple': {
            'format': '%(levelname)s: %(message)s'
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
            'formatter': 'simple'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': LOG_FILE,
            'formatter': 'verbose'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['null'],
            'propagate': True,
            'level': 'INFO'
        },
        'django.request': {
            'handlers': ['file', 'mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'students.signals': {
            'handlers': ['console', 'file', 'mail_admins'],
            'level': 'INFO'
        },
        'students.views.contact_admin': {
            'handlers': ['console', 'file', 'mail_admins'],
            'level': 'INFO'
        }
    }
}
<|endoftext|>


<commit_before>import logging

from pylons import request, tmpl_context as c, response
from pylons.controllers.util import abort, redirect
from pylons.i18n import _

from openspending import model
from openspending.ui.lib.base import BaseController, render
from openspending.ui.lib.page import Page
from openspending.ui.lib.views import handle_request
from openspending.ui.lib.helpers import url_for
from openspending.ui.lib.cache import AggregationCache
from openspending.lib.csvexport import write_csv
from openspending.lib.jsonexport import write_json, to_jsonp

log = logging.getLogger(__name__)

PAGE_SIZE = 100

class DimensionController(BaseController):

    def _get_member(self, dataset, dimension_name, name):
        self._get_dataset(dataset)
        c.dimension = dimension_name
        for dimension in c.dataset.compounds:
            if dimension.name == dimension_name:
                cond = dimension.alias.c.name==name
                members = list(dimension.members(cond, limit=1))
                if not len(members):
                    abort(404, _('Sorry, there is no member named %r')
                            % name)
                c.dimension = dimension
                c.member = members.pop()
                c.num_entries = dimension.num_entries(cond)
                return
        abort(404, _('Sorry, there is no dimension named %r') % dimension_name)

    def index(self, dataset, format='html'):
        self._get_dataset(dataset)
        if format == 'json':
            return to_jsonp([d.as_dict() for d in c.dataset.dimensions])
        else:
            return render('dimension/index.html')

    def view(self, dataset, dimension, format='html'):
        self._get_dataset(dataset)
        try:
            c.dimension = c.dataset[dimension]
        except KeyError:
            abort(404, _('This is not a dimension'))
        if not isinstance(c.dimension, model.Dimension):
            abort(404, _('This is not a dimension'))

        page = self._get_page('page')
        cache = AggregationCache(c.dataset)
        result = cache.aggregate(drilldowns=[dimension], page=page,
                                 pagesize=PAGE_SIZE)
        items = result.get('drilldown', [])
        c.values = [(d.get(dimension), d.get('amount')) for d in items]

        if format == 'json':
            return to_jsonp({"values": c.values, "meta": c.dimension.as_dict()})

        c.page = Page(c.values, page=page,
                      item_count=result['summary']['num_drilldowns'],
                      items_per_page=PAGE_SIZE,
                      presliced_list=True)
        return render('dimension/view.html')

    def member(self, dataset, dimension, name, format="html"):
        self._get_member(dataset, dimension, name)

        handle_request(request, c, c.member, c.dimension.name)

        # If there are no views set up, then go direct to the entries search page
        if c.view is None and format is "html":
            return redirect(url_for(controller='dimension', action='entries'))

        if format == 'json':
            return write_json([c.member], response)
        elif format == 'csv':
            return write_csv([c.member], response)
        else:
            return render('dimension/member.html')

    def entries(self, dataset, dimension, name, format='html'):
        self._get_member(dataset, dimension, name)

        handle_request(request, c, c.member, c.dimension.name)

        entries = c.dataset.entries(c.dimension.alias.c.name == c.member['name'])
        attachment_name = '__'.join([dataset, dimension, name])

        if format == 'json':
            return write_json(entries, response, filename=attachment_name + '.json')
        elif format == 'csv':
            return write_csv(entries, response, filename=attachment_name + '.csv')
        else:
            return render('dimension/entries.html')
<commit_msg>Fix url building in dimension member view. <commit_after>import logging

from pylons import request, tmpl_context as c, response
from pylons.controllers.util import abort, redirect
from pylons.i18n import _

from openspending import model
from openspending.ui.lib.base import BaseController, render
from openspending.ui.lib.page import Page
from openspending.ui.lib.views import handle_request
from openspending.ui.lib.helpers import url_for
from openspending.ui.lib.cache import AggregationCache
from openspending.lib.csvexport import write_csv
from openspending.lib.jsonexport import write_json, to_jsonp

log = logging.getLogger(__name__)

PAGE_SIZE = 100

class DimensionController(BaseController):

    def _get_member(self, dataset, dimension_name, name):
        self._get_dataset(dataset)
        c.dimension = dimension_name
        for dimension in c.dataset.compounds:
            if dimension.name == dimension_name:
                cond = dimension.alias.c.name==name
                members = list(dimension.members(cond, limit=1))
                if not len(members):
                    abort(404, _('Sorry, there is no member named %r')
                            % name)
                c.dimension = dimension
                c.member = members.pop()
                c.num_entries = dimension.num_entries(cond)
                return
        abort(404, _('Sorry, there is no dimension named %r') % dimension_name)

    def index(self, dataset, format='html'):
        self._get_dataset(dataset)
        if format == 'json':
            return to_jsonp([d.as_dict() for d in c.dataset.dimensions])
        else:
            return render('dimension/index.html')

    def view(self, dataset, dimension, format='html'):
        self._get_dataset(dataset)
        try:
            c.dimension = c.dataset[dimension]
        except KeyError:
            abort(404, _('This is not a dimension'))
        if not isinstance(c.dimension, model.Dimension):
            abort(404, _('This is not a dimension'))

        page = self._get_page('page')
        cache = AggregationCache(c.dataset)
        result = cache.aggregate(drilldowns=[dimension], page=page,
                                 pagesize=PAGE_SIZE)
        items = result.get('drilldown', [])
        c.values = [(d.get(dimension), d.get('amount')) for d in items]

        if format == 'json':
            return to_jsonp({"values": c.values, "meta": c.dimension.as_dict()})

        c.page = Page(c.values, page=page,
                      item_count=result['summary']['num_drilldowns'],
                      items_per_page=PAGE_SIZE,
                      presliced_list=True)
        return render('dimension/view.html')

    def member(self, dataset, dimension, name, format="html"):
        self._get_member(dataset, dimension, name)

        handle_request(request, c, c.member, c.dimension.name)

        # If there are no views set up, then go direct to the entries search page
        if c.view is None and format is "html":
            return redirect(url_for(controller='dimension', action='entries',
                dataset=c.dataset.name, dimension=dimension, name=name))

        if format == 'json':
            return write_json([c.member], response)
        elif format == 'csv':
            return write_csv([c.member], response)
        else:
            return render('dimension/member.html')

    def entries(self, dataset, dimension, name, format='html'):
        self._get_member(dataset, dimension, name)

        handle_request(request, c, c.member, c.dimension.name)

        entries = c.dataset.entries(c.dimension.alias.c.name == c.member['name'])
        attachment_name = '__'.join([dataset, dimension, name])

        if format == 'json':
            return write_json(entries, response, filename=attachment_name + '.json')
        elif format == 'csv':
            return write_csv(entries, response, filename=attachment_name + '.csv')
        else:
            return render('dimension/entries.html')
<|endoftext|>


<commit_before>import os

import yaml

from . import utils
from .structure import Structure

class Schema(object):

    def __init__(self, name=None, entity_type='Project', config_name=None):

        name = name or 'v1'
        root = os.path.join(
            os.path.dirname(os.path.dirname(__file__)),
            'schemas',
            name,
        )
        if not os.path.exists(root):
            raise ValueError('schema %r does not exist' % name)

        self.root = root
        self.entity_type = entity_type
        self.config_name = config_name or entity_type + '.yml'
        self.config = yaml.load(open(self._join_path(self.config_name)).read())

        # Set some defaults on the config.
        self.config.setdefault('type', 'entity')
        default_template = self._join_path(os.path.splitext(self.config_name)[0])
        if os.path.exists(default_template):
            self.config.setdefault('template', default_template)

        # Load all the children.
        self.children = {}
        for child_type, child_config_name in self.config.get('children', {}).iteritems():
            self.children[child_type] = Schema(root, child_type, child_config_name)

    def _join_path(self, *args):
        return os.path.join(self.root, *args)

    def __repr__(self):
        return '<Schema %s:%s at 0x%x>' % (os.path.basename(self.root), self.entity_type, id(self))

    def pprint(self, depth=0):
        print '%s%s "%s"' % (
            '\t' * depth,
            self.entity_type,
            self.config_name,
        ),
        if not self.children:
            print
            return

        print '{'
        for type_, child in sorted(self.children.iteritems()):
            child.pprint(depth + 1)
        print '\t' * depth + '}'

    def structure(self, context):

        # Make sure that this schema matches the context we have been asked to
        # create a structure for.
        if self.entity_type != context.entity['type']:
            raise ValueError('context entity type does not match; %r != %r' % (
                self.entity_type, context.entity['type']
            ))

        # Create the structure node for this entity.
        structure = Structure.from_context(context, self.config.copy())
        if not structure:
            return

        # Create all of the Context's child nodes as well.
        for child_context in context.children:
            child_type = child_context.entity['type']
            if child_type in self.children:
                child_structure = self.children[child_type].structure(child_context)
                if child_structure:
                    structure.children.append(child_structure)

        return structure

<commit_msg>Abspath the schema name<commit_after>import os

import yaml

from . import utils
from .structure import Structure

class Schema(object):

    def __init__(self, name=None, entity_type='Project', config_name=None):

        name = name or 'v1'
        root = os.path.abspath(os.path.join(
            __file__,
            os.pardir,
            os.pardir,
            'schemas',
            name,
        ))
        if not os.path.exists(root):
            raise ValueError('schema %r does not exist' % name)

        self.root = root
        self.entity_type = entity_type
        self.config_name = config_name or entity_type + '.yml'
        self.config = yaml.load(open(self._join_path(self.config_name)).read())

        # Set some defaults on the config.
        self.config.setdefault('type', 'entity')
        default_template = self._join_path(os.path.splitext(self.config_name)[0])
        if os.path.exists(default_template):
            self.config.setdefault('template', default_template)

        # Load all the children.
        self.children = {}
        for child_type, child_config_name in self.config.get('children', {}).iteritems():
            self.children[child_type] = Schema(root, child_type, child_config_name)

    def _join_path(self, *args):
        return os.path.join(self.root, *args)

    def __repr__(self):
        return '<Schema %s:%s at 0x%x>' % (os.path.basename(self.root), self.entity_type, id(self))

    def pprint(self, depth=0):
        print '%s%s "%s"' % (
            '\t' * depth,
            self.entity_type,
            self.config_name,
        ),
        if not self.children:
            print
            return

        print '{'
        for type_, child in sorted(self.children.iteritems()):
            child.pprint(depth + 1)
        print '\t' * depth + '}'

    def structure(self, context):

        # Make sure that this schema matches the context we have been asked to
        # create a structure for.
        if self.entity_type != context.entity['type']:
            raise ValueError('context entity type does not match; %r != %r' % (
                self.entity_type, context.entity['type']
            ))

        # Create the structure node for this entity.
        structure = Structure.from_context(context, self.config.copy())
        if not structure:
            return

        # Create all of the Context's child nodes as well.
        for child_context in context.children:
            child_type = child_context.entity['type']
            if child_type in self.children:
                child_structure = self.children[child_type].structure(child_context)
                if child_structure:
                    structure.children.append(child_structure)

        return structure

<|endoftext|>


<commit_before>from django.conf.urls import include, url
from buckets.test import views

urlpatterns = [
    url(r'^', include('buckets.urls')),
    url(r'^media/s3/uploads$', views.fake_s3_upload, name='fake_s3_upload'),
]
<commit_msg>Add trailing slash to fake_s3_upload url<commit_after>from django.conf.urls import include, url
from buckets.test import views

urlpatterns = [
    url(r'^', include('buckets.urls')),
    url(r'^media/s3/uploads/$', views.fake_s3_upload, name='fake_s3_upload'),
]
<|endoftext|>


<commit_before>'''
Created on Mar 19, 2017

@author: Daniel
'''
import unittest
from bibleutils.versification import convert_refs, expand_refs, parse_refs, \
                                     ReferenceFormID
from puzzles.core.etcbc import Corpus, get_words
from puzzles.wordsearch.wordsearch import WordSearch

class Test(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testGreekWordsearch(self):
        refs = expand_refs(convert_refs(parse_refs('Luke 1:2,5', form='ETCBCG'),
                           ReferenceFormID.ETCBCG))

        # Get the list of words from TF
        sections = []
        [sections.append((r.st_book, r.st_ch, r.st_vs)) for r in refs ]
        word_list = get_words(*sections, work=Corpus.GREEK)

        ws = WordSearch(set(word_list), 10, 10, WordSearch.LTR)
        ws.dump('html')

        # Verify results
        # Due to randomization of direction in the layout it is impossible to
        # simply verify the layout. For now check only the number of words and
        # approximate grid size
        self.assertEqual(len(ws._words), 37, 'Incorrect number of words')
        self.assertGreater(ws._right, 10, 'right extent too small')
        self.assertLess(ws._right, 16, 'right extent too large')
        self.assertGreater(ws._bottom, 12, 'bottom extent too small')
        self.assertLess(ws._bottom, 16, 'bottom extent too large')

    def testHebrewWordsearch(self):
        refs = expand_refs(convert_refs(parse_refs('Genesis 1:5', form='ETCBCH'),
                           ReferenceFormID.ETCBCH))

        # Get the list of words from TF
        sections = []
        [sections.append((r.st_book, r.st_ch, r.st_vs)) for r in refs ]
        word_list = get_words(*sections, work=Corpus.HEBREW)

        ws = WordSearch(set(word_list), 10, 10, WordSearch.RTL)
        ws.dump('html')

        self.assertEqual(len(ws._words), 18, 'Incorrect number of words')
        self.assertGreater(ws._left, -10, 'left extent too small')
        self.assertLess(ws._left, -5, 'left extent too large')
        self.assertGreater(ws._bottom, 5, 'bottom extent too small')
        self.assertLess(ws._bottom, 9, 'bottom extent too large')

    def testHebrewWordsearchMultiChapter(self):
        refs = expand_refs(convert_refs(parse_refs('Genesis 1:5, Exodus 1:2',
                                                   form='ETCBCH'),
                           ReferenceFormID.ETCBCH))

        # Get the list of words from TF
        sections = []
        [sections.append((r.st_book, r.st_ch, r.st_vs)) for r in refs ]
        word_list = get_words(*sections, work=Corpus.HEBREW)

        ws = WordSearch(set(word_list), 10, 10, WordSearch.RTL)
        ws.dump('html')

        self.assertEqual(len(ws._words), 18, 'Incorrect number of words')
        self.assertGreater(ws._left, -10, 'left extent too small')
        self.assertLess(ws._left, -5, 'left extent too large')
        self.assertGreater(ws._bottom, 5, 'bottom extent too small')
        self.assertLess(ws._bottom, 9, 'bottom extent too large')

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()<commit_msg>fix expected res on the multi-ws test<commit_after>'''
Created on Mar 19, 2017

@author: Daniel
'''
import unittest
from bibleutils.versification import convert_refs, expand_refs, parse_refs, \
                                     ReferenceFormID
from puzzles.core.etcbc import Corpus, get_words
from puzzles.wordsearch.wordsearch import WordSearch

class Test(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testGreekWordsearch(self):
        refs = expand_refs(convert_refs(parse_refs('Luke 1:2,5', form='ETCBCG'),
                           ReferenceFormID.ETCBCG))

        # Get the list of words from TF
        sections = []
        [sections.append((r.st_book, r.st_ch, r.st_vs)) for r in refs ]
        word_list = get_words(*sections, work=Corpus.GREEK)

        ws = WordSearch(set(word_list), 10, 10, WordSearch.LTR)
        ws.dump('html')

        # Verify results
        # Due to randomization of direction in the layout it is impossible to
        # simply verify the layout. For now check only the number of words and
        # approximate grid size
        self.assertEqual(len(ws._words), 37, 'Incorrect number of words')
        self.assertGreater(ws._right, 10, 'right extent too small')
        self.assertLess(ws._right, 16, 'right extent too large')
        self.assertGreater(ws._bottom, 12, 'bottom extent too small')
        self.assertLess(ws._bottom, 16, 'bottom extent too large')

    def testHebrewWordsearch(self):
        refs = expand_refs(convert_refs(parse_refs('Genesis 1:5', form='ETCBCH'),
                           ReferenceFormID.ETCBCH))

        # Get the list of words from TF
        sections = []
        [sections.append((r.st_book, r.st_ch, r.st_vs)) for r in refs ]
        word_list = get_words(*sections, work=Corpus.HEBREW)

        ws = WordSearch(set(word_list), 10, 10, WordSearch.RTL)
        ws.dump('html')

        self.assertEqual(len(ws._words), 18, 'Incorrect number of words')
        self.assertGreater(ws._left, -10, 'left extent too small')
        self.assertLess(ws._left, -5, 'left extent too large')
        self.assertGreater(ws._bottom, 5, 'bottom extent too small')
        self.assertLess(ws._bottom, 9, 'bottom extent too large')

    def testHebrewWordsearchMultiChapter(self):
        refs = expand_refs(convert_refs(parse_refs('Genesis 1:5, Exodus 1:2',
                                                   form='ETCBCH'),
                           ReferenceFormID.ETCBCH))

        # Get the list of words from TF
        sections = []
        [sections.append((r.st_book, r.st_ch, r.st_vs)) for r in refs ]
        word_list = get_words(*sections, work=Corpus.HEBREW)

        ws = WordSearch(set(word_list), 10, 10, WordSearch.RTL)
        ws.dump('html')

        self.assertEqual(len(ws._words), 23, 'Incorrect number of words')
        self.assertGreater(ws._left, -10, 'left extent too small')
        self.assertLess(ws._left, -5, 'left extent too large')
        self.assertGreater(ws._bottom, 5, 'bottom extent too small')
        self.assertLess(ws._bottom, 10, 'bottom extent too large')

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()<|endoftext|>


<commit_before># Copyright 2008-2009 WebDriver committers
# Copyright 2008-2009 Google Inc.
#
# 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.

"""WebElement implementation."""
from command import Command
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchAttributeException

class WebElement(object):
    """Represents an HTML element.

    Generally, all interesting operations to do with interacting with a page
    will be performed through this interface."""
    def __init__(self, parent, id_):
        self._parent = parent
        self._id = id_

    @property
    def tag_name(self):
        """Gets this element's tagName property."""
        return self.execute(Command.GET_ELEMENT_TAG_NAME)['value']

    @property
    def text(self):
        """Gets the text of the element."""
        return self.execute(Command.GET_ELEMENT_TEXT)['value']

    def click(self):
        """Clicks the element."""
        self.execute(Command.CLICK_ELEMENT)

    def submit(self):
        """Submits a form."""
        self.execute(Command.SUBMIT_ELEMENT)

    @property
    def value(self):
        """Gets the value of the element's value attribute."""
        return self.execute(Command.GET_ELEMENT_VALUE)['value']

    def clear(self):
        """Clears the text if it's a text entry element."""
        self.execute(Command.CLEAR_ELEMENT)

    def get_attribute(self, name):
        """Gets the attribute value."""
        resp = self.execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name})
        attributeValue = ''
        if resp['value'] is None:
            attributeValue = None
        else:
            attributeValue = str(resp['value'])
            if type(resp['value']) is bool:
                attributeValue = attributeValue.lower()

        return attributeValue

    def toggle(self):
        """Toggles the element state."""
        resp = self.execute(Command.TOGGLE_ELEMENT)
        return resp['value']

    def is_selected(self):
        """Whether the element is selected."""
        return self.execute(Command.IS_ELEMENT_SELECTED)['value']

    def select(self):
        """Selects an element."""
        self.execute(Command.SET_ELEMENT_SELECTED)

    def is_enabled(self):
        """Whether the element is enabled."""
        return self.execute(Command.IS_ELEMENT_ENABLED)['value']

    def find_element_by_id(self, id_):
        """Finds element by id."""
        return self.find_element(by=By.ID, value=id_)

    def find_elements_by_id(self, id_):
        return self.find_elements(by=By.ID, value=id_)

    def find_element_by_name(self, name):
        """Find element by name."""
        return self.find_element(by=By.NAME, value=name)

    def find_elements_by_name(self, name):
        return self.find_elements(by=By.NAME, value=name)

    def find_element_by_link_text(self, link_text):
        """Finds element by link text."""
        return self.find_element(by=By.LINK_TEXT, value=link_text)

    def find_elements_by_link_text(self, link_text):
        return self.find_elements(by=By.LINK_TEXT, value=link_text)

    def find_element_by_partial_link_text(self, link_text):
        return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)

    def find_elements_by_partial_link_text(self, link_text):
        return self.find_elements(by=By.PARTIAL_LINK_TEXT, value=link_text)

    def find_element_by_tag_name(self, name):
        return self.find_element(by=By.TAG_NAME, value=name)

    def find_elements_by_tag_name(self, name):
        return self.find_elements(by=By.TAG_NAME, value=name)

    def find_element_by_xpath(self, xpath):
        """Finds element by xpath."""
        return self.find_element(by=By.XPATH, value=xpath)

    def find_elements_by_xpath(self, xpath):
        """Finds elements within the elements by xpath."""
        return self.find_elements(by=By.XPATH, value=xpath)

    def find_element_by_class_name(self, name):
        """Finds an element by their class name."""
        return self.find_element(by=By.CLASS_NAME, value=name)

    def find_elements_by_class_name(self, name):
        """Finds elements by their class name."""
        return self.find_elements(by=By.CLASS_NAME, value=name)

    def find_element_by_css_selector(self, css_selector):
        """Find and return an element by CSS selector."""
        return self.find_element(by=By.CSS_SELECTOR, value=css_selector)

    def find_elements_by_css_selector(self, css_selector):
        """Find and return list of multiple elements by CSS selector."""
        return self.find_elements(by=By.CSS_SELECTOR, value=css_selector)

    def send_keys(self, *value):
        """Simulates typing into the element."""
        self.execute(Command.SEND_KEYS_TO_ELEMENT, {'value': value})

    # RenderedWebElement Items
    def is_displayed(self):
        """Whether the element would be visible to a user"""
        return self.execute(Command.IS_ELEMENT_DISPLAYED)['value']

    @property
    def size(self):
        """ Returns the size of the element """
        return self.execute(Command.GET_ELEMENT_SIZE)['value']

    def value_of_css_property(self, property_name):
        """ Returns the value of a CSS property """
        return self.execute(Command.GET_ELEMENT_VALUE_OF_CSS_PROPERTY,
                        {'propertyName': property_name})['value']

    @property
    def parent(self):
        return self._parent

    @property
    def id(self):
        return self._id

    # Private Methods
    def execute(self, command, params=None):
        """Executes a command against the underlying HTML element.

        Args:
          command: The name of the command to execute as a string.
          params: A dictionary of named parameters to send with the command.

        Returns:
          The command's JSON response loaded into a dictionary object.
        """
        if not params:
            params = {}
        params['id'] = self._id
        return self._parent.execute(command, params)

    def find_element(self, by=By.ID, value=None):
        return self.execute(Command.FIND_CHILD_ELEMENT,
                             {"using": by, "value": value})['value']

    def find_elements(self, by=By.ID, value=None):
        return self.execute(Command.FIND_CHILD_ELEMENTS,
                             {"using": by, "value": value})['value']
<commit_msg>DavidBurns making execute a "private" method so users dont try use it<commit_after># Copyright 2008-2009 WebDriver committers
# Copyright 2008-2009 Google Inc.
#
# 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.

"""WebElement implementation."""
from command import Command
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchAttributeException

class WebElement(object):
    """Represents an HTML element.

    Generally, all interesting operations to do with interacting with a page
    will be performed through this interface."""
    def __init__(self, parent, id_):
        self._parent = parent
        self._id = id_

    @property
    def tag_name(self):
        """Gets this element's tagName property."""
        return self._execute(Command.GET_ELEMENT_TAG_NAME)['value']

    @property
    def text(self):
        """Gets the text of the element."""
        return self._execute(Command.GET_ELEMENT_TEXT)['value']

    def click(self):
        """Clicks the element."""
        self._execute(Command.CLICK_ELEMENT)

    def submit(self):
        """Submits a form."""
        self._execute(Command.SUBMIT_ELEMENT)

    @property
    def value(self):
        """Gets the value of the element's value attribute."""
        return self._execute(Command.GET_ELEMENT_VALUE)['value']

    def clear(self):
        """Clears the text if it's a text entry element."""
        self._execute(Command.CLEAR_ELEMENT)

    def get_attribute(self, name):
        """Gets the attribute value."""
        resp = self._execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name})
        attributeValue = ''
        if resp['value'] is None:
            attributeValue = None
        else:
            attributeValue = str(resp['value'])
            if type(resp['value']) is bool:
                attributeValue = attributeValue.lower()

        return attributeValue

    def toggle(self):
        """Toggles the element state."""
        resp = self._execute(Command.TOGGLE_ELEMENT)
        return resp['value']

    def is_selected(self):
        """Whether the element is selected."""
        return self._execute(Command.IS_ELEMENT_SELECTED)['value']

    def select(self):
        """Selects an element."""
        self._execute(Command.SET_ELEMENT_SELECTED)

    def is_enabled(self):
        """Whether the element is enabled."""
        return self._execute(Command.IS_ELEMENT_ENABLED)['value']

    def find_element_by_id(self, id_):
        """Finds element by id."""
        return self.find_element(by=By.ID, value=id_)

    def find_elements_by_id(self, id_):
        return self.find_elements(by=By.ID, value=id_)

    def find_element_by_name(self, name):
        """Find element by name."""
        return self.find_element(by=By.NAME, value=name)

    def find_elements_by_name(self, name):
        return self.find_elements(by=By.NAME, value=name)

    def find_element_by_link_text(self, link_text):
        """Finds element by link text."""
        return self.find_element(by=By.LINK_TEXT, value=link_text)

    def find_elements_by_link_text(self, link_text):
        return self.find_elements(by=By.LINK_TEXT, value=link_text)

    def find_element_by_partial_link_text(self, link_text):
        return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)

    def find_elements_by_partial_link_text(self, link_text):
        return self.find_elements(by=By.PARTIAL_LINK_TEXT, value=link_text)

    def find_element_by_tag_name(self, name):
        return self.find_element(by=By.TAG_NAME, value=name)

    def find_elements_by_tag_name(self, name):
        return self.find_elements(by=By.TAG_NAME, value=name)

    def find_element_by_xpath(self, xpath):
        """Finds element by xpath."""
        return self.find_element(by=By.XPATH, value=xpath)

    def find_elements_by_xpath(self, xpath):
        """Finds elements within the elements by xpath."""
        return self.find_elements(by=By.XPATH, value=xpath)

    def find_element_by_class_name(self, name):
        """Finds an element by their class name."""
        return self.find_element(by=By.CLASS_NAME, value=name)

    def find_elements_by_class_name(self, name):
        """Finds elements by their class name."""
        return self.find_elements(by=By.CLASS_NAME, value=name)

    def find_element_by_css_selector(self, css_selector):
        """Find and return an element by CSS selector."""
        return self.find_element(by=By.CSS_SELECTOR, value=css_selector)

    def find_elements_by_css_selector(self, css_selector):
        """Find and return list of multiple elements by CSS selector."""
        return self.find_elements(by=By.CSS_SELECTOR, value=css_selector)

    def send_keys(self, *value):
        """Simulates typing into the element."""
        self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': value})

    # RenderedWebElement Items
    def is_displayed(self):
        """Whether the element would be visible to a user"""
        return self._execute(Command.IS_ELEMENT_DISPLAYED)['value']

    @property
    def size(self):
        """ Returns the size of the element """
        return self._execute(Command.GET_ELEMENT_SIZE)['value']

    def value_of_css_property(self, property_name):
        """ Returns the value of a CSS property """
        return self._execute(Command.GET_ELEMENT_VALUE_OF_CSS_PROPERTY,
                        {'propertyName': property_name})['value']

    @property
    def parent(self):
        return self._parent

    @property
    def id(self):
        return self._id

    # Private Methods
    def _execute(self, command, params=None):
        """Executes a command against the underlying HTML element.

        Args:
          command: The name of the command to _execute as a string.
          params: A dictionary of named parameters to send with the command.

        Returns:
          The command's JSON response loaded into a dictionary object.
        """
        if not params:
            params = {}
        params['id'] = self._id
        return self._parent.execute(command, params)

    def find_element(self, by=By.ID, value=None):
        return self._execute(Command.FIND_CHILD_ELEMENT,
                             {"using": by, "value": value})['value']

    def find_elements(self, by=By.ID, value=None):
        return self._execute(Command.FIND_CHILD_ELEMENTS,
                             {"using": by, "value": value})['value']
<|endoftext|>


<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import time

from configman.config_manager import RequiredConfig
from configman import Namespace

#------------------------------------------------------------------------------
def string_to_list_of_ints(a_string):
    a_string = a_string.replace('"', '')
    a_string = a_string.replace("'", "")
    ints_as_strings = a_string.split(',')
    return [int(x) for x in ints_as_strings]

#==============================================================================
class TransactionExecutor(RequiredConfig):
    required_config = Namespace()

    #--------------------------------------------------------------------------
    def __init__(self, config, db_conn_context_source,
                 quit_check_callback=None):
        self.config = config
        self.db_conn_context_source = db_conn_context_source
        self.quit_check = quit_check_callback or (lambda: False)
        self.do_quit_check = True

    #--------------------------------------------------------------------------
    def __call__(self, function, *args, **kwargs):
        """execute a function within the context of a transaction"""
        if self.do_quit_check:
            self.quit_check()
        with self.db_conn_context_source() as connection:
            try:
                #self.config.logger.debug('starting transaction')
                result = function(connection, *args, **kwargs)
                connection.commit()
                return result
            except:
                connection.rollback()
                self.config.logger.error(
                  'Exception raised during transaction',
                  exc_info=True)
                self.db_conn_context_source.force_reconnect()
                raise

#==============================================================================
class TransactionExecutorWithInfiniteBackoff(TransactionExecutor):
    # back off times
    required_config = Namespace()
    required_config.add_option('backoff_delays',
                               default="10, 30, 60, 120, 300",
                               doc='delays in seconds between retries',
                               from_string_converter=string_to_list_of_ints)
    # wait_log_interval
    required_config.add_option('wait_log_interval',
                               default=10,
                               doc='seconds between log during retries')

    #--------------------------------------------------------------------------
    def backoff_generator(self):
        """Generate a series of integers used for the length of the sleep
        between retries.  It produces after exhausting the list, it repeats
        the last value from the list forever.  This generator will never raise
        the StopIteration exception."""
        for x in self.config.backoff_delays:
            yield x
        while True:
            yield self.config.backoff_delays[-1]

    #--------------------------------------------------------------------------
    def responsive_sleep(self, seconds, wait_reason=''):
        """Sleep for the specified number of seconds, logging every
        'wait_log_interval' seconds with progress info."""
        for x in xrange(int(seconds)):
            if (self.config.wait_log_interval and
                not x % self.config.wait_log_interval):
                self.config.logger.debug(
                  '%s: %dsec of %dsec' % (wait_reason, x, seconds))
            self.quit_check()
            time.sleep(1.0)

    #--------------------------------------------------------------------------
    def __call__(self, function, *args, **kwargs):
        """execute a function within the context of a transaction"""
        for wait_in_seconds in self.backoff_generator():
            try:
                if self.do_quit_check:
                    self.quit_check()
                # self.db_conn_context_source is an instance of a
                # wrapper class on the actual connection driver
                with self.db_conn_context_source() as connection:
                    try:
                        result = function(connection, *args, **kwargs)
                        connection.commit()
                        return result
                    except:
                        connection.rollback()
                        raise
            except self.db_conn_context_source.conditional_exceptions, x:
                # these exceptions may or may not be retriable
                # the test is for is a last ditch effort to see if
                # we can retry
                if not self.db_conn_context_source.is_operational_exception(x):
                    self.config.logger.critical(
                      'Unrecoverable transaction error',
                       exc_info=True
                    )
                    raise
                self.config.logger.critical(
                  'transaction error eligible for retry',
                  exc_info=True)

            except self.db_conn_context_source.operational_exceptions:
                self.config.logger.critical(
                  'transaction error eligible for retry',
                  exc_info=True)
            self.db_conn_context_source.force_reconnect()
            self.config.logger.debug(
              'retry in %s seconds' % wait_in_seconds
            )
            self.responsive_sleep(
              wait_in_seconds,
              'waiting for retry after failure in transaction'
            )
        raise

#==============================================================================
class TransactionExecutorWithLimitedBackoff(
                                       TransactionExecutorWithInfiniteBackoff):
    #--------------------------------------------------------------------------
    def backoff_generator(self):
        """Generate a series of integers used for the length of the sleep
        between retries."""
        for x in self.config.backoff_delays:
            yield x
<commit_msg>tagged logging of transaction failures with name of the resource experiencing failure.<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import time

from configman.config_manager import RequiredConfig
from configman import Namespace

#------------------------------------------------------------------------------
def string_to_list_of_ints(a_string):
    a_string = a_string.replace('"', '')
    a_string = a_string.replace("'", "")
    ints_as_strings = a_string.split(',')
    return [int(x) for x in ints_as_strings]

#==============================================================================
class TransactionExecutor(RequiredConfig):
    required_config = Namespace()

    #--------------------------------------------------------------------------
    def __init__(self, config, db_conn_context_source,
                 quit_check_callback=None):
        self.config = config
        self.db_conn_context_source = db_conn_context_source
        self.quit_check = quit_check_callback or (lambda: False)
        self.do_quit_check = True

    #--------------------------------------------------------------------------
    @property
    def connection_source_type(self):
        return self.db_conn_context_source.__module__

    #--------------------------------------------------------------------------
    def __call__(self, function, *args, **kwargs):
        """execute a function within the context of a transaction"""
        if self.do_quit_check:
            self.quit_check()
        with self.db_conn_context_source() as connection:
            try:
                #self.config.logger.debug('starting transaction')
                result = function(connection, *args, **kwargs)
                connection.commit()
                return result
            except:
                connection.rollback()
                self.config.logger.error(
                    'Exception raised during %s transaction',
                    self.connection_source_type,
                    exc_info=True)
                self.db_conn_context_source.force_reconnect()
                raise

#==============================================================================
class TransactionExecutorWithInfiniteBackoff(TransactionExecutor):
    # back off times
    required_config = Namespace()
    required_config.add_option('backoff_delays',
                               default="10, 30, 60, 120, 300",
                               doc='delays in seconds between retries',
                               from_string_converter=string_to_list_of_ints)
    # wait_log_interval
    required_config.add_option('wait_log_interval',
                               default=10,
                               doc='seconds between log during retries')

    #--------------------------------------------------------------------------
    def backoff_generator(self):
        """Generate a series of integers used for the length of the sleep
        between retries.  It produces after exhausting the list, it repeats
        the last value from the list forever.  This generator will never raise
        the StopIteration exception."""
        for x in self.config.backoff_delays:
            yield x
        while True:
            yield self.config.backoff_delays[-1]

    #--------------------------------------------------------------------------
    def responsive_sleep(self, seconds, wait_reason=''):
        """Sleep for the specified number of seconds, logging every
        'wait_log_interval' seconds with progress info."""
        for x in xrange(int(seconds)):
            if (self.config.wait_log_interval and
                not x % self.config.wait_log_interval):
                self.config.logger.debug(
                    '%s: %dsec of %dsec' % (wait_reason, x, seconds)
                )
            self.quit_check()
            time.sleep(1.0)

    #--------------------------------------------------------------------------
    def __call__(self, function, *args, **kwargs):
        """execute a function within the context of a transaction"""
        for wait_in_seconds in self.backoff_generator():
            try:
                if self.do_quit_check:
                    self.quit_check()
                # self.db_conn_context_source is an instance of a
                # wrapper class on the actual connection driver
                with self.db_conn_context_source() as connection:
                    try:
                        result = function(connection, *args, **kwargs)
                        connection.commit()
                        return result
                    except:
                        connection.rollback()
                        raise
            except self.db_conn_context_source.conditional_exceptions, x:
                # these exceptions may or may not be retriable
                # the test is for is a last ditch effort to see if
                # we can retry
                if not self.db_conn_context_source.is_operational_exception(x):
                    self.config.logger.critical(
                        'Unrecoverable %s transaction error',
                        self.connection_source_type,
                        exc_info=True
                    )
                    raise
                self.config.logger.critical(
                    '%s transaction error eligible for retry',
                    self.connection_source_type,
                    exc_info=True)

            except self.db_conn_context_source.operational_exceptions:
                self.config.logger.critical(
                    '%s transaction error eligible for retry',
                    self.connection_source_type,
                    exc_info=True)
            self.db_conn_context_source.force_reconnect()
            self.config.logger.debug(
                'retry in %s seconds' % wait_in_seconds
            )
            self.responsive_sleep(
                wait_in_seconds,
                'waiting for retry after failure in %s transaction' %
                    self.connection_source_type,
            )
        raise

#==============================================================================
class TransactionExecutorWithLimitedBackoff(
                                       TransactionExecutorWithInfiniteBackoff):
    #--------------------------------------------------------------------------
    def backoff_generator(self):
        """Generate a series of integers used for the length of the sleep
        between retries."""
        for x in self.config.backoff_delays:
            yield x
<|endoftext|>


<commit_before>import networkx as nx
from ClusterUtility import ClusterUtility

class ConnectedComponents:
    """This is a class for connected component detection method to cluster event logs [Studiawan2016a]_.

    References
    ----------
    .. [Studiawan2016a] H. Studiawan, B. A. Pratomo, and R. Anggoro, Connected component detection for authentication log
                        clustering, The 2nd International Seminar on Science and Technology, 2016, pp. 495-496.
    """
    def __init__(self, graph):
        """This is a constructor for ConnectedComponent class.

        Parameters
        ----------
        graph : graph
            A graph to be clustered.
        """
        self.graph = graph

    def get_clusters(self):
        """This method find any connected component in a graph.

        A component represents a cluster and each component will be given a cluster identifier.
        This method heavily rely on the cosine similarity threshold to build an edge in a graph.

        Returns
        -------
        clusters : dict[list]
            Dictionary of cluster list, where each list contains index (line number) of event log.
        """
        clusters = {}
        cluster_id = 0
        for components in nx.connected_components(self.graph):
            clusters[cluster_id] = components
            cluster_id += 1

        ClusterUtility.set_cluster_id(self.graph, clusters)

        return clusters
<commit_msg>Tidy up the docstring<commit_after>import networkx as nx
from ClusterUtility import ClusterUtility

class ConnectedComponents:
    """This is a class for connected component detection method to cluster event logs [Studiawan2016a]_.

    References
    ----------
    .. [Studiawan2016a] H. Studiawan, B. A. Pratomo, and R. Anggoro, Connected component detection for
                        authentication log clustering, The 2nd International Seminar on Science and Technology,
                        pp. 495-496, 2016.
    """
    def __init__(self, graph):
        """This is a constructor for ConnectedComponent class.

        Parameters
        ----------
        graph : graph
            A graph to be clustered.
        """
        self.graph = graph

    def get_clusters(self):
        """This method find any connected component in a graph.

        A component represents a cluster and each component will be given a cluster identifier.
        This method heavily rely on the cosine similarity threshold to build an edge in a graph.

        Returns
        -------
        clusters : dict[list]
            Dictionary of cluster list, where each list contains index (line number) of event log.
        """
        clusters = {}
        cluster_id = 0
        for components in nx.connected_components(self.graph):
            clusters[cluster_id] = components
            cluster_id += 1

        ClusterUtility.set_cluster_id(self.graph, clusters)

        return clusters
<|endoftext|>