<commit_before>import os
import shutil
import glob
from functools import wraps

import geopandas
from flask import current_app
from flask_restplus import Namespace, Resource, fields, abort
from staticmap import StaticMap, Polygon
from shapely.geometry import shape

import cea.inputlocator
from cea.utilities.standardize_coordinates import get_geographic_coordinate_system

api = Namespace('Project', description='Current project for CEA')

# PATH_REGEX = r'(^[a-zA-Z]:\\[\\\S|*\S]?.*$)|(^(/[^/ ]*)+/?$)'

PROJECT_PATH_MODEL = api.model('Project Path', {
    'path': fields.String(description='Path of Project'),
    'scenario': fields.String(description='Path of Scenario')
})

PROJECT_MODEL = api.inherit('Project', PROJECT_PATH_MODEL, {
    'name': fields.String(description='Name of Project'),
    'scenario': fields.String(description='Name of Current Scenario'),
    'scenarios': fields.List(fields.String, description='Name of Current Scenario')
})

NEW_PROJECT_MODEL = api.model('New Project', {
    'name': fields.String(description='Name of Project'),
    'path': fields.String(description='Path of Project')
})

@api.route('/')
class Project(Resource):
    @api.marshal_with(PROJECT_MODEL)
    def get(self):
        config = current_app.cea_config
        name = os.path.basename(config.project)
        if not os.path.exists(config.project):
            abort(400, 'Project path does not exist')
        if not os.path.exists(config.scenario):
            config.scenario_name = ''
            config.save()
        return {'name': name, 'path': config.project, 'scenario': config.scenario_name,
                'scenarios': config.get_parameter('general:scenario-name')._choices}

    @api.doc(body=PROJECT_PATH_MODEL, responses={200: 'Success', 400: 'Invalid Path given'})
    def put(self):
        """Update Project"""
        config = current_app.cea_config
        payload = api.payload

        if 'path' in payload:
            path = payload['path']
            if os.path.exists(path):
                config.project = path
                if 'scenario' not in payload:
                    config.scenario_name = ''
                    config.save()
                    return {'message': 'Project path changed'}
            else:
                abort(400, 'Path given does not exist')

        if 'scenario' in payload:
            scenario = payload['scenario']
            choices = config.get_parameter('general:scenario-name')._choices
            if scenario in choices:
                config.scenario_name = scenario
                config.save()
                return {'message': 'Scenario changed'}
            else:
                abort(400, 'Scenario does not exist', choices=choices)

    @api.doc(body=NEW_PROJECT_MODEL, responses={200: 'Success'})
    def post(self):
        """Create new project"""
        config = current_app.cea_config
        payload = api.payload

        if 'path' in payload:
            path = payload['path']
            name = payload['name']
            project_path = os.path.join(path, name)
            try:
                os.makedirs(project_path)
            except OSError as e:
                abort(400, str(e))

            config.project = project_path
            config.scenario_name = ''
            config.save()

            return {'message': 'Project created at {}'.format(project_path)}

@api.route('/scenario/')
class Scenarios(Resource):
    def post(self):
        """Create new scenario"""
        config = current_app.cea_config
        payload = api.payload

        # Make sure that the scenario folder exists
        try:
            os.makedirs(os.path.join(config.project, payload['name']))
        except OSError as e:
            print(e.message)

        config.scenario_name = payload['name']
        config.save()

        scenario_path = config.scenario

        locator = cea.inputlocator.InputLocator(scenario_path)

        if payload['input-data'] == 'import':
            files = payload['files']

            if files is not None:
                # since we're creating a new scenario, go ahead and and make sure we have
                # the folders _before_ we try copying to them
                locator.ensure_parent_folder_exists(locator.get_zone_geometry())
                locator.ensure_parent_folder_exists(locator.get_terrain())
                locator.ensure_parent_folder_exists(locator.get_building_age())
                locator.ensure_parent_folder_exists(locator.get_building_occupancy())
                locator.ensure_parent_folder_exists(locator.get_street_network())

                if 'zone' in files:
                    for filename in glob_shapefile_auxilaries(files['zone']):
                        shutil.copy(filename, locator.get_building_geometry_folder())
                if 'surroundings' in files:
                    for filename in glob_shapefile_auxilaries(files['surroundings']):
                        shutil.copy(filename, locator.get_building_geometry_folder())
                if 'surroundings' in files:
                    for filename in glob_shapefile_auxilaries(files['surroundings']):
                        shutil.copy(filename, locator.get_building_geometry_folder())
                if 'terrain' in files:
                    shutil.copyfile(files['terrain'], locator.get_terrain())
                if 'streets' in files:
                    shutil.copyfile(files['streets'], locator.get_street_network())

                from cea.datamanagement.zone_helper import calculate_age_file, calculate_occupancy_file
                if 'age' in files and files['age'] != '':
                    shutil.copyfile(files['age'], locator.get_building_age())
                elif 'zone' in files:
                    zone_df = geopandas.read_file(files['zone'])
                    calculate_age_file(zone_df, None, locator.get_building_age())

                if 'occupancy' in files and files['occupancy'] != '':
                    shutil.copyfile(files['occupancy'], locator.get_building_occupancy())
                elif 'zone' in files:
                    zone_df = geopandas.read_file(files['zone'])
                    if 'category' not in zone_df.columns:
                        # set 'MULTI_RES' as default
                        calculate_occupancy_file(zone_df, 'MULTI_RES', locator.get_building_occupancy())
                    else:
                        calculate_occupancy_file(zone_df, 'Get it from open street maps', locator.get_building_occupancy())

        elif payload['input-data'] == 'copy':
            source_scenario = os.path.join(config.project, payload['copy-scenario'])
            shutil.copytree(cea.inputlocator.InputLocator(source_scenario).get_input_folder(),
                            locator.get_input_folder())

        elif payload['input-data'] == 'generate':
            tools = payload['tools']
            if tools is not None:
                for tool in tools:
                    if tool == 'zone':
                        # FIXME: Setup a proper endpoint for site creation
                        site = geopandas.GeoDataFrame(crs=get_geographic_coordinate_system(),
                                                      geometry=[shape(payload['geojson']['features'][0]['geometry'])])
                        site_path = locator.get_site_polygon()
                        locator.ensure_parent_folder_exists(site_path)
                        site.to_file(site_path)
                        print('site.shp file created at %s' % site_path)
                        cea.api.zone_helper(config)
                    elif tool == 'surroundings':
                        cea.api.surroundings_helper(config)
                    elif tool == 'surroundings':
                        cea.api.surroundings_helper(config)
                    elif tool == 'streets':
                        cea.api.streets_helper(config)
                    elif tool == 'terrain':
                        cea.api.terrain_helper(config)
                    elif tool == 'weather':
                        cea.api.weather_helper(config)

        return {'scenarios': config.get_parameter('general:scenario-name')._choices}

def glob_shapefile_auxilaries(shapefile_path):
    """Returns a list of files in the same folder as ``shapefile_path``, but allows for varying extensions.
    This gets the .dbf, .shx, .prj, .shp and .cpg files"""
    return glob.glob('{basepath}.*'.format(basepath=os.path.splitext(shapefile_path)[0]))

def check_scenario_exists(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        config = current_app.cea_config
        choices = config.get_parameter('general:scenario-name')._choices
        if kwargs['scenario'] not in choices:
            abort(400, 'Scenario does not exist', choices=choices)
        else:
            return func(*args, **kwargs)
    return wrapper

# FIXME: Potential Issue. Need to check if the scenario being deleted/renamed is running in scripts.
@api.route('/scenario/<string:scenario>')
class Scenario(Resource):
    method_decorators = [check_scenario_exists]

    def get(self, scenario):
        """Scenario details"""
        return {'name': scenario}

    def put(self, scenario):
        """Update scenario"""
        config = current_app.cea_config
        scenario_path = os.path.join(config.project, scenario)
        payload = api.payload
        try:
            if 'name' in payload:
                new_path = os.path.join(config.project, payload['name'])
                os.rename(scenario_path, new_path)
                if config.scenario_name == scenario:
                    config.scenario_name = payload['name']
                    config.save()
                return {'name': payload['name']}
        except WindowsError:
            abort(400, 'Make sure that the scenario you are trying to rename is not open in any application. '
                       'Try and refresh the page again.')

    def delete(self, scenario):
        """Delete scenario from project"""
        config = current_app.cea_config
        scenario_path = os.path.join(config.project, scenario)
        try:
            if config.scenario_name == scenario:
                config.scenario_name = ''
                config.save()
            shutil.rmtree(scenario_path)
            return {'scenarios': config.get_parameter('general:scenario-name')._choices}
        except WindowsError:
            abort(400, 'Make sure that the scenario you are trying to delete is not open in any application. '
                       'Try and refresh the page again.')

@api.route('/scenario/<string:scenario>/image')
class ScenarioImage(Resource):
    def get(self, scenario):
        config = current_app.cea_config
        choices = config.get_parameter('general:scenario-name')._choices
        if scenario in choices:
            locator = cea.inputlocator.InputLocator(os.path.join(config.project, scenario))
            zone_path = locator.get_zone_geometry()
            if os.path.isfile(zone_path):
                cache_path = os.path.join(config.project, '.cache')
                image_path = os.path.join(cache_path, scenario + '.png')

                zone_modified = os.path.getmtime(zone_path)
                if not os.path.isfile(image_path):
                    image_modified = 0
                else:
                    image_modified = os.path.getmtime(image_path)

                if zone_modified > image_modified:
                    # Make sure .cache folder exists
                    if not os.path.exists(cache_path):
                        os.makedirs(cache_path)

                    try:
                        zone_df = geopandas.read_file(zone_path)
                        zone_df = zone_df.to_crs(get_geographic_coordinate_system())
                        polygons = zone_df['geometry']

                        polygons = [list(polygons.geometry.exterior[row_id].coords) for row_id in range(polygons.shape[0])]

                        m = StaticMap(256, 160)
                        for polygon in polygons:
                            out = Polygon(polygon, 'blue', 'black', False)
                            m.add_polygon(out)

                        image = m.render()
                        image.save(image_path)
                    except Exception as e:
                        abort(400, str(e))

                import base64
                with open(image_path, 'rb') as imgFile:
                    image = base64.b64encode(imgFile.read())

                return {'image': image}
            abort(400, 'Zone file not found')
        else:
            abort(400, 'Scenario does not exist', choices=choices)

<commit_msg>Use a temp config for helper tools<commit_after>import os
import shutil
import glob
from functools import wraps

import geopandas
from flask import current_app
from flask_restplus import Namespace, Resource, fields, abort
from staticmap import StaticMap, Polygon
from shapely.geometry import shape

import cea.inputlocator
import cea.api
import cea.config
from cea.utilities.standardize_coordinates import get_geographic_coordinate_system

api = Namespace('Project', description='Current project for CEA')

# PATH_REGEX = r'(^[a-zA-Z]:\\[\\\S|*\S]?.*$)|(^(/[^/ ]*)+/?$)'

PROJECT_PATH_MODEL = api.model('Project Path', {
    'path': fields.String(description='Path of Project'),
    'scenario': fields.String(description='Path of Scenario')
})

PROJECT_MODEL = api.inherit('Project', PROJECT_PATH_MODEL, {
    'name': fields.String(description='Name of Project'),
    'scenario': fields.String(description='Name of Current Scenario'),
    'scenarios': fields.List(fields.String, description='Name of Current Scenario')
})

NEW_PROJECT_MODEL = api.model('New Project', {
    'name': fields.String(description='Name of Project'),
    'path': fields.String(description='Path of Project')
})

@api.route('/')
class Project(Resource):
    @api.marshal_with(PROJECT_MODEL)
    def get(self):
        config = current_app.cea_config
        name = os.path.basename(config.project)
        if not os.path.exists(config.project):
            abort(400, 'Project path does not exist')
        if not os.path.exists(config.scenario):
            config.scenario_name = ''
            config.save()
        return {'name': name, 'path': config.project, 'scenario': config.scenario_name,
                'scenarios': config.get_parameter('general:scenario-name')._choices}

    @api.doc(body=PROJECT_PATH_MODEL, responses={200: 'Success', 400: 'Invalid Path given'})
    def put(self):
        """Update Project"""
        config = current_app.cea_config
        payload = api.payload

        if 'path' in payload:
            path = payload['path']
            if os.path.exists(path):
                config.project = path
                if 'scenario' not in payload:
                    config.scenario_name = ''
                    config.save()
                    return {'message': 'Project path changed'}
            else:
                abort(400, 'Path given does not exist')

        if 'scenario' in payload:
            scenario = payload['scenario']
            choices = config.get_parameter('general:scenario-name')._choices
            if scenario in choices:
                config.scenario_name = scenario
                config.save()
                return {'message': 'Scenario changed'}
            else:
                abort(400, 'Scenario does not exist', choices=choices)

    @api.doc(body=NEW_PROJECT_MODEL, responses={200: 'Success'})
    def post(self):
        """Create new project"""
        config = current_app.cea_config
        payload = api.payload

        if 'path' in payload:
            path = payload['path']
            name = payload['name']
            project_path = os.path.join(path, name)
            try:
                os.makedirs(project_path)
            except OSError as e:
                abort(400, str(e))

            config.project = project_path
            config.scenario_name = ''
            config.save()

            return {'message': 'Project created at {}'.format(project_path)}

@api.route('/scenario/')
class Scenarios(Resource):
    def post(self):
        """Create new scenario"""
        config = current_app.cea_config
        temp_config = cea.config.Configuration()
        payload = api.payload
        scenario_path = os.path.join(temp_config.project, payload['name'])

        # Make sure that the scenario folder exists
        try:
            os.makedirs(scenario_path)
        except OSError as e:
            print(e.message)

        locator = cea.inputlocator.InputLocator(scenario_path)

        if payload['input-data'] == 'import':
            files = payload['files']

            if files is not None:
                # since we're creating a new scenario, go ahead and and make sure we have
                # the folders _before_ we try copying to them
                locator.ensure_parent_folder_exists(locator.get_zone_geometry())
                locator.ensure_parent_folder_exists(locator.get_terrain())
                locator.ensure_parent_folder_exists(locator.get_building_age())
                locator.ensure_parent_folder_exists(locator.get_building_occupancy())
                locator.ensure_parent_folder_exists(locator.get_street_network())

                if 'zone' in files:
                    for filename in glob_shapefile_auxilaries(files['zone']):
                        shutil.copy(filename, locator.get_building_geometry_folder())
                if 'surroundings' in files:
                    for filename in glob_shapefile_auxilaries(files['surroundings']):
                        shutil.copy(filename, locator.get_building_geometry_folder())
                if 'surroundings' in files:
                    for filename in glob_shapefile_auxilaries(files['surroundings']):
                        shutil.copy(filename, locator.get_building_geometry_folder())
                if 'terrain' in files:
                    shutil.copyfile(files['terrain'], locator.get_terrain())
                if 'streets' in files:
                    shutil.copyfile(files['streets'], locator.get_street_network())

                from cea.datamanagement.zone_helper import calculate_age_file, calculate_occupancy_file
                if 'age' in files and files['age'] != '':
                    shutil.copyfile(files['age'], locator.get_building_age())
                elif 'zone' in files:
                    zone_df = geopandas.read_file(files['zone'])
                    calculate_age_file(zone_df, None, locator.get_building_age())

                if 'occupancy' in files and files['occupancy'] != '':
                    shutil.copyfile(files['occupancy'], locator.get_building_occupancy())
                elif 'zone' in files:
                    zone_df = geopandas.read_file(files['zone'])
                    if 'category' not in zone_df.columns:
                        # set 'MULTI_RES' as default
                        calculate_occupancy_file(zone_df, 'MULTI_RES', locator.get_building_occupancy())
                    else:
                        calculate_occupancy_file(zone_df, 'Get it from open street maps', locator.get_building_occupancy())

        elif payload['input-data'] == 'copy':
            source_scenario = os.path.join(temp_config.project, payload['copy-scenario'])
            shutil.copytree(cea.inputlocator.InputLocator(source_scenario).get_input_folder(),
                            locator.get_input_folder())

        elif payload['input-data'] == 'generate':
            tools = payload['tools']
            if tools is not None:
                for tool in tools:
                    if tool == 'zone':
                        # FIXME: Setup a proper endpoint for site creation
                        site = geopandas.GeoDataFrame(crs=get_geographic_coordinate_system(),
                                                      geometry=[shape(payload['geojson']['features'][0]['geometry'])])
                        site_path = locator.get_site_polygon()
                        locator.ensure_parent_folder_exists(site_path)
                        site.to_file(site_path)
                        print('site.shp file created at %s' % site_path)
                        cea.api.zone_helper(temp_config, scenario=scenario_path)
                    elif tool == 'surroundings':
                        cea.api.surroundings_helper(temp_config, scenario=scenario_path)
                    elif tool == 'surroundings':
                        cea.api.surroundings_helper(temp_config, scenario=scenario_path)
                    elif tool == 'streets':
                        cea.api.streets_helper(temp_config, scenario=scenario_path)
                    elif tool == 'terrain':
                        cea.api.terrain_helper(temp_config, scenario=scenario_path)
                    elif tool == 'weather':
                        cea.api.weather_helper(temp_config, scenario=scenario_path)

        return {'scenarios': config.get_parameter('general:scenario-name')._choices}

def glob_shapefile_auxilaries(shapefile_path):
    """Returns a list of files in the same folder as ``shapefile_path``, but allows for varying extensions.
    This gets the .dbf, .shx, .prj, .shp and .cpg files"""
    return glob.glob('{basepath}.*'.format(basepath=os.path.splitext(shapefile_path)[0]))

def check_scenario_exists(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        config = current_app.cea_config
        choices = config.get_parameter('general:scenario-name')._choices
        if kwargs['scenario'] not in choices:
            abort(400, 'Scenario does not exist', choices=choices)
        else:
            return func(*args, **kwargs)
    return wrapper

# FIXME: Potential Issue. Need to check if the scenario being deleted/renamed is running in scripts.
@api.route('/scenario/<string:scenario>')
class Scenario(Resource):
    method_decorators = [check_scenario_exists]

    def get(self, scenario):
        """Scenario details"""
        return {'name': scenario}

    def put(self, scenario):
        """Update scenario"""
        config = current_app.cea_config
        scenario_path = os.path.join(config.project, scenario)
        payload = api.payload
        try:
            if 'name' in payload:
                new_path = os.path.join(config.project, payload['name'])
                os.rename(scenario_path, new_path)
                if config.scenario_name == scenario:
                    config.scenario_name = payload['name']
                    config.save()
                return {'name': payload['name']}
        except WindowsError:
            abort(400, 'Make sure that the scenario you are trying to rename is not open in any application. '
                       'Try and refresh the page again.')

    def delete(self, scenario):
        """Delete scenario from project"""
        config = current_app.cea_config
        scenario_path = os.path.join(config.project, scenario)
        try:
            if config.scenario_name == scenario:
                config.scenario_name = ''
                config.save()
            shutil.rmtree(scenario_path)
            return {'scenarios': config.get_parameter('general:scenario-name')._choices}
        except WindowsError:
            abort(400, 'Make sure that the scenario you are trying to delete is not open in any application. '
                       'Try and refresh the page again.')

@api.route('/scenario/<string:scenario>/image')
class ScenarioImage(Resource):
    def get(self, scenario):
        config = current_app.cea_config
        choices = config.get_parameter('general:scenario-name')._choices
        if scenario in choices:
            locator = cea.inputlocator.InputLocator(os.path.join(config.project, scenario))
            zone_path = locator.get_zone_geometry()
            if os.path.isfile(zone_path):
                cache_path = os.path.join(config.project, '.cache')
                image_path = os.path.join(cache_path, scenario + '.png')

                zone_modified = os.path.getmtime(zone_path)
                if not os.path.isfile(image_path):
                    image_modified = 0
                else:
                    image_modified = os.path.getmtime(image_path)

                if zone_modified > image_modified:
                    # Make sure .cache folder exists
                    if not os.path.exists(cache_path):
                        os.makedirs(cache_path)

                    try:
                        zone_df = geopandas.read_file(zone_path)
                        zone_df = zone_df.to_crs(get_geographic_coordinate_system())
                        polygons = zone_df['geometry']

                        polygons = [list(polygons.geometry.exterior[row_id].coords) for row_id in range(polygons.shape[0])]

                        m = StaticMap(256, 160)
                        for polygon in polygons:
                            out = Polygon(polygon, 'blue', 'black', False)
                            m.add_polygon(out)

                        image = m.render()
                        image.save(image_path)
                    except Exception as e:
                        abort(400, str(e))

                import base64
                with open(image_path, 'rb') as imgFile:
                    image = base64.b64encode(imgFile.read())

                return {'image': image}
            abort(400, 'Zone file not found')
        else:
            abort(400, 'Scenario does not exist', choices=choices)

<|endoftext|>


<commit_before>"""
Updates the plugin index with the latest version and descriptions from
PyPI.

The index file (index.json) contains all plugins found by a search for
"pytest-" in PyPI in order to find all pytest plugins. We then save their
latest version and description into index.json, which will be read by
"run.py" in order to execute compatibility tests between the plugins
and pytest/python versions. See "run.py" for more details.

Usage:

    python update_index.py

If index.json was updated, it should be pushed back to GitHub, which will
trigger a new travis build using the new versions.
"""
from __future__ import print_function, with_statement, division

import json
import os
import sys
from distutils.version import LooseVersion

if sys.version_info[0] == 3:
    from xmlrpc.client import ServerProxy
else:
    from xmlrpclib import ServerProxy

INDEX_FILE_NAME = os.path.join(os.path.dirname(__file__), 'index.json')

def iter_plugins(client):
    '''
    Returns an iterator of (name, latest version, summary) from PyPI.

    :param client: xmlrpclib.ServerProxy
    :param search: package names to search for
    '''
    # previously we used the more efficient "search" XMLRPC method, but
    # that stopped returning all results after a while
    package_names = [x for x in client.list_packages()
                     if x.startswith('pytest-')]
    names_and_versions = {}
    for name in package_names:
        versions = client.package_releases(name)
        if versions:  # Package can exist without public releases
            names_and_versions[name] = sorted(versions, key=LooseVersion)[0]

    print('pytest-*: %d packages' % len(names_and_versions))

    # search for the new Pytest classifier
    found = client.browse(['Framework :: Pytest'])
    valid = 0
    for name, version in found:
        if name and version:
            names_and_versions[name] = version
            valid += 1
    print('classifier: %d packages (%d valid)' % (len(found), valid))
    print('total: %d packages' % len(names_and_versions))

    for name, version in names_and_versions.items():
        plug_data = client.release_data(name, version)
        yield plug_data['name'], plug_data['version'], plug_data['summary']

def write_plugins_index(file_name, plugins):
    """
    Writes the list of (name, version, description) of the plugins given
    into the index file in JSON format.
    Returns True if the file was actually updated, or False if it was already
    up-to-date.
    """
    # separators is given to avoid trailing whitespaces; see docs
    plugin_contents = []
    for (name, version, description) in plugins:
        plugin_contents.append({
            'name': name,
            'version': version,
            'description': description,
        })
    contents = json.dumps(plugin_contents, indent=2, separators=(',', ': '),
                          sort_keys=True)
    if os.path.isfile(file_name):
        with open(file_name, 'rU') as f:
            current_contents = f.read()
    else:
        current_contents = ''

    if contents != current_contents:
        with open(file_name, 'w') as f:
            f.write(contents)
        return True
    else:
        return False

def main():
    client = ServerProxy('https://pypi.python.org/pypi')
    plugins = sorted(iter_plugins(client))

    if write_plugins_index(INDEX_FILE_NAME, plugins):
        print(INDEX_FILE_NAME, 'updated, push to GitHub.')
    else:
        print(INDEX_FILE_NAME, 'skipped, no changes.')

if __name__ == '__main__':
    main()
<commit_msg>avoid deprecations on 3.7<commit_after>"""
Updates the plugin index with the latest version and descriptions from
PyPI.

The index file (index.json) contains all plugins found by a search for
"pytest-" in PyPI in order to find all pytest plugins. We then save their
latest version and description into index.json, which will be read by
"run.py" in order to execute compatibility tests between the plugins
and pytest/python versions. See "run.py" for more details.

Usage:

    python update_index.py

If index.json was updated, it should be pushed back to GitHub, which will
trigger a new travis build using the new versions.
"""
from __future__ import print_function, with_statement, division

import json
import os
import sys
from distutils.version import LooseVersion

if sys.version_info[0] == 3:
    from xmlrpc.client import ServerProxy
else:
    from xmlrpclib import ServerProxy

INDEX_FILE_NAME = os.path.join(os.path.dirname(__file__), 'index.json')

def iter_plugins(client):
    '''
    Returns an iterator of (name, latest version, summary) from PyPI.

    :param client: xmlrpclib.ServerProxy
    :param search: package names to search for
    '''
    # previously we used the more efficient "search" XMLRPC method, but
    # that stopped returning all results after a while
    package_names = [x for x in client.list_packages()
                     if x.startswith('pytest-')]
    names_and_versions = {}
    for name in package_names:
        versions = client.package_releases(name)
        if versions:  # Package can exist without public releases
            names_and_versions[name] = sorted(versions, key=LooseVersion)[0]

    print('pytest-*: %d packages' % len(names_and_versions))

    # search for the new Pytest classifier
    found = client.browse(['Framework :: Pytest'])
    valid = 0
    for name, version in found:
        if name and version:
            names_and_versions[name] = version
            valid += 1
    print('classifier: %d packages (%d valid)' % (len(found), valid))
    print('total: %d packages' % len(names_and_versions))

    for name, version in names_and_versions.items():
        plug_data = client.release_data(name, version)
        yield plug_data['name'], plug_data['version'], plug_data['summary']

def write_plugins_index(file_name, plugins):
    """
    Writes the list of (name, version, description) of the plugins given
    into the index file in JSON format.
    Returns True if the file was actually updated, or False if it was already
    up-to-date.
    """
    # separators is given to avoid trailing whitespaces; see docs
    plugin_contents = []
    for (name, version, description) in plugins:
        plugin_contents.append({
            'name': name,
            'version': version,
            'description': description,
        })
    contents = json.dumps(plugin_contents, indent=2, separators=(',', ': '),
                          sort_keys=True)
    if os.path.isfile(file_name):
        if sys.version_info[0] < (3,):
            mode = 'rU'
        else:
            # universal newlines is enabled by default, and specifying it
            # will cause deprecation warnings
            mode = 'r'
        with open(file_name, mode) as f:
            current_contents = f.read()
    else:
        current_contents = ''

    if contents != current_contents:
        with open(file_name, 'w') as f:
            f.write(contents)
        return True
    else:
        return False

def main():
    client = ServerProxy('https://pypi.python.org/pypi')
    plugins = sorted(iter_plugins(client))

    if write_plugins_index(INDEX_FILE_NAME, plugins):
        print(INDEX_FILE_NAME, 'updated, push to GitHub.')
    else:
        print(INDEX_FILE_NAME, 'skipped, no changes.')

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


<commit_before>from __future__ import print_function
from psychopy import sound, monitors, core, visual, event, data, gui
from psychopy import logging as ppLog
import numpy as np
from copy import deepcopy
from math import atan, cos, sin, pi, sqrt, pow
import time, colorsys
import sys, platform, os, StringIO
from pylink import *

applescript="\'tell application \"Finder\" to quit\'" #quit Finder.
shellCmd = 'osascript -e '+applescript
os.system(shellCmd)

timeAndDateStr = time.strftime("%d%b%Y_%H-%M", time.localtime())
subject = 'CF-Practice'
if os.path.isdir('.'+os.sep+'data'):
    dataDir='data'
else:
    print('"data" directory does not exist, so saving data in present working directory')
    dataDir='.'
fileName = os.path.join(dataDir, subject+'probeDropTask'+timeAndDateStr)
dataFile = open(fileName+'.txt', 'w')  # sys.stdout  #StringIO.StringIO()
saveCodeCmd = 'cp \'' + sys.argv[0] + '\' '+ fileName + '.py'
os.system(saveCodeCmd)  #save a copy of the code as it was when that subject was run
logFname = fileName+'.log'
ppLogF = ppLog.LogFile(logFname,
    filemode='w',#if you set this to 'a' it will append instead of overwriting
    level=ppLog.INFO)#errors, data and warnings will be sent to this logfile
trialClock = core.Clock()

ballStdDev = 0.8
autoLogging = False

infoFirst = { 'Check refresh etc':False, 'Fullscreen (timing errors if not)': False, 'Screen refresh rate': 60 }
OK = gui.DlgFromDict(dictionary=infoFirst,
    title='Szinte & Cavanagh spatiotopic apparent motion',
    order=[ 'Check refresh etc', 'Fullscreen (timing errors if not)'],
    tip={'Check refresh etc': 'To confirm refresh rate and that can keep up, at least when drawing a grating'},
    #fixed=['Check refresh etc'])#this attribute can't be changed by the user
    )
if not OK.OK:
    print('User cancelled from dialog box'); core.quit()
checkRefreshEtc = infoFirst['Check refresh etc']
fullscr = infoFirst['Fullscreen (timing errors if not)']
refreshRate = infoFirst['Screen refresh rate']
quitFinder = False
if checkRefreshEtc:
    quitFinder = True
if quitFinder:
    import os
    applescript="\'tell application \"Finder\" to quit\'"
    shellCmd = 'osascript -e '+applescript
    os.system(shellCmd)

fullscr=0
scrn=0 #1 means second screen
widthPix =1024#1024  #monitor width in pixels
heightPix =768#768  #monitor height in pixels
monitorwidth = 40. #28.5 #monitor width in centimeters
viewdist = 57.; #cm
pixelperdegree = widthPix/ (atan(monitorwidth/viewdist) / np.pi*180)
bgColor = [0,0,0] #"gray background"
monitorname = 'mitsubishi' #in psychopy Monitors Center
allowGUI = False
waitBlank = False
units = 'deg'

mon = monitors.Monitor(monitorname,width=monitorwidth, distance=viewdist)#fetch the most recent calib for this monitor
mon.setSizePix( (widthPix,heightPix) )
myWin = visual.Window(monitor=mon,size=(widthPix,heightPix),allowGUI=allowGUI,units=units,rgb=bgColor,fullscr=fullscr,screen=scrn,waitBlanking=waitBlank) #Holcombe lab monitor

targetDot = visual.ImageStim(myWin,mask='circle',colorSpace='rgb', color = (-1, 1.0, -1), size=ballStdDev,autoLog=autoLogging, contrast=1, opacity = 1.0)
foilDot = visual.ImageStim(myWin,mask='circle',colorSpace='rgb', color = (.8, 0, 1),size=ballStdDev,autoLog=autoLogging, contrast=1, opacity = 1.0)
blackDot = visual.ImageStim(myWin,mask='circle',colorSpace='rgb', color = (-1,-1,-1),size=ballStdDev,autoLog=autoLogging, contrast=0.5, opacity = 1.0)

beforeTrialsText = visual.TextStim(myWin,pos=(0, 0),rgb = (-1,-1,-1),alignHoriz='center', alignVert='center', height = 0.05, units='norm',autoLog=autoLogging)
respPromptText = visual.TextStim(myWin,pos=(0, -.3),rgb = (-1,-1,-1),alignHoriz='center', alignVert='center', height = 0.07, units='norm',autoLog=autoLogging)
betweenTrialsText = visual.TextStim(myWin,pos=(0, -.4),rgb = (-1,-1,-1),alignHoriz='center', alignVert='center', units='norm',autoLog=autoLogging)

#while True:
#    targetDot.pos= (x)
#    foilDot.pos= (y)
#    targetDot.draw()
#    foilDot.draw()
#    myWin.flip()
locationOfProbe= np.array([[-10,1.5],[0,1.5],[10,1.5]]) #Potential other conditions:[-10,6.5],[0,6.5],[10,6.5],[-10,-3.5],[0,-3.5],[10,-3.5]

stimList=[]
for location in locationOfProbe: #location of the probe for the trial
    for downUp in [-1,1]: #switching between probe moving top to bottom; and bottom to top
        for tilt in [-2,2]: # [-0.875,0,0.875]: #adjusting whether the probe jump is vertical, or slanted
            for jitter in [-0.875,0,0.875]:#shifting each condition slightly from the location to ensure participants dont recognise tilted trials by the location of the initial probe
                probeLocation = [location[0]+jitter, location[1]]
                stimList.append({'location': probeLocation, 'downUp': downUp, 'tilt': tilt, 'jitter': jitter})

blockReps = 1
trials = data.TrialHandler(stimList, blockReps)
thisTrial = trials.next()

trialDurFramesTotal = int(1.7*refreshRate) #total duration of each trial
initialDur = 0.6*refreshRate #target and foil dot without probe for the first 600 ms
trialWithProbe = 1.0*refreshRate #probe appears for 400 ms
probeFirstDisappearance = 1.1*refreshRate # probe disappears for 100 ms whilst target and foil dot remain the same
switchCues = 1.2*refreshRate # target and foil dots switch positions for 100 ms
probeSecondAppearance = 1.6*refreshRate # probe returns on the other side of the horizontal meridian for 400 ms
probeSecondDisappearance = 1.7*refreshRate # probe disappears

def oneFrameOfStim(n,targetDotPos,foilDotPos,probePos1,probePos2): #trial stimulus function

    if n <= initialDur:   #show target and foil only, either because first part of trial
        pass #dont draw black dot, dont change positions
    elif initialDur <= n < probeFirstDisappearance: #show first position of probe
        blackDot.pos = (probePos1)
        blackDot.draw()
    elif probeFirstDisappearance <= n < switchCues:  #after probe first disappearance, but before target moves
        pass #dont draw black dot, don't change positions
    elif switchCues <= n < probeSecondAppearance: #target and foil in exchanged positions, probe in new location
        targetDotPos*=-1
        foilDotPos*=-1
        blackDot.pos = (probePos2)
        blackDot.draw()
    elif (probeSecondAppearance <= n < probeSecondDisappearance): #target and foil, in exchanged positions
        targetDotPos*=-1
        foilDotPos*=-1

    targetDot.pos= (targetDotPos)
    foilDot.pos= (foilDotPos)
    targetDot.draw()
    foilDot.draw()
    myWin.flip()

print('trialnum\tsubject\tlocation\tdownUp\tTilt\tJitter\tDirection\t', file=dataFile)
#print >>dataFile2, 'trialnum\tsubject\tlocation\tdownUp\tTilt\tDirection\t'

expStop = False
nDone = 1
while nDone<= trials.nTotal and not expStop:
    if nDone ==1:
        beforeTrialsText.setText("In this task you are required to look at the green dot on the screen. Look directly at the green dot, wherever it moves on the screen "
                   "Whilst looking at the green dot, you will see a black dot that will either move upwards or downwards during the "
                   "trial. At the end of the trial you are required to identify whether or not the black dot moved in a clockwise "
                   "or anticlockwise direction. Press the right arrow if its trajectory had the tilt of  a forward slash / \n"
                   "and the left arrow if it had the tilt of a backslash \ ")
        respPromptText.setText("anticlockwise \                          clockwise /      ")
        beforeTrialsText.draw()
        respPromptText.draw()
        betweenTrialsText.setText('Press SPACE to continue')
        betweenTrialsText.draw()
        myWin.flip(clearBuffer=True)
        keysPressed = event.waitKeys(maxWait = 120, keyList = ['space','escape'], timeStamped = False)
        if 'escape' in keysPressed:
            print('User cancelled by pressing <escape>'); core.quit()
        myWin.clearBuffer()
    elif nDone == trials.nTotal/2 + 1:
        beforeTrials.setText("""In this next part of the task, the green dot will appear on the right and jump to the left. Press SPACE to continue.""")
        beforeTrials.draw()
        myWin.flip(clearBuffer=True)
        keysPressed = event.waitKeys(maxWait = 120, keyList = ['space','escape'], timeStamped = False)
        if 'escape' in keysPressed:
            print('User cancelled by pressing <escape>'); core.quit()
        myWin.clearBuffer()

    if nDone<=trials.nTotal/2:
        targetDotPos=np.array([-5,0]) # position of the green and grey stimulus for first half of trials - left to right - this has not been coded in to the data file, may be a good idea to do so
        foilDotPos =np.array([5,0])
    else:
        targetDotPos=np.array([5,0])  #position of the green and grey stimulus for second half of trials - right to left
        foilDotPos =np.array([-5,0])
    probePos1= (thisTrial['location'][0]+thisTrial['tilt'], thisTrial['location'][1]*thisTrial['downUp'])
    probePos2 =([thisTrial['location'][0]-thisTrial['tilt'], probePos1[-1]*-1])

    for n in range(trialDurFramesTotal): #Loop for the trial stimulus
        oneFrameOfStim(n,targetDotPos,foilDotPos,probePos1,probePos2)

    keysPressed = event.waitKeys(maxWait = 120, keyList = ['left','right','escape'], timeStamped = False) #'down' removed
    if 'escape' in keysPressed:
            expStop=True
    if not expStop:
        if 'left' in keysPressed: #recoding key presses as 0 (anticlockwise) or 1 (clockwise) for data analysis
            respLeftRight = 0
        else:
            respLeftRight = 1

        #header print('trialnum\tsubject\tlocation\tdownUp\tTilt\tJitter\tDirection\t', file=dataFile)
        oneTrialOfData= "%2.2f\t"%thisTrial['location'][0]
        oneTrialOfData = (str(nDone)+'\t'+subject+'\t'+ "%2.2f\t"%thisTrial['location'][0] +"%r\t"%thisTrial['downUp'] +
                                        "%r\t"%thisTrial['tilt'] + "%r\t"%thisTrial['jitter']+ "%r"%respLeftRight)
        print(oneTrialOfData, file= dataFile)
        if nDone< trials.nTotal:
            #betweenTrialsText.setText('Press SPACE to continue')
            betweenTrialsText.draw()
            myWin.flip(clearBuffer=True)
            keysPressedBetweenTrials = event.waitKeys(maxWait = 120, keyList = ['space'], timeStamped = False)
            if 'escape' in keysPressedBetweenTrials:
                    expStop=True
            thisTrial=trials.next()
            myWin.clearBuffer()
        nDone+=1

if expStop:
    print("Experiment stopped because user cancelled")
else:
    print("Experiment finished")
if  nDone >0:
    print('Of ',nDone,' trials, on ',3, '% of all trials all targets reported exactly correct',sep='')

    <commit_msg>fixed<commit_after>from __future__ import print_function
from psychopy import sound, monitors, core, visual, event, data, gui
from psychopy import logging as ppLog
import numpy as np
from copy import deepcopy
from math import atan, cos, sin, pi, sqrt, pow
import time, colorsys
import sys, platform, os, StringIO
from pylink import *

applescript="\'tell application \"Finder\" to quit\'" #quit Finder.
shellCmd = 'osascript -e '+applescript
os.system(shellCmd)

timeAndDateStr = time.strftime("%d%b%Y_%H-%M", time.localtime())
subject = 'CF-Practice'
if os.path.isdir('.'+os.sep+'data'):
    dataDir='data'
else:
    print('"data" directory does not exist, so saving data in present working directory')
    dataDir='.'
fileName = os.path.join(dataDir, subject+'probeDropTask'+timeAndDateStr)
dataFile = open(fileName+'.txt', 'w')  # sys.stdout  #StringIO.StringIO()
saveCodeCmd = 'cp \'' + sys.argv[0] + '\' '+ fileName + '.py'
os.system(saveCodeCmd)  #save a copy of the code as it was when that subject was run
logFname = fileName+'.log'
ppLogF = ppLog.LogFile(logFname,
    filemode='w',#if you set this to 'a' it will append instead of overwriting
    level=ppLog.INFO)#errors, data and warnings will be sent to this logfile
trialClock = core.Clock()

ballStdDev = 0.8
autoLogging = False

infoFirst = { 'Check refresh etc':False, 'Fullscreen (timing errors if not)': False, 'Screen refresh rate': 60 }
OK = gui.DlgFromDict(dictionary=infoFirst,
    title='Szinte & Cavanagh spatiotopic apparent motion',
    order=[ 'Check refresh etc', 'Fullscreen (timing errors if not)'],
    tip={'Check refresh etc': 'To confirm refresh rate and that can keep up, at least when drawing a grating'},
    #fixed=['Check refresh etc'])#this attribute can't be changed by the user
    )
if not OK.OK:
    print('User cancelled from dialog box'); core.quit()
checkRefreshEtc = infoFirst['Check refresh etc']
fullscr = infoFirst['Fullscreen (timing errors if not)']
refreshRate = infoFirst['Screen refresh rate']
quitFinder = False
if checkRefreshEtc:
    quitFinder = True
if quitFinder:
    import os
    applescript="\'tell application \"Finder\" to quit\'"
    shellCmd = 'osascript -e '+applescript
    os.system(shellCmd)

fullscr=0
scrn=0 #1 means second screen
widthPix =1024#1024  #monitor width in pixels
heightPix =768#768  #monitor height in pixels
monitorwidth = 40. #28.5 #monitor width in centimeters
viewdist = 57.; #cm
pixelperdegree = widthPix/ (atan(monitorwidth/viewdist) / np.pi*180)
bgColor = [0,0,0] #"gray background"
monitorname = 'mitsubishi' #in psychopy Monitors Center
allowGUI = False
waitBlank = False
units = 'deg'

mon = monitors.Monitor(monitorname,width=monitorwidth, distance=viewdist)#fetch the most recent calib for this monitor
mon.setSizePix( (widthPix,heightPix) )
myWin = visual.Window(monitor=mon,size=(widthPix,heightPix),allowGUI=allowGUI,units=units,rgb=bgColor,fullscr=fullscr,screen=scrn,waitBlanking=waitBlank) #Holcombe lab monitor

targetDot = visual.ImageStim(myWin,mask='circle',colorSpace='rgb', color = (-1, 1.0, -1), size=ballStdDev,autoLog=autoLogging, contrast=1, opacity = 1.0)
foilDot = visual.ImageStim(myWin,mask='circle',colorSpace='rgb', color = (.8, 0, 1),size=ballStdDev,autoLog=autoLogging, contrast=1, opacity = 1.0)
blackDot = visual.ImageStim(myWin,mask='circle',colorSpace='rgb', color = (-1,-1,-1),size=ballStdDev,autoLog=autoLogging, contrast=0.5, opacity = 1.0)

beforeTrialsText = visual.TextStim(myWin,pos=(0, 0),rgb = (-1,-1,-1),alignHoriz='center', alignVert='center', height = 0.05, units='norm',autoLog=autoLogging)
respPromptText = visual.TextStim(myWin,pos=(0, -.3),rgb = (-1,-1,-1),alignHoriz='center', alignVert='center', height = 0.07, units='norm',autoLog=autoLogging)
betweenTrialsText = visual.TextStim(myWin,pos=(0, -.4),rgb = (-1,-1,-1),alignHoriz='center', alignVert='center', units='norm',autoLog=autoLogging)

#while True:
#    targetDot.pos= (x)
#    foilDot.pos= (y)
#    targetDot.draw()
#    foilDot.draw()
#    myWin.flip()
locationOfProbe= np.array([[-10,1.5],[0,1.5],[10,1.5]]) #Potential other conditions:[-10,6.5],[0,6.5],[10,6.5],[-10,-3.5],[0,-3.5],[10,-3.5]

stimList=[]
for location in locationOfProbe: #location of the probe for the trial
    for downUp in [-1,1]: #switching between probe moving top to bottom; and bottom to top
        for tilt in [-2,2]: # [-0.875,0,0.875]: #adjusting whether the probe jump is vertical, or slanted
            for jitter in [-0.875,0,0.875]:#shifting each condition slightly from the location to ensure participants dont recognise tilted trials by the location of the initial probe
                probeLocation = [location[0]+jitter, location[1]]
                stimList.append({'location': probeLocation, 'downUp': downUp, 'tilt': tilt, 'jitter': jitter})

blockReps = 1
trials = data.TrialHandler(stimList, blockReps)
thisTrial = trials.next()

trialDurFramesTotal = int(1.7*refreshRate) #total duration of each trial
initialDur = 0.6*refreshRate #target and foil dot without probe for the first 600 ms
trialWithProbe = 1.0*refreshRate #probe appears for 400 ms
probeFirstDisappearance = 1.1*refreshRate # probe disappears for 100 ms whilst target and foil dot remain the same
switchCues = 1.2*refreshRate # target and foil dots switch positions for 100 ms
probeSecondAppearance = 1.6*refreshRate # probe returns on the other side of the horizontal meridian for 400 ms
probeSecondDisappearance = 1.7*refreshRate # probe disappears

def oneFrameOfStim(n,targetDotPos,foilDotPos,probePos1,probePos2): #trial stimulus function
    targetDotPosThis = deepcopy(targetDotPos) #dont change starting value
    foilDotPosThis =  deepcopy(foilDotPos)

    if n <= initialDur:   #show target and foil only, either because first part of trial
        pass #dont draw black dot, dont change positions
    elif initialDur <= n < probeFirstDisappearance: #show first position of probe
        blackDot.pos = (probePos1)
        blackDot.draw()
    elif probeFirstDisappearance <= n < switchCues:  #after probe first disappearance, but before target moves
        pass #dont draw black dot, don't change positions
    elif switchCues <= n < probeSecondAppearance: #target and foil in exchanged positions, probe in new location
        targetDotPosThis *=-1
        foilDotPosThis *= -1
        blackDot.pos = (probePos2)
        blackDot.draw()
    elif (probeSecondAppearance <= n < probeSecondDisappearance): #target and foil, in exchanged positions
        targetDotPosThis *=-1
        foilDotPosThis *=-1

    targetDot.pos= (targetDotPosThis)
    foilDot.pos= (foilDotPosThis)
    targetDot.draw()
    foilDot.draw()
    myWin.flip()

print('trialnum\tsubject\tlocation\tdownUp\tTilt\tJitter\tDirection\t', file=dataFile)
#print >>dataFile2, 'trialnum\tsubject\tlocation\tdownUp\tTilt\tDirection\t'

expStop = False
nDone = 1
while nDone<= trials.nTotal and not expStop:
    if nDone ==1:
        beforeTrialsText.setText("In this task you are required to look at the green dot on the screen. Look directly at the green dot, wherever it moves on the screen "
                   "Whilst looking at the green dot, you will see a black dot that will either move upwards or downwards during the "
                   "trial. At the end of the trial you are required to identify whether or not the black dot moved in a clockwise "
                   "or anticlockwise direction. Press the right arrow if its trajectory had the tilt of  a forward slash / \n"
                   "and the left arrow if it had the tilt of a backslash \ ")
        respPromptText.setText("anticlockwise \                          clockwise /      ")
        beforeTrialsText.draw()
        respPromptText.draw()
        betweenTrialsText.setText('Press SPACE to continue')
        betweenTrialsText.draw()
        myWin.flip(clearBuffer=True)
        keysPressed = event.waitKeys(maxWait = 120, keyList = ['space','escape'], timeStamped = False)
        if 'escape' in keysPressed:
            print('User cancelled by pressing <escape>'); core.quit()
        myWin.clearBuffer()
    elif nDone == trials.nTotal/2 + 1:
        beforeTrials.setText("""In this next part of the task, the green dot will appear on the right and jump to the left. Press SPACE to continue.""")
        beforeTrials.draw()
        myWin.flip(clearBuffer=True)
        keysPressed = event.waitKeys(maxWait = 120, keyList = ['space','escape'], timeStamped = False)
        if 'escape' in keysPressed:
            print('User cancelled by pressing <escape>'); core.quit()
        myWin.clearBuffer()

    if nDone<=trials.nTotal/2:
        targetDotPos=np.array([-5,0]) # position of the green and grey stimulus for first half of trials - left to right - this has not been coded in to the data file, may be a good idea to do so
        foilDotPos =np.array([5,0])
    else:
        targetDotPos=np.array([5,0])  #position of the green and grey stimulus for second half of trials - right to left
        foilDotPos =np.array([-5,0])
    probePos1= (thisTrial['location'][0]+thisTrial['tilt'], thisTrial['location'][1]*thisTrial['downUp'])
    probePos2 =([thisTrial['location'][0]-thisTrial['tilt'], probePos1[-1]*-1])

    for n in range(trialDurFramesTotal): #Loop for the trial stimulus
        oneFrameOfStim(n,targetDotPos,foilDotPos,probePos1,probePos2)

    keysPressed = event.waitKeys(maxWait = 120, keyList = ['left','right','escape'], timeStamped = False) #'down' removed
    if 'escape' in keysPressed:
            expStop=True
    if not expStop:
        if 'left' in keysPressed: #recoding key presses as 0 (anticlockwise) or 1 (clockwise) for data analysis
            respLeftRight = 0
        else:
            respLeftRight = 1

        #header print('trialnum\tsubject\tlocation\tdownUp\tTilt\tJitter\tDirection\t', file=dataFile)
        oneTrialOfData= "%2.2f\t"%thisTrial['location'][0]
        oneTrialOfData = (str(nDone)+'\t'+subject+'\t'+ "%2.2f\t"%thisTrial['location'][0] +"%r\t"%thisTrial['downUp'] +
                                        "%r\t"%thisTrial['tilt'] + "%r\t"%thisTrial['jitter']+ "%r"%respLeftRight)
        print(oneTrialOfData, file= dataFile)
        if nDone< trials.nTotal:
            #betweenTrialsText.setText('Press SPACE to continue')
            betweenTrialsText.draw()
            myWin.flip(clearBuffer=True)
            keysPressedBetweenTrials = event.waitKeys(maxWait = 120, keyList = ['space'], timeStamped = False)
            if 'escape' in keysPressedBetweenTrials:
                    expStop=True
            thisTrial=trials.next()
            myWin.clearBuffer()
        nDone+=1

if expStop:
    print("Experiment stopped because user cancelled")
else:
    print("Experiment finished")
if  nDone >0:
    print('Of ',nDone,' trials, on ',3, '% of all trials all targets reported exactly correct',sep='')

    <|endoftext|>


<commit_before>#######################################################################
# Simpleacl - A small access control list
# Copyright (C) 2010  Ivan Zakrevsky <ivzak [at] yandex [dot] ru>
# Copyright (C) 2010  Kyle Terry <kyle [at] fiverlabs [dot] com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#######################################################################
from __future__ import absolute_import, unicode_literals
try:
    import simplejson as json
except:
    import json

from simpleacl.exceptions import MissingRole, MissingActiveRole,\
    MissingPrivilege

try:
    str = unicode  # Python 2.* compatible
except NameError:
    pass

ALL_PRIVILEGES = 'all'

class Role(object):
    """Holds a role value"""

    _parents = []  # Order is important, so use the list(), not set

    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name.__eq__(getattr(other, 'name', other))

    def __ne__(self, other):
        return self.name.__ne__(getattr(other, 'name', other))

    def __hash__(self):
        return self.name.__hash__()

    def __bytes__(self):
        return str(self.name).encode('utf-8')

    def __str__(self):
        return str(self.name)

    def get_name(self):
        return self.name

    def add_parent(self, parent):
        if parent not in self._parents:
            self._parents.append(parent)

    def get_parents(self):
        return self._parents

class Privilege(object):
    """Holds a privilege value"""

    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name.__eq__(getattr(other, 'name', other))

    def __ne__(self, other):
        return self.name.__ne__(getattr(other, 'name', other))

    def __hash__(self):
        return self.name.__hash__()

    def __bytes__(self):
        return str(self.name).encode('utf-8')

    def __str__(self):
        return str(self.name)

    def get_name(self):
        return self.name

class Context(object):
    """Holder for context value.

    Sometimes apposite for usege as context wrapper."""

    _parents = []  # Order is important, so use the list(), not set

    def __init__(self, base):
        self.base = base

    def __eq__(self, other):
        return self.base.__eq__(getattr(other, 'base', other))

    def __ne__(self, other):
        return self.base.__ne__(getattr(other, 'base', other))

    def __hash__(self):
        return self.base.__hash__()

    def add_parent(self, parent):
        if parent not in self._parents:
            self._parents.append(parent)

    def get_parents(self):
        return self._parents

class SimpleBackend(object):
    """A simple storage."""
    _roles = None
    _privileges = None
    _acl = None
    role_class = Role
    privilege_class = Privilege

    def __init__(self):
        """Constructor."""
        self._roles = {}
        self._privileges = {}
        self._acl = {}

    def add_role(self, role, parents=None):
        """Adds role"""
        self._roles.setdefault(role.get_name(), role)
        return self

    def get_role(self, role_name):
        """Returns a role instance"""
        try:
            return self._roles[role_name]
        except KeyError:
            raise MissingRole(
                'Role must be added before requested.'
            )

    def add_privilege(self, privilege):
        """Adds privilege"""
        self._privileges.setdefault(privilege.get_name(), privilege)
        return self

    def get_privilege(self, privilege_name):
        """Returns a privilege instance"""
        try:
            return self._privileges[privilege_name]
        except KeyError:
            raise MissingPrivilege(
                'Privilege must be added before requested.'
            )

    def add_rule(self, role, privilege=ALL_PRIVILEGES,
                 context=None, allow=True):
        """Adds rule to the ACL"""
        acl = self._acl.setdefault(context, {})
        role_rules = acl.setdefault(role, {})
        role_rules[privilege] = allow
        return self

    def remove_rule(self, role, privilege=ALL_PRIVILEGES,
                    context=None, allow=True):
        """Removes rule from ACL"""
        try:
            if self._acl[context][role][privilege] == allow:
                del self._acl[context][role][privilege]
        except KeyError:
            pass
        return self

    def role_has_privilege(self, role, privilege, context=None, allow=True):
        """Removes rule from ACL"""
        try:
            return self._acl[context][role][privilege] == allow
        except KeyError:
            return False

    def is_allowed(self, role, privilege, context=None, undef=None):
        """Returns True if active role is allowed

        for given privilege in given given context
        """
        try:
            return self._acl[context][role][privilege]
        except KeyError:
            return undef

class Acl(object):
    """Access control list."""

    active_role = None

    def __init__(self, backend_class=None):
        """Constructor."""
        if backend_class is None:
            backend_class = SimpleBackend
        self._backend = backend_class()
        self.add_privilege(ALL_PRIVILEGES)

    def add_role(self, name_or_instance, parents=None):
        """Adds a role to the ACL"""
        if isinstance(name_or_instance, bytes):
            name_or_instance = str(name_or_instance)
        if isinstance(name_or_instance, str):
            instance = self._backend.role_class(name_or_instance)
        elif isinstance(name_or_instance, self._backend.role_class):
            instance = name_or_instance
        else:
            raise Exception(
                'Unable to add a role of type: {0}'\
                    .format(type(name_or_instance).__name__)
            )
        self._backend.add_role(instance)

        # Parents support for roles
        if parents is not None:
            for parent in parents:
                parent = self.add_role(parent)
                instance.add_parent(parent)

        # Hierarchical support for roles
        if '.' in instance.get_name():
            parent = instance.get_name().rsplit('.', 1).pop(0)
            parent = self.add_role(parent)  # Recursive
        return instance

    def get_role(self, name_or_instance):
        """Returns the identified role instance"""
        if isinstance(name_or_instance, self._backend.role_class):
            return name_or_instance
        return self._backend.get_role(name_or_instance)

    def add_privilege(self, name_or_instance):
        """Adds a privilege to the ACL"""
        if isinstance(name_or_instance, bytes):
            name_or_instance = str(name_or_instance)
        if isinstance(name_or_instance, str):
            instance = self._backend.privilege_class(name_or_instance)
        elif isinstance(name_or_instance, self._backend.privilege_class):
            instance = name_or_instance
        else:
            raise Exception(
                'Unable to add a privilege of type: {0}'\
                    .format(type(name_or_instance).__name__)
            )
        self._backend.add_privilege(instance)

        # Hierarchical support for instances
        if '.' in instance.get_name():
            parent = instance.get_name().rsplit('.', 1).pop(0)
            parent = self.add_instance(parent)  # Recursive
        return instance

    def get_privilege(self, name_or_instance):
        """Returns the identified privilege instance"""
        if isinstance(name_or_instance, bytes):
            name_or_instance = str(name_or_instance)
        if isinstance(name_or_instance, str):
            return self._backend.get_privilege(name_or_instance)
        if isinstance(name_or_instance, self._backend.privilege_class):
            return name_or_instance
        raise Exception(
            'Unable to get a Privelege of type: {0}'\
                .format(type(name_or_instance).__name__)
        )

    def add_rule(self, role, privileges=ALL_PRIVILEGES,
                 context=None, allow=True):
        """Adds rule to the ACL"""
        if not hasattr(privileges, '__iter__'):
            privileges = (privileges, )
        for priv in privileges:
            self._backend.add_rule(
                self.get_role(role), self.get_privilege(priv), context, allow
            )
        return self

    def remove_rule(self, role, privileges=ALL_PRIVILEGES,
                    context=None, allow=True):
        """Removes rule from ACL"""
        if not hasattr(privileges, '__iter__'):
            privileges = (privileges, )
        for priv in privileges:
            self._backend.remove_rule(
                self.get_role(role), self.get_privilege(priv), context, allow
            )
        return self

    def allow(self, role, privileges=ALL_PRIVILEGES, context=None):
        """Adds an "allow" rule to the ACL"""
        return self.add_rule(role, privileges, context, True)

    def remove_allow(self, role, privileges=ALL_PRIVILEGES, context=None):
        """Removes an "allow" rule from the ACL"""
        return self.remove_rule(role, privileges, context, True)

    def deny(self, role, privileges=ALL_PRIVILEGES, context=None):
        """Adds a "deny" rule to the ACL"""
        return self.add_rule(role, privileges, context, False)

    def remove_deny(self, role, privileges=ALL_PRIVILEGES, context=None):
        """Removes a "deny" rule from the ACL"""
        return self.remove_rule(role, privileges, context, False)

    def role_has_privilege(self, role, privilege, context=None, allow=True):
        """Returns True if role has privilege"""
        try:
            return self._backend.role_has_privilege(
                self.get_role(role), self.get_privilege(privilege),
                context, allow
            )
        except MissingPrivilege:
            return False

    def active_role_is(self, role):
        """Sets active role"""
        self.active_role = self.get_role(role)
        return self

    def set_active_role(self, role):
        """Just alias for self.active_role_is()"""
        return self.active_role_is(role)

    def is_allowed(self, privilege, context=None, undef=False):
        """Returns True if active role is allowed

        for given privilege in given given context
        """
        if not self.active_role:
            raise MissingActiveRole(
                "A role must be set active before checking permissions"
            )

        role = self.active_role
        privilege = self.get_privilege(privilege)

        allow = self._backend.is_allowed(role, privilege, context, None)
        if allow is not None:
            return allow

        allow = self._backend.is_allowed(
            role, self.get_privilege(ALL_PRIVILEGES), context, None
        )
        if allow is not None:
            return allow

        # Parents support for roles
        for parent in role.get_parents():
            self.active_role_is(parent)
            allow = self.is_allowed(privilege, context, None)
            if allow is not None:
                return allow

        # Hierarchical support for roles
        if '.' in role.get_name():
            parent = self.get_role(role.get_name().rsplit('.', 1).pop(0))
            self.active_role_is(parent)
            allow = self.is_allowed(privilege, context, None)
            if allow is not None:
                return allow

        self.active_role_is(role)

        # Hierarchical support for privileges
        if '.' in privilege.get_name():
            parent = self.get_privilege(
                privilege.get_name().rsplit('.', 1).pop(0)
            )
            allow = self.is_allowed(parent, context, None)
            if allow is not None:
                return allow

        # Parents support for context
        if hasattr(context, 'get_parents'):
            for parent in context.get_parents():
                allow = self.is_allowed(privilege, parent, None)
                if allow is not None:
                    return allow

        return undef

    def bulk_load(self, json_or_dict, context=None):
        """You can store your roles, privileges and allow list (many to many)
        in a json encoded string and pass it into this method to build
        the object without having to call add_role or add_privilege for each
        one. TODO: make better documentation for this method.
        """
        if isinstance(json_or_dict, bytes):
            json_or_dict = str(json_or_dict)
        if isinstance(json_or_dict, str):
            clean = json.loads(json_or_dict)
        else:
            clean = json_or_dict

        if 'roles' in clean:
            for value in clean['roles']:
                if hasattr(value, '__iter__'):
                    self.add_role(*value)
                elif isinstance(value, dict):
                    self.add_role(**value)
                else:
                    self.add_role(value)

        if 'privileges' in clean:
            for value in clean['privileges']:
                self.add_privilege(value)

        if 'acl' in clean:
            for row in clean['acl']:
                self.allow(row['role'], row['privilege'],
                           context, row['allow'])
        return self

    @classmethod
    def create_instance(cls, json_or_dict):
        """You can store your roles, privileges and allow list (many to many)
        in a json encoded string and pass it into this method to build
        the object without having to call add_role or add_privilege for each
        one. TODO: make better documentation for this method.
        """
        obj = cls()
        obj.bulk_load(json_or_dict)
        return obj

# Python 2.* compatible
try:
    unicode
except NameError:
    pass
else:
    Role.__unicode__ = Role.__str__
    Role.__str__ = Role.__bytes__

    Privilege.__unicode__ = Role.__str__
    Privilege.__str__ = Role.__bytes__
<commit_msg>Fixed minor bug<commit_after>#######################################################################
# Simpleacl - A small access control list
# Copyright (C) 2010  Ivan Zakrevsky <ivzak [at] yandex [dot] ru>
# Copyright (C) 2010  Kyle Terry <kyle [at] fiverlabs [dot] com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#######################################################################
from __future__ import absolute_import, unicode_literals
try:
    import simplejson as json
except:
    import json

from simpleacl.exceptions import MissingRole, MissingActiveRole,\
    MissingPrivilege

try:
    str = unicode  # Python 2.* compatible
except NameError:
    pass

ALL_PRIVILEGES = 'all'

class Role(object):
    """Holds a role value"""

    _parents = []  # Order is important, so use the list(), not set

    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name.__eq__(getattr(other, 'name', other))

    def __ne__(self, other):
        return self.name.__ne__(getattr(other, 'name', other))

    def __hash__(self):
        return self.name.__hash__()

    def __bytes__(self):
        return str(self.name).encode('utf-8')

    def __str__(self):
        return str(self.name)

    def get_name(self):
        return self.name

    def add_parent(self, parent):
        if parent not in self._parents:
            self._parents.append(parent)

    def get_parents(self):
        return self._parents

class Privilege(object):
    """Holds a privilege value"""

    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name.__eq__(getattr(other, 'name', other))

    def __ne__(self, other):
        return self.name.__ne__(getattr(other, 'name', other))

    def __hash__(self):
        return self.name.__hash__()

    def __bytes__(self):
        return str(self.name).encode('utf-8')

    def __str__(self):
        return str(self.name)

    def get_name(self):
        return self.name

class Context(object):
    """Holder for context value.

    Sometimes apposite for usege as context wrapper."""

    _parents = []  # Order is important, so use the list(), not set

    def __init__(self, base):
        self.base = base

    def __eq__(self, other):
        return self.base.__eq__(getattr(other, 'base', other))

    def __ne__(self, other):
        return self.base.__ne__(getattr(other, 'base', other))

    def __hash__(self):
        return self.base.__hash__()

    def add_parent(self, parent):
        if parent not in self._parents:
            self._parents.append(parent)

    def get_parents(self):
        return self._parents

class SimpleBackend(object):
    """A simple storage."""
    _roles = None
    _privileges = None
    _acl = None
    role_class = Role
    privilege_class = Privilege

    def __init__(self):
        """Constructor."""
        self._roles = {}
        self._privileges = {}
        self._acl = {}

    def add_role(self, role, parents=None):
        """Adds role"""
        self._roles.setdefault(role.get_name(), role)
        return self

    def get_role(self, role_name):
        """Returns a role instance"""
        try:
            return self._roles[role_name]
        except KeyError:
            raise MissingRole(
                'Role must be added before requested.'
            )

    def add_privilege(self, privilege):
        """Adds privilege"""
        self._privileges.setdefault(privilege.get_name(), privilege)
        return self

    def get_privilege(self, privilege_name):
        """Returns a privilege instance"""
        try:
            return self._privileges[privilege_name]
        except KeyError:
            raise MissingPrivilege(
                'Privilege must be added before requested.'
            )

    def add_rule(self, role, privilege=ALL_PRIVILEGES,
                 context=None, allow=True):
        """Adds rule to the ACL"""
        acl = self._acl.setdefault(context, {})
        role_rules = acl.setdefault(role, {})
        role_rules[privilege] = allow
        return self

    def remove_rule(self, role, privilege=ALL_PRIVILEGES,
                    context=None, allow=True):
        """Removes rule from ACL"""
        try:
            if self._acl[context][role][privilege] == allow:
                del self._acl[context][role][privilege]
        except KeyError:
            pass
        return self

    def role_has_privilege(self, role, privilege, context=None, allow=True):
        """Removes rule from ACL"""
        try:
            return self._acl[context][role][privilege] == allow
        except KeyError:
            return False

    def is_allowed(self, role, privilege, context=None, undef=None):
        """Returns True if active role is allowed

        for given privilege in given given context
        """
        try:
            return self._acl[context][role][privilege]
        except KeyError:
            return undef

class Acl(object):
    """Access control list."""

    active_role = None

    def __init__(self, backend_class=None):
        """Constructor."""
        if backend_class is None:
            backend_class = SimpleBackend
        self._backend = backend_class()
        self.add_privilege(ALL_PRIVILEGES)

    def add_role(self, name_or_instance, parents=None):
        """Adds a role to the ACL"""
        if isinstance(name_or_instance, bytes):
            name_or_instance = str(name_or_instance)
        if isinstance(name_or_instance, str):
            instance = self._backend.role_class(name_or_instance)
        elif isinstance(name_or_instance, self._backend.role_class):
            instance = name_or_instance
        else:
            raise Exception(
                'Unable to add a role of type: {0}'\
                    .format(type(name_or_instance).__name__)
            )
        self._backend.add_role(instance)

        # Parents support for roles
        if parents is not None:
            for parent in parents:
                parent = self.add_role(parent)
                instance.add_parent(parent)

        # Hierarchical support for roles
        if '.' in instance.get_name():
            parent = instance.get_name().rsplit('.', 1).pop(0)
            parent = self.add_role(parent)  # Recursive
        return instance

    def get_role(self, name_or_instance):
        """Returns the identified role instance"""
        if isinstance(name_or_instance, self._backend.role_class):
            return name_or_instance
        return self._backend.get_role(name_or_instance)

    def add_privilege(self, name_or_instance):
        """Adds a privilege to the ACL"""
        if isinstance(name_or_instance, bytes):
            name_or_instance = str(name_or_instance)
        if isinstance(name_or_instance, str):
            instance = self._backend.privilege_class(name_or_instance)
        elif isinstance(name_or_instance, self._backend.privilege_class):
            instance = name_or_instance
        else:
            raise Exception(
                'Unable to add a privilege of type: {0}'\
                    .format(type(name_or_instance).__name__)
            )
        self._backend.add_privilege(instance)

        # Hierarchical support for instances
        if '.' in instance.get_name():
            parent = instance.get_name().rsplit('.', 1).pop(0)
            parent = self.add_instance(parent)  # Recursive
        return instance

    def get_privilege(self, name_or_instance):
        """Returns the identified privilege instance"""
        if isinstance(name_or_instance, bytes):
            name_or_instance = str(name_or_instance)
        if isinstance(name_or_instance, str):
            return self._backend.get_privilege(name_or_instance)
        if isinstance(name_or_instance, self._backend.privilege_class):
            return name_or_instance
        raise Exception(
            'Unable to get a Privelege of type: {0}'\
                .format(type(name_or_instance).__name__)
        )

    def add_rule(self, role, privileges=ALL_PRIVILEGES,
                 context=None, allow=True):
        """Adds rule to the ACL"""
        if not hasattr(privileges, '__iter__'):
            privileges = (privileges, )
        for priv in privileges:
            self._backend.add_rule(
                self.get_role(role), self.get_privilege(priv), context, allow
            )
        return self

    def remove_rule(self, role, privileges=ALL_PRIVILEGES,
                    context=None, allow=True):
        """Removes rule from ACL"""
        if not hasattr(privileges, '__iter__'):
            privileges = (privileges, )
        for priv in privileges:
            self._backend.remove_rule(
                self.get_role(role), self.get_privilege(priv), context, allow
            )
        return self

    def allow(self, role, privileges=ALL_PRIVILEGES, context=None):
        """Adds an "allow" rule to the ACL"""
        return self.add_rule(role, privileges, context, True)

    def remove_allow(self, role, privileges=ALL_PRIVILEGES, context=None):
        """Removes an "allow" rule from the ACL"""
        return self.remove_rule(role, privileges, context, True)

    def deny(self, role, privileges=ALL_PRIVILEGES, context=None):
        """Adds a "deny" rule to the ACL"""
        return self.add_rule(role, privileges, context, False)

    def remove_deny(self, role, privileges=ALL_PRIVILEGES, context=None):
        """Removes a "deny" rule from the ACL"""
        return self.remove_rule(role, privileges, context, False)

    def role_has_privilege(self, role, privilege, context=None, allow=True):
        """Returns True if role has privilege"""
        try:
            return self._backend.role_has_privilege(
                self.get_role(role), self.get_privilege(privilege),
                context, allow
            )
        except MissingPrivilege:
            return False

    def active_role_is(self, role):
        """Sets active role"""
        self.active_role = self.get_role(role)
        return self

    def set_active_role(self, role):
        """Just alias for self.active_role_is()"""
        return self.active_role_is(role)

    def is_allowed(self, privilege, context=None, undef=False):
        """Returns True if active role is allowed

        for given privilege in given given context
        """
        if not self.active_role:
            raise MissingActiveRole(
                "A role must be set active before checking permissions"
            )

        role = self.active_role
        privilege = self.get_privilege(privilege)

        allow = self._backend.is_allowed(role, privilege, context, None)
        if allow is not None:
            return allow

        allow = self._backend.is_allowed(
            role, self.get_privilege(ALL_PRIVILEGES), context, None
        )
        if allow is not None:
            return allow

        # Parents support for roles
        for parent in role.get_parents():
            self.active_role_is(parent)
            allow = self.is_allowed(privilege, context, None)
            if allow is not None:
                return allow

        # Hierarchical support for roles
        if '.' in role.get_name():
            parent = self.get_role(role.get_name().rsplit('.', 1).pop(0))
            self.active_role_is(parent)
            allow = self.is_allowed(privilege, context, None)
            if allow is not None:
                return allow

        self.active_role_is(role)

        # Hierarchical support for privileges
        if '.' in privilege.get_name():
            parent = self.get_privilege(
                privilege.get_name().rsplit('.', 1).pop(0)
            )
            allow = self.is_allowed(parent, context, None)
            if allow is not None:
                return allow

        # Parents support for context
        if hasattr(context, 'get_parents'):
            for parent in context.get_parents():
                allow = self.is_allowed(privilege, parent, None)
                if allow is not None:
                    return allow

        return undef

    def bulk_load(self, json_or_dict, context=None):
        """You can store your roles, privileges and allow list (many to many)
        in a json encoded string and pass it into this method to build
        the object without having to call add_role or add_privilege for each
        one. TODO: make better documentation for this method.
        """
        if isinstance(json_or_dict, bytes):
            json_or_dict = str(json_or_dict)
        if isinstance(json_or_dict, str):
            clean = json.loads(json_or_dict)
        else:
            clean = json_or_dict

        if 'roles' in clean:
            for value in clean['roles']:
                if hasattr(value, '__iter__'):
                    self.add_role(*value)
                elif isinstance(value, dict):
                    self.add_role(**value)
                else:
                    self.add_role(value)

        if 'privileges' in clean:
            for value in clean['privileges']:
                self.add_privilege(value)

        if 'acl' in clean:
            for row in clean['acl']:
                self.allow(row['role'], row['privilege'],
                           context, row['allow'])
        return self

    @classmethod
    def create_instance(cls, json_or_dict):
        """You can store your roles, privileges and allow list (many to many)
        in a json encoded string and pass it into this method to build
        the object without having to call add_role or add_privilege for each
        one. TODO: make better documentation for this method.
        """
        obj = cls()
        obj.bulk_load(json_or_dict)
        return obj

# Python 2.* compatible
try:
    unicode
except NameError:
    pass
else:
    Role.__unicode__ = Role.__str__
    Role.__str__ = Role.__bytes__

    Privilege.__unicode__ = Privilege.__str__
    Privilege.__str__ = Privilege.__bytes__
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from random import randint
from time import sleep

from django.db import transaction
from django.core.urlresolvers import reverse
from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView

from braces.views import LoginRequiredMixin

from .models import SEMScan
from .forms import DropzoneForm
from .response import JSONResponse, response_mimetype

class SEMList(LoginRequiredMixin, ListView):
    """
    List the most recent sem data
    """
    model = SEMScan
    template_name = 'sem/sem_list.html'
    paginate_by = 25

class SEMDetail(LoginRequiredMixin, DetailView):
    """
    Detail view of the sem model.
    """
    model = SEMScan
    template_name = 'sem/sem_detail.html'

    def get_context_data(self, **kwargs):
        context = super(SEMDetail, self).get_context_data(**kwargs)
        context['sample_siblings'] = []
        context['pocket_siblings'] = []
        context['growth_siblings'] = []
        return context

class SEMCreate(LoginRequiredMixin, CreateView):
    """
    View for creation of new sem data.
    """
    model = SEMScan
    template_name = 'sem/sem_create.html'

#@transaction.atomic
class SEMUpload(LoginRequiredMixin, CreateView):
    """
    View for creation of new sem data.
    """
    model = SEMScan
    template_name = 'sem/sem_upload.html'
    form_class = DropzoneForm

    #def post(self, request, *args, **kwargs):
    #    form_class = self.get_form_class()
    #    form = self.get_form(form_class)
    #    if form.is_valid():
    #        with transaction.atomic():
    #            return self.form_valid(form)
    #    else:
    #        return self.form_invalid(form)

    def form_valid(self, form):
        #self.object = form.save(commit=False)
        #self.object = SEMScan()
        #SEMScan.objects.bulk_create([SEMScan(image_number=0,
        #                                     image_source='esem_600',
        #                                     image=i) for i in self.request.FILES.getlist('file')])

        #self.object.image_source = 'esem_600'
        #self.object.image_number = 0
        self.object = form.save(commit=False)
        self.object.image_source = 'esem_600'
        self.object.image_number = 0
        self.object.save()
        #SEMScan.save()
        #sleep(0.001*randint(1,99))
        #self.object.save()
        data = {'status': 'success'}
        response = JSONResponse(data, mimetype=response_mimetype(self.request))
        return response

class SEMUpdate(LoginRequiredMixin, UpdateView):
    """
    View for updating sem data.
    """
    model = SEMScan
    template_name = 'sem/sem_update.html'

class SEMDelete(LoginRequiredMixin, DeleteView):
    """
    View for deleting sem data
    """
    model = SEMScan
    template_name = 'sem/sem_delete.html'

    def get_success_url(self):
        return reverse('sem_list')
<commit_msg>Fixed to use atomic for uploads<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from random import randint
from time import sleep

from django.db import transaction
from django.core.urlresolvers import reverse
from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView

from braces.views import LoginRequiredMixin

from .models import SEMScan
from .forms import DropzoneForm
from .response import JSONResponse, response_mimetype
from .image_helper import is_tiff, get_image_source

class SEMList(LoginRequiredMixin, ListView):
    """
    List the most recent sem data
    """
    model = SEMScan
    template_name = 'sem/sem_list.html'
    paginate_by = 25

class SEMDetail(LoginRequiredMixin, DetailView):
    """
    Detail view of the sem model.
    """
    model = SEMScan
    template_name = 'sem/sem_detail.html'

    def get_context_data(self, **kwargs):
        context = super(SEMDetail, self).get_context_data(**kwargs)
        context['sample_siblings'] = []
        context['pocket_siblings'] = []
        context['growth_siblings'] = []
        return context

class SEMCreate(LoginRequiredMixin, CreateView):
    """
    View for creation of new sem data.
    """
    model = SEMScan
    template_name = 'sem/sem_create.html'

class SEMUpload(LoginRequiredMixin, CreateView):
    """
    View for creation of new sem data.
    """
    model = SEMScan
    template_name = 'sem/sem_upload.html'
    form_class = DropzoneForm

    def form_valid(self, form):

        with transaction.atomic():

            obj = SEMScan.objects.create(image_source='esem_600',
                                   image_number=0,
                                   image=self.request.FILES['file'])
            obj.save()
        data = {'status': 'success'}
        response = JSONResponse(data, mimetype=response_mimetype(self.request))
        return response

class SEMUpdate(LoginRequiredMixin, UpdateView):
    """
    View for updating sem data.
    """
    model = SEMScan
    template_name = 'sem/sem_update.html'

class SEMDelete(LoginRequiredMixin, DeleteView):
    """
    View for deleting sem data
    """
    model = SEMScan
    template_name = 'sem/sem_delete.html'

    def get_success_url(self):
        return reverse('sem_list')
<|endoftext|>


<commit_before># Copyright 2015 Huawei Technologies Co., Ltd.
# 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.

from oslo_config import fixture as fixture_config
import oslo_messaging

from ceilometer.api import hooks
from ceilometer.tests import base

class TestTestNotifierHook(base.BaseTestCase):

    def setUp(self):
        super(TestTestNotifierHook, self).setUp()
        self.CONF = self.useFixture(fixture_config.Config()).conf

    def test_init_notifier_with_drivers(self):
        self.CONF.set_override('telemetry_driver', 'messagingv2',
                               group='publisher_notifier')
        hook = hooks.NotifierHook(self.CONF)
        notifier = hook.notifier
        self.assertIsInstance(notifier, oslo_messaging.Notifier)
        self.assertEqual(['messagingv2'], notifier._driver_names)
<commit_msg>Remove unused override<commit_after># Copyright 2015 Huawei Technologies Co., Ltd.
# 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.

from oslo_config import fixture as fixture_config
import oslo_messaging

from ceilometer.api import hooks
from ceilometer.tests import base

class TestTestNotifierHook(base.BaseTestCase):

    def setUp(self):
        super(TestTestNotifierHook, self).setUp()
        self.CONF = self.useFixture(fixture_config.Config()).conf

    def test_init_notifier_with_drivers(self):
        hook = hooks.NotifierHook(self.CONF)
        notifier = hook.notifier
        self.assertIsInstance(notifier, oslo_messaging.Notifier)
        self.assertEqual(['messagingv2'], notifier._driver_names)
<|endoftext|>


<commit_before>#!/usr/bin/python
# vim: set fileencoding=UTF-8 :
"""
dbcommand

Send and execute a command in database. Optionally get a result back. See usage.

NB! Depends on dboperator which takes care of connection and other db stuff.
"""
import sys,getopt
from time import localtime, strftime

import dboperator

def show(message):
  print strftime("%Y-%m-%d %H:%M:%S", localtime())+" "+message

def load(command,expect,verbose=False):
  if verbose: show("begin with "+command) # no point in trying to show expect!

  sql = command
  ret = None
  try:
    # quick hack for getting return value. do fix me!
    if expect:
      resql = dboperator.get(sql)
      if verbose: show("command returned %d rows"%(len(resql)))
      # print out (as a return value) the entire result or row count or what, some kind of figuring?
      if expect=='row count':
        ret = len(resql)
      elif len(resql)==1 and expect in resql[0]:
        ret = resql[0][expect]
      else:
        ret = resql
    else:
      dboperator.execute(sql)
  except Exception as e:
    print(e)
    show("Something went wrong. Over and out.")
    dboperator.close()
    exit(2) # lopeta virheeseen

  if verbose: show("ready")
  return ret

def usage():
  print """
usage: dbcommand.py -c|--command <string> [-e|--expect <string>] [-v|--verbose] [-r|--return]

command is mandatory argument. The SQL to execute.
expect  is for getting results from a query.
        For ease of use one can tell from one row return query the column which value is expected.
        Special case is "row count" which will just return the number of rows query returns.
        Somewhat default behaviour is to return the whole result set as an array of dicts.
        You can achieve default behaviour by giving a value to this argument that is not part
        of column names in result, for ex. "*", and/or results give exactly NOT one row.
return is an optional argument. By default the dbcommand prints the result to shell. With this
        paremeter the dbcommand returns the result to user.
"""

def main(argv):
  # variables that are given as arguments with possible default values
  command = ""
  expect = ""
  verbose = False
  return_wanted = False

  try:
    opts, args = getopt.getopt(argv,"c:e:v:r",["command=","expect=","verbose","return"])
  except getopt.GetoptError as err:
    print(err)
    usage()
    sys.exit(2)
  for opt, arg in opts:
    if opt in ("-c", "--command"): command = arg
    elif opt in ("-e", "--expect"): expect = arg
    elif opt in ("-v", "--verbose"): verbose = True
    elif opt in ("-r", "--return"): return_wanted = True
  if not command:
    usage()
    sys.exit(2)

  if expect:
    verbose = False # would interfere with "return" value
  ret = load(command,expect,verbose)
  if expect:
     if return_wanted:
         return ret
     else:
         print ret

  #dboperator.close()  this to be optional ?

if __name__ == "__main__":
  main(sys.argv[1:])
<commit_msg>show instead of print<commit_after>#!/usr/bin/python
# vim: set fileencoding=UTF-8 :
"""
dbcommand

Send and execute a command in database. Optionally get a result back. See usage.

NB! Depends on dboperator which takes care of connection and other db stuff.
"""
import sys,getopt
from time import localtime, strftime

import dboperator

def show(message):
  print strftime("%Y-%m-%d %H:%M:%S", localtime())+" "+message

def load(command,expect,verbose=False):
  if verbose: show("begin with "+command) # no point in trying to show expect!

  sql = command
  ret = None
  try:
    # quick hack for getting return value. do fix me!
    if expect:
      resql = dboperator.get(sql)
      if verbose: show("command returned %d rows"%(len(resql)))
      # print out (as a return value) the entire result or row count or what, some kind of figuring?
      if expect=='row count':
        ret = len(resql)
      elif len(resql)==1 and expect in resql[0]:
        ret = resql[0][expect]
      else:
        ret = resql
    else:
      dboperator.execute(sql)
  except Exception as e:
    show(e)
    show("Something went wrong. Over and out.")
    dboperator.close()
    exit(2) # lopeta virheeseen

  if verbose: show("ready")
  return ret

def usage():
  print """
usage: dbcommand.py -c|--command <string> [-e|--expect <string>] [-v|--verbose] [-r|--return]

command is mandatory argument. The SQL to execute.
expect  is for getting results from a query.
        For ease of use one can tell from one row return query the column which value is expected.
        Special case is "row count" which will just return the number of rows query returns.
        Somewhat default behaviour is to return the whole result set as an array of dicts.
        You can achieve default behaviour by giving a value to this argument that is not part
        of column names in result, for ex. "*", and/or results give exactly NOT one row.
return is an optional argument. By default the dbcommand prints the result to shell. With this
        paremeter the dbcommand returns the result to user.
"""

def main(argv):
  # variables that are given as arguments with possible default values
  command = ""
  expect = ""
  verbose = False
  return_wanted = False

  try:
    opts, args = getopt.getopt(argv,"c:e:v:r",["command=","expect=","verbose","return"])
  except getopt.GetoptError as err:
    print(err)
    usage()
    sys.exit(2)
  for opt, arg in opts:
    if opt in ("-c", "--command"): command = arg
    elif opt in ("-e", "--expect"): expect = arg
    elif opt in ("-v", "--verbose"): verbose = True
    elif opt in ("-r", "--return"): return_wanted = True
  if not command:
    usage()
    sys.exit(2)

  if expect:
    verbose = False # would interfere with "return" value
  ret = load(command,expect,verbose)
  if expect:
     if return_wanted:
         return ret
     else:
         print ret

  #dboperator.close()  this to be optional ?

if __name__ == "__main__":
  main(sys.argv[1:])
<|endoftext|>