<commit_before>from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin, UserPassesTestMixin
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy, reverse
from django.views.generic import DetailView, ListView, CreateView, UpdateView, DeleteView

from reservations.forms import QueueForm
from reservations.models import Queue, Reservation

class QueueDetailView(DetailView):
    model = Queue

class QueueListView(ListView):
    model = Queue
    queryset = Queue.objects.filter(published=True)

class UnpublishedQueueListView(QueueListView):
    queryset = Queue.objects.filter(published=False)

class QueueCreateView(PermissionRequiredMixin, CreateView):
    model = Queue
    redirect_field_name = '/'
    permission_required = 'reservations.add_queue'
    form_class = QueueForm
    success_url = reverse_lazy('reservations:queue_list')

class QueueUpdateView(PermissionRequiredMixin, UpdateView):
    model = Queue
    form_class = QueueForm
    permission_required = 'reservations.edit_queue'
    success_url = reverse_lazy('reservations:queue_list')
    redirect_field_name = '/'

class QueueDeleteView(PermissionRequiredMixin, DeleteView):
    model = Queue
    redirect_field_name = '/'
    permission_required = 'reservations.delete_queue'
    success_url = reverse_lazy('reservations:queue_list')

class ReservationCreateView(LoginRequiredMixin, CreateView):
    model = Reservation
    redirect_field_name = 'login/'
    fields = ('date', 'start_time', 'end_time')

class ReservationDeleteView(UserPassesTestMixin, DeleteView):
    model = Reservation
    redirect_field_name = 'login/'
    success_url = reverse_lazy('reservations:queue_list')

    def test_func(self):
        return self.request.user == get_object_or_404(Reservation, pk=self.kwargs['pk']).user \
               or self.request.user.has_perm('reservations.delete_queue')

class ReservationUpdateView(UserPassesTestMixin, UpdateView):
    model = Reservation
    redirect_field_name = 'login/'

    def get_success_url(self):
        return reverse(
            'reservations:queue_detail',
            kwargs={'pk': get_object_or_404(Reservation, pk=self.kwargs['pk']).parent_queue.pk}

        )

    def test_func(self):
        return self.request.user == get_object_or_404(Reservation, pk=self.kwargs['pk']).user \
               or self.request.user.has_perm('reservations.edit_reservation')

<commit_msg>removed unnecessary newline<commit_after>from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin, UserPassesTestMixin
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy, reverse
from django.views.generic import DetailView, ListView, CreateView, UpdateView, DeleteView

from reservations.forms import QueueForm
from reservations.models import Queue, Reservation

class QueueDetailView(DetailView):
    model = Queue

class QueueListView(ListView):
    model = Queue
    queryset = Queue.objects.filter(published=True)

class UnpublishedQueueListView(QueueListView):
    queryset = Queue.objects.filter(published=False)

class QueueCreateView(PermissionRequiredMixin, CreateView):
    model = Queue
    redirect_field_name = '/'
    permission_required = 'reservations.add_queue'
    form_class = QueueForm
    success_url = reverse_lazy('reservations:queue_list')

class QueueUpdateView(PermissionRequiredMixin, UpdateView):
    model = Queue
    form_class = QueueForm
    permission_required = 'reservations.edit_queue'
    success_url = reverse_lazy('reservations:queue_list')
    redirect_field_name = '/'

class QueueDeleteView(PermissionRequiredMixin, DeleteView):
    model = Queue
    redirect_field_name = '/'
    permission_required = 'reservations.delete_queue'
    success_url = reverse_lazy('reservations:queue_list')

class ReservationCreateView(LoginRequiredMixin, CreateView):
    model = Reservation
    redirect_field_name = 'login/'
    fields = ('date', 'start_time', 'end_time')

class ReservationDeleteView(UserPassesTestMixin, DeleteView):
    model = Reservation
    redirect_field_name = 'login/'
    success_url = reverse_lazy('reservations:queue_list')

    def test_func(self):
        return self.request.user == get_object_or_404(Reservation, pk=self.kwargs['pk']).user \
               or self.request.user.has_perm('reservations.delete_queue')

class ReservationUpdateView(UserPassesTestMixin, UpdateView):
    model = Reservation
    redirect_field_name = 'login/'

    def get_success_url(self):
        return reverse(
            'reservations:queue_detail',
            kwargs={'pk': get_object_or_404(Reservation, pk=self.kwargs['pk']).parent_queue.pk}
        )

    def test_func(self):
        return self.request.user == get_object_or_404(Reservation, pk=self.kwargs['pk']).user \
               or self.request.user.has_perm('reservations.edit_reservation')

<|endoftext|>


<commit_before>import os
import logging
import shutil
from contextlib import contextmanager
from typing import Union

from smac.facade.smac_facade import SMAC
from smac.optimizer.objective import average_cost
from smac.utils.io.input_reader import InputReader
from smac.runhistory.runhistory import RunKey, RunValue, RunHistory
from smac.scenario.scenario import Scenario
from smac.utils.io.traj_logging import TrajLogger
from smac.utils.validate import Validator
from smac.optimizer.objective import _cost

@contextmanager
def changedir(newdir):
    olddir = os.getcwd()
    os.chdir(os.path.expanduser(newdir))
    try:
        yield
    finally:
        os.chdir(olddir)

class SMACrun(SMAC):
    """
    SMACrun keeps all information on a specific SMAC run. Extends the standard
    SMAC-facade.
    """
    def __init__(self, folder: str, ta_exec_dir: Union[str, None]=None):
        """Initialize scenario, runhistory and incumbent from folder, execute
        init-method of SMAC facade (so you could simply use SMAC-instances instead)

        Parameters
        ----------
        folder: string
            output-dir of this run
        ta_exec_dir: string
            if the execution directory for the SMAC-run differs from the cwd,
            there might be problems loading instance-, feature- or PCS-files
            in the scenario-object. since instance- and PCS-files are necessary,
            specify the path to the execution-dir of SMAC here
        """
        run_1_existed = os.path.exists('run_1')
        self.logger = logging.getLogger("cave.SMACrun.{}".format(folder))
        in_reader = InputReader()

        self.folder = folder
        self.logger.debug("Loading from %s", folder)

        split_folder = os.path.split(folder)
        self.logger.info(split_folder)
        if ta_exec_dir is None:
            ta_exec_dir = '.'

        self.scen_fn = os.path.join(folder, 'scenario.txt')
        self.rh_fn = os.path.join(folder, 'runhistory.json')
        self.validated_rh_fn = os.path.join(folder, 'validated_runhistory.json')
        self.traj_fn = os.path.join(folder, 'traj_aclib2.json')
        self.traj_old_fn = os.path.join(folder, 'traj_old.csv')

        # Create Scenario (disable output_dir to avoid cluttering)
        scen_dict = in_reader.read_scenario_file(self.scen_fn)
        scen_dict['output_dir'] = ""
        with changedir(ta_exec_dir):
            self.scen = Scenario(scen_dict)

        # Load trajectory
        self.traj = TrajLogger.read_traj_aclib_format(fn=self.traj_fn,
                                                      cs=self.scen.cs)
        self.default = self.scen.cs.get_default_configuration
        self.incumbent = self.traj[-1]['incumbent']
        self.train_inst = self.scen.train_insts
        self.test_inst = self.scen.test_insts

        # Load runhistory
        self.original_runhistory = RunHistory(average_cost)
        self.original_runhistory.update_from_json(self.rh_fn, self.scen.cs)
        if os.path.exists(self.validated_rh_fn):
            self.validated_runhistory = RunHistory(average_cost)
            self.validated_runhistory.update_from_json(self.validated_rh_fn, self.scen.cs)
            self.logger.debug("Found validated runhistory for \"%s\" and using "
                              "it for evaluation. #configs in validated rh: %d",
                              self.folder, len(self.validated_runhistory.config_ids))
            # Check validated runhistory for completeness
            if not self._check_rh_for_inc_and_def(self.validated_runhistory):
                self.validated_runhistory = False
        else:
            self.logger.debug("No validated runhistory for \"%s\" found (probably ok)")
            self.validated_runhistory = False
        self.combined_runhistory = RunHistory(average_cost)
        self.combined_runhistory.update(self.original_runhistory)
        if self.validated_runhistory:
            self.combined_runhistory.update(self.validated_runhistory)

        # for config in self.traj:
        #     time = config['wallclock_time']
        #     config = config['incumbent']
        #     c = _cost(config, self.runhistory, self.runhistory.get_runs_for_config(config))
        #     print(len(c), time)

        # Initialize SMAC-object
        super().__init__(scenario=self.scen, runhistory=self.combined_runhistory)
                #restore_incumbent=incumbent)
        # TODO use restore, delete next line
        self.solver.incumbent = self.incumbent

        if (not run_1_existed) and os.path.exists('run_1'):
            shutil.rmtree('run_1')

    def get_incumbent(self):
        return self.solver.incumbent

    def _check_rh_for_inc_and_def(self, rh):
        """
        Check if default and incumbent are evaluated on all instances in this rh

        Returns
        -------
        return_value: bool
            False if either inc or def was not evaluated on all
            train/test-instances
        """
        return_value = True
        for c_name, c in [("default", self.default), ("inc", self.incumbent)]:
            runs = rh.get_runs_for_config(c)
            evaluated = set([inst for inst, seed in runs])
            for i_name, i in [("train", self.train_inst),
                              ("test", self.test_inst)]:
                not_evaluated = set(i) - evaluated
                if len(not_evaluated) > 0:
                    self.logger.warning("RunHistory only evaluated on %d/%d %s-insts "
                                        "for %s in folder %s",
                                        len(i) - len(not_evaluated), len(i),
                                        i_name, c_name, self.folder)
                    return_value = False
        return return_value
<commit_msg>FIX default recognition in configurator-run<commit_after>import os
import logging
import shutil
from contextlib import contextmanager
from typing import Union

from smac.facade.smac_facade import SMAC
from smac.optimizer.objective import average_cost
from smac.utils.io.input_reader import InputReader
from smac.runhistory.runhistory import RunKey, RunValue, RunHistory
from smac.scenario.scenario import Scenario
from smac.utils.io.traj_logging import TrajLogger
from smac.utils.validate import Validator
from smac.optimizer.objective import _cost

@contextmanager
def changedir(newdir):
    olddir = os.getcwd()
    os.chdir(os.path.expanduser(newdir))
    try:
        yield
    finally:
        os.chdir(olddir)

class SMACrun(SMAC):
    """
    SMACrun keeps all information on a specific SMAC run. Extends the standard
    SMAC-facade.
    """
    def __init__(self, folder: str, ta_exec_dir: Union[str, None]=None):
        """Initialize scenario, runhistory and incumbent from folder, execute
        init-method of SMAC facade (so you could simply use SMAC-instances instead)

        Parameters
        ----------
        folder: string
            output-dir of this run
        ta_exec_dir: string
            if the execution directory for the SMAC-run differs from the cwd,
            there might be problems loading instance-, feature- or PCS-files
            in the scenario-object. since instance- and PCS-files are necessary,
            specify the path to the execution-dir of SMAC here
        """
        run_1_existed = os.path.exists('run_1')
        self.logger = logging.getLogger("cave.SMACrun.{}".format(folder))
        in_reader = InputReader()

        self.folder = folder
        self.logger.debug("Loading from %s", folder)

        split_folder = os.path.split(folder)
        self.logger.info(split_folder)
        if ta_exec_dir is None:
            ta_exec_dir = '.'

        self.scen_fn = os.path.join(folder, 'scenario.txt')
        self.rh_fn = os.path.join(folder, 'runhistory.json')
        self.validated_rh_fn = os.path.join(folder, 'validated_runhistory.json')
        self.traj_fn = os.path.join(folder, 'traj_aclib2.json')
        self.traj_old_fn = os.path.join(folder, 'traj_old.csv')

        # Create Scenario (disable output_dir to avoid cluttering)
        scen_dict = in_reader.read_scenario_file(self.scen_fn)
        scen_dict['output_dir'] = ""
        with changedir(ta_exec_dir):
            self.scen = Scenario(scen_dict)

        # Load trajectory
        self.traj = TrajLogger.read_traj_aclib_format(fn=self.traj_fn,
                                                      cs=self.scen.cs)
        self.default = self.scen.cs.get_default_configuration()
        self.incumbent = self.traj[-1]['incumbent']
        self.train_inst = self.scen.train_insts
        self.test_inst = self.scen.test_insts

        # Load runhistory
        self.original_runhistory = RunHistory(average_cost)
        self.original_runhistory.update_from_json(self.rh_fn, self.scen.cs)
        if os.path.exists(self.validated_rh_fn):
            self.validated_runhistory = RunHistory(average_cost)
            self.validated_runhistory.update_from_json(self.validated_rh_fn, self.scen.cs)
            self.logger.debug("Found validated runhistory for \"%s\" and using "
                              "it for evaluation. #configs in validated rh: %d",
                              self.folder, len(self.validated_runhistory.config_ids))
            # Check validated runhistory for completeness
            if not self._check_rh_for_inc_and_def(self.validated_runhistory):
                self.validated_runhistory = False
        else:
            self.logger.debug("No validated runhistory for \"%s\" found (probably ok)")
            self.validated_runhistory = False
        self.combined_runhistory = RunHistory(average_cost)
        self.combined_runhistory.update(self.original_runhistory)
        if self.validated_runhistory:
            self.combined_runhistory.update(self.validated_runhistory)

        # for config in self.traj:
        #     time = config['wallclock_time']
        #     config = config['incumbent']
        #     c = _cost(config, self.runhistory, self.runhistory.get_runs_for_config(config))
        #     print(len(c), time)

        # Initialize SMAC-object
        super().__init__(scenario=self.scen, runhistory=self.combined_runhistory)
                #restore_incumbent=incumbent)
        # TODO use restore, delete next line
        self.solver.incumbent = self.incumbent

        if (not run_1_existed) and os.path.exists('run_1'):
            shutil.rmtree('run_1')

    def get_incumbent(self):
        return self.solver.incumbent

    def _check_rh_for_inc_and_def(self, rh):
        """
        Check if default and incumbent are evaluated on all instances in this rh

        Returns
        -------
        return_value: bool
            False if either inc or def was not evaluated on all
            train/test-instances
        """
        return_value = True
        for c_name, c in [("default", self.default), ("inc", self.incumbent)]:
            runs = rh.get_runs_for_config(c)
            evaluated = set([inst for inst, seed in runs])
            for i_name, i in [("train", self.train_inst),
                              ("test", self.test_inst)]:
                not_evaluated = set(i) - evaluated
                if len(not_evaluated) > 0:
                    self.logger.warning("RunHistory only evaluated on %d/%d %s-insts "
                                        "for %s in folder %s",
                                        len(i) - len(not_evaluated), len(i),
                                        i_name, c_name, self.folder)
                    return_value = False
        return return_value
<|endoftext|>


<commit_before>########
# Copyright (c) 2013 GigaSpaces Technologies 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.

import os

from setuptools import setup

install_requires = [
    'Flask==0.10.1',
    'flask-restful==0.2.5',
    'flask-restful-swagger==0.12',
    'flask-sqlalchemy==2.1',
    'flask-security==1.7.5',
    'supervise==1.1.1',
    'cloudify-dsl-parser==4.0rc1',
    'requests==2.7.0',
    'gunicorn==18.0',
    'PyYAML==3.10',
    'elasticsearch==1.6.0',
    'celery==3.1.17',
    'psutil==3.3.0',
    'jsonpickle==0.9.2',
    'wagon==0.3.2',
    'python-dateutil==2.5.3',
    'voluptuous==0.9.3',
    'toolz==0.8.2',
]

if os.environ.get('REST_SERVICE_BUILD'):
    # since psycopg2 installation require postgres,
    # we're adding it only to the build process,
    # where we know there is postgresql..
    # tests will use pg8000, which doesn't require postgres
    install_requires.append('psycopg2==2.6.2')

setup(
    name='cloudify-rest-service',
    version='4.0rc1',
    author='Gigaspaces',
    author_email='cosmo-admin@gigaspaces.com',
    packages=[
        'manager_rest',
        'manager_rest.rest',
        'manager_rest.rest.resources_v1',
        'manager_rest.rest.resources_v2',
        'manager_rest.deployment_update',
        'manager_rest.storage',
        'manager_rest.security',
    ],
    package_data={'manager_rest': ['VERSION']},
    license='LICENSE',
    description='Cloudify manager rest service',
    zip_safe=False,
    install_requires=install_requires)
<commit_msg>CFY-6475 change requests version range to >=2.7,<3.0 (#756)<commit_after>########
# Copyright (c) 2013 GigaSpaces Technologies 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.

import os

from setuptools import setup

install_requires = [
    'Flask==0.10.1',
    'flask-restful==0.2.5',
    'flask-restful-swagger==0.12',
    'flask-sqlalchemy==2.1',
    'flask-security==1.7.5',
    'supervise==1.1.1',
    'cloudify-dsl-parser==4.0rc1',
    'requests>=2.7.0,<3.0.0',
    'gunicorn==18.0',
    'PyYAML==3.10',
    'elasticsearch==1.6.0',
    'celery==3.1.17',
    'psutil==3.3.0',
    'jsonpickle==0.9.2',
    'wagon==0.3.2',
    'python-dateutil==2.5.3',
    'voluptuous==0.9.3',
    'toolz==0.8.2',
]

if os.environ.get('REST_SERVICE_BUILD'):
    # since psycopg2 installation require postgres,
    # we're adding it only to the build process,
    # where we know there is postgresql..
    # tests will use pg8000, which doesn't require postgres
    install_requires.append('psycopg2==2.6.2')

setup(
    name='cloudify-rest-service',
    version='4.0rc1',
    author='Gigaspaces',
    author_email='cosmo-admin@gigaspaces.com',
    packages=[
        'manager_rest',
        'manager_rest.rest',
        'manager_rest.rest.resources_v1',
        'manager_rest.rest.resources_v2',
        'manager_rest.deployment_update',
        'manager_rest.storage',
        'manager_rest.security',
    ],
    package_data={'manager_rest': ['VERSION']},
    license='LICENSE',
    description='Cloudify manager rest service',
    zip_safe=False,
    install_requires=install_requires)
<|endoftext|>


<commit_before>from flask import Blueprint, flash, redirect, render_template, request, url_for

from viaduct import db
from viaduct.models.company import Company
from viaduct.models.location import Location
from viaduct.models.contact import Contact
from viaduct.forms import CompanyForm

blueprint = Blueprint('company', __name__)

@blueprint.route('/companies/', methods=['GET', 'POST'])
@blueprint.route('/companies/<int:page>/', methods=['GET', 'POST'])
def list(page=1):
	companies = Company.query.paginate(page, 15, False)

	return render_template('company/list.htm', companies=companies)

@blueprint.route('/companies/create/', methods=['GET'])
@blueprint.route('/companies/edit/<int:company_id>/', methods=['GET'])
def view(company_id=None):
	''' FRONTEND
	Create, view or edit a company. '''

	# Select company.
	if company_id:
		company = Company.query.get(company_id)
	else:
		company = Company()

	form = CompanyForm(request.form, company)

	# Add locations.
	locations = Location.query.order_by('address').order_by('city')
	form.location_id.choices = \
			[(l.id, l.address + ', ' + l.city) for l in locations]

	if company.location:
		location = company.location
	else:
		location = locations.first()

	# Add contacts.
	form.contact_id.choices = \
			[(c.id, c.name) for c in Contact.query\
					.filter_by(location=location).order_by('name')]

	return render_template('company/view.htm', company=company, form=form)

@blueprint.route('/companies/create/', methods=['POST'])
@blueprint.route('/companies/edit/<int:company_id>/', methods=['POST'])
def update(company_id=None):
	''' BACKEND
	Create, view or edit a company. '''

	# Select company.
	if company_id:
		company = Company.query.get(company_id)
	else:
		company = Company()

	form = CompanyForm(request.form, company)

	error_found = False
	if not form.name.data:
		flash('Geen titel opgegeven', 'error')
		error_found = True
	if not form.description.data:
		flash('Geen beschrijving opgegeven', 'error')
		error_found = True
	if not form.contract_start_date.data:
		flash('Geen contract begindatum opgegeven', 'error')
		error_found = True
	if not form.contract_end_date.data:
		flash('Geen contract einddatum opgegeven', 'error')
		error_found = True
	if not form.location_id.data:
		flash('Geen locatie opgegeven', 'error')
		error_found = True
	if not form.contact_id.data:
		flash('Geen contactpersoon opgegeven', 'error')
		error_found = True

	if error_found:
		return redirect(url_for('company.view', company_id=company_id))

	company.name = form.name.data
	company.description = form.description.data
	company.contract_start_date = form.contract_start_date.data
	company.contract_end_date = form.contract_end_date.data
	company.location = Location.query.get(form.location_id.data)
	company.contact = Contact.query.get(form.contact_id.data)

	db.session.add(company)
	db.session.commit()

	if company_id:
		flash('Bedrijf opgeslagen', 'success')
	else:
		company_id = company.id
		flash('Bedrijf aangemaakt', 'success')

	return redirect(url_for('company.view', company_id=company_id))

@blueprint.route('/companies/delete/<int:company_id>/', methods=['POST'])
def delete(company_id):
	''' BACKEND
	Delete a company. '''

	company = Company.query.get(company_id)
	if not company:
		return abort(404)

	db.session.delete(company)
	db.session.commit()
	flash('Bedrijf verwijderd', 'success')

	return redirect(url_for('company.list'))<commit_msg>Fixed selection check.<commit_after>from flask import Blueprint, flash, redirect, render_template, request, url_for

from viaduct import db
from viaduct.models.company import Company
from viaduct.models.location import Location
from viaduct.models.contact import Contact
from viaduct.forms import CompanyForm

blueprint = Blueprint('company', __name__)

@blueprint.route('/companies/', methods=['GET', 'POST'])
@blueprint.route('/companies/<int:page>/', methods=['GET', 'POST'])
def list(page=1):
	companies = Company.query.paginate(page, 15, False)

	return render_template('company/list.htm', companies=companies)

@blueprint.route('/companies/create/', methods=['GET'])
@blueprint.route('/companies/edit/<int:company_id>/', methods=['GET'])
def view(company_id=None):
	''' FRONTEND
	Create, view or edit a company. '''

	# Select company.
	if company_id:
		company = Company.query.get(company_id)
	else:
		company = Company()

	form = CompanyForm(request.form, company)

	# Add locations.
	locations = Location.query.order_by('address').order_by('city')
	form.location_id.choices = \
			[(l.id, l.address + ', ' + l.city) for l in locations]

	if company.location:
		location = company.location
	else:
		location = locations.first()

	# Add contacts.
	form.contact_id.choices = \
			[(c.id, c.name) for c in Contact.query\
					.filter_by(location=location).order_by('name')]

	return render_template('company/view.htm', company=company, form=form)

@blueprint.route('/companies/create/', methods=['POST'])
@blueprint.route('/companies/edit/<int:company_id>/', methods=['POST'])
def update(company_id=None):
	''' BACKEND
	Create, view or edit a company. '''

	# Select company.
	if company_id:
		company = Company.query.get(company_id)
	else:
		company = Company()

	form = CompanyForm(request.form, company)

	error_found = False
	if not form.name.data:
		flash('Geen titel opgegeven', 'error')
		error_found = True
	if not form.description.data:
		flash('Geen beschrijving opgegeven', 'error')
		error_found = True
	if not form.contract_start_date.data:
		flash('Geen contract begindatum opgegeven', 'error')
		error_found = True
	if not form.contract_end_date.data:
		flash('Geen contract einddatum opgegeven', 'error')
		error_found = True
	if not 'location_id' in request.form:
		flash('Geen locatie opgegeven', 'error')
		error_found = True
	if not 'contact_id' in request.form:
		flash('Geen contactpersoon opgegeven', 'error')
		error_found = True

	if error_found:
		return redirect(url_for('company.view', company_id=company_id))

	company.name = form.name.data
	company.description = form.description.data
	company.contract_start_date = form.contract_start_date.data
	company.contract_end_date = form.contract_end_date.data
	company.location = Location.query.get(form.location_id.data)
	company.contact = Contact.query.get(form.contact_id.data)

	db.session.add(company)
	db.session.commit()

	if company_id:
		flash('Bedrijf opgeslagen', 'success')
	else:
		company_id = company.id
		flash('Bedrijf aangemaakt', 'success')

	return redirect(url_for('company.view', company_id=company_id))

@blueprint.route('/companies/delete/<int:company_id>/', methods=['POST'])
def delete(company_id):
	''' BACKEND
	Delete a company. '''

	company = Company.query.get(company_id)
	if not company:
		return abort(404)

	db.session.delete(company)
	db.session.commit()
	flash('Bedrijf verwijderd', 'success')

	return redirect(url_for('company.list'))<|endoftext|>


<commit_before>#!/usr/bin/python

"""Module docstring

Usage: ./autotune.py <options> <program>

Options:
  -p             Number of threads to target
                   - Must be greater than 1
                   - Default is tunerNumOfWorkers + 1 (from config file)
  -n, --random   Size of random data to optimize on
                   - Default is 100000
  --min          Min size to autotune on
                   - Default is 64
  --max          Max size to autotune on
                   - Default is 4096
  -h, --help     This help screen

"""

import re
import sys
import os
import getopt
import subprocess

from xml.dom.minidom import parse

config_tool_path = ""
app = ""

maxint = 2147483647

ignore_list = []

def getConfigVal(key):
  run_command = [config_tool_path, app + ".cfg", "get", key]
  p = subprocess.Popen(run_command, stdout = subprocess.PIPE)
  os.waitpid(p.pid, 0)
  return p.stdout.read()

def setConfigVal(key, val):
  run_command = [config_tool_path, app + ".cfg", "set", key, str(val)]
  p = subprocess.Popen(run_command)
  os.waitpid(p.pid, 0)

def getTunables(xml, type):
  transforms = xml.getElementsByTagName("transform")

  algchoices = []
  for transform in transforms:
    algchoices += transform.getElementsByTagName("tunable")

  tunables = []
  for algchoice in algchoices:
    choice = str(algchoice.getAttribute("name"))
    if choice not in ignore_list:
      if algchoice.getAttribute("type") == type:
        tunables.append(choice)
  return tunables

def getAlgChoices(xml):
  transforms = xml.getElementsByTagName("transform")

  algchoices = []
  for transform in transforms:
    algchoices += transform.getElementsByTagName("algchoice")

  static_choices = []
  dynamic_choices = []
  for algchoice in algchoices:
    choice = str(algchoice.getAttribute("name"))
    if choice not in ignore_list:
      if algchoice.getAttribute("type") == "sequential":
        static_choices.append(choice)
      else:
        dynamic_choices.append(choice)
  return (static_choices, dynamic_choices)

def autotune(choice, trials, min, max):
  print "Autotuning:", choice
  run_command = ["./" + app, "--autotune", choice, "--min", str(min), "--max", str(max), "--trials", str(trials)]
  #print run_command
  p = subprocess.Popen(run_command, stdout = subprocess.PIPE, stderr =
      subprocess.PIPE)
  os.waitpid(p.pid, 0)
  lines = p.stdout.readlines()
  print "Result:" + lines[-int(getConfigVal("autotune_alg_slots"))],

def getIgnoreList():

  f = open(app + ".ignore")
  try:
    for line in f:
      ignore_list.append(line.replace("\n", ""))
  finally:
    f.close()

def optimize(tunable, size):
  print "Optimizing:", tunable
  run_command = ["./" + app, "--optimize", tunable, "--random", str(size)]
  p = subprocess.Popen(run_command, stdout = subprocess.PIPE)
  os.waitpid(p.pid, 0)
  print "Result:", getConfigVal(tunable),

def main(argv):

  if len(argv) == 1:
    print "Error.  For help, run:", argv[0], "-h"
    sys.exit(2)

  global config_tool_path
  global app
  global ignore_list

  config_tool_path = os.path.split(argv[0])[0] + "/configtool.py"
  app = argv[-1]
  num_threads = -1 # -1 -> use config file
  data_size = 100000
  min = 64
  max = 4096

  try:
    opts, args = getopt.getopt(argv[1:-1], "hn:p:", ["help","random=","min=","max="])
  except getopt.error, msg:
    print "Error.  For help, run:", argv[0], "-h"
    sys.exit(2)
  # process options
  for o, a in opts:
    if o in ["-h", "--help"]:
      print __doc__
      sys.exit(0)
    if o == "-p":
      num_threads = int(a)
      setConfigVal("tunerNumOfWorkers", num_threads - 1)
    if o in ["-n", "--random"]:
      data_size = int(a)
    if o == "--min":
      min = int(a)
    if o == "--max":
      max = int(a)

  # process arguments
  if num_threads == -1:
    num_threads = int(getConfigVal("tunerNumOfWorkers")) + 1

  getIgnoreList()

  try:
    infoxml = parse(app + ".info")
  except:
    print "Cannot parse:", app + ".info"
    sys.exit(-1)

  if num_threads == 1:
    print "Tuning", app, "with", num_threads, "thread..."
  else:
    print "Tuning", app, "with", num_threads, "threads.."

  (static_choices, dynamic_choices) = getAlgChoices(infoxml)
  seq_cutoff_tunables = getTunables(infoxml, "system.seqcutoff")
  splitsize_tunables = getTunables(infoxml, "system.splitsize")
  user_tunables = getTunables(infoxml, "user.tunable")

  # Autotune sequential code
  for tunable in seq_cutoff_tunables:
    setConfigVal(tunable, maxint)
  for choice in static_choices:
    autotune(choice, 1, min, max / 5)

  # Autotune parallel code
  for tunable in seq_cutoff_tunables:
    setConfigVal(tunable, 50)
  for choice in dynamic_choices:
    autotune(choice, 1, min, max)

  # Optimize sequential cutoffs
  for tunable in seq_cutoff_tunables:
    optimize(tunable, data_size)

  # Optimize split sizes
  for tunable in splitsize_tunables:
    optimize(tunable, data_size)

  # Optimize user tunables
  for tunable in user_tunables:
    optimize(tunable, data_size)

  # Autotune parallel code
  for choice in dynamic_choices:
    autotune(choice, 1, min, max)

  # Optimize sequential cutoffs
  for tunable in seq_cutoff_tunables:
    optimize(tunable, data_size)

  # Optimize split sizes
  for tunable in splitsize_tunables:
    optimize(tunable, data_size)

  # Optimize user tunables
  for tunable in user_tunables:
    optimize(tunable, data_size)

  # Autotune parallel code
  for choice in dynamic_choices:
    autotune(choice, 1, min, max)

if __name__ == "__main__":
    main(sys.argv)
<commit_msg>fixed bug in autotune.py (no longer crashes when .ignore file is missing)<commit_after>#!/usr/bin/python

"""Module docstring

Usage: ./autotune.py <options> <program>

Options:
  -p             Number of threads to target
                   - Must be greater than 1
                   - Default is tunerNumOfWorkers + 1 (from config file)
  -n, --random   Size of random data to optimize on
                   - Default is 100000
  --min          Min size to autotune on
                   - Default is 64
  --max          Max size to autotune on
                   - Default is 4096
  -h, --help     This help screen

"""

import re
import sys
import os
import getopt
import subprocess

from xml.dom.minidom import parse

config_tool_path = ""
app = ""

maxint = 2147483647

ignore_list = []

def getConfigVal(key):
  run_command = [config_tool_path, app + ".cfg", "get", key]
  p = subprocess.Popen(run_command, stdout = subprocess.PIPE)
  os.waitpid(p.pid, 0)
  return p.stdout.read()

def setConfigVal(key, val):
  run_command = [config_tool_path, app + ".cfg", "set", key, str(val)]
  p = subprocess.Popen(run_command)
  os.waitpid(p.pid, 0)

def getTunables(xml, type):
  transforms = xml.getElementsByTagName("transform")

  algchoices = []
  for transform in transforms:
    algchoices += transform.getElementsByTagName("tunable")

  tunables = []
  for algchoice in algchoices:
    choice = str(algchoice.getAttribute("name"))
    if choice not in ignore_list:
      if algchoice.getAttribute("type") == type:
        tunables.append(choice)
  return tunables

def getAlgChoices(xml):
  transforms = xml.getElementsByTagName("transform")

  algchoices = []
  for transform in transforms:
    algchoices += transform.getElementsByTagName("algchoice")

  static_choices = []
  dynamic_choices = []
  for algchoice in algchoices:
    choice = str(algchoice.getAttribute("name"))
    if choice not in ignore_list:
      if algchoice.getAttribute("type") == "sequential":
        static_choices.append(choice)
      else:
        dynamic_choices.append(choice)
  return (static_choices, dynamic_choices)

def autotune(choice, trials, min, max):
  print "Autotuning:", choice
  run_command = ["./" + app, "--autotune", choice, "--min", str(min), "--max", str(max), "--trials", str(trials)]
  #print run_command
  p = subprocess.Popen(run_command, stdout = subprocess.PIPE, stderr =
      subprocess.PIPE)
  os.waitpid(p.pid, 0)
  lines = p.stdout.readlines()
  print "Result:" + lines[-int(getConfigVal("autotune_alg_slots"))],

def getIgnoreList():

  try:
    f = open(app + ".ignore")
    try:
      for line in f:
        ignore_list.append(line.replace("\n", ""))
    finally:
      f.close()
  except:
    pass

def optimize(tunable, size):
  print "Optimizing:", tunable
  run_command = ["./" + app, "--optimize", tunable, "--random", str(size)]
  p = subprocess.Popen(run_command, stdout = subprocess.PIPE)
  os.waitpid(p.pid, 0)
  print "Result:", getConfigVal(tunable),

def main(argv):

  if len(argv) == 1:
    print "Error.  For help, run:", argv[0], "-h"
    sys.exit(2)

  global config_tool_path
  global app
  global ignore_list

  config_tool_path = os.path.split(argv[0])[0] + "/configtool.py"
  app = argv[-1]
  num_threads = -1 # -1 -> use config file
  data_size = 100000
  min = 64
  max = 4096

  try:
    opts, args = getopt.getopt(argv[1:-1], "hn:p:", ["help","random=","min=","max="])
  except getopt.error, msg:
    print "Error.  For help, run:", argv[0], "-h"
    sys.exit(2)
  # process options
  for o, a in opts:
    if o in ["-h", "--help"]:
      print __doc__
      sys.exit(0)
    if o == "-p":
      num_threads = int(a)
      setConfigVal("tunerNumOfWorkers", num_threads - 1)
    if o in ["-n", "--random"]:
      data_size = int(a)
    if o == "--min":
      min = int(a)
    if o == "--max":
      max = int(a)

  # process arguments
  if num_threads == -1:
    num_threads = int(getConfigVal("tunerNumOfWorkers")) + 1

  getIgnoreList()

  try:
    infoxml = parse(app + ".info")
  except:
    print "Cannot parse:", app + ".info"
    sys.exit(-1)

  if num_threads == 1:
    print "Tuning", app, "with", num_threads, "thread..."
  else:
    print "Tuning", app, "with", num_threads, "threads.."

  (static_choices, dynamic_choices) = getAlgChoices(infoxml)
  seq_cutoff_tunables = getTunables(infoxml, "system.seqcutoff")
  splitsize_tunables = getTunables(infoxml, "system.splitsize")
  user_tunables = getTunables(infoxml, "user.tunable")

  # Autotune sequential code
  for tunable in seq_cutoff_tunables:
    setConfigVal(tunable, maxint)
  for choice in static_choices:
    autotune(choice, 1, min, max / 5)

  # Autotune parallel code
  for tunable in seq_cutoff_tunables:
    setConfigVal(tunable, 50)
  for choice in dynamic_choices:
    autotune(choice, 1, min, max)

  # Optimize sequential cutoffs
  for tunable in seq_cutoff_tunables:
    optimize(tunable, data_size)

  # Optimize split sizes
  for tunable in splitsize_tunables:
    optimize(tunable, data_size)

  # Optimize user tunables
  for tunable in user_tunables:
    optimize(tunable, data_size)

  # Autotune parallel code
  for choice in dynamic_choices:
    autotune(choice, 1, min, max)

  # Optimize sequential cutoffs
  for tunable in seq_cutoff_tunables:
    optimize(tunable, data_size)

  # Optimize split sizes
  for tunable in splitsize_tunables:
    optimize(tunable, data_size)

  # Optimize user tunables
  for tunable in user_tunables:
    optimize(tunable, data_size)

  # Autotune parallel code
  for choice in dynamic_choices:
    autotune(choice, 1, min, max)

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


<commit_before># coding=utf-8
from shitpostbot import *
import atexit
import multiprocessing
path = os.path.dirname(sys.argv[0])

def getUniques(l):
	nl = l[:]
	cl = l[:]
	for i in l:
		intimes = 0
		while i in cl:
			intimes += 1
			cl.remove(i)
		if intimes > 1:
			for j in xrange(intimes-1):
				nl.remove(i)
	return nl

class cSL(tweepy.StreamListener):

	def on_data(self, data):
		jdata = json.loads(data.strip())
		name = jdata.get('user', {}).get('screen_name', 'Name Not Found')
		selfname = api.me().screen_name
		print jdata.get('user', {}).get('name', 'Name Not Found')
		print jdata.get('text')

		retweeted = jdata.get('retweeted', False)
		from_self = jdata.get('user', {}).get('id',0) == api.me().id
		people = jdata.get('entities', {}).get('user_mentions', [])
		peoplenames = getUniques([i.get('screen_name') for i in people])
		if name in peoplenames:     peoplenames.remove(name)
		if selfname in peoplenames: peoplenames.remove(selfname)
		peoplenames.insert(0, name)
		names = " ".join(["@{}".format(i) for i in peoplenames if i])

		dot = "." if "public" in jdata.get('text', '') else ""

		if "just do it" in jdata.get('text', ''):
			dot, names = "", ""

		if jdata.get('text', '')[:2] != "RT" and not retweeted and not from_self:
			try:
				text = generate(debug=True)
				api.update_status(status=dot+names+" "+text, in_reply_to_status_id = jdata.get('id_str', ''))
			except Exception, e:
				print "failed: "+str(e)
		time.sleep(2)

		return True

	def on_error(self, status):
		print status
		time.sleep(5)
		return True

def tweetStream():
	config = json.load(open(os.path.join(path, "shitpostconfig.json")))
	l = cSL()
	stream = tweepy.Stream(api.auth, l)

	targets = []
	if isinstance(config.get("searchfor"), str):
		targets.append(config.get("searchfor"))
	elif isinstance(config.get("searchfor"), list):
		targets = config.get("searchfor")
	else:
		targets = ["gimme a shitpost", "gimme a public shitpost"]

	while True:
		try:
			while not connected_to_internet():
				time.sleep(1)
			stream.filter(track=targets)
		except Exception, e:
			if type(e) is KeyboardInterrupt: break
			print "failed: "+str(e)

def cleanup():
	global replyThread, mainThread
	replyThread.terminate()
	mainThread.terminate()

def centralmain():
	global replyThread, mainThread
	atexit.register(cleanup)
	if not os.path.exists(os.path.join(path, "shitpostconfig.json")):
		json.dump({
			"time": 360,
			"notifytime": 60,
			"searchfor": ["gimme a shitpost", "gimme a public shitpost"]
			}, open(os.path.join(path, "shitpostconfig.json"), "w"), indent=2, sort_keys=True)
	replyThread = multiprocessing.Process(target=tweetStream)
	mainThread = multiprocessing.Process(target=main)
	replyThread.start()
	mainThread.start()
	try:
		while True: time.sleep(0.001)
	except KeyboardInterrupt: cleanup()

if __name__ == "__main__": centralmain()<commit_msg>why do i do this<commit_after># coding=utf-8
from shitpostbot import *
import atexit
import multiprocessing
path = os.path.dirname(sys.argv[0])

def getUniques(l):
	nl = l[:]
	cl = l[:]
	for i in l:
		intimes = 0
		while i in cl:
			intimes += 1
			cl.remove(i)
		if intimes > 1:
			for j in xrange(intimes-1):
				nl.remove(i)
	return nl

class cSL(tweepy.StreamListener):

	def on_data(self, data):
		jdata = json.loads(data.strip())
		name = jdata.get('user', {}).get('screen_name', 'Name Not Found')
		selfname = api.me().screen_name
		print jdata.get('user', {}).get('name', 'Name Not Found')
		print jdata.get('text')

		retweeted = jdata.get('retweeted', False)
		from_self = jdata.get('user', {}).get('id',0) == api.me().id
		people = jdata.get('entities', {}).get('user_mentions', [])
		peoplenames = getUniques([i.get('screen_name') for i in people])
		if name in peoplenames:     peoplenames.remove(name)
		if selfname in peoplenames: peoplenames.remove(selfname)
		peoplenames.insert(0, name)
		names = " ".join(["@{}".format(i) for i in peoplenames if i])

		dot = "." if "public" in jdata.get('text', '') else ""

		if "just" in jdata.get('text', '') and "do it" in jdata.get('text', ''):
			dot, names = "", ""

		if jdata.get('text', '')[:2] != "RT" and not retweeted and not from_self:
			try:
				text = generate(debug=True)
				api.update_status(status=dot+names+" "+text, in_reply_to_status_id = jdata.get('id_str', ''))
			except Exception, e:
				print "failed: "+str(e)
		time.sleep(2)

		return True

	def on_error(self, status):
		print status
		time.sleep(5)
		return True

def tweetStream():
	config = json.load(open(os.path.join(path, "shitpostconfig.json")))
	l = cSL()
	stream = tweepy.Stream(api.auth, l)

	targets = []
	if isinstance(config.get("searchfor"), str):
		targets.append(config.get("searchfor"))
	elif isinstance(config.get("searchfor"), list):
		targets = config.get("searchfor")
	else:
		targets = ["gimme a shitpost", "gimme a public shitpost"]

	while True:
		try:
			while not connected_to_internet():
				time.sleep(1)
			stream.filter(track=targets)
		except Exception, e:
			if type(e) is KeyboardInterrupt: break
			print "failed: "+str(e)

def cleanup():
	global replyThread, mainThread
	replyThread.terminate()
	mainThread.terminate()

def centralmain():
	global replyThread, mainThread
	atexit.register(cleanup)
	if not os.path.exists(os.path.join(path, "shitpostconfig.json")):
		json.dump({
			"time": 360,
			"notifytime": 60,
			"searchfor": ["gimme a shitpost", "gimme a public shitpost"]
			}, open(os.path.join(path, "shitpostconfig.json"), "w"), indent=2, sort_keys=True)
	replyThread = multiprocessing.Process(target=tweetStream)
	mainThread = multiprocessing.Process(target=main)
	replyThread.start()
	mainThread.start()
	try:
		while True: time.sleep(0.001)
	except KeyboardInterrupt: cleanup()

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


<commit_before># encoding: utf-8

from __future__ import unicode_literals

from collections import deque
from weakref import ref
from inspect import isclass
from marrow.package.host import PluginManager

log = __import__('logging').getLogger(__name__)

class WebDispatchers(PluginManager):
	__isabstractmethod__ = False  # Jerry, it's a weird bug, Jerry.

	def __init__(self, ctx):
		self._ctx = ref(ctx)  # Stored for later use during dispatcher configuration.
		# The above is a weak reference to try to reduce cycles; a WebDispatchers instance is itself stored in the context.

		super(WebDispatchers, self).__init__('web.dispatch')

	def __call__(self, context, handler, path):
		"""Having been bound to an appropriate context, find a handler for the request path.

		This is the part of the WebCore request cycle that speaks the Dispatch protocol and performs event bridging.

		This requires a context prepared with a `context.extension.signal.dispatch` list and dispatch plugin registry
		as `context.dispatch`.  This does not use `self` to allow for more creative overriding.

		For details, see: https://github.com/marrow/WebCore/wiki/Dispatch-Protocol
		"""

		is_endpoint = False  # We'll search until we find an endpoint.

		# This technically doesn't help Pypy at all, but saves repeated deep lookup in CPython.(E)
		callbacks = context.extension.signal.dispatch  # These extensions want to be notified.
		dispatch = context.dispatch  # Dispatch plugin registry.

		if __debug__:
			log.debug("Preparing dispatch.", extra=dict(
					request = id(context.request),
					path = context.request.path_info,
					handler = repr(handler)
				))

		try:
			while not is_endpoint:
				# Pull the dispatcher out of the current handler, defaulting to object dispatch.
				dispatcher = self[getattr(handler, '__dispatch__', 'object')]

				# Now we need the remaining path elements as a deque.
				path = deque(path.split('/'))

				# We don't want a singular leading / in the path to cause trouble.
				if path and not path[0]:
					path.popleft()

				# Iterate dispatch events, issuing appropriate callbacks as we descend.
				for consumed, handler, is_endpoint in dispatcher(context, handler, path):
					# DO NOT add production logging statements (ones not wrapped in `if __debug__`) to this callback!
					for ext in callbacks: ext(context, consumed, handler, is_endpoint)

		# Dispatch failed utterly.
		except LookupError:
			pass  # `is_endpoint` can only be `False` here.

		return is_endpoint, handler if is_endpoint else None

	def __getitem__(self, name):
		if hasattr(name, '__call__'):
			return name

		# If the dispatcher isn't already executable, it's probably an entry point reference. Load it from cache.
		dispatcher = super(WebDispatchers, self).__getitem__(name)

		# If it's uninstantiated, instantiate it.
		if isclass(dispatcher):
			# TODO: Extract **kw settings from context.
			dispatcher = self.named[name] = dispatcher()  # Instantiate and update the entry point cache.

		if __debug__:
			log.debug("Loaded dispatcher.", extra=dict(dispatcher=repr(dispatcher)))

		return dispatcher
<commit_msg>Moved code were it ought to have been in the first place, corrected unused variable assignment.<commit_after># encoding: utf-7

from __future__ import unicode_literals

from collections import deque
from weakref import ref
from inspect import isclass
from marrow.package.host import PluginManager

log = __import__('logging').getLogger(__name__)

class WebDispatchers(PluginManager):
	__isabstractmethod__ = False  # Jerry, it's a weird bug, Jerry.

	def __init__(self, ctx):
		self._ctx = ref(ctx)  # Stored for later use during dispatcher configuration.
		# The above is a weak reference to try to reduce cycles; a WebDispatchers instance is itself stored in the context.

		super(WebDispatchers, self).__init__('web.dispatch')

	def __call__(self, context, handler, path):
		"""Having been bound to an appropriate context, find a handler for the request path.

		This is the part of the WebCore request cycle that speaks the Dispatch protocol and performs event bridging.

		This requires a context prepared with a `context.extension.signal.dispatch` list and dispatch plugin registry
		as `context.dispatch`.  This does not use `self` to allow for more creative overriding.

		For details, see: https://github.com/marrow/WebCore/wiki/Dispatch-Protocol
		"""

		is_endpoint = False  # We'll search until we find an endpoint.

		# This technically doesn't help Pypy at all, but saves repeated deep lookup in CPython.(E)
		callbacks = context.extension.signal.dispatch  # These extensions want to be notified.
		self = context.dispatch  # Dispatch plugin registry.

		if __debug__:
			log.debug("Preparing dispatch.", extra=dict(
					request = id(context.request),
					path = context.request.path_info,
					handler = repr(handler)
				))

		# Now we need the remaining path elements as a deque.
		path = deque(path.split('/'))

		# We don't want a singular leading / in the path to cause trouble.
		if path and not path[0]:
			path.popleft()

		try:
			while not is_endpoint:
				# Pull the dispatcher out of the current handler, defaulting to object dispatch.
				dispatcher = self[getattr(handler, '__dispatch__', 'object')]

				# Iterate dispatch events, issuing appropriate callbacks as we descend.
				for consumed, handler, is_endpoint in dispatcher(context, handler, path):
					# DO NOT add production logging statements (ones not wrapped in `if __debug__`) to this callback!
					for ext in callbacks: ext(context, consumed, handler, is_endpoint)

				# Repeat of earlier, we do this after extensions in case anything above modifies the environ path.
				path = deque(context.environ['PATH_INFO'].split('/'))
				if path and not path[0]:
					path.popleft()

		# Dispatch failed utterly.
		except LookupError:
			pass  # `is_endpoint` can only be `False` here.

		return is_endpoint, handler if is_endpoint else None

	def __getitem__(self, name):
		if hasattr(name, '__call__'):
			return name

		# If the dispatcher isn't already executable, it's probably an entry point reference. Load it from cache.
		dispatcher = super(WebDispatchers, self).__getitem__(name)

		# If it's uninstantiated, instantiate it.
		if isclass(dispatcher):
			# TODO: Extract **kw settings from context.
			dispatcher = self.named[name] = dispatcher()  # Instantiate and update the entry point cache.

		if __debug__:
			log.debug("Loaded dispatcher.", extra=dict(dispatcher=repr(dispatcher)))

		return dispatcher
<|endoftext|>


<commit_before>#!/usr/bin/env python
import sys
import re
import docutils.core
import docutils.writers.html4css1
from datetime import datetime
from jinja2 import Environment, Template
from jinja2.filters import do_dictsort
from spec_util import is_dynamic_pinned, verify_node, isAbstract, setdefault, trim_docstring
from ir_spec import nodes

tags = None
linkbase = None

def format_doxygrouplink(string, link=None):
	global tags
	if link == None:
		link = string
	if tags == None:
		return string
	e = tags.xpath("//compound[name/text()='%s']" % link)
	if len(e) == 0:
		return string
	e = e[0]
	anchorfile = e.xpath("filename/text()")
	if len(anchorfile) == 0:
		return string
	global linkbase
	return "%s" % (linkbase, anchorfile[0], string)

def format_doxylink(string, link=None):
	global tags
	if link == None:
		link = string
	if tags == None:
		return string
	e = tags.xpath("//member[name/text()='%s']" % link)
	if len(e) == 0:
		return string
	e = e[0]
	anchorfile = e.xpath("anchorfile/text()")
	anchor = e.xpath("anchor/text()")
	if len(anchorfile) == 0 or len(anchor) == 0:
		return string
	global linkbase
	return "%s" % (linkbase, anchorfile[0], anchor[0], string)

def format_docutils(string):
	writer = docutils.writers.html4css1.Writer()
	document = docutils.core.publish_parts(string, writer=writer)['body']
	return document

env = Environment()
env.filters['docutils'] = format_docutils
env.filters['doxylink'] = format_doxylink
env.filters['doxygrouplink'] = format_doxygrouplink

docu_template = env.from_string(
'''

		<title>libFirm node specifications</title>
		<link rel='stylesheet' type='text/css' href='style.css'/>

Firm Node Types

				{% for node in nodes %}

{{node.name}}

					{{node.doc|docutils}}

Inputs

					{% for input in node.ins %}

{{input[0]}}

{{input[1]}}

					{% endfor %}
					{% if node.arity == "variable" %}

...

additional inputs (oparity_variable)

					{% elif node.arity == "dynamic" %}

...

inputs dynamically mananged (oparity_dynamic)

					{% endif %}

					{% if node.outs %}

Outputs

					{% for output in node.outs %}

{{output[0]}}

{{output[1]}}

					{% endfor %}

					{% endif %}
					{% if node.attrs %}

Attributes

					{% for attr in node.attrs %}

{{attr.name}}

{{attr.comment}} ({{attr.type}})

					{% endfor %}
					{% endif %}

					{% set comma = joiner(", ") %}

Flags

					{% for flag in node.flags -%}
						{{comma()}}{{flag|doxylink("irop_flag_" + flag)}}
					{%- endfor %}

{{"API"|doxygrouplink(node.name)}}

				{% endfor %}

Table Of Contents

Firm Node Types

						{% for node in nodes %}

{{node.name}}

						{% endfor %}

			Generated {{time}}

''')

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

def preprocess_node(node):
	node.doc = trim_docstring(node.__doc__)

def prepare_nodes():
	real_nodes = []
	for node in nodes:
		preprocess_node(node)
		if isAbstract(node):
			continue
		real_nodes.append(node)

	return real_nodes

def main(argv):
	global tags
	output = sys.stdout
	if len(argv) > 1:
		output = open(argv[-1], "w")
	if len(argv) > 3:
		tagfile = open(argv[-3], "r")
		global linkbase
		linkbase = argv[-2]
		if linkbase != "":
			linkbase += "/"
		try:
			from lxml import etree
			tags = etree.parse(tagfile)
		except:
			tags = None

	real_nodes = prepare_nodes()
	time = datetime.now().replace(microsecond=0).isoformat(' ')
	output.write(docu_template.render(nodes=real_nodes, time=time))
	if output != sys.stdout:
		output.close()

main(sys.argv)
<commit_msg>gen_docu: output 'None' for an empty flags list<commit_after>#!/usr/bin/env python
import sys
import re
import docutils.core
import docutils.writers.html4css1
from datetime import datetime
from jinja2 import Environment, Template
from jinja2.filters import do_dictsort
from spec_util import is_dynamic_pinned, verify_node, isAbstract, setdefault, trim_docstring
from ir_spec import nodes

tags = None
linkbase = None

def format_doxygrouplink(string, link=None):
	global tags
	if link == None:
		link = string
	if tags == None:
		return string
	e = tags.xpath("//compound[name/text()='%s']" % link)
	if len(e) == 0:
		return string
	e = e[0]
	anchorfile = e.xpath("filename/text()")
	if len(anchorfile) == 0:
		return string
	global linkbase
	return "%s" % (linkbase, anchorfile[0], string)

def format_doxylink(string, link=None):
	global tags
	if link == None:
		link = string
	if tags == None:
		return string
	e = tags.xpath("//member[name/text()='%s']" % link)
	if len(e) == 0:
		return string
	e = e[0]
	anchorfile = e.xpath("anchorfile/text()")
	anchor = e.xpath("anchor/text()")
	if len(anchorfile) == 0 or len(anchor) == 0:
		return string
	global linkbase
	return "%s" % (linkbase, anchorfile[0], anchor[0], string)

def format_docutils(string):
	writer = docutils.writers.html4css1.Writer()
	document = docutils.core.publish_parts(string, writer=writer)['body']
	return document

env = Environment()
env.filters['docutils'] = format_docutils
env.filters['doxylink'] = format_doxylink
env.filters['doxygrouplink'] = format_doxygrouplink

docu_template = env.from_string(
'''

		<title>libFirm node specifications</title>
		<link rel='stylesheet' type='text/css' href='style.css'/>

Firm Node Types

				{% for node in nodes %}

{{node.name}}

					{{node.doc|docutils}}

Inputs

					{% for input in node.ins %}

{{input[0]}}

{{input[1]}}

					{% endfor %}
					{% if node.arity == "variable" %}

...

additional inputs (oparity_variable)

					{% elif node.arity == "dynamic" %}

...

inputs dynamically mananged (oparity_dynamic)

					{% endif %}

					{% if node.outs %}

Outputs

					{% for output in node.outs %}

{{output[0]}}

{{output[1]}}

					{% endfor %}

					{% endif %}
					{% if node.attrs %}

Attributes

					{% for attr in node.attrs %}

{{attr.name}}

{{attr.comment}} ({{attr.type}})

					{% endfor %}
					{% endif %}

					{% set comma = joiner(", ") %}

Flags

					{% if node.flags.__len__() > 0 %}
					{% for flag in node.flags -%}
						{{comma()}}{{flag|doxylink("irop_flag_" + flag)}}
					{%- endfor %}
					{% else %}
					None
					{% endif %}

{{"API"|doxygrouplink(node.name)}}

				{% endfor %}

Table Of Contents

Firm Node Types

						{% for node in nodes %}

{{node.name}}

						{% endfor %}

			Generated {{time}}

''')

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

def preprocess_node(node):
	node.doc = trim_docstring(node.__doc__)

def prepare_nodes():
	real_nodes = []
	for node in nodes:
		preprocess_node(node)
		if isAbstract(node):
			continue
		real_nodes.append(node)

	return real_nodes

def main(argv):
	global tags
	output = sys.stdout
	if len(argv) > 1:
		output = open(argv[-1], "w")
	if len(argv) > 3:
		tagfile = open(argv[-3], "r")
		global linkbase
		linkbase = argv[-2]
		if linkbase != "":
			linkbase += "/"
		try:
			from lxml import etree
			tags = etree.parse(tagfile)
		except:
			tags = None

	real_nodes = prepare_nodes()
	time = datetime.now().replace(microsecond=0).isoformat(' ')
	output.write(docu_template.render(nodes=real_nodes, time=time))
	if output != sys.stdout:
		output.close()

main(sys.argv)
<|endoftext|>


<commit_before># Class for RWIS obs

import mx.DateTime, re, os
from pyIEM import mesonet

ERROR_VAL = "32767"

RWISconvert = {
'00' : 'RDAI4', '01' : 'RALI4', '02' : 'RTNI4', '03' : 'RTOI4', '04' : 'RAMI4', '05' : 'RAKI4', '06' : 'RAVI4', '07' : 'RBUI4', '08' : 'RCAI4', '09' : 'RCDI4', '10' : 'RCII4', '11' : 'RCNI4', '12' : 'RCBI4', '13' : 'RCEI4', '14' : 'RDVI4', '15' : 'RDCI4', '16' : 'RDSI4', '17' : 'RDMI4', '18' : 'RDEI4', '19' : 'RDWI4', '20' : 'RDBI4', '21' : 'RFDI4', '22' : 'RGRI4', '23' : 'RIAI4', '24' : 'RIOI4', '25' : 'RJFI4', '26' : 'RLEI4', '27' : 'RMNI4', '28' : 'RMQI4', '29' : 'RMTI4', '30' : 'RMCI4', '31' : 'RMVI4', '32' : 'RMPI4', '33' : 'RNHI4', '34' : 'RONI4', '35' : 'ROSI4', '36' : 'ROTI4', '37' : 'RPLI4', '38' : 'RROI4', '39' : 'RSDI4', '40' : 'RSGI4', '41' : 'RSCI4', '42' : 'RSPI4', '43' : 'RSLI4', '44' : 'RTPI4', '45' : 'RURI4', '46' : 'RWLI4', '47' : 'RWII4', '48' : 'RWBI4', '49' : 'RHAI4', '50' : 'RSBI4', '51' : 'RIGI4', '52' : 'RCRI4', '53' : 'RCFI4', '54' : 'RSYI4',
'55' : 'RBFI4', '56' : 'RDYI4', '57' : 'RTMI4', '58' : 'RPFI4', '59' : 'RCTI4',
'60' : 'RDNI4', '61' : 'RQCI4', '62' : 'RSMI4',
}

RWISconvert2 = {
'00': 'XADA',   '01': 'XALG',   '02': 'XATN',   '03': 'XALT',
'04': 'XAME',   '05': 'XANK',   '06': 'XAVO',   '07': 'XBUR',
'08': 'XCAR',   '09': 'XCDR',   '10': 'XCID',   '11': 'XCEN',
'12': 'XCOU',   '13': 'XCRE',   '14': 'XDAV',   '15': 'XDEC',
'16': 'XDSM',   '17': 'XDES',   '18': 'XDST',   '19': 'XDEW',
'20': 'XDUB',   '21': 'XFOD',   '22': 'XGRI',   '23': 'XIAC',
'24': 'XIOW',   '25': 'XJEF',   '26': 'XLEO',   '27': 'XMAN',
'28': 'XMAQ',   '29': 'XMAR',   '30': 'XMCW',   '31': 'XMIS',
'32': 'XMOU',   '33': 'XNEW',   '34': 'XONA',   '35': 'XOSC',
'36': 'XOTT',   '37': 'XPEL',   '38': 'XRED',   '39': 'XSID',
'40': 'XSIG',   '41': 'XSIO',   '42': 'XSPE',   '43': 'XSTO',
'44': 'XTIP',   '45': 'XURB',   '46': 'XWAT',   '47': 'XWIL',
'48': 'XWBG',   '49': 'XHAN',   '50': 'XSBI',   '51': 'XIGI',
'52': 'XCRI',   '53': 'XCFI',   '54': 'XSYI',   '55': 'XBFI',
'56': 'XDYI',   '57': 'XTMI',   '58': 'XPFI',   '59': 'XCTI',
'60': 'XDNI',   '61': 'XQCI',   '62': 'XSMI'
}

class RWISOb(object):

  def __init__(self):
    self.tmpf       = None
    self.tmpc       = None
    self.dwpf       = None
    self.dwpc       = None
    self.sknt       = None
    self.drct       = None
    self.pDay       = None
    self.gmt_ts     = None
    self.windkmh    = None
    self.windGkmh   = None
    self.ts         = None
    self.stationNum = None
    self.stationID  = None
    self.metarID    = None
    self.gust       = None
    self.vsby       = None
    self.error      = 0
    self.subT = -99
    self.sfdata = [0]*4
    for i in range(4):
      self.sfdata[i] = {"dry": -99, "tmpc": -99, "tmpf": -99}

  def add_sfdata(self, dict):
    sensorid = int(dict["Senid"])
    self.stationNum = int( dict["Rpuid"] )
    self.stationID = RWISconvert["%02i" % (self.stationNum) ]
    self.metarID = RWISconvert2["%02i" % (self.stationNum) ]
    self.parseTime( dict["DtTm"] )

    ttempf = dict["sftemp"]
    tsub = dict["subsftemp"]
    self.sfdata[sensorid]["dry"] = dict["sfcond"]

    if (len(ttempf) > 0 and ttempf != ERROR_VAL):
      self.sfdata[sensorid]["tmpf"] =round(mesonet.c2f( float( ttempf) / 100.00),2)

    if (len(tsub) > 0 and tsub != ERROR_VAL):
      self.subT = round(mesonet.c2f( float(tsub) / 100.00 ),2)

  def add_atdata(self, dict):
    self.stationNum = int( dict["Rpuid"] )
    self.stationID = RWISconvert["%02i" % (self.stationNum) ]
    self.metarID = RWISconvert2["%02i" % (self.stationNum) ]

    self.parseTime( dict["DtTm"] )
    # Air Temperature
    if dict["AirTemp"] not in ['', ERROR_VAL]:
      self.tmpc = float(dict['AirTemp']) / 100.0
      self.tmpf = round( mesonet.c2f(self.tmpc), 1)
    # Dewpoint
    if dict["Dewpoint"] not in ['', ERROR_VAL]:
      self.dwpc = float(dict['Dewpoint']) / 100.0
      self.dwpf = round( mesonet.c2f(self.dwpc), 1)
    # Wind Speed
    if dict["SpdAvg"] not in ['', '255']:
      self.sknt = float(dict['SpdAvg']) * 0.53995
    # Wind Gust
    if dict["SpdGust"] not in ['', '255']:
      self.gust = float(dict['SpdGust']) * 0.53995
    # Wind Direction
    if dict["DirMin"] not in ['', ERROR_VAL]:
      self.drct = int(dict['DirMin'])
    # Today precipitation
    if dict["PcAccum"] not in ['', ERROR_VAL, '-1']:
      self.pday = float(dict['PcAccum']) * 0.00098425  # Convert to inches
    # Visibility
    if dict["Visibility"] not in ['', ERROR_VAL, '-1']:
      self.vsby = float(dict['Visibility']) / 1609.344  # meters to miles

    if self.gust and self.gust >= 50:
      self.windAlert()

  def windAlert(self):
    if self.stationID in ('RBFI4','RTMI4','RWII4','RDNI4'):
      return
    from pyIEM import stationTable, iemdb
    st = stationTable.stationTable("/mesonet/TABLES/RWIS.stns")
    gmtNow = mx.DateTime.gmt()
    stname = st.sts[self.stationID]["name"]
    mf = """At %s, a wind gust of %s knots was recorded at the %s RWIS station"""
    mailStr = mf % (self.ts.strftime("%d %b %Y - %I:%M %p"), self.gust, stname)

    fp = "/tmp/%s.%s" % (self.ts.strftime("%Y%m%d%H%M"), self.stationID)
    if (os.path.isfile(fp)):
      print "SKIP", self.stationID
      return
    o = open(fp, 'w')
    o.write(" ")
    o.close()

    if ( int(gmtNow - self.gmt_ts) < (30*60) ): # 30 minutes
      os.system("echo '"+mailStr+"' | mail -s 'RWIS OB' -c akrherz@iastate.edu iarwis-alert@mesonet.agron.iastate.edu")

    else:
      cmd = "echo '"+mailStr+"' | mail -s 'RWIS OB [NO ALERT]' akrherz@iastate.edu"
      os.system(cmd)

  def parseTime(self, inStr):
    if self.ts is not None:
        return
    self.gmt_ts = mx.DateTime.strptime(inStr, "%m/%d/%y %H:%M")
    self.ts = self.gmt_ts.localtime()

  def METARtemp(self, inStr):
    f_temp = float(inStr)
    i_temp = int( round(f_temp,0) )
    f1_temp = int( round(f_temp *10,0) )
    if (i_temp < 0):
      i_temp = 0 - i_temp
      m_temp = "M"+str( "%02i" % (i_temp) )
    else:
      m_temp = str( "%02i" % (i_temp) )

    if (f1_temp < 0):
      t_temp = "1"+str( "%03i" % (0 - f1_temp) )
    else:
      t_temp = "0"+str( "%03i" % (f1_temp) )

    return m_temp, t_temp

  def metar_wind(self):
    """
    Return a string for METAR wind information
    """
    if self.sknt is None or self.drct is None:
      return ""
    s = ""
    d5 = self.drct
    if str(d5)[-1] == "5":
      d5 -= 5
    s += "%03i%02i" % (d5, self.sknt)
    if self.gust is not None:
      s += "G%02i" % (self.gust,)
    s += "KT"
    return s

  def printMETAR(self, out):

    windTxt = self.metar_wind()

    tempTxt = ""
    t_tempTxt = ""
    if self.tmpc is not None and self.dwpc is not None:
      m_tmpc, t_tmpc = self.METARtemp(self.tmpc)
      m_dwpc, t_dwpc = self.METARtemp(self.dwpc)
      tempTxt = "%s/%s" % (m_tmpc, m_dwpc)
      t_tempTxt = "T%s%s " % (t_tmpc, t_dwpc)
  #  print tempTxt, t_tempTxt

    out.write("%s" % (self.stationID[:4]) + \
     " %s" % (self.gmt_ts.strftime("%d%H%MZ")) + \
     " %s" % (windTxt) + \
     " %s" % (tempTxt) + \
     " RMK %s%s\015\015\012" % (t_tempTxt, "=") )

  def printMETAR2(self, out):

    windTxt = self.metar_wind()

    tempTxt = ""
    t_tempTxt = ""
    if (self.tmpc != None and self.dwpc != None):
      m_tmpc, t_tmpc = self.METARtemp(self.tmpc)
      m_dwpc, t_dwpc = self.METARtemp(self.dwpc)
      tempTxt = "%s/%s" % (m_tmpc, m_dwpc)
      t_tempTxt = "T%s%s " % (t_tmpc, t_dwpc)
  #  print tempTxt, t_tempTxt

    out.write("%s" % (self.metarID) + \
     " %s" % (self.gmt_ts.strftime("%d%H%MZ")) + \
     " %s" % (windTxt) + \
     " %s" % (tempTxt) + \
     " RMK %s%s\015\015\012" % (t_tempTxt, "=") )

  def printCDF(self, out):
    tmpf = self.tmpf
    if (tmpf == None):
      tmpf = ""
    dwpf = self.dwpf
    if (dwpf == None):
      dwpf = ""
    drct = self.drct
    if (drct == None):
      drct = ""
    gust = self.gust
    if (gust == None):
      gust = ""
    sknt = self.sknt
    if (sknt == None):
      sknt = ""
    out.write("%s,%s,%s,%s,%s,%s,%s,%s\n" % \
     (self.stationID, self.gmt_ts, tmpf, dwpf, \
      drct, sknt, self.pDay, gust))
<commit_msg>slight formatting change for METAR<commit_after># Class for RWIS obs

import mx.DateTime, re, os
from pyIEM import mesonet

ERROR_VAL = "32767"

RWISconvert = {
'00' : 'RDAI4', '01' : 'RALI4', '02' : 'RTNI4', '03' : 'RTOI4', '04' : 'RAMI4', '05' : 'RAKI4', '06' : 'RAVI4', '07' : 'RBUI4', '08' : 'RCAI4', '09' : 'RCDI4', '10' : 'RCII4', '11' : 'RCNI4', '12' : 'RCBI4', '13' : 'RCEI4', '14' : 'RDVI4', '15' : 'RDCI4', '16' : 'RDSI4', '17' : 'RDMI4', '18' : 'RDEI4', '19' : 'RDWI4', '20' : 'RDBI4', '21' : 'RFDI4', '22' : 'RGRI4', '23' : 'RIAI4', '24' : 'RIOI4', '25' : 'RJFI4', '26' : 'RLEI4', '27' : 'RMNI4', '28' : 'RMQI4', '29' : 'RMTI4', '30' : 'RMCI4', '31' : 'RMVI4', '32' : 'RMPI4', '33' : 'RNHI4', '34' : 'RONI4', '35' : 'ROSI4', '36' : 'ROTI4', '37' : 'RPLI4', '38' : 'RROI4', '39' : 'RSDI4', '40' : 'RSGI4', '41' : 'RSCI4', '42' : 'RSPI4', '43' : 'RSLI4', '44' : 'RTPI4', '45' : 'RURI4', '46' : 'RWLI4', '47' : 'RWII4', '48' : 'RWBI4', '49' : 'RHAI4', '50' : 'RSBI4', '51' : 'RIGI4', '52' : 'RCRI4', '53' : 'RCFI4', '54' : 'RSYI4',
'55' : 'RBFI4', '56' : 'RDYI4', '57' : 'RTMI4', '58' : 'RPFI4', '59' : 'RCTI4',
'60' : 'RDNI4', '61' : 'RQCI4', '62' : 'RSMI4',
}

RWISconvert2 = {
'00': 'XADA',   '01': 'XALG',   '02': 'XATN',   '03': 'XALT',
'04': 'XAME',   '05': 'XANK',   '06': 'XAVO',   '07': 'XBUR',
'08': 'XCAR',   '09': 'XCDR',   '10': 'XCID',   '11': 'XCEN',
'12': 'XCOU',   '13': 'XCRE',   '14': 'XDAV',   '15': 'XDEC',
'16': 'XDSM',   '17': 'XDES',   '18': 'XDST',   '19': 'XDEW',
'20': 'XDUB',   '21': 'XFOD',   '22': 'XGRI',   '23': 'XIAC',
'24': 'XIOW',   '25': 'XJEF',   '26': 'XLEO',   '27': 'XMAN',
'28': 'XMAQ',   '29': 'XMAR',   '30': 'XMCW',   '31': 'XMIS',
'32': 'XMOU',   '33': 'XNEW',   '34': 'XONA',   '35': 'XOSC',
'36': 'XOTT',   '37': 'XPEL',   '38': 'XRED',   '39': 'XSID',
'40': 'XSIG',   '41': 'XSIO',   '42': 'XSPE',   '43': 'XSTO',
'44': 'XTIP',   '45': 'XURB',   '46': 'XWAT',   '47': 'XWIL',
'48': 'XWBG',   '49': 'XHAN',   '50': 'XSBI',   '51': 'XIGI',
'52': 'XCRI',   '53': 'XCFI',   '54': 'XSYI',   '55': 'XBFI',
'56': 'XDYI',   '57': 'XTMI',   '58': 'XPFI',   '59': 'XCTI',
'60': 'XDNI',   '61': 'XQCI',   '62': 'XSMI'
}

class RWISOb(object):

  def __init__(self):
    self.tmpf       = None
    self.tmpc       = None
    self.dwpf       = None
    self.dwpc       = None
    self.sknt       = None
    self.drct       = None
    self.pDay       = None
    self.gmt_ts     = None
    self.windkmh    = None
    self.windGkmh   = None
    self.ts         = None
    self.stationNum = None
    self.stationID  = None
    self.metarID    = None
    self.gust       = None
    self.vsby       = None
    self.error      = 0
    self.subT = -99
    self.sfdata = [0]*4
    for i in range(4):
      self.sfdata[i] = {"dry": -99, "tmpc": -99, "tmpf": -99}

  def add_sfdata(self, dict):
    sensorid = int(dict["Senid"])
    self.stationNum = int( dict["Rpuid"] )
    self.stationID = RWISconvert["%02i" % (self.stationNum) ]
    self.metarID = RWISconvert2["%02i" % (self.stationNum) ]
    self.parseTime( dict["DtTm"] )

    ttempf = dict["sftemp"]
    tsub = dict["subsftemp"]
    self.sfdata[sensorid]["dry"] = dict["sfcond"]

    if (len(ttempf) > 0 and ttempf != ERROR_VAL):
      self.sfdata[sensorid]["tmpf"] =round(mesonet.c2f( float( ttempf) / 100.00),2)

    if (len(tsub) > 0 and tsub != ERROR_VAL):
      self.subT = round(mesonet.c2f( float(tsub) / 100.00 ),2)

  def add_atdata(self, dict):
    self.stationNum = int( dict["Rpuid"] )
    self.stationID = RWISconvert["%02i" % (self.stationNum) ]
    self.metarID = RWISconvert2["%02i" % (self.stationNum) ]

    self.parseTime( dict["DtTm"] )
    # Air Temperature
    if dict["AirTemp"] not in ['', ERROR_VAL]:
      self.tmpc = float(dict['AirTemp']) / 100.0
      self.tmpf = round( mesonet.c2f(self.tmpc), 1)
    # Dewpoint
    if dict["Dewpoint"] not in ['', ERROR_VAL]:
      self.dwpc = float(dict['Dewpoint']) / 100.0
      self.dwpf = round( mesonet.c2f(self.dwpc), 1)
    # Wind Speed
    if dict["SpdAvg"] not in ['', '255']:
      self.sknt = float(dict['SpdAvg']) * 0.53995
    # Wind Gust
    if dict["SpdGust"] not in ['', '255']:
      self.gust = float(dict['SpdGust']) * 0.53995
    # Wind Direction
    if dict["DirMin"] not in ['', ERROR_VAL]:
      self.drct = int(dict['DirMin'])
    # Today precipitation
    if dict["PcAccum"] not in ['', ERROR_VAL, '-1']:
      self.pday = float(dict['PcAccum']) * 0.00098425  # Convert to inches
    # Visibility
    if dict["Visibility"] not in ['', ERROR_VAL, '-1']:
      self.vsby = float(dict['Visibility']) / 1609.344  # meters to miles

    if self.gust and self.gust >= 50:
      self.windAlert()

  def windAlert(self):
    if self.stationID in ('RBFI4','RTMI4','RWII4','RDNI4'):
      return
    from pyIEM import stationTable, iemdb
    st = stationTable.stationTable("/mesonet/TABLES/RWIS.stns")
    gmtNow = mx.DateTime.gmt()
    stname = st.sts[self.stationID]["name"]
    mf = """At %s, a wind gust of %s knots was recorded at the %s RWIS station"""
    mailStr = mf % (self.ts.strftime("%d %b %Y - %I:%M %p"), self.gust, stname)

    fp = "/tmp/%s.%s" % (self.ts.strftime("%Y%m%d%H%M"), self.stationID)
    if (os.path.isfile(fp)):
      print "SKIP", self.stationID
      return
    o = open(fp, 'w')
    o.write(" ")
    o.close()

    if ( int(gmtNow - self.gmt_ts) < (30*60) ): # 30 minutes
      os.system("echo '"+mailStr+"' | mail -s 'RWIS OB' -c akrherz@iastate.edu iarwis-alert@mesonet.agron.iastate.edu")

    else:
      cmd = "echo '"+mailStr+"' | mail -s 'RWIS OB [NO ALERT]' akrherz@iastate.edu"
      os.system(cmd)

  def parseTime(self, inStr):
    if self.ts is not None:
        return
    self.gmt_ts = mx.DateTime.strptime(inStr, "%m/%d/%y %H:%M")
    self.ts = self.gmt_ts.localtime()

  def METARtemp(self, inStr):
    f_temp = float(inStr)
    i_temp = int( round(f_temp,0) )
    f1_temp = int( round(f_temp *10,0) )
    if (i_temp < 0):
      i_temp = 0 - i_temp
      m_temp = "M"+str( "%02i" % (i_temp) )
    else:
      m_temp = str( "%02i" % (i_temp) )

    if (f1_temp < 0):
      t_temp = "1"+str( "%03i" % (0 - f1_temp) )
    else:
      t_temp = "0"+str( "%03i" % (f1_temp) )

    return m_temp, t_temp

  def metar_wind(self):
    """
    Return a string for METAR wind information
    """
    if self.sknt is None or self.drct is None:
      return ""
    s = ""
    d5 = self.drct
    if str(d5)[-1] == "5":
      d5 -= 5
    s += "%03i%02i" % (d5, self.sknt)
    if self.gust is not None:
      s += "G%02i" % (self.gust,)
    s += "KT"
    return s

  def printMETAR(self, out):

    windTxt = self.metar_wind()

    tempTxt = ""
    t_tempTxt = ""
    if self.tmpc is not None and self.dwpc is not None:
      m_tmpc, t_tmpc = self.METARtemp(self.tmpc)
      m_dwpc, t_dwpc = self.METARtemp(self.dwpc)
      tempTxt = "%s/%s" % (m_tmpc, m_dwpc)
      t_tempTxt = "T%s%s " % (t_tmpc, t_dwpc)
  #  print tempTxt, t_tempTxt

    out.write("%s %s %s %s RMK AO2 %s%s\015\015\012" % (self.stationID[:4],
      self.gmt_ts.strftime("%d%H%MZ"), windTxt, tempTxt, t_tempTxt, "=") )

  def printMETAR2(self, out):

    windTxt = self.metar_wind()

    tempTxt = ""
    t_tempTxt = ""
    if (self.tmpc != None and self.dwpc != None):
      m_tmpc, t_tmpc = self.METARtemp(self.tmpc)
      m_dwpc, t_dwpc = self.METARtemp(self.dwpc)
      tempTxt = "%s/%s" % (m_tmpc, m_dwpc)
      t_tempTxt = "T%s%s " % (t_tmpc, t_dwpc)
  #  print tempTxt, t_tempTxt

    out.write("%s %s %s %s RMK AO2 %s%s\015\015\012" % (self.metarID,
      self.gmt_ts.strftime("%d%H%MZ"), windTxt, tempTxt, t_tempTxt, "=") )

  def printCDF(self, out):
    tmpf = self.tmpf
    if (tmpf == None):
      tmpf = ""
    dwpf = self.dwpf
    if (dwpf == None):
      dwpf = ""
    drct = self.drct
    if (drct == None):
      drct = ""
    gust = self.gust
    if (gust == None):
      gust = ""
    sknt = self.sknt
    if (sknt == None):
      sknt = ""
    out.write("%s,%s,%s,%s,%s,%s,%s,%s\n" % \
     (self.stationID, self.gmt_ts, tmpf, dwpf, \
      drct, sknt, self.pDay, gust))
<|endoftext|>


<commit_before># coding: utf-8

""" Compute Lyapunov exponents for the mean orbit fits """

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os

# Third-party
from astropy import log as logger
import numpy as np
import gary.dynamics as gd

# This project
import ophiuchus.potential as op

def main(path, overwrite=False):
    dt = 2.
    nsteps = 2560000 # 256 steps per orbit -- 10000 orbital periods

    path = os.path.abspath(path)
    name = os.path.basename(path)
    output_file = os.path.join(path, 'lyap.npy')
    t_output_file = os.path.join(path, 'lyap_t.npy')
    ws_output_file = os.path.join(path, 'lyap_ws.npy')

    if os.path.exists(output_file) and overwrite:
        os.remove(output_file)

    if os.path.exists(output_file):
        logger.info("Output file exists. Exiting.")

    # load initial conditions file
    w0 = np.ascontiguousarray(np.load(os.path.join(path, 'w0.npy'))[0]) # load mean orbit
    pot = op.load_potential(name)

    # compute lyapunov exponent
    lyap,t,ws = gd.fast_lyapunov_max(w0, pot, dt=dt, nsteps=nsteps)
    np.save(output_file, lyap)
    np.save(t_output_file, t)
    np.save(ws_output_file, ws)

if __name__ == "__main__":
    from argparse import ArgumentParser
    import logging

    parser = ArgumentParser(description="")
    parser.add_argument("-v", "--verbose", action="store_true",
                        dest="verbose", default=False,
                        help="Be chatty! (default = False)")
    parser.add_argument("-q", "--quiet", action="store_true", dest="quiet",
                        default=False, help="Be quiet! (default = False)")
    parser.add_argument("-o", "--overwrite", dest="overwrite", default=False,
                        action="store_true", help="Overwrite any existing data.")

    parser.add_argument("--path", dest="path",
                        required=True, help="Path to the output file.")

    args = parser.parse_args()

    if args.verbose:
        logger.setLevel(logging.DEBUG)
    elif args.quiet:
        logger.setLevel(logging.ERROR)
    else:
        logger.setLevel(logging.INFO)

    main(args.path, overwrite=args.overwrite)
<commit_msg>update lyapunov script<commit_after># coding: utf-8

""" Compute Lyapunov exponents for the mean orbit fits """

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os

# Third-party
from astropy import log as logger
import numpy as np
import gary.dynamics as gd
from six.moves import cPickle as pickle

# This project
from ophiuchus import RESULTSPATH
import ophiuchus.potential as op

def main(potential_name, results_path=None, overwrite=False):
    # top-level output path for saving (this will create a subdir within output_path)
    if results_path is None:
        top_path = RESULTSPATH
    else:
        top_path = os.path.abspath(os.path.expanduser(results_path))

    if top_path is None:
        raise ValueError("If $PROJECTSPATH is not set, you must provide a path to save "
                         "the results in with the --results_path argument.")

    output_path = os.path.join(top_path, potential_name, "lyapunov")
    logger.debug("Output path: {}".format(output_path))

    if not os.path.exists(output_path):
        os.mkdir(output_path)

    T = 512. # The orbits have periods ~500 Myr
    dt = T / 512 # 512 steps per orbit
    # nsteps = int(16384 * 512) # 16384 orbital periods
    nsteps = int(64 * 512) # TESTING HACK HACK HACK

    output_file = os.path.join(output_path, 'lyap.pickle')
    if os.path.exists(output_file) and overwrite:
        os.remove(output_file)

    if os.path.exists(output_file):
        logger.info("Output file exists. Exiting.")
        return

    # load initial conditions file
    w0_path = os.path.join(top_path, potential_name, 'orbitfit', 'w0.npy')
    w0 = np.ascontiguousarray(np.load(w0_path)[0]) # load only the mean orbit
    pot = op.load_potential(potential_name)

    # compute lyapunov exponent
    lyap,orbit = gd.fast_lyapunov_max(w0, pot, dt=dt, nsteps=nsteps)
    with open(output_file, 'wb') as f:
        pickle.dump(lyap, f)
    # np.save(output_file, lyap.decompose(pot.units).value)

    # orbit_output_file = os.path.join(output_path, 'orbit.npy')
    orbit_output_file = os.path.join(output_path, 'orbit.pickle')
    with open(orbit_output_file, 'wb') as f:
        pickle.dump(orbit, f)
    # np.save(orbit_output_file, (orbit.t.decompose(pot.units).value,
    #                             orbit.pos.decompose(pot.units).value,
    #                             orbit.vel.decompose(pot.units).value))

if __name__ == "__main__":
    from argparse import ArgumentParser
    import logging

    parser = ArgumentParser(description="")
    parser.add_argument("-v", "--verbose", action="store_true",
                        dest="verbose", default=False,
                        help="Be chatty! (default = False)")
    parser.add_argument("-q", "--quiet", action="store_true", dest="quiet",
                        default=False, help="Be quiet! (default = False)")
    parser.add_argument("-o", "--overwrite", dest="overwrite", default=False,
                        action="store_true", help="Overwrite any existing data.")

    parser.add_argument("--results-path", dest="results_path", default=None,
                        help="Path to save the output file.")
    parser.add_argument("--potential", dest="potential_name", required=True,
                        help="Name of the potential YAML file.")

    args = parser.parse_args()

    if args.verbose:
        logger.setLevel(logging.DEBUG)
    elif args.quiet:
        logger.setLevel(logging.ERROR)
    else:
        logger.setLevel(logging.INFO)

    main(args.potential_name, results_path=args.results_path, overwrite=args.overwrite)
<|endoftext|>


<commit_before>import sys, sim

"""
Conversion factors for subsecondtime (femtoseconds) to other units
"""

class Time:
  FS = 1.
  PS = 1e3
  NS = 1e6
  US = 1e9
  MS = 1e12
  S  = 1e15

"""
Register an object as being a hooks listener.
  Will register hooks callback functions to the object's member functions with matching (lowercase) names
  I.e. obj.hook_roi_begin will be called at HOOK_ROI_BEGIN, etc.
  Additionally, obj.setup(arg) will be called with <arg> being the script's arguments
"""

def register(obj):

  for name, hook in sim.hooks.hooks.items():
    func = getattr(obj, name.lower(), None)
    if func and callable(func):
      sim.hooks.register(hook, func)

  if hasattr(obj, 'setup') and callable(obj.setup):
    obj.setup(sys.argv[1])

"""
Register a callback for a given SimUser command.
  E.g.: Python script calls sim.util.register_command(0x123, myfunc)
        Application calls SimUser(0x123, <arg>) from core <core>
        Python callback is made for myfunc(<core>, <arg>)
        Return value of SimUser() is that of myfunc()
"""

def register_command(mycmd, func):
  def callback(thread, core, cmd, arg):
    if cmd == mycmd:
      return func(core, arg)
  sim.hooks.register(sim.hooks.HOOK_MAGIC_USER, callback)

"""
Delta manager for statistics.
  StatsDeltaMetric keeps the current, last, and delta value for a given statistic
  StatsDelta keeps a list of StatsDeltaMetric metrics and updates them all at once

Example usage:

  import sim, simutil

  class PrintIpc:
    def __init__(self):
      self.sd = simutil.StatsDelta()
      self.instrs = self.sd.getter("performance_model", 0, "instruction_count")
      self.time = self.sd.getter("performance_model", 0, "elapsed_time")

    def hook_periodic(self, time):
      if self.sd.update(): # returns False the first time (no delta available)
        cycles = self.time.delta * sim.dvfs.get_frequency(0) / 1e6 # convert fs to cycles
        print self.instrs.delta / (cycles or 1)

  simutil.register(PrintIpc())
"""

class StatsDelta:

  class StatsDeltaMetric:
    """Internal object to store current, last and delta stats value.

    Do not instantiate directly, use StatsDelta.getter() instead."""
    def __init__(self, objectName, index, metricName):
      self.getter = sim.stats.getter(objectName, index, metricName)
      self.last = None
      self.delta = None

    def update(self):
      now = float(self.getter())
      if self.last is not None:
        self.delta = now - self.last
      self.last = now

  def __init__(self):
    self.isFirst = True
    self.members = []

  def getter(self, objectName, index, metricName):
    getter = self.StatsDeltaMetric(objectName, index, metricName)
    self.members.append(getter)
    return getter

  def update(self):
    for member in self.members:
      member.update()
    if self.isFirst:
      self.isFirst = False
      return False
    else:
      return True

class Every:
  def __init__(self, interval, callback, statsdelta = None, roi_only = True):
    self.interval = interval
    self.callback = callback
    self.statsdelta = statsdelta
    self.roi_only = roi_only
    self.time_next = 0
    self.time_last = 0
    self.in_roi = False
    register(self)

  def hook_roi_begin(self):
    self.in_roi = True
    self.hook_periodic(sim.stats.time())

  def hook_roi_end(self):
    self.hook_periodic(sim.stats.time())
    self.in_roi = False

  def hook_periodic(self, time):
    if (not self.roi_only or self.in_roi) and time >= self.time_next:
      if self.statsdelta:
        doCall = self.statsdelta.update()
      else:
        doCall = True

      if doCall:
        self.callback(time, time - self.time_last)

      self.time_next = time + self.interval
      self.time_last = time

class EveryIns:
  def __init__(self, interval, callback, roi_only = True):
    self.interval = interval
    self.callback = callback
    self.roi_only = roi_only
    self.icount_next = interval
    self.icount_last = 0
    self.in_roi = False
    register(self)

  def hook_roi_begin(self):
    self.in_roi = True

  def hook_roi_end(self):
    self.in_roi = False

  def hook_periodic_ins(self, icount):
    if (not self.roi_only or self.in_roi) and icount >= self.icount_next:
      self.callback(icount, icount - self.icount_last)

      self.icount_next += self.interval
      self.icount_last = icount
<commit_msg>[sim.util] Every.periodic(): Update time_next before doing the callback, this avoids unintended recursion when the callback triggers ROI end<commit_after>import sys, sim

"""
Conversion factors for subsecondtime (femtoseconds) to other units
"""

class Time:
  FS = 1.
  PS = 1e3
  NS = 1e6
  US = 1e9
  MS = 1e12
  S  = 1e15

"""
Register an object as being a hooks listener.
  Will register hooks callback functions to the object's member functions with matching (lowercase) names
  I.e. obj.hook_roi_begin will be called at HOOK_ROI_BEGIN, etc.
  Additionally, obj.setup(arg) will be called with <arg> being the script's arguments
"""

def register(obj):

  for name, hook in sim.hooks.hooks.items():
    func = getattr(obj, name.lower(), None)
    if func and callable(func):
      sim.hooks.register(hook, func)

  if hasattr(obj, 'setup') and callable(obj.setup):
    obj.setup(sys.argv[1])

"""
Register a callback for a given SimUser command.
  E.g.: Python script calls sim.util.register_command(0x123, myfunc)
        Application calls SimUser(0x123, <arg>) from core <core>
        Python callback is made for myfunc(<core>, <arg>)
        Return value of SimUser() is that of myfunc()
"""

def register_command(mycmd, func):
  def callback(thread, core, cmd, arg):
    if cmd == mycmd:
      return func(core, arg)
  sim.hooks.register(sim.hooks.HOOK_MAGIC_USER, callback)

"""
Delta manager for statistics.
  StatsDeltaMetric keeps the current, last, and delta value for a given statistic
  StatsDelta keeps a list of StatsDeltaMetric metrics and updates them all at once

Example usage:

  import sim, simutil

  class PrintIpc:
    def __init__(self):
      self.sd = simutil.StatsDelta()
      self.instrs = self.sd.getter("performance_model", 0, "instruction_count")
      self.time = self.sd.getter("performance_model", 0, "elapsed_time")

    def hook_periodic(self, time):
      if self.sd.update(): # returns False the first time (no delta available)
        cycles = self.time.delta * sim.dvfs.get_frequency(0) / 1e6 # convert fs to cycles
        print self.instrs.delta / (cycles or 1)

  simutil.register(PrintIpc())
"""

class StatsDelta:

  class StatsDeltaMetric:
    """Internal object to store current, last and delta stats value.

    Do not instantiate directly, use StatsDelta.getter() instead."""
    def __init__(self, objectName, index, metricName):
      self.getter = sim.stats.getter(objectName, index, metricName)
      self.last = None
      self.delta = None

    def update(self):
      now = float(self.getter())
      if self.last is not None:
        self.delta = now - self.last
      self.last = now

  def __init__(self):
    self.isFirst = True
    self.members = []

  def getter(self, objectName, index, metricName):
    getter = self.StatsDeltaMetric(objectName, index, metricName)
    self.members.append(getter)
    return getter

  def update(self):
    for member in self.members:
      member.update()
    if self.isFirst:
      self.isFirst = False
      return False
    else:
      return True

class Every:
  def __init__(self, interval, callback, statsdelta = None, roi_only = True):
    self.interval = interval
    self.callback = callback
    self.statsdelta = statsdelta
    self.roi_only = roi_only
    self.time_next = 0
    self.time_last = 0
    self.in_roi = False
    register(self)

  def hook_roi_begin(self):
    self.in_roi = True
    self.hook_periodic(sim.stats.time())

  def hook_roi_end(self):
    self.hook_periodic(sim.stats.time())
    self.in_roi = False

  def hook_periodic(self, time):
    if (not self.roi_only or self.in_roi) and time >= self.time_next:
      self.time_next = time + self.interval
      self.time_last = time

      if self.statsdelta:
        doCall = self.statsdelta.update()
      else:
        doCall = True

      if doCall:
        self.callback(time, time - self.time_last)

class EveryIns:
  def __init__(self, interval, callback, roi_only = True):
    self.interval = interval
    self.callback = callback
    self.roi_only = roi_only
    self.icount_next = interval
    self.icount_last = 0
    self.in_roi = False
    register(self)

  def hook_roi_begin(self):
    self.in_roi = True

  def hook_roi_end(self):
    self.in_roi = False

  def hook_periodic_ins(self, icount):
    if (not self.roi_only or self.in_roi) and icount >= self.icount_next:
      self.icount_next += self.interval
      self.icount_last = icount

      self.callback(icount, icount - self.icount_last)
<|endoftext|>


<commit_before>from DmxPro import DmxPro

dmx = DmxPro('/dev/ttyUSB0')

dmx.setChannel(1,252) # DMX mode
dmx.setChannel(2,152) # pattern
dmx.setChannel(3,120) # zoom
dmx.setChannel(4,0) # rotation y
dmx.setChannel(5,0) # rotation x
dmx.setChannel(6,0)  # rotation z
dmx.setChannel(7,0)  # move to x position
dmx.setChannel(8,0)  # move to y position
dmx.setChannel(9,32)  # color: 16=red, 32=green, 48=yellow (both), 127=changing

dmx.send()

<commit_msg>Delete test_pro.py<commit_after><|endoftext|>