<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 getpass
import unittest
import time

from worker_installer.tests import get_remote_runner, get_local_runner, \
    id_generator, get_local_worker_config, get_local_context, get_remote_context, \
    get_remote_management_worker_config, VAGRANT_MACHINE_IP, \
    get_remote_worker_config, get_local_management_worker_config

__author__ = 'elip'

import os

from celery import Celery

from worker_installer.tasks import install, start, build_env_string, uninstall, stop

def _extract_registered_plugins(broker_url, worker_name):

    c = Celery(broker=broker_url, backend=broker_url)
    tasks = c.control.inspect.registered(c.control.inspect())
    print "tasks : {0}".format(tasks)

    # retry a few times
    attempt = 0
    while tasks is None and attempt <= 3:
        tasks = c.control.inspect.registered(c.control.inspect())
        attempt += 1
        time.sleep(3)
    if tasks is None:
        return set()

    plugins = set()
    worker_name = "celery.{0}".format(worker_name)
    if worker_name in tasks:
        worker_tasks = tasks.get(worker_name)
        print "worker tasks are : {0}".format(worker_tasks)
        for node_tasks in worker_tasks:
            for task in node_tasks:
                plugin_name = task.split('.')[0]
                full_plugin_name = '{0}@{1}'.format(worker_name, plugin_name)
                plugins.add(full_plugin_name)
    return plugins

class TestRemoteInstallerCase(unittest.TestCase):

    VM_ID = "TestRemoteInstallerCase"
    RUNNER = None
    RAN_ID = id_generator(3)

    @classmethod
    def setUpClass(cls):
        from vagrant_helper import launch_vagrant
        launch_vagrant(cls.VM_ID, cls.RAN_ID)
        cls.RUNNER = get_remote_runner()

    @classmethod
    def tearDownClass(cls):
        from vagrant_helper import terminate_vagrant
        terminate_vagrant(cls.VM_ID, cls.RAN_ID)

    def test_install_worker(self):

        ctx = get_remote_context()
        worker_config = get_remote_worker_config()

        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('celery.{0}@plugin_installer'.format(worker_config["name"]) in plugins)
        self.assertTrue('celery.{0}@kv_store'.format(worker_config["name"]) in plugins)

    def test_install_multiple_workers(self):

        ctx = get_remote_context()
        worker_config = get_remote_worker_config()
        name1 = worker_config["name"]

        # install first worker
        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        worker_config = get_remote_worker_config()
        name2 = worker_config["name"]

        # install second worker
        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        # lets make sure it did
        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('celery.{0}@plugin_installer'.format(name1) in plugins)
        self.assertTrue('celery.{0}@kv_store'.format(name1) in plugins)
        self.assertTrue('celery.{0}@plugin_installer'.format(name2) in plugins)
        self.assertTrue('celery.{0}@kv_store'.format(name2) in plugins)

    def test_install_management_worker(self):

        ctx = get_remote_context()
        worker_config = get_remote_management_worker_config()

        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('celery.{0}@plugin_installer'.format(worker_config["name"]) in plugins)
        self.assertTrue('celery.{0}@kv_store'.format(worker_config["name"]) in plugins)
        self.assertTrue('celery.{0}@worker_installer'.format(worker_config["name"]) in plugins)

    def test_remove_worker(self):

        ctx = get_remote_context()
        worker_config = get_remote_worker_config()

        # install first worker
        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        stop(ctx, worker_config, local=False)
        uninstall(ctx, worker_config, local=False)

        plugins = _extract_registered_plugins(worker_config["env"]["BROKER_URL"], worker_config["name"])

        # make sure the worker has stopped
        self.assertTrue(len(plugins) == 0)

        # make sure files are deleted
        service_file_path = "/etc/init.d/celeryd-{0}".format(worker_config["name"])
        defaults_file_path = "/etc/default/celeryd-{0}".format(worker_config["name"])

        self.assertFalse(self.RUNNER.exists(service_file_path))
        self.assertFalse(self.RUNNER.exists(defaults_file_path))

    def test_uninstall_non_existing_worker(self):

        worker_config = {
            "name": "non-existing-worker",
            "user": "vagrant",
            "port": 22,
            "key": "~/.vagrant.d/insecure_private_key",
            "env": {
                "BROKER_URL": "amqp://guest:guest@10.0.0.1:5672//",
                "MANAGEMENT_IP": VAGRANT_MACHINE_IP,
                "MANAGER_REST_PORT": 8100
            }
        }
        uninstall(get_remote_context(), worker_config, True)

    def test_stop_non_existing_worker(self):

        worker_config = {
            "name": "non-existing-worker",
            "user": "vagrant",
            "port": 22,
            "key": "~/.vagrant.d/insecure_private_key",
            "env": {
                "BROKER_URL": "amqp://guest:guest@10.0.0.1:5672//",
                "MANAGEMENT_IP": VAGRANT_MACHINE_IP,
                "MANAGER_REST_PORT": 8100
            }
        }
        stop(get_remote_context(), worker_config, True)

class TestLocalInstallerCase(unittest.TestCase):

    RUNNER = None

    @classmethod
    def setUpClass(cls):
        cls.RUNNER = get_local_runner()
        os.environ["BROKER_URL"] = "localhost"
        os.environ["MANAGEMENT_IP"] = "localhost"

    def test_install_worker(self):

        ctx = get_local_context()
        worker_config = get_local_worker_config()

        install(ctx, worker_config, local=True)
        start(ctx, worker_config, local=True)

        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('celery.{0}@plugin_installer'.format(worker_config["name"]) in plugins)
        self.assertTrue('celery.{0}@kv_store'.format(worker_config["name"]) in plugins)

    def test_install_management_worker(self):

        ctx = get_local_context()
        worker_config = get_local_management_worker_config()

        install(ctx, worker_config, local=True)
        start(ctx, worker_config, local=True)

        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('celery.{0}@plugin_installer'.format(worker_config["name"]) in plugins)
        self.assertTrue('celery.{0}@kv_store'.format(worker_config["name"]) in plugins)
        self.assertTrue('celery.{0}@worker_installer'.format(worker_config["name"]) in plugins)

    def test_remove_worker(self):

        ctx = get_local_context()
        worker_config = get_local_worker_config()

        # install first worker
        install(ctx, worker_config, local=True)
        start(ctx, worker_config, local=True)

        stop(ctx, worker_config, local=True)
        uninstall(ctx, worker_config, local=True)

        plugins = _extract_registered_plugins(worker_config["env"]["BROKER_URL"], worker_config["name"])

        # make sure the worker has stopped
        self.assertTrue(len(plugins) == 0)

        # make sure files are deleted
        service_file_path = "/etc/init.d/celeryd-{0}".format(worker_config["name"])
        defaults_file_path = "/etc/default/celeryd-{0}".format(worker_config["name"])

        self.assertFalse(self.RUNNER.exists(service_file_path))
        self.assertFalse(self.RUNNER.exists(defaults_file_path))

    def test_create_env_string(self):
        env = {
            "TEST_KEY1": "TEST_VALUE1",
            "TEST_KEY2": "TEST_VALUE2"
        }

        expected_string = "export TEST_KEY2=\"TEST_VALUE2\"\nexport TEST_KEY1=\"TEST_VALUE1\"\n"

        assert expected_string == build_env_string(env)

    def test_create_empty_env_string(self):

        expected_string = ""

        assert expected_string == build_env_string({})

    def test_uninstall_non_existing_worker(self):

        worker_config = {
            "user": getpass.getuser(),
            "name": "non-existing-worker",
            "env": {
                "BROKER_URL": "amqp://guest:guest@10.0.0.1:5672//",
                "MANAGEMENT_IP": VAGRANT_MACHINE_IP,
                "MANAGER_REST_PORT": 8100
            }
        }
        uninstall(get_remote_context(), worker_config, True)

    def test_stop_non_existing_worker(self):

        worker_config = {
            "user": getpass.getuser(),
            "name": "non-existing-worker",
            "env": {
                "BROKER_URL": "amqp://guest:guest@10.0.0.1:5672//",
                "MANAGEMENT_IP": VAGRANT_MACHINE_IP,
                "MANAGER_REST_PORT": 8100
            }
        }
        stop(get_remote_context(), worker_config, True)

if __name__ == '__main__':
    unittest.main()<commit_msg>CLOUDIFY-2514 fix worker name<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 getpass
import unittest
import time

from worker_installer.tests import get_remote_runner, get_local_runner, \
    id_generator, get_local_worker_config, get_local_context, get_remote_context, \
    get_remote_management_worker_config, VAGRANT_MACHINE_IP, \
    get_remote_worker_config, get_local_management_worker_config

__author__ = 'elip'

import os

from celery import Celery

from worker_installer.tasks import install, start, build_env_string, uninstall, stop

def _extract_registered_plugins(broker_url, worker_name):

    c = Celery(broker=broker_url, backend=broker_url)
    tasks = c.control.inspect.registered(c.control.inspect())
    print "tasks : {0}".format(tasks)

    # retry a few times
    attempt = 0
    while tasks is None and attempt <= 3:
        tasks = c.control.inspect.registered(c.control.inspect())
        attempt += 1
        time.sleep(3)
    if tasks is None:
        return set()

    plugins = set()
    full_worker_name = "celery.{0}".format(worker_name)
    if worker_name in tasks:
        worker_tasks = tasks.get(full_worker_name)
        print "worker tasks are : {0}".format(worker_tasks)
        for node_tasks in worker_tasks:
            for task in node_tasks:
                plugin_name = task.split('.')[0]
                full_plugin_name = '{0}@{1}'.format(worker_name, plugin_name)
                plugins.add(full_plugin_name)
    return plugins

class TestRemoteInstallerCase(unittest.TestCase):

    VM_ID = "TestRemoteInstallerCase"
    RUNNER = None
    RAN_ID = id_generator(3)

    @classmethod
    def setUpClass(cls):
        from vagrant_helper import launch_vagrant
        launch_vagrant(cls.VM_ID, cls.RAN_ID)
        cls.RUNNER = get_remote_runner()

    @classmethod
    def tearDownClass(cls):
        from vagrant_helper import terminate_vagrant
        terminate_vagrant(cls.VM_ID, cls.RAN_ID)

    def test_install_worker(self):

        ctx = get_remote_context()
        worker_config = get_remote_worker_config()

        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('{0}@plugin_installer'.format(worker_config["name"]) in plugins)
        self.assertTrue('{0}@kv_store'.format(worker_config["name"]) in plugins)

    def test_install_multiple_workers(self):

        ctx = get_remote_context()
        worker_config = get_remote_worker_config()
        name1 = worker_config["name"]

        # install first worker
        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        worker_config = get_remote_worker_config()
        name2 = worker_config["name"]

        # install second worker
        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        # lets make sure it did
        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('{0}@plugin_installer'.format(name1) in plugins)
        self.assertTrue('{0}@kv_store'.format(name1) in plugins)
        self.assertTrue('{0}@plugin_installer'.format(name2) in plugins)
        self.assertTrue('{0}@kv_store'.format(name2) in plugins)

    def test_install_management_worker(self):

        ctx = get_remote_context()
        worker_config = get_remote_management_worker_config()

        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('{0}@plugin_installer'.format(worker_config["name"]) in plugins)
        self.assertTrue('{0}@kv_store'.format(worker_config["name"]) in plugins)
        self.assertTrue('{0}@worker_installer'.format(worker_config["name"]) in plugins)

    def test_remove_worker(self):

        ctx = get_remote_context()
        worker_config = get_remote_worker_config()

        # install first worker
        install(ctx, worker_config, local=False)
        start(ctx, worker_config, local=False)

        stop(ctx, worker_config, local=False)
        uninstall(ctx, worker_config, local=False)

        plugins = _extract_registered_plugins(worker_config["env"]["BROKER_URL"], worker_config["name"])

        # make sure the worker has stopped
        self.assertTrue(len(plugins) == 0)

        # make sure files are deleted
        service_file_path = "/etc/init.d/celeryd-{0}".format(worker_config["name"])
        defaults_file_path = "/etc/default/celeryd-{0}".format(worker_config["name"])

        self.assertFalse(self.RUNNER.exists(service_file_path))
        self.assertFalse(self.RUNNER.exists(defaults_file_path))

    def test_uninstall_non_existing_worker(self):

        worker_config = {
            "name": "non-existing-worker",
            "user": "vagrant",
            "port": 22,
            "key": "~/.vagrant.d/insecure_private_key",
            "env": {
                "BROKER_URL": "amqp://guest:guest@10.0.0.1:5672//",
                "MANAGEMENT_IP": VAGRANT_MACHINE_IP,
                "MANAGER_REST_PORT": 8100
            }
        }
        uninstall(get_remote_context(), worker_config, True)

    def test_stop_non_existing_worker(self):

        worker_config = {
            "name": "non-existing-worker",
            "user": "vagrant",
            "port": 22,
            "key": "~/.vagrant.d/insecure_private_key",
            "env": {
                "BROKER_URL": "amqp://guest:guest@10.0.0.1:5672//",
                "MANAGEMENT_IP": VAGRANT_MACHINE_IP,
                "MANAGER_REST_PORT": 8100
            }
        }
        stop(get_remote_context(), worker_config, True)

class TestLocalInstallerCase(unittest.TestCase):

    RUNNER = None

    @classmethod
    def setUpClass(cls):
        cls.RUNNER = get_local_runner()
        os.environ["BROKER_URL"] = "localhost"
        os.environ["MANAGEMENT_IP"] = "localhost"

    def test_install_worker(self):

        ctx = get_local_context()
        worker_config = get_local_worker_config()

        install(ctx, worker_config, local=True)
        start(ctx, worker_config, local=True)

        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('{0}@plugin_installer'.format(worker_config["name"]) in plugins)
        self.assertTrue('{0}@kv_store'.format(worker_config["name"]) in plugins)

    def test_install_management_worker(self):

        ctx = get_local_context()
        worker_config = get_local_management_worker_config()

        install(ctx, worker_config, local=True)
        start(ctx, worker_config, local=True)

        ctx.logger.info("extracting plugins from newly installed worker")
        plugins = _extract_registered_plugins(worker_config['env']['BROKER_URL'], worker_config["name"])
        if not plugins:
            raise AssertionError("No plugins were detected on the installed worker")

        ctx.logger.info("Detected plugins : {0}".format(plugins))

        # check built in agent plugins are registered
        self.assertTrue('{0}@plugin_installer'.format(worker_config["name"]) in plugins)
        self.assertTrue('{0}@kv_store'.format(worker_config["name"]) in plugins)
        self.assertTrue('{0}@worker_installer'.format(worker_config["name"]) in plugins)

    def test_remove_worker(self):

        ctx = get_local_context()
        worker_config = get_local_worker_config()

        # install first worker
        install(ctx, worker_config, local=True)
        start(ctx, worker_config, local=True)

        stop(ctx, worker_config, local=True)
        uninstall(ctx, worker_config, local=True)

        plugins = _extract_registered_plugins(worker_config["env"]["BROKER_URL"], worker_config["name"])

        # make sure the worker has stopped
        self.assertTrue(len(plugins) == 0)

        # make sure files are deleted
        service_file_path = "/etc/init.d/celeryd-{0}".format(worker_config["name"])
        defaults_file_path = "/etc/default/celeryd-{0}".format(worker_config["name"])

        self.assertFalse(self.RUNNER.exists(service_file_path))
        self.assertFalse(self.RUNNER.exists(defaults_file_path))

    def test_create_env_string(self):
        env = {
            "TEST_KEY1": "TEST_VALUE1",
            "TEST_KEY2": "TEST_VALUE2"
        }

        expected_string = "export TEST_KEY2=\"TEST_VALUE2\"\nexport TEST_KEY1=\"TEST_VALUE1\"\n"

        assert expected_string == build_env_string(env)

    def test_create_empty_env_string(self):

        expected_string = ""

        assert expected_string == build_env_string({})

    def test_uninstall_non_existing_worker(self):

        worker_config = {
            "user": getpass.getuser(),
            "name": "non-existing-worker",
            "env": {
                "BROKER_URL": "amqp://guest:guest@10.0.0.1:5672//",
                "MANAGEMENT_IP": VAGRANT_MACHINE_IP,
                "MANAGER_REST_PORT": 8100
            }
        }
        uninstall(get_remote_context(), worker_config, True)

    def test_stop_non_existing_worker(self):

        worker_config = {
            "user": getpass.getuser(),
            "name": "non-existing-worker",
            "env": {
                "BROKER_URL": "amqp://guest:guest@10.0.0.1:5672//",
                "MANAGEMENT_IP": VAGRANT_MACHINE_IP,
                "MANAGER_REST_PORT": 8100
            }
        }
        stop(get_remote_context(), worker_config, True)

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


<commit_before>from __future__ import division

import sys
import math
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

import scipy.stats
import numpy as NP

def mad(l):
    """
    Compute the median absolute deviation (a robust measure of spread)
    of the list of values *l*.
    """
    median = NP.median(l)
    return NP.median([abs(x - median) for x in l]), median

def robust_std(l, alpha=1/scipy.stats.norm.ppf(0.75)):
    """
    Compute a robust estimate of the standard deviation for the list
    of values *l* (by default, for normally distributed samples ---
    see https://en.wikipedia.org/wiki/Median_absolute_deviation).
    """
    return alpha * mad(l)[0]

def weighted_avg_and_std(values, weights):
    """
    Return the weighted average and standard deviation.

    values, weights -- Numpy ndarrays with the same shape.

    References:
    - http://stackoverflow.com/questions/2413522/weighted-standard-deviation-in-numpy
    - https://en.wikipedia.org/wiki/Mean_square_weighted_deviation

    Note: The method is biased (division by N and not (N-1)).
    """
    average = NP.average(values, weights=weights)
    variance = NP.average((values-average)**2, weights=weights)  # Fast and numerically precise
    return (average, math.sqrt(variance))

class Stats(object):
    def __init__(self):
        self.reset()

    def reset(self):
        self.mu = 0
        self.s2 = 0
        self.N = 0

    @property
    def mean(self):
        return self.mu

    @property
    def var(self):
        return self.s2 / (self.N-1) if self.N >= 2 else float('nan')

    @property
    def sigma(self):
        return math.sqrt(self.var)

    @property
    def rms(self):
        return math.sqrt(self.mean**2 + self.var**2)

    def __call__(self, *x):
        """
        """
        for x_i in x:
            self.N += 1
            mu_previous = self.mu
            self.mu += (x_i - self.mu) / self.N
            self.s2 += (x_i - mu_previous) * (x_i - self.mu)
        return self

    def __str__(self):
        return str(self.mean) + ' ' + str(self.sigma)

def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = ArgumentParser('Report mean and standard deviation for the stream of numbers read from stdin',
                            formatter_class=ArgumentDefaultsHelpFormatter)
    args = parser.parse_args(argv[1:])

    stats = Stats()
    for line in sys.stdin:
        stats(float(line))
    print(stats)

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>Added min / max tracking and construction from list.<commit_after>from __future__ import division

import sys
import math
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

import scipy.stats
import numpy as NP

def mad(l):
    """
    Compute the median absolute deviation (a robust measure of spread)
    of the list of values *l*.
    """
    median = NP.median(l)
    return NP.median([abs(x - median) for x in l]), median

def robust_std(l, alpha=1/scipy.stats.norm.ppf(0.75)):
    """
    Compute a robust estimate of the standard deviation for the list
    of values *l* (by default, for normally distributed samples ---
    see https://en.wikipedia.org/wiki/Median_absolute_deviation).
    """
    return alpha * mad(l)[0]

def weighted_avg_and_std(values, weights):
    """
    Return the weighted average and standard deviation.

    values, weights -- Numpy ndarrays with the same shape.

    References:
    - http://stackoverflow.com/questions/2413522/weighted-standard-deviation-in-numpy
    - https://en.wikipedia.org/wiki/Mean_square_weighted_deviation

    Note: The method is biased (division by N and not (N-1)).
    """
    average = NP.average(values, weights=weights)
    variance = NP.average((values-average)**2, weights=weights)  # Fast and numerically precise
    return (average, math.sqrt(variance))

class Stats(object):
    def __init__(self, *args):
        self.reset()
        self(*args)

    def reset(self):
        self.mu = 0
        self.s2 = 0
        self.N = 0
        self._max = -float('inf')
        self._min = float('inf')

    @property
    def mean(self):
        return self.mu

    @property
    def var(self):
        return self.s2 / (self.N-1) if self.N >= 2 else float('nan')

    @property
    def sigma(self):
        return math.sqrt(self.var)

    @property
    def rms(self):
        return math.sqrt(self.mean**2 + self.var**2)

    @property
    def max(self):
        return self._max

    @property
    def min(self):
        return self._min

    def __call__(self, *x):
        """
        """
        for x_i in x:
            self.N += 1
            mu_previous = self.mu
            self.mu += (x_i - self.mu) / self.N
            self.s2 += (x_i - mu_previous) * (x_i - self.mu)
            if x_i < self._min:
                self._min = x_i
            if x_i > self._max:
                self._max = x_i
        return self

    def __str__(self):
        return 'Stats: mean={:f} sigma={:f}'.format(self.mean, self.sigma)

def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = ArgumentParser('Report mean and standard deviation for the stream of numbers read from stdin',
                            formatter_class=ArgumentDefaultsHelpFormatter)
    args = parser.parse_args(argv[1:])

    stats = Stats()
    for line in sys.stdin:
        stats(float(line))
    print(stats)

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


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# gPodder - A media aggregator and podcast client
# Copyright (c) 2005-2011 Thomas Perl and the gPodder Team
#
# gPodder 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.
#
# gPodder 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/>.
#

# Soundcloud.com API client module for gPodder
# Thomas Perl <thp@gpodder.org>; 2009-11-03

import os
import time

import re
import email
import email.header

from django.conf import settings
from django.utils.translation import gettext as _

from feedservice.parse.models import Feed, Episode, File
from feedservice.parse.feed import Feedparser, FeedparserEpisodeParser
from feedservice.parse.models import ParserException
from feedservice.parse.mimetype import get_mimetype
from feedservice.utils import json, requests

import logging
logger = logging.getLogger(__name__)

class SoundcloudError(ParserException):
    """ General Exception raised by the Soundcloud parser """

class SoundcloudUser(object):
    def __init__(self, username):
        self.username = username

    def get_coverart(self):
        key = ':'.join((self.username, 'avatar_url'))

        image = None
        json_url = 'https://api.soundcloud.com/users/%s.json?consumer_key=%s' \
            % (self.username, settings.SOUNDCLOUD_CONSUMER_KEY)

        user_info = requests.get(json_url).json()
        return user_info.get('avatar_url', None)

    def get_tracks(self, feed):
        """Get a generator of tracks from a SC user

        The generator will give you a dictionary for every
        track it can find for its user."""

        json_url = 'https://api.soundcloud.com/users/%(user)s/%(feed)s.json?filter=downloadable&consumer_key=%(consumer_key)s&limit=200' \
                % {
            "user":self.get_user_id(),
            "feed":feed,
            "consumer_key": settings.SOUNDCLOUD_CONSUMER_KEY
        }

        logger.debug("loading %s", json_url)

        response = requests.get(json_url)
        json_tracks = response.json()

        self._check_error(response)

        tracks = [track for track in json_tracks if track['downloadable']]
        total_count = len(tracks) + len([track for track in json_tracks
                                          if not track['downloadable']])

        if len(tracks) == 0 and total_count > 0:
            logger.warn("Download of all %i %s of user %s is disabled" %
                        (total_count, feed, self.username))
        else:
            logger.warn("%i/%i downloadable tracks for user %s %s feed" %
                        (len(tracks), total_count, self.username, feed))

        for track in tracks:
            # Prefer stream URL (MP3), fallback to download URL
            url = track.get('stream_url', track['download_url']) + \
                '?consumer_key=%(consumer_key)s' \
                % { 'consumer_key': settings.SOUNDCLOUD_CONSUMER_KEY }

            filesize, filetype, filename = self.get_metadata(url)

            yield {
                'title': track.get('title', track.get('permalink')) or _('Unknown track'),
                'link': track.get('permalink_url') or 'https://soundcloud.com/' + self.username,
                'description': track.get('description') or _('No description available'),
                'url': url,
                'file_size': int(filesize),
                'mime_type': filetype,
                'guid': track.get('permalink', track.get('id')),
                'published': self.parsedate(track.get('created_at', None)),
            }

    def get_user_info(self):
        key = ':'.join((self.username, 'user_info'))

        json_url = 'https://api.soundcloud.com/users/%s.json?consumer_key=%s' % (self.username, settings.SOUNDCLOUD_CONSUMER_KEY)
        user_info = requests.get(json_url).json()

        return user_info

    def get_user_id(self):
        user_info = self.get_user_info()
        return user_info.get('id', None)

    def _check_error(self, response):
        if 'errors' not in response:
            return

        errors = response['errors']
        msg = ';'.join(err['error_message'] for err in errors)
        raise SoundcloudError(msg)

    @staticmethod
    def get_param(s, param='filename', header='content-disposition'):
        """Get a parameter from a string of headers

        By default, this gets the "filename" parameter of
        the content-disposition header. This works fine
        for downloads from Soundcloud.
        """
        msg = email.message_from_string(s)
        if header in msg:
            value = msg.get_param(param, header=header)
            decoded_list = email.Header.decode_header(value)
            value = []
            for part, encoding in decoded_list:
                if encoding:
                    value.append(part.decode(encoding))
                else:
                    value.append(str(part))
            return ''.join(value)

        return None

    def get_metadata(self, url):
        """Get file download metadata

        Returns a (size, type, name) from the given download
        URL. Will use the network connection to determine the
        metadata via the HTTP header fields.
        """

        res = requests.head(url)
        return (res.headers['Content-Length'], res.headers['Content-Type'],
                os.path.basename(os.path.dirname(res.url)))

    @staticmethod
    def parsedate(s):
        """Parse a string into a unix timestamp

        Only strings provided by Soundcloud's API are
        parsed with this function (2009/11/03 13:37:00).
        """
        m = re.match(r'(\d{4})/(\d{2})/(\d{2}) (\d{2}):(\d{2}):(\d{2})', s)
        return time.mktime(tuple([int(x) for x in m.groups()]+[0, 0, -1]))

class SoundcloudParser(Feedparser):
    URL_REGEX = re.compile('https?://([a-z]+\.)?soundcloud\.com/([^/]+)$', re.I)

    @classmethod
    def handles_url(cls, url):
        return bool(cls.URL_REGEX.match(url))

    def __init__(self, feed_url, resp, text_processor=None):
        m = self.__class__.URL_REGEX.match(feed_url)
        subdomain, self.username = m.groups()
        self.sc_user = SoundcloudUser(self.username)

        super(SoundcloudParser, self).__init__(feed_url, resp,
                                               text_processor=text_processor)

    def get_title(self):
        return '%s on Soundcloud' % self.username

    def get_logo_url(self):
        return self.sc_user.get_coverart()

    def get_link(self):
        return 'https://soundcloud.com/%s' % self.username

    def get_description(self):
        return 'Tracks published by %s on Soundcloud.' % self.username

    def get_author(self):
        return self.username

    def get_episodes(self):
        tracks = self.sc_user.get_tracks('tracks')
        parsers = [SoundcloudEpisodeParser(t, self.get_author(),
                   text_processor=self.text_processor) for t in tracks]
        return [p.get_episode() for p in parsers]

class SoundcloudFavParser(SoundcloudParser):
    URL_REGEX = re.compile(
        'https?://([a-z]+\.)?soundcloud\.com/([^/]+)/favorites', re.I)

    def __init__(self, *args, **kwargs):
        super(SoundcloudFavParser, self).__init__(*args, **kwargs)

    @classmethod
    def handles_url(cls, url):
        return bool(cls.URL_REGEX.match(url))

    def get_title(self):
        return '%s\'s favorites on Soundcloud' % self.username

    def get_link(self):
        return 'https://soundcloud.com/%s/favorites' % self.username

    def get_description(self):
        return 'Tracks favorited by %s on Soundcloud.' % self.username

class SoundcloudEpisodeParser(FeedparserEpisodeParser):

    def __init__(self, track, author, text_processor=None):
        super(SoundcloudEpisodeParser, self).__init__(
            track, text_processor=text_processor)
        self.author = author

    def get_guid(self):
        return self.entry.get('guid', None)

    def get_title(self):
        return self.entry.get('title', None)

    def get_link(self):
        return self.entry.get('link', None)

    def get_author(self):
        return self.author

    def get_description(self):
        return self.entry.get('description', None)

    def get_duration(self):
        return None

    def get_language(self):
        return None

    def get_files(self):
        url = self.entry.get('url', None)
        # we don't want to leak the consumer key
        url = url.replace(settings.SOUNDCLOUD_CONSUMER_KEY, '')
        mimetype = get_mimetype(self.entry.get('mimetype', None), url)
        filesize = self.entry.get('length', None)

        yield File([url], mimetype, filesize)

    def get_timestamp(self):
        pd = self.entry.get('pubDate', None)
        try:
            return int(pd)
        except TypeError:
            return None
<commit_msg>Fix error handling<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# gPodder - A media aggregator and podcast client
# Copyright (c) 2005-2011 Thomas Perl and the gPodder Team
#
# gPodder 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.
#
# gPodder 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/>.
#

# Soundcloud.com API client module for gPodder
# Thomas Perl <thp@gpodder.org>; 2009-11-03

import os
import time

import re
import email
import email.header

from django.conf import settings
from django.utils.translation import gettext as _

from feedservice.parse.models import Feed, Episode, File
from feedservice.parse.feed import Feedparser, FeedparserEpisodeParser
from feedservice.parse.models import ParserException
from feedservice.parse.mimetype import get_mimetype
from feedservice.utils import json, requests

import logging
logger = logging.getLogger(__name__)

class SoundcloudError(ParserException):
    """ General Exception raised by the Soundcloud parser """

class SoundcloudUser(object):
    def __init__(self, username):
        self.username = username

    def get_coverart(self):
        key = ':'.join((self.username, 'avatar_url'))

        image = None
        json_url = 'https://api.soundcloud.com/users/%s.json?consumer_key=%s' \
            % (self.username, settings.SOUNDCLOUD_CONSUMER_KEY)

        user_info = requests.get(json_url).json()
        return user_info.get('avatar_url', None)

    def get_tracks(self, feed):
        """Get a generator of tracks from a SC user

        The generator will give you a dictionary for every
        track it can find for its user."""

        json_url = 'https://api.soundcloud.com/users/%(user)s/%(feed)s.json?filter=downloadable&consumer_key=%(consumer_key)s&limit=200' \
                % {
            "user":self.get_user_id(),
            "feed":feed,
            "consumer_key": settings.SOUNDCLOUD_CONSUMER_KEY
        }

        logger.debug("loading %s", json_url)

        response = requests.get(json_url)
        json_tracks = response.json()

        self._check_error(json_tracks)

        tracks = [track for track in json_tracks if track['downloadable']]
        total_count = len(tracks) + len([track for track in json_tracks
                                          if not track['downloadable']])

        if len(tracks) == 0 and total_count > 0:
            logger.warn("Download of all %i %s of user %s is disabled" %
                        (total_count, feed, self.username))
        else:
            logger.warn("%i/%i downloadable tracks for user %s %s feed" %
                        (len(tracks), total_count, self.username, feed))

        for track in tracks:
            # Prefer stream URL (MP3), fallback to download URL
            url = track.get('stream_url', track['download_url']) + \
                '?consumer_key=%(consumer_key)s' \
                % { 'consumer_key': settings.SOUNDCLOUD_CONSUMER_KEY }

            filesize, filetype, filename = self.get_metadata(url)

            yield {
                'title': track.get('title', track.get('permalink')) or _('Unknown track'),
                'link': track.get('permalink_url') or 'https://soundcloud.com/' + self.username,
                'description': track.get('description') or _('No description available'),
                'url': url,
                'file_size': int(filesize),
                'mime_type': filetype,
                'guid': track.get('permalink', track.get('id')),
                'published': self.parsedate(track.get('created_at', None)),
            }

    def get_user_info(self):
        key = ':'.join((self.username, 'user_info'))

        json_url = 'https://api.soundcloud.com/users/%s.json?consumer_key=%s' % (self.username, settings.SOUNDCLOUD_CONSUMER_KEY)
        user_info = requests.get(json_url).json()

        return user_info

    def get_user_id(self):
        user_info = self.get_user_info()
        return user_info.get('id', None)

    def _check_error(self, response):
        if 'errors' not in response:
            return

        errors = response['errors']
        msg = ';'.join(err['error_message'] for err in errors)
        raise SoundcloudError(msg)

    @staticmethod
    def get_param(s, param='filename', header='content-disposition'):
        """Get a parameter from a string of headers

        By default, this gets the "filename" parameter of
        the content-disposition header. This works fine
        for downloads from Soundcloud.
        """
        msg = email.message_from_string(s)
        if header in msg:
            value = msg.get_param(param, header=header)
            decoded_list = email.Header.decode_header(value)
            value = []
            for part, encoding in decoded_list:
                if encoding:
                    value.append(part.decode(encoding))
                else:
                    value.append(str(part))
            return ''.join(value)

        return None

    def get_metadata(self, url):
        """Get file download metadata

        Returns a (size, type, name) from the given download
        URL. Will use the network connection to determine the
        metadata via the HTTP header fields.
        """

        res = requests.head(url)
        return (res.headers['Content-Length'], res.headers['Content-Type'],
                os.path.basename(os.path.dirname(res.url)))

    @staticmethod
    def parsedate(s):
        """Parse a string into a unix timestamp

        Only strings provided by Soundcloud's API are
        parsed with this function (2009/11/03 13:37:00).
        """
        m = re.match(r'(\d{4})/(\d{2})/(\d{2}) (\d{2}):(\d{2}):(\d{2})', s)
        return time.mktime(tuple([int(x) for x in m.groups()]+[0, 0, -1]))

class SoundcloudParser(Feedparser):
    URL_REGEX = re.compile('https?://([a-z]+\.)?soundcloud\.com/([^/]+)$', re.I)

    @classmethod
    def handles_url(cls, url):
        return bool(cls.URL_REGEX.match(url))

    def __init__(self, feed_url, resp, text_processor=None):
        m = self.__class__.URL_REGEX.match(feed_url)
        subdomain, self.username = m.groups()
        self.sc_user = SoundcloudUser(self.username)

        super(SoundcloudParser, self).__init__(feed_url, resp,
                                               text_processor=text_processor)

    def get_title(self):
        return '%s on Soundcloud' % self.username

    def get_logo_url(self):
        return self.sc_user.get_coverart()

    def get_link(self):
        return 'https://soundcloud.com/%s' % self.username

    def get_description(self):
        return 'Tracks published by %s on Soundcloud.' % self.username

    def get_author(self):
        return self.username

    def get_episodes(self):
        tracks = self.sc_user.get_tracks('tracks')
        parsers = [SoundcloudEpisodeParser(t, self.get_author(),
                   text_processor=self.text_processor) for t in tracks]
        return [p.get_episode() for p in parsers]

class SoundcloudFavParser(SoundcloudParser):
    URL_REGEX = re.compile(
        'https?://([a-z]+\.)?soundcloud\.com/([^/]+)/favorites', re.I)

    def __init__(self, *args, **kwargs):
        super(SoundcloudFavParser, self).__init__(*args, **kwargs)

    @classmethod
    def handles_url(cls, url):
        return bool(cls.URL_REGEX.match(url))

    def get_title(self):
        return '%s\'s favorites on Soundcloud' % self.username

    def get_link(self):
        return 'https://soundcloud.com/%s/favorites' % self.username

    def get_description(self):
        return 'Tracks favorited by %s on Soundcloud.' % self.username

class SoundcloudEpisodeParser(FeedparserEpisodeParser):

    def __init__(self, track, author, text_processor=None):
        super(SoundcloudEpisodeParser, self).__init__(
            track, text_processor=text_processor)
        self.author = author

    def get_guid(self):
        return self.entry.get('guid', None)

    def get_title(self):
        return self.entry.get('title', None)

    def get_link(self):
        return self.entry.get('link', None)

    def get_author(self):
        return self.author

    def get_description(self):
        return self.entry.get('description', None)

    def get_duration(self):
        return None

    def get_language(self):
        return None

    def get_files(self):
        url = self.entry.get('url', None)
        # we don't want to leak the consumer key
        url = url.replace(settings.SOUNDCLOUD_CONSUMER_KEY, '')
        mimetype = get_mimetype(self.entry.get('mimetype', None), url)
        filesize = self.entry.get('length', None)

        yield File([url], mimetype, filesize)

    def get_timestamp(self):
        pd = self.entry.get('pubDate', None)
        try:
            return int(pd)
        except TypeError:
            return None
<|endoftext|>


<commit_before>from imports import *

class Background(pygame.sprite.Sprite):

	def __init__(self, game):
		pygame.sprite.Sprite.__init__(self)
		self.game = game
		#self.rect = self.game.get_rect()

		self.image = pygame.Surface(self.game.rect.size).convert_alpha()

		# Lista di immagini tile
		self.tiles = list()

		# Tutti i tiles
		# .::Fogna::.
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna2.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna3.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna_doppio.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna_doppio2.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna_doppio3.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna_doppio4.png").convert())

		# .::Dungeon::.

		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon2.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon3.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon_doppio2.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon_doppio3.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon_doppio4.png").convert())

		# .::Inferno::.
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_morte.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_morte2.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_morte3.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_morte_doppio.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_inferno_doppio2.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_inferno_doppio3.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_inferno_doppio4.png").convert())

		# rect di tile
		self.side = self.tiles[0].get_rect().h

		# offeset per aggiungere rows
		self.offset = 0

		# Lista di rows
		self.rows = list()
		for i in xrange(0, self.game.rect.h/self.side + 1):
			self.rows.append(self.new_row())

	# Metodo che crea rows secondo un indice random per i tiles
	def new_row(self):
		row = pygame.Surface((self.game.rect.w, self.side)).convert_alpha()
		# row.fill((0, 0, 255))
		position = -self.side/2
		for i in xrange(0, self.game.rect.w/self.side + 1):
			if (self.game.frenzy <= F_CALM):
				random_index = random.randint(0, NUM_TILES_FOGNA - 1)
			elif (self.game.frenzy <= F_TRANS2):
				random_index = random.randint(NUM_TILES_FOGNA, NUM_TILES_TOT - NUM_TILES_INFERNO - 1)
			else:
				random_index = random.randint(NUM_TILES_DUNGEON, NUM_TILES_INFERNO - 1)
			row.blit(self.tiles[random_index], (position, 0))
			position += self.tiles[random_index].get_rect().w

		return row

	def update(self):
		self.offset += self.game.tick * self.game.speed
		if (self.offset > self.side):
			self.offset = 0
			del self.rows[-1]
			self.rows.insert(0, self.new_row())
		for i in xrange(len(self.rows)):
			self.image.blit(self.rows[i], (0, i * self.side + self.offset))

		self.image.blit(self.rows[-1], (0, -self.side + self.offset))<commit_msg>Background: sistemato errore sulle condizioni per generare i tiles<commit_after>from imports import *

class Background(pygame.sprite.Sprite):

	def __init__(self, game):
		pygame.sprite.Sprite.__init__(self)
		self.game = game
		#self.rect = self.game.get_rect()

		self.image = pygame.Surface(self.game.rect.size).convert_alpha()

		# Lista di immagini tile
		self.tiles = list()

		# Tutti i tiles
		# .::Fogna::.
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna2.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna3.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna_doppio.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna_doppio2.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna_doppio3.png").convert())
		self.tiles.append(pygame.image.load("img/Fogna/tile_pavim_fogna_doppio4.png").convert())

		# .::Dungeon::.

		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon2.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon3.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon_doppio2.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon_doppio3.png").convert())
		self.tiles.append(pygame.image.load("img/Dungeon/tile_pavim_dungeon_doppio4.png").convert())

		# .::Inferno::.
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_morte.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_morte2.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_morte3.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_morte_doppio.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_inferno_doppio2.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_inferno_doppio3.png").convert())
		self.tiles.append(pygame.image.load("img/Inferno/tile_pavim_inferno_doppio4.png").convert())

		# rect di tile
		self.side = self.tiles[0].get_rect().h

		# offeset per aggiungere rows
		self.offset = 0

		# Lista di rows
		self.rows = list()
		for i in xrange(0, self.game.rect.h/self.side + 1):
			self.rows.append(self.new_row())

	# Metodo che crea rows secondo un indice random per i tiles
	def new_row(self):
		row = pygame.Surface((self.game.rect.w, self.side)).convert_alpha()
		# row.fill((0, 0, 255))
		position = -self.side/2
		for i in xrange(0, self.game.rect.w/self.side + 1):

			if (self.game.frenzy <= F_CALM):
				random_index = random.randint(0, NUM_TILES_FOGNA - 1)
			elif (self.game.frenzy <= F_TRANS2):
				random_index = random.randint(NUM_TILES_FOGNA, NUM_TILES_TOT - NUM_TILES_INFERNO - 1)
			else:
				random_index = random.randint(NUM_TILES_FOGNA + NUM_TILES_DUNGEON, NUM_TILES_TOT - 1)

			row.blit(self.tiles[random_index], (position, 0))
			position += self.tiles[random_index].get_rect().w

		return row

	def update(self):
		self.offset += self.game.tick * self.game.speed
		if (self.offset > self.side):
			self.offset = 0
			del self.rows[-1]
			self.rows.insert(0, self.new_row())
		for i in xrange(len(self.rows)):
			self.image.blit(self.rows[i], (0, i * self.side + self.offset))

		self.image.blit(self.rows[-1], (0, -self.side + self.offset))<|endoftext|>


<commit_before>import logging
import_stmts = (
    'from pysnappy2 import snappy',
    'from pysnappy2_24 import snappy'
)

for status, stmt in enumerate(import_stmts):
    try:
        exec stmt
        break
    except ImportError:
        status = None
if status is None:
    logging.warn("could not import snappy module. Compress/uncompress function will be skipped.")

def uncompress(data, decoding=None):
    try:
        return snappy.uncompress(data, decoding)
    except Exception, err:
        return data

def compress(data, encoding='utf-8'):
    try:
        return snappy.compress(data, encoding)
    except Exception, err:
        return data
<commit_msg>Mb-12196: Remove warning message<commit_after>import logging
import_stmts = (
    'from pysnappy2 import snappy',
    'from pysnappy2_24 import snappy'
)

for status, stmt in enumerate(import_stmts):
    try:
        exec stmt
        break
    except ImportError:
        status = None
#if status is None:
#    logging.warn("could not import snappy module. Compress/uncompress function will be skipped.")

def uncompress(data, decoding=None):
    try:
        return snappy.uncompress(data, decoding)
    except Exception, err:
        return data

def compress(data, encoding='utf-8'):
    try:
        return snappy.compress(data, encoding)
    except Exception, err:
        return data
<|endoftext|>


<commit_before>from multiprocessing import Process

def background(func):
	def threader(*args, **kwargs):
		p = Process(
			target = func,
			args = [*args],
			kwargs = {**kwargs},
		)
		p.start()
	return threader
<commit_msg>Swiched to using Threading<commit_after>from threading import Thread

def background(func):
	def threader(*args, **kwargs):
		p = Thread(
			target = func,
			args = [*args],
			kwargs = {**kwargs},
		)
		p.start()
	return threader
<|endoftext|>


<commit_before>from django.core.management.base import BaseCommand, CommandError
from workflows.models import Category, AbstractWidget, AbstractInput, AbstractOutput, AbstractOption
from django.core import serializers
from collections import Counter
from optparse import make_option

class Command(BaseCommand):
    args = 'file_name'
    help = 'Imports all models from the file named "file_name". All models in the database which have the same uuid as imported models are updated. The folowing models are included in inport: AbstractWidget, Category, AbstractInput, AbstractOutput, and AbstractOption.'

    def handle(self, *args, **options):
        if (len(args)<1):
            raise CommandError('Arguments "file_name" and "package_name" are required!')

        try:
            instr = open(args[0], 'r').read()
        except:
            raise CommandError('There was a problem with opening given input file')

        #get all objects from file and eliminate empty UID and check for UID duplicates
        objsFile = list(serializers.deserialize("json", instr))
        objsFileNoUid = [x for x in objsFile if len(x.object.uid) == 0]
        objsFile = [x for x in objsFile if len(x.object.uid) != 0]
        if len(objsFileNoUid)>0:
            self.stdout.write('File contains %i model(s) without UID field set. Those will not be imported! If you wish to'
                              ' assign them random UIDs then use the "-n" option when exporting models with the "export_package"'
                              ' command. Afterwards, you will be able to import them.\n' % len(objsFileNoUid))
        if len(Counter([x.object.uid for x in objsFile])) != len(objsFile):
            raise CommandError('Input process terminated without any changes to the database. There were multiple equal '
                               'UIDs defined on different models in the given input file. The input procedure can not continue '
                               'from safety reasons. Please resolve manually!')

        #divide new objects by type
        wids = [x for x in objsFile if isinstance(x.object, AbstractWidget)]
        inps = [x for x in objsFile if isinstance(x.object, AbstractInput)]
        outs = [x for x in objsFile if isinstance(x.object, AbstractOutput)]
        opts = [x for x in objsFile if isinstance(x.object, AbstractOption)]
        cats = [x for x in objsFile if isinstance(x.object, Category)]

        #ouput statistics about file
        self.stdout.write('File contains:\n')
        self.stdout.write('    % 4i AbstractWidget(s)\n' % len(wids))
        self.stdout.write('    % 4i AbstractInput(s)\n' % len(inps))
        self.stdout.write('    % 4i AbstractOutput(s)\n' % len(outs))
        self.stdout.write('    % 4i AbstractOption(s)\n' % len(opts))
        self.stdout.write('    % 4i Category(s)\n' % len(cats))

        #get all objects from database
        objsDb = []
        objsDb.extend(AbstractWidget.objects.all())
        objsDb.extend(AbstractInput.objects.all())
        objsDb.extend(AbstractOutput.objects.all())
        objsDb.extend(AbstractOption.objects.all())
        objsDb.extend(Category.objects.all())

        #check for DB UID duplicates
        objsdbDict = dict((x.uid,x) for x in objsDb if len(x.uid) != 0)
        if len([x for x in objsDb if len(x.uid) != 0]) != len(objsdbDict):
            raise CommandError('Input process terminated without any changes to the database. There were multiple equal '
                               'UIDs defined on different models in the database. The input procedure can not continue '
                               'from safety reasons. Please resolve manually!')

        #create new to existing id mapping and check for type match
        idMappingDict = dict()
        for objFile in objsFile:
            if objsdbDict.has_key(objFile.object.uid):
                objDb = objsdbDict[objFile.object.uid]
                objFileTypeId = str(type(objFile.object))+':'+str(objFile.object.id)
                objDbTypeId = str(type(objDb))+':'+str(objDb.id)
                if type(objFile.object) == type(objsdbDict[objFile.object.uid]):
                    idMappingDict[objFileTypeId] = objDb.id
                else:
                    raise CommandError('Input process terminated without any changes to the database. Two models match by uid but not '
                                       'by type:\n    - from file: %s\n    - from database: %s\n    Please resolve manually!'% (objFileTypeId, objDbTypeId))

        #ouput statistics about database
        self.stdout.write('Current database contains %i models,\n' % len(objsDb))
        self.stdout.write('    of which %i models have UID set,\n' % len(objsdbDict))
        self.stdout.write('    of which %i models match with the imported models and will be updated.\n' % len(idMappingDict))

        #prepare statistics
        statDict = dict([('old:'+str(t),len(t.objects.all())) for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]])
        for objFile in objsFile:
            statDict['mod:'+str(type(objFile.object))]=0
            statDict['add:'+str(type(objFile.object))]=0

        #save models to the database - update the ids for the matching models and remove the ids (to get a new one) for the non matching models
        self.stdout.write('Merging file and database models ...')
        for objFile in objsFile:
            objFileTypeId = str(type(objFile.object))+':'+str(objFile.object.id)
            if isinstance(objFile.object, AbstractWidget):
                objFile.old_category_id = objFile.object.category_id
            if isinstance(objFile.object, AbstractInput):
                objFile.old_widget_id = objFile.object.widget_id
            if isinstance(objFile.object, AbstractOutput):
                objFile.old_widget_id = objFile.object.widget_id
            if isinstance(objFile.object, AbstractOption):
                objFile.old_abstract_input_id = objFile.object.abstract_input_id
            if isinstance(objFile.object, Category):
                if not objFile.object.parent_id is None:
                    objFile.old_parent_id = objFile.object.parent_id

            if idMappingDict.has_key(objFileTypeId):
                statDict['mod:'+str(type(objFile.object))]+=1
                objFile.object.id = idMappingDict[objFileTypeId]
            else:
                statDict['add:'+str(type(objFile.object))]+=1
                objFile.object.id = None
            objFile.save()
            idMappingDict[objFileTypeId] = objFile.object.id
        self.stdout.write(' done.\n')

        #correct also the foreign keys
        self.stdout.write('Updating model\'s foreign keys ...')
        for objFile in wids:
            objFile.object.category = Category.objects.get(id=idMappingDict[str(Category)+':'+str(objFile.old_category_id)])
            objFile.save()
        for objFile in inps:
            objFile.object.widget = AbstractWidget.objects.get(id=idMappingDict[str(AbstractWidget)+':'+str(objFile.old_widget_id)])
            objFile.save()
        for objFile in outs:
            objFile.object.widget = AbstractWidget.objects.get(id=idMappingDict[str(AbstractWidget)+':'+str(objFile.old_widget_id)])
            objFile.save()
        for objFile in opts:
            objFile.object.abstract_input = AbstractInput.objects.get(id=idMappingDict[str(AbstractInput)+':'+str(objFile.old_abstract_input_id)])
            objFile.save()
        for objFile in cats:
            if not objFile.object.parent_id is None:
                objFile.object.parent = Category.objects.get(id=idMappingDict[str(Category)+':'+str(objFile.old_parent_id)])
                objFile.save()
        self.stdout.write(' done.\n')

        #update and output statistics
        statDict = dict(statDict.items() + dict([('new:'+str(t),len(t.objects.all())) for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]]).items())
        self.stdout.write('Database models count statistics: pre-import + ( added | modified ) = after-import\n')
        for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]:
            self.stdout.write('    % 15s: % 5i + (% 4i | % 4i ) = % 5i\n' % (t.__name__, statDict['old:'+str(t)], statDict['add:'+str(t)], statDict['mod:'+str(t)], statDict['new:'+str(t)]))

        self.stdout.write('Import procedure successfully finished.\n')<commit_msg>Improved import_package functionality - now import can remove widget's outputs,inputs and options which are not beeing used.<commit_after>from django.core.management.base import BaseCommand, CommandError
from workflows.models import Category, AbstractWidget, AbstractInput, AbstractOutput, AbstractOption
from django.core import serializers
from collections import Counter
from optparse import make_option

class Command(BaseCommand):
    args = 'file_name'
    help = 'Imports all models from the file named "file_name". All models in the database which have the same uuid as imported models are updated. The folowing models are included in inport: AbstractWidget, Category, AbstractInput, AbstractOutput, and AbstractOption.'
    option_list = BaseCommand.option_list + (
        make_option('-r', '--replace',
            action="store_true",
            dest='replace',
            default=False,
            help='Completely replace whole widgets with the new one where UIDs match. Default behaviour merges widgets submodels (AbstractInputs, AbstractOutputs and AbstratcOptions)'
                 'based on their submodel\'s own UID. When using this option all widget\'s old submodels are deleted and completely replaced by new submodels.)'
        ),
    )

    def handle(self, *args, **options):
        if (len(args)<1):
            raise CommandError('Arguments "file_name" and "package_name" are required!')

        try:
            instr = open(args[0], 'r').read()
        except:
            raise CommandError('There was a problem with opening given input file')

        self.importString(self, instr, options['replace'])
        self.stdout.write('Import procedure successfully finished.\n')

    def importString(self, obj, instr, replace):
        #get all objects from file and eliminate empty UID and check for UID duplicates
        objsFile = list(serializers.deserialize("json", instr))
        objsFileNoUid = [x for x in objsFile if len(x.object.uid) == 0]
        objsFile = [x for x in objsFile if len(x.object.uid) != 0]
        if len(objsFileNoUid)>0:
            obj.stdout.write('File contains %i model(s) without UID field set. Those will not be imported! If you wish to'
                              ' assign them random UIDs then use the "-n" option when exporting models with the "export_package"'
                              ' command. Afterwards, you will be able to import them.\n' % len(objsFileNoUid))
        if len(Counter([x.object.uid for x in objsFile])) != len(objsFile):
            raise CommandError('Input process terminated without any changes to the database. There were multiple equal '
                               'UIDs defined on different models in the given input file. The input procedure can not continue '
                               'from safety reasons. Please resolve manually!')

        #divide new objects by type
        wids = [x for x in objsFile if isinstance(x.object, AbstractWidget)]
        inps = [x for x in objsFile if isinstance(x.object, AbstractInput)]
        outs = [x for x in objsFile if isinstance(x.object, AbstractOutput)]
        opts = [x for x in objsFile if isinstance(x.object, AbstractOption)]
        cats = [x for x in objsFile if isinstance(x.object, Category)]

        #ouput statistics about file
        obj.stdout.write('Import contains:\n')
        obj.stdout.write('    % 4i AbstractWidget(s)\n' % len(wids))
        obj.stdout.write('    % 4i AbstractInput(s)\n' % len(inps))
        obj.stdout.write('    % 4i AbstractOutput(s)\n' % len(outs))
        obj.stdout.write('    % 4i AbstractOption(s)\n' % len(opts))
        obj.stdout.write('    % 4i Category(s)\n' % len(cats))

        #get all objects from database
        objsDb = []
        objsDb.extend(AbstractWidget.objects.all())
        objsDb.extend(AbstractInput.objects.all())
        objsDb.extend(AbstractOutput.objects.all())
        objsDb.extend(AbstractOption.objects.all())
        objsDb.extend(Category.objects.all())

        #check for DB UID duplicates
        objsdbDict = dict((x.uid,x) for x in objsDb if len(x.uid) != 0)
        if len([x for x in objsDb if len(x.uid) != 0]) != len(objsdbDict):
            raise CommandError('Input process terminated without any changes to the database. There were multiple equal '
                               'UIDs defined on different models in the database. The input procedure can not continue '
                               'from safety reasons. Please resolve manually!')

        #create new to existing id mapping and check for type match
        idMappingDict = dict()
        for objFile in objsFile:
            if objsdbDict.has_key(objFile.object.uid):
                objDb = objsdbDict[objFile.object.uid]
                objFileTypeId = str(type(objFile.object))+':'+str(objFile.object.id)
                objDbTypeId = str(type(objDb))+':'+str(objDb.id)
                if type(objFile.object) == type(objsdbDict[objFile.object.uid]):
                    idMappingDict[objFileTypeId] = objDb.id
                else:
                    raise CommandError('Input process terminated without any changes to the database. Two models match by uid but not '
                                       'by type:\n    - from file: %s\n    - from database: %s\n    Please resolve manually!'% (objFileTypeId, objDbTypeId))

        #ouput statistics about database
        obj.stdout.write('Current database contains %i models,\n' % len(objsDb))
        obj.stdout.write('    of which %i models have UID set,\n' % len(objsdbDict))
        obj.stdout.write('    of which %i models match with the imported models and will be updated.\n' % len(idMappingDict))

        #prepare statistics
        statDict = dict([('old:'+str(t),len(t.objects.all())) for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]])
        for objFile in objsFile:
            for operation in ['mod','add','del']:
                statDict[operation+':'+str(type(objFile.object))]=0

        #sort so that AbstractWidgets come in front (are processsed first in the following block)
        #objsFileNew = []
        #objsFileNew.extend([objFile for objFile in objsFile if isinstance(objFile.object, AbstractWidget)])
        #objsFileNew.extend([objFile for objFile in objsFile if not isinstance(objFile.object, AbstractWidget)])
        #objsFile = objsFileNew

        #save models to the database - update the ids for the matching models and remove the ids (to get a new one) for the non matching models
        obj.stdout.write('Merging file and database models ...')
        importedUids = dict()
        for objFile in objsFile:
            objFileTypeId = str(type(objFile.object))+':'+str(objFile.object.id)
            if isinstance(objFile.object, AbstractWidget):
                objFile.old_category_id = objFile.object.category_id
            if isinstance(objFile.object, AbstractInput):
                objFile.old_widget_id = objFile.object.widget_id
            if isinstance(objFile.object, AbstractOutput):
                objFile.old_widget_id = objFile.object.widget_id
            if isinstance(objFile.object, AbstractOption):
                objFile.old_abstract_input_id = objFile.object.abstract_input_id
            if isinstance(objFile.object, Category):
                if not objFile.object.parent_id is None:
                    objFile.old_parent_id = objFile.object.parent_id

            if idMappingDict.has_key(objFileTypeId):
                #there is already an existing model with same uid
                statDict['mod:'+str(type(objFile.object))]+=1
                objFile.object.id = idMappingDict[objFileTypeId]
            else:
                #there is no model jet, add it
                statDict['add:'+str(type(objFile.object))]+=1
                objFile.object.id = None
            objFile.save()
            idMappingDict[objFileTypeId] = objFile.object.id
            importedUids[objFile.object.uid]=True
        obj.stdout.write(' done.\n')

        #correct also the foreign keys
        obj.stdout.write('Updating model\'s foreign keys ...')
        for objFile in wids:
            objFile.object.category = Category.objects.get(id=idMappingDict[str(Category)+':'+str(objFile.old_category_id)])
            objFile.save()
        for objFile in inps:
            objFile.object.widget = AbstractWidget.objects.get(id=idMappingDict[str(AbstractWidget)+':'+str(objFile.old_widget_id)])
            objFile.save()
        for objFile in outs:
            objFile.object.widget = AbstractWidget.objects.get(id=idMappingDict[str(AbstractWidget)+':'+str(objFile.old_widget_id)])
            objFile.save()
        for objFile in opts:
            objFile.object.abstract_input = AbstractInput.objects.get(id=idMappingDict[str(AbstractInput)+':'+str(objFile.old_abstract_input_id)])
            objFile.save()
        for objFile in cats:
            if not objFile.object.parent_id is None:
                objFile.object.parent = Category.objects.get(id=idMappingDict[str(Category)+':'+str(objFile.old_parent_id)])
                objFile.save()
        obj.stdout.write(' done.\n')

        if replace:
            obj.stdout.write('Removing unnecessary inputs/options/outputs...')
            for wid in [wid for wid in objsFile if isinstance(wid.object, AbstractWidget)]:
                for inp in AbstractInput.objects.filter(widget = wid.object.id):
                    for opt in AbstractOption.objects.filter(abstract_input = inp.id):
                        if not importedUids.has_key(opt.uid):
                            statDict['del:'+str(AbstractOption)]+=1
                            opt.delete()
                    if not importedUids.has_key(inp.uid):
                        statDict['del:'+str(AbstractInput)]+=1
                        inp.delete()
                for out in AbstractOutput.objects.filter(widget = wid.object.id):
                    if not importedUids.has_key(out.uid):
                        statDict['del:'+str(AbstractOutput)]+=1
                        out.delete()
            obj.stdout.write(' done.\n')

        #update and output statistics
        statDict = dict(statDict.items() + dict([('new:'+str(t),len(t.objects.all())) for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]]).items())
        obj.stdout.write('Database models count statistics: pre-import + ( added | modified | deleted ) = after-import\n')
        for t in [AbstractWidget, AbstractInput, AbstractOutput, AbstractOption, Category]:
            obj.stdout.write('    % 15s: % 5i + (% 4i | % 4i | % 4i ) = % 5i\n' %
                             (t.__name__,
                              statDict['old:'+str(t)],
                              statDict['add:'+str(t)],
                              statDict['mod:'+str(t)],
                              statDict['del:'+str(t)],
                              statDict['new:'+str(t)]))
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# tpm2-tss documentation build configuration file, created by
# sphinx-quickstart on Thu Aug 29 14:00:06 2019.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os
import subprocess

# Create (unused) root file
index_rst = open('index.rst', 'w')
index_rst.write('.. toctree::\n   :maxdepth: 2')
index_rst.close()

# Build doxygen documentation
subprocess.call(r"""
        cd ..
        version="master"
        echo "Version: $version"
        sed "s/@top_srcdir@/./g;
             s/@top_builddir@/./g;
             s/@PACKAGE_NAME@/tpm2-tss/g;
             s/@VERSION@/$version/g" Doxyfile.in > Doxyfile
        SRCDIR='.' PROJECT='tpm2-tss' VERSION='2.3.0-dev' PERL_PATH='/usr/bin/perl' HAVE_DOT='NO' GENERATE_MAN='YES' GENERATE_RTF='YES' GENERATE_XML='NO' GENERATE_HTMLHELP='NO' GENERATE_CHI='NO' GENERATE_HTML='YES' GENERATE_LATEX='NO' DOCDIR=doxygen-doc doxygen Doxyfile
        cd sphinx
    """, shell=True)

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = []

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = 'tpm2-tss'
copyright = '2019, open source community'
author = 'open source community'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = ''
# The full version, including alpha/beta/rc tags.
release = ''

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']

# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'alabaster'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
#html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (relative to this directory) to use as a favicon of
# the docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
html_extra_path = ['../doxygen-doc/html']

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
#   'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja'
#   'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr'
#html_search_language = 'en'

# A dictionary with options for the search language support, empty by default.
# Now only 'ja' uses this config value
#html_search_options = {'type': 'default'}

# The name of a javascript file (relative to the configuration directory) that
# implements a search results scorer. If empty, the default will be used.
#html_search_scorer = 'scorer.js'

# Output file base name for HTML help builder.
htmlhelp_basename = 'tpm2-tssdoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',

# Latex figure (float) alignment
#'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'tpm2-tss.tex', 'tpm2-tss Documentation',
     'open source community', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'tpm2-tss', 'tpm2-tss Documentation',
     [author], 1)
]

# If true, show URL addresses after external links.
#man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (master_doc, 'tpm2-tss', 'tpm2-tss Documentation',
     author, 'tpm2-tss', 'One line description of project.',
     'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
#texinfo_appendices = []

# If false, no module index is generated.
#texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False
<commit_msg>sphinx: remove unused imports from conf.py<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# tpm2-tss documentation build configuration file, created by
# sphinx-quickstart on Thu Aug 29 14:00:06 2019.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import subprocess

# Create (unused) root file
index_rst = open('index.rst', 'w')
index_rst.write('.. toctree::\n   :maxdepth: 2')
index_rst.close()

# Build doxygen documentation
subprocess.call(r"""
        cd ..
        version="master"
        echo "Version: $version"
        sed "s/@top_srcdir@/./g;
             s/@top_builddir@/./g;
             s/@PACKAGE_NAME@/tpm2-tss/g;
             s/@VERSION@/$version/g" Doxyfile.in > Doxyfile
        SRCDIR='.' PROJECT='tpm2-tss' VERSION='2.3.0-dev' PERL_PATH='/usr/bin/perl' HAVE_DOT='NO' GENERATE_MAN='YES' GENERATE_RTF='YES' GENERATE_XML='NO' GENERATE_HTMLHELP='NO' GENERATE_CHI='NO' GENERATE_HTML='YES' GENERATE_LATEX='NO' DOCDIR=doxygen-doc doxygen Doxyfile
        cd sphinx
    """, shell=True)

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = []

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = 'tpm2-tss'
copyright = '2019, open source community'
author = 'open source community'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = ''
# The full version, including alpha/beta/rc tags.
release = ''

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']

# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'alabaster'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
#html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (relative to this directory) to use as a favicon of
# the docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
html_extra_path = ['../doxygen-doc/html']

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
#   'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja'
#   'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr'
#html_search_language = 'en'

# A dictionary with options for the search language support, empty by default.
# Now only 'ja' uses this config value
#html_search_options = {'type': 'default'}

# The name of a javascript file (relative to the configuration directory) that
# implements a search results scorer. If empty, the default will be used.
#html_search_scorer = 'scorer.js'

# Output file base name for HTML help builder.
htmlhelp_basename = 'tpm2-tssdoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',

# Latex figure (float) alignment
#'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'tpm2-tss.tex', 'tpm2-tss Documentation',
     'open source community', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'tpm2-tss', 'tpm2-tss Documentation',
     [author], 1)
]

# If true, show URL addresses after external links.
#man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (master_doc, 'tpm2-tss', 'tpm2-tss Documentation',
     author, 'tpm2-tss', 'One line description of project.',
     'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
#texinfo_appendices = []

# If false, no module index is generated.
#texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False
<|endoftext|>