<commit_before>import unittest

import numpy
import six

from chainer import functions
from chainer import testing
from chainer import utils

@testing.parameterize(*(
    testing.product({
        'shape': [(3, 2, 4)],
        'axis': [None, 0, 1, 2, -1, (0, 1), (1, -1)],
        'dtype': [numpy.float16, numpy.float32, numpy.float64],
        'use_weights': [True, False],
        'keepdims': [True, False],
        'use_variable_method': [True, False],
    }) +
    testing.product({
        'shape': [()],
        'axis': [None],
        'dtype': [numpy.float16, numpy.float32, numpy.float64],
        'use_weights': [True, False],
        'keepdims': [True, False],
        'use_variable_method': [True, False],
    })))
@testing.fix_random()
@testing.inject_backend_tests(
    None,
    # CPU tests
    [
        {},
    ]
    # GPU tests
    + testing.product({
        'use_cuda': [True],
        'cuda_device': [0, 1],
    })
    # ChainerX tests
    + testing.product({
        'use_chainerx': [True],
        'chainerx_device': ['native:0', 'cuda:0', 'cuda:1'],
    }))
class TestAverage(testing.FunctionTestCase):

    def setUp(self):
        self.skip_double_backward_test = True

        if self.dtype == numpy.float16:
            self.check_forward_options.update({'atol': 5e-3, 'rtol': 5e-3})
            self.check_backward_options.update({'atol': 1e-2, 'rtol': 1e-1})
        else:
            self.check_backward_options.update({'atol': 1e-2, 'rtol': 1e-2})

    def before_test(self, test_name):
        if self.use_weights and isinstance(self.axis, tuple):
            # This condition is not supported
            raise unittest.SkipTest(
                'Tuple axis is not supported when weights is given')

    def generate_inputs(self):
        x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)

        if self.axis is None:
            w_shape = self.shape
        elif isinstance(self.axis, int):
            axis = self.axis
            if axis < 0:
                ndim = len(self.shape)
                axis += ndim
            w_shape = self.shape[axis],
        else:
            w_shape = tuple(self.shape[a] for a in self.axis)

        # Sample weights. Weights should not sum to 0.
        while True:
            w = numpy.random.uniform(-2, 2, w_shape).astype(self.dtype)
            w_sum_eps = 1.0 if self.dtype == numpy.float16 else 5e-2
            if abs(w.sum()) > w_sum_eps:
                break

        return x, w

    def forward(self, inputs, device):
        x, w = inputs
        if not self.use_weights:
            w = None
        if self.use_variable_method:
            y = x.mean(axis=self.axis, weights=w, keepdims=self.keepdims)
        else:
            y = functions.average(
                x, axis=self.axis, weights=w, keepdims=self.keepdims)
        return y,

    def forward_expected(self, inputs):
        x, w = inputs
        if not self.use_weights:
            w = None
        y_expect = numpy.average(x, axis=self.axis, weights=w)
        if self.keepdims:
            # numpy.average does not support keepdims
            axis = self.axis
            if axis is None:
                axis = list(six.moves.range(x.ndim))
            elif isinstance(axis, int):
                axis = axis,
            shape = list(x.shape)
            for i in six.moves.range(len(shape)):
                if i in axis or i - len(shape) in axis:
                    shape[i] = 1
            y_expect = y_expect.reshape(shape)
        y_expect = utils.force_array(y_expect, dtype=self.dtype)
        return y_expect,

@testing.parameterize(*testing.product({
    'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestAverageDuplicateValueInAxis(unittest.TestCase):

    def test_duplicate_value(self):
        x = numpy.random.uniform(-1, 1, 24).reshape(2, 3, 4).astype(self.dtype)
        with self.assertRaises(ValueError):
            functions.average(x, axis=(0, 0))

    def test_duplicate_value_negative(self):
        x = numpy.random.uniform(-1, 1, 24).reshape(2, 3, 4).astype(self.dtype)
        with self.assertRaises(ValueError):
            functions.average(x, axis=(1, -2))

    def test_weights_and_axis(self):
        x = numpy.random.uniform(-1, 1, 24).reshape(2, 3, 4).astype(self.dtype)
        w = numpy.random.uniform(-1, 1, 6).reshape(2, 3).astype(self.dtype)
        with self.assertRaises(ValueError):
            functions.average(x, axis=(0, 1), weights=w)

testing.run_module(__name__, __file__)
<commit_msg>Test if `F.average` overflows<commit_after>import unittest

import numpy
import six

from chainer import functions
from chainer import testing
from chainer.testing import attr
from chainer import utils

@testing.parameterize(*(
    testing.product({
        'shape': [(3, 2, 4)],
        'axis': [None, 0, 1, 2, -1, (0, 1), (1, -1)],
        'dtype': [numpy.float16, numpy.float32, numpy.float64],
        'use_weights': [True, False],
        'keepdims': [True, False],
        'use_variable_method': [True, False],
    }) +
    testing.product({
        'shape': [()],
        'axis': [None],
        'dtype': [numpy.float16, numpy.float32, numpy.float64],
        'use_weights': [True, False],
        'keepdims': [True, False],
        'use_variable_method': [True, False],
    })))
@testing.fix_random()
@testing.inject_backend_tests(
    None,
    # CPU tests
    [
        {},
    ]
    # GPU tests
    + testing.product({
        'use_cuda': [True],
        'cuda_device': [0, 1],
    })
    # ChainerX tests
    + testing.product({
        'use_chainerx': [True],
        'chainerx_device': ['native:0', 'cuda:0', 'cuda:1'],
    }))
class TestAverage(testing.FunctionTestCase):

    def setUp(self):
        self.skip_double_backward_test = True

        if self.dtype == numpy.float16:
            self.check_forward_options.update({'atol': 5e-3, 'rtol': 5e-3})
            self.check_backward_options.update({'atol': 1e-2, 'rtol': 1e-1})
        else:
            self.check_backward_options.update({'atol': 1e-2, 'rtol': 1e-2})

    def before_test(self, test_name):
        if self.use_weights and isinstance(self.axis, tuple):
            # This condition is not supported
            raise unittest.SkipTest(
                'Tuple axis is not supported when weights is given')

    def generate_inputs(self):
        x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)

        if self.axis is None:
            w_shape = self.shape
        elif isinstance(self.axis, int):
            axis = self.axis
            if axis < 0:
                ndim = len(self.shape)
                axis += ndim
            w_shape = self.shape[axis],
        else:
            w_shape = tuple(self.shape[a] for a in self.axis)

        # Sample weights. Weights should not sum to 0.
        while True:
            w = numpy.random.uniform(-2, 2, w_shape).astype(self.dtype)
            w_sum_eps = 1.0 if self.dtype == numpy.float16 else 5e-2
            if abs(w.sum()) > w_sum_eps:
                break

        return x, w

    def forward(self, inputs, device):
        x, w = inputs
        if not self.use_weights:
            w = None
        if self.use_variable_method:
            y = x.mean(axis=self.axis, weights=w, keepdims=self.keepdims)
        else:
            y = functions.average(
                x, axis=self.axis, weights=w, keepdims=self.keepdims)
        return y,

    def forward_expected(self, inputs):
        x, w = inputs
        if not self.use_weights:
            w = None
        y_expect = numpy.average(x, axis=self.axis, weights=w)
        if self.keepdims:
            # numpy.average does not support keepdims
            axis = self.axis
            if axis is None:
                axis = list(six.moves.range(x.ndim))
            elif isinstance(axis, int):
                axis = axis,
            shape = list(x.shape)
            for i in six.moves.range(len(shape)):
                if i in axis or i - len(shape) in axis:
                    shape[i] = 1
            y_expect = y_expect.reshape(shape)
        y_expect = utils.force_array(y_expect, dtype=self.dtype)
        return y_expect,

@testing.parameterize(*(
    testing.product({
        'shape': [(30, 20, 40)],
        'axis': [None, 0, 1, 2, -1, (0, 1), (1, -1)],
        'dtype': [numpy.float16],
        'use_weights': [False],  # np.average overflows when `weights` is used
        'keepdims': [True, False],
    })
))
@testing.inject_backend_tests(
    None,
    # CPU tests
    [
        {},
    ]
    # GPU tests
    + testing.product({
        'use_cuda': [True],
        'cuda_device': [0, 1],
    })
    # ChainerX tests
    + testing.product({
        'use_chainerx': [True],
        'chainerx_device': ['native:0', 'cuda:0', 'cuda:1'],
    }))
@attr.slow
class TestAverageOverflowingSum(testing.FunctionTestCase):

    def setUp(self):
        self.check_forward_options.update({'atol': 1e-2, 'rtol': 2e-3})
        self.check_backward_options.update({'atol': 1e-2, 'rtol': 1e-2})
        self.check_double_backward_options.update({'atol': 1e-2, 'rtol': 1e-2})

    def generate_inputs(self):
        x = numpy.random.uniform(3000, 7000, self.shape).astype(self.dtype)
        return x,

    def forward(self, inputs, device):
        x, = inputs
        y = functions.average(
            x, self.axis, keepdims=self.keepdims)
        return y,

    def forward_expected(self, inputs):
        x, = inputs
        y_expect = numpy.mean(
            x.astype(numpy.float64), self.axis, keepdims=self.keepdims
        ).astype(self.dtype)
        return utils.force_array(y_expect),

@testing.parameterize(*testing.product({
    'dtype': [numpy.float16, numpy.float32, numpy.float64],
}))
class TestAverageDuplicateValueInAxis(unittest.TestCase):

    def test_duplicate_value(self):
        x = numpy.random.uniform(-1, 1, 24).reshape(2, 3, 4).astype(self.dtype)
        with self.assertRaises(ValueError):
            functions.average(x, axis=(0, 0))

    def test_duplicate_value_negative(self):
        x = numpy.random.uniform(-1, 1, 24).reshape(2, 3, 4).astype(self.dtype)
        with self.assertRaises(ValueError):
            functions.average(x, axis=(1, -2))

    def test_weights_and_axis(self):
        x = numpy.random.uniform(-1, 1, 24).reshape(2, 3, 4).astype(self.dtype)
        w = numpy.random.uniform(-1, 1, 6).reshape(2, 3).astype(self.dtype)
        with self.assertRaises(ValueError):
            functions.average(x, axis=(0, 1), weights=w)

testing.run_module(__name__, __file__)
<|endoftext|>


<commit_before># Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class Latte(CMakePackage):
    """Open source density functional tight binding molecular dynamics."""

    homepage = "https://github.com/lanl/latte"
    url      = "https://github.com/lanl/latte/tarball/v1.2.1"
    git      = "https://github.com/lanl/latte.git"

    tags = ['ecp', 'ecp-apps']

    version('develop', branch='master')
    version('1.2.1', sha256='a21dda5ebdcefa56e9ff7296d74ef03f89c200d2e110a02af7a84612668bf702')

    variant('mpi', default=True,
            description='Build with mpi')
    variant('progress', default=False,
            description='Use progress for fast')
    variant('shared', default=True, description='Build shared libs')

    depends_on("cmake@3.1:", type='build')
    depends_on('blas')
    depends_on('lapack')
    depends_on('mpi', when='+mpi')
    depends_on('qmd-progress', when='+progress')

    root_cmakelists_dir = 'cmake'

    def cmake_args(self):
        options = []
        if '+shared' in self.spec:
            options.append('-DBUILD_SHARED_LIBS=ON')
        else:
            options.append('-DBUILD_SHARED_LIBS=OFF')
        if '+mpi' in self.spec:
            options.append('-DO_MPI=yes')
        if '+progress' in self.spec:
            options.append('-DPROGRESS=yes')

        blas_list = ';'.join(self.spec['blas'].libs)
        lapack_list = ';'.join(self.spec['lapack'].libs)
        options.append('-DBLAS_LIBRARIES={0}'.format(blas_list))
        options.append('-DLAPACK_LIBRARIES={0}'.format(lapack_list))

        return options
<commit_msg>latte: added v1.2.2 and master (#17714)<commit_after># Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class Latte(CMakePackage):
    """Open source density functional tight binding molecular dynamics."""

    homepage = "https://github.com/lanl/latte"
    url      = "https://github.com/lanl/latte/tarball/v1.2.1"
    git      = "https://github.com/lanl/latte.git"

    tags = ['ecp', 'ecp-apps']

    version('master', branch='master')
    version('1.2.2', sha256='ab1346939dbd70ffd89c5e5bf8d24946cb3655dc25b203bec7fc59c6c63e4c79')
    version('1.2.1', sha256='a21dda5ebdcefa56e9ff7296d74ef03f89c200d2e110a02af7a84612668bf702')

    variant('mpi', default=True,
            description='Build with mpi')
    variant('progress', default=False,
            description='Use progress for fast')
    variant('shared', default=True, description='Build shared libs')

    depends_on("cmake@3.1:", type='build')
    depends_on('blas')
    depends_on('lapack')
    depends_on('mpi', when='+mpi')
    depends_on('qmd-progress', when='+progress')

    root_cmakelists_dir = 'cmake'

    def cmake_args(self):
        options = []
        if '+shared' in self.spec:
            options.append('-DBUILD_SHARED_LIBS=ON')
        else:
            options.append('-DBUILD_SHARED_LIBS=OFF')
        if '+mpi' in self.spec:
            options.append('-DO_MPI=yes')
        if '+progress' in self.spec:
            options.append('-DPROGRESS=yes')

        blas_list = ';'.join(self.spec['blas'].libs)
        lapack_list = ';'.join(self.spec['lapack'].libs)
        options.append('-DBLAS_LIBRARIES={0}'.format(blas_list))
        options.append('-DLAPACK_LIBRARIES={0}'.format(lapack_list))

        return options
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2019, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document

class ListViewSettings(Document):

	def on_update(self):
		frappe.clear_document_cache(self.doctype, self.name)

@frappe.whitelist()
def get_listview_columns(doctype):
	meta = frappe.get_meta(doctype)
	listview_columns = []

	subject_field = {
		"label": "Name",
		"fieldname": "name"
	}

	if meta.title_field:
		title_field = meta.get_field(meta.title_field)

		subject_field["label"] = title_field.label
		subject_field["fieldname"] = title_field.fieldname

	listview_columns.append(subject_field)

	for field in meta.fields:
		if field.in_list_view and field.label and field.fieldname:
			listview_columns.append({
				"label": field.label,
				"fieldname": field.fieldname
			})

	return listview_columns
<commit_msg>fix: refresh cache on update<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2019, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document

class ListViewSettings(Document):

	def on_update(self):
		frappe.clear_document_cache(self.doctype, self.name)
		frappe.get_cached_doc(self.doctype, self.name)

@frappe.whitelist()
def get_listview_columns(doctype):
	meta = frappe.get_meta(doctype)
	listview_columns = []

	subject_field = {
		"label": "Name",
		"fieldname": "name"
	}

	if meta.title_field:
		title_field = meta.get_field(meta.title_field)

		subject_field["label"] = title_field.label
		subject_field["fieldname"] = title_field.fieldname

	listview_columns.append(subject_field)

	for field in meta.fields:
		if field.in_list_view and field.label and field.fieldname:
			listview_columns.append({
				"label": field.label,
				"fieldname": field.fieldname
			})

	return listview_columns
<|endoftext|>


<commit_before>"""Support for monitoring a Neurio energy sensor."""
import logging
from datetime import timedelta

import requests.exceptions
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_API_KEY, POWER_WATT, ENERGY_KILO_WATT_HOUR
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util

_LOGGER = logging.getLogger(__name__)

CONF_API_SECRET = "api_secret"
CONF_SENSOR_ID = "sensor_id"

ACTIVE_NAME = "Energy Usage"
DAILY_NAME = "Daily Energy Usage"

ACTIVE_TYPE = "active"
DAILY_TYPE = "daily"

ICON = "mdi:flash"

MIN_TIME_BETWEEN_DAILY_UPDATES = timedelta(seconds=150)
MIN_TIME_BETWEEN_ACTIVE_UPDATES = timedelta(seconds=10)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {
        vol.Required(CONF_API_KEY): cv.string,
        vol.Required(CONF_API_SECRET): cv.string,
        vol.Optional(CONF_SENSOR_ID): cv.string,
    }
)

def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Neurio sensor."""
    api_key = config.get(CONF_API_KEY)
    api_secret = config.get(CONF_API_SECRET)
    sensor_id = config.get(CONF_SENSOR_ID)

    data = NeurioData(api_key, api_secret, sensor_id)

    @Throttle(MIN_TIME_BETWEEN_DAILY_UPDATES)
    def update_daily():
        """Update the daily power usage."""
        data.get_daily_usage()

    @Throttle(MIN_TIME_BETWEEN_ACTIVE_UPDATES)
    def update_active():
        """Update the active power usage."""
        data.get_active_power()

    update_daily()
    update_active()

    # Active power sensor
    add_entities([NeurioEnergy(data, ACTIVE_NAME, ACTIVE_TYPE, update_active)])
    # Daily power sensor
    add_entities([NeurioEnergy(data, DAILY_NAME, DAILY_TYPE, update_daily)])

class NeurioData:
    """Stores data retrieved from Neurio sensor."""

    def __init__(self, api_key, api_secret, sensor_id):
        """Initialize the data."""
        import neurio

        self.api_key = api_key
        self.api_secret = api_secret
        self.sensor_id = sensor_id

        self._daily_usage = None
        self._active_power = None

        self._state = None

        neurio_tp = neurio.TokenProvider(key=api_key, secret=api_secret)
        self.neurio_client = neurio.Client(token_provider=neurio_tp)

        if not self.sensor_id:
            user_info = self.neurio_client.get_user_information()
            _LOGGER.warning(
                "Sensor ID auto-detected: %s",
                user_info["locations"][0]["sensors"][0]["sensorId"],
            )
            self.sensor_id = user_info["locations"][0]["sensors"][0]["sensorId"]

    @property
    def daily_usage(self):
        """Return latest daily usage value."""
        return self._daily_usage

    @property
    def active_power(self):
        """Return latest active power value."""
        return self._active_power

    def get_active_power(self):
        """Return current power value."""
        try:
            sample = self.neurio_client.get_samples_live_last(self.sensor_id)
            self._active_power = sample["consumptionPower"]
        except (requests.exceptions.RequestException, ValueError, KeyError):
            _LOGGER.warning("Could not update current power usage")
            return None

    def get_daily_usage(self):
        """Return current daily power usage."""
        kwh = 0
        start_time = dt_util.start_of_local_day().astimezone(dt_util.UTC).isoformat()
        end_time = dt_util.utcnow().isoformat()

        _LOGGER.debug("Start: %s, End: %s", start_time, end_time)

        try:
            history = self.neurio_client.get_samples_stats(
                self.sensor_id, start_time, "days", end_time
            )
        except (requests.exceptions.RequestException, ValueError, KeyError):
            _LOGGER.warning("Could not update daily power usage")
            return None

        for result in history:
            kwh += result["consumptionEnergy"] / 3600000

        self._daily_usage = round(kwh, 2)

class NeurioEnergy(Entity):
    """Implementation of a Neurio energy sensor."""

    def __init__(self, data, name, sensor_type, update_call):
        """Initialize the sensor."""
        self._name = name
        self._data = data
        self._sensor_type = sensor_type
        self.update_sensor = update_call
        self._state = None

        if sensor_type == ACTIVE_TYPE:
            self._unit_of_measurement = POWER_WATT
        elif sensor_type == DAILY_TYPE:
            self._unit_of_measurement = ENERGY_KILO_WATT_HOUR

    @property
    def name(self):
        """Return the name of the sensor."""
        return self._name

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    @property
    def unit_of_measurement(self):
        """Return the unit of measurement of this entity, if any."""
        return self._unit_of_measurement

    @property
    def icon(self):
        """Icon to use in the frontend, if any."""
        return ICON

    def update(self):
        """Get the latest data, update state."""
        self.update_sensor()

        if self._sensor_type == ACTIVE_TYPE:
            self._state = self._data.active_power
        elif self._sensor_type == DAILY_TYPE:
            self._state = self._data.daily_usage
<commit_msg>Move imports in neurio_energy component (#28035)<commit_after>"""Support for monitoring a Neurio energy sensor."""
from datetime import timedelta
import logging

import neurio
import requests.exceptions
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_API_KEY, ENERGY_KILO_WATT_HOUR, POWER_WATT
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import homeassistant.util.dt as dt_util

_LOGGER = logging.getLogger(__name__)

CONF_API_SECRET = "api_secret"
CONF_SENSOR_ID = "sensor_id"

ACTIVE_NAME = "Energy Usage"
DAILY_NAME = "Daily Energy Usage"

ACTIVE_TYPE = "active"
DAILY_TYPE = "daily"

ICON = "mdi:flash"

MIN_TIME_BETWEEN_DAILY_UPDATES = timedelta(seconds=150)
MIN_TIME_BETWEEN_ACTIVE_UPDATES = timedelta(seconds=10)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {
        vol.Required(CONF_API_KEY): cv.string,
        vol.Required(CONF_API_SECRET): cv.string,
        vol.Optional(CONF_SENSOR_ID): cv.string,
    }
)

def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Neurio sensor."""
    api_key = config.get(CONF_API_KEY)
    api_secret = config.get(CONF_API_SECRET)
    sensor_id = config.get(CONF_SENSOR_ID)

    data = NeurioData(api_key, api_secret, sensor_id)

    @Throttle(MIN_TIME_BETWEEN_DAILY_UPDATES)
    def update_daily():
        """Update the daily power usage."""
        data.get_daily_usage()

    @Throttle(MIN_TIME_BETWEEN_ACTIVE_UPDATES)
    def update_active():
        """Update the active power usage."""
        data.get_active_power()

    update_daily()
    update_active()

    # Active power sensor
    add_entities([NeurioEnergy(data, ACTIVE_NAME, ACTIVE_TYPE, update_active)])
    # Daily power sensor
    add_entities([NeurioEnergy(data, DAILY_NAME, DAILY_TYPE, update_daily)])

class NeurioData:
    """Stores data retrieved from Neurio sensor."""

    def __init__(self, api_key, api_secret, sensor_id):
        """Initialize the data."""
        self.api_key = api_key
        self.api_secret = api_secret
        self.sensor_id = sensor_id

        self._daily_usage = None
        self._active_power = None

        self._state = None

        neurio_tp = neurio.TokenProvider(key=api_key, secret=api_secret)
        self.neurio_client = neurio.Client(token_provider=neurio_tp)

        if not self.sensor_id:
            user_info = self.neurio_client.get_user_information()
            _LOGGER.warning(
                "Sensor ID auto-detected: %s",
                user_info["locations"][0]["sensors"][0]["sensorId"],
            )
            self.sensor_id = user_info["locations"][0]["sensors"][0]["sensorId"]

    @property
    def daily_usage(self):
        """Return latest daily usage value."""
        return self._daily_usage

    @property
    def active_power(self):
        """Return latest active power value."""
        return self._active_power

    def get_active_power(self):
        """Return current power value."""
        try:
            sample = self.neurio_client.get_samples_live_last(self.sensor_id)
            self._active_power = sample["consumptionPower"]
        except (requests.exceptions.RequestException, ValueError, KeyError):
            _LOGGER.warning("Could not update current power usage")
            return None

    def get_daily_usage(self):
        """Return current daily power usage."""
        kwh = 0
        start_time = dt_util.start_of_local_day().astimezone(dt_util.UTC).isoformat()
        end_time = dt_util.utcnow().isoformat()

        _LOGGER.debug("Start: %s, End: %s", start_time, end_time)

        try:
            history = self.neurio_client.get_samples_stats(
                self.sensor_id, start_time, "days", end_time
            )
        except (requests.exceptions.RequestException, ValueError, KeyError):
            _LOGGER.warning("Could not update daily power usage")
            return None

        for result in history:
            kwh += result["consumptionEnergy"] / 3600000

        self._daily_usage = round(kwh, 2)

class NeurioEnergy(Entity):
    """Implementation of a Neurio energy sensor."""

    def __init__(self, data, name, sensor_type, update_call):
        """Initialize the sensor."""
        self._name = name
        self._data = data
        self._sensor_type = sensor_type
        self.update_sensor = update_call
        self._state = None

        if sensor_type == ACTIVE_TYPE:
            self._unit_of_measurement = POWER_WATT
        elif sensor_type == DAILY_TYPE:
            self._unit_of_measurement = ENERGY_KILO_WATT_HOUR

    @property
    def name(self):
        """Return the name of the sensor."""
        return self._name

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    @property
    def unit_of_measurement(self):
        """Return the unit of measurement of this entity, if any."""
        return self._unit_of_measurement

    @property
    def icon(self):
        """Icon to use in the frontend, if any."""
        return ICON

    def update(self):
        """Get the latest data, update state."""
        self.update_sensor()

        if self._sensor_type == ACTIVE_TYPE:
            self._state = self._data.active_power
        elif self._sensor_type == DAILY_TYPE:
            self._state = self._data.daily_usage
<|endoftext|>


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

import pygame

import os
import time

from multiprocessing import Process, Lock, Event
from datetime import datetime

import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open('config.cfg'))

GPIO_ACTIVE = config.getboolean('System','gpio')

if GPIO_ACTIVE:
    import RPi.GPIO as GPIO
    GPIO.setmode(GPIO.BOARD)

    GPIO.setup(13, GPIO.OUT)
    GPIO.output(13, GPIO.HIGH)

    # GPIO.setup(15, GPIO.OUT)
    # GPIO.output(15, GPIO.HIGH)

    # GPIO.setup(29, GPIO.OUT)
    # GPIO.output(29, GPIO.HIGH)

    # GPIO.setup(31, GPIO.OUT)
    # GPIO.output(31, GPIO.HIGH)

    # GPIO.setup(33, GPIO.OUT)
    # GPIO.output(33, GPIO.HIGH)

    # GPIO.setup(35, GPIO.OUT)
    # GPIO.output(35, GPIO.HIGH)

from post_production import clearRecFolder, buildVideo, uploadToDropbox as upload, generateQrCode
from recorder import recordFrames

play_status_movie = True

frame = 1
stop_recording_event = Event()
rec_timestamps = [0]
first_rec_timestamp = -1

def postproduction(stop_recording_event, rec_timeout_flag, rec_truly_started_event, postproduction_flag, postproduction_finished_flag):
    stop_recording_event.set()
    rec_timeout_flag.clear()
    rec_truly_started_event.clear()

    print('{} - Start building video'.format(datetime.now()))
    buildVideo(RECORD_PATH, VIDEO_PATH, rec_timestamps, REC_FPS)
    print('{} - Finished building video'.format(datetime.now()))
    print('{} - Start uploading'.format(datetime.now()))
    url = upload(VIDEO_PATH)
    print('{} - Finished uploading'.format(datetime.now()))

    f = open('url', 'w')
    f.write(url)
    f.close()

    generateQrCode(url, os.path.join(ROOT_PATH,'img') )

    postproduction_finished_flag.set()

    return pygame.time.get_ticks()

def toggleRelais(pin):
    GPIO.output(pin, not GPIO.input(pin))

def changeVideo(path, width, height):
    print('changeVideo', path, width, height)
    movie = pygame.movie.Movie(path)
    print(movie.get_size())
    movie_screen = pygame.Surface(movie.get_size()).convert()
    movie.set_display(movie_screen)
    movie.play()

    return movie, movie_screen

# not sure alexa stole this code from the internets
def scale(img,(bx,by)):
    """ Scales 'img' to fit into box bx/by.
     This method will retain the original image's aspect ratio """
    ix,iy = img.get_size()
    if ix > iy:
        # fit to width
        scale_factor = bx/float(ix)
        sy = scale_factor * iy
        if sy > by:
            scale_factor = by/float(iy)
            sx = scale_factor * ix
            sy = by
        else:
            sx = bx
    else:
        # fit to height
        scale_factor = by/float(iy)
        sx = scale_factor * ix
        if sx > bx:
            scale_factor = bx/float(ix)
            sx = bx
            sy = scale_factor * iy
        else:
            sy = by

    return pygame.transform.scale(img, (int(sx),int(sy)))

if __name__ == '__main__':

    WIDTH  = config.getint('Screen','width')
    HEIGHT = config.getint('Screen','height')
    FULLSCREEN = config.getboolean('Screen','fullscreen')

    ROOT_PATH = config.get('System', 'root_folder')
    RECORD_PATH = config.get('System', 'rec_folder')
    VIDEO_PATH = config.get('System','video_path')

    TIMEOUT = config.getint('System', 'timeout')
    QR_TIMEOUT = config.getint('System', 'qr_timeout')

    FRAMES_PATH = config.get('System','frames_folder')
    ALL_FRAMES = [os.path.join(FRAMES_PATH,f) for f in sorted(os.listdir(FRAMES_PATH))]

    path = os.path.join(ROOT_PATH, 'img', 'timer')
    TIMER_IMGS = [os.path.join(path,f) for f in sorted(os.listdir(path))]

    REC_PATH = config.get('System','rec_folder')
    REC_DURATION = config.getint('Recorder','rec_duration') # seconds
    REC_FPS = config.getint('Recorder','rec_fps')

    rec_timestamps = [0]
    first_rec_timestamp = -1
    last_frame = -1

    print('{} - start magicKurbelKamera\nresolution: {} x {}'.format(datetime.now(), WIDTH,HEIGHT))

    # Lock Object for recording, only one recording should take place at once
    rec_lock = Lock()
    recording_flag = Event()
    stop_recording_event = Event()
    rec_timeout_flag = Event()
    rec_truly_started_event = Event()
    reset_flag = Event()

    postproduction_flag = Event()
    postproduction_finished_flag = Event()

    # Playing movie state
    play_status_movie = True
    show_qr_code = False

    # pygame setup
    pygame.init()
    clock = pygame.time.Clock()
    pygame.display.set_caption('MagicKurbelKamera') # set a window title
    if FULLSCREEN:
        screen = pygame.display.set_mode((WIDTH,HEIGHT), pygame.FULLSCREEN)
        pygame.mouse.set_visible(0)
    else:
        screen = pygame.display.set_mode((WIDTH,HEIGHT))

    background = pygame.Surface(screen.get_size()) # dunno
    background.fill((0,0,0)) # black background
    background = background.convert() # dunno
    # end of pygame setup

    # load init video
    movie, movie_screen = changeVideo(os.path.join(ROOT_PATH,'img','magic.mp4'), WIDTH, HEIGHT)

    frame = 1
    ticks = 0
    mainloop = True

    # magic ahead
    while mainloop:
        diff = pygame.time.get_ticks() - last_event

        # Normal timeout after some time
        if diff > (TIMEOUT * 1000) and not show_qr_code and not play_status_movie:
            reset_flag.set()
            last_event = pygame.time.get_ticks()

        # Recoding is timed out
        elif rec_timeout_flag.is_set():
            postproduction_flag.set()
            Process(target=postproduction,
                    args = (stop_recording_event,
                            rec_timeout_flag,
                            rec_truly_started_event,
                            postproduction_flag,
                            postproduction_finished_flag)).start()

        # A little longer timeout so you have time to scan the qr code
        elif diff > (QR_TIMEOUT * 1000) and show_qr_code:
            reset_flag.set()
            last_event = pygame.time.get_ticks()

        if (reset_flag.is_set()):
            movie, movie_screen = changeVideo(os.path.join(ROOT_PATH,'img','magic.mp4'), WIDTH, HEIGHT)
            play_status_movie = True
            show_qr_code = False
            movie.stop()
            frame = 1
            stop_recording_event.set()
            rec_timestamps = []
            first_rec_timestamp = -1
            clearRecFolder(REC_PATH, rec_timestamps, REC_FPS)
            postproduction_finished_flag.clear()

            GPIO.setwarnings(False)
            GPIO.setup( 7, GPIO.OUT)
            GPIO.setup( 11, GPIO.OUT)

            GPIO.output(7,GPIO.HIGH) # turn on led
            GPIO.output(11,GPIO.HIGH) # turn on led

            reset_flag.clear()

        # display video
        if play_status_movie:
            #screen.blit(background,(0,0))
            screen.blit(movie_screen,(0,0))
            pygame.display.update()
            clock.tick(60)

            if movie.get_busy() == 0:
                movie.rewind()
                movie.play()

        # if we are not in postproduction state
        if not postproduction_flag.is_set() and not show_qr_code:

            for event in pygame.event.get():

                # pygame window closed by user
                if event.type == pygame.QUIT: mainloop = False

                # key is released
                elif event.type == pygame.KEYUP:
                    last_event = pygame.time.get_ticks()

                    # left and right arrow
                    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

                        play_status_movie = False

                        if GPIO_ACTIVE:
                            toggleRelais(13) # klick klack

                        # increment or decrement the actual frame number
                        if event.key == pygame.K_LEFT:   frame -= 1
                        elif event.key == pygame.K_RIGHT: frame += 1

                        frame = (frame % len(ALL_FRAMES))
                        if frame == 0: frame = len(ALL_FRAMES)

                        image = ALL_FRAMES[frame - 1]
                        img = scale(pygame.image.load(image), (WIDTH,HEIGHT))
                        screen.blit(img,(0,0))
                        # pygame.display.update() # pygame.display.flip()
                        # clock.tick(60)

                        # count ms since first tick
                        if recording_flag.is_set() and not first_rec_timestamp == -1:
                            rec_truly_started_event.set()
                            rec_timestamps.append(pygame.time.get_ticks() - first_rec_timestamp)
                        else:
                            first_rec_timestamp = pygame.time.get_ticks()
                            rec_timestamps = [0]

                        ticks += 1

                        if ticks % 50 == 5:
                            clearRecFolder(REC_PATH, rec_timestamps, REC_FPS)

                    # escape
                    elif event.key == pygame.K_ESCAPE:
                        mainloop = False # user pressed ESC

                    # Record Button
                    elif event.key == pygame.K_r and config.getboolean('System','camera'):
                        if not recording_flag.is_set():

                            play_status_movie = False
                            stop_recording_event.clear()

                            first_rec_timestamp = pygame.time.get_ticks()
                            rec_timestamps = []

                            # show pre_rec screen
                            bg = pygame.image.load(os.path.join(ROOT_PATH,'img','pre_rec.png'))
                            screen.blit(bg,(0,0))
                            pygame.display.update()
                            clock.tick(60)

                            rec_timeout_flag.clear()
                            Process(target=recordFrames,
                                    args=(rec_lock,
                                          recording_flag,
                                          # WIDTH, HEIGHT,
                                          342, 238,
                                          REC_DURATION,
                                          REC_FPS,
                                          stop_recording_event,
                                          rec_timeout_flag,
                                          rec_truly_started_event)).start()

                        else:
                            # start postproduction
                            postproduction_flag.set()
                            Process(target=postproduction,
                                    args = (stop_recording_event,
                                            rec_timeout_flag,
                                            rec_truly_started_event,
                                            postproduction_flag,
                                            postproduction_finished_flag)).start()

                    # this is the bin button
                    elif event.key == pygame.K_e:
                        reset_flag.set()

                    # escape
                    elif event.key == pygame.K_ESCAPE:
                        mainloop = False # user pressed ESC

                    elif event.key == pygame.K_h:
                        os.system('sudo halt')

        elif not postproduction_flag.is_set() and show_qr_code:
            # if we are showing the qr code then it is possible to press X
            for event in pygame.event.get():
                if event.key == pygame.K_e:
                    print "real"
                    reset_flag.set()

        else:
            # ignore keyboard-Events while in postproduction
            for event in pygame.event.get():
                pass

            # here we are in postproduction state
            if not postproduction_finished_flag.is_set():

                if not play_status_movie:
                    movie, movie_screen = changeVideo(os.path.join(ROOT_PATH,'img','development.mp4'), WIDTH, HEIGHT)
                    play_status_movie = True

            elif postproduction_finished_flag.is_set() and not show_qr_code:
                bg = pygame.image.load(os.path.join(ROOT_PATH,'img','deinvideo.png'))
                screen.blit(bg,(0,0))
                #show shortlink
                f = open('url', 'r')
                url = f.read()
                font = pygame.font.Font(None, 24)
                text = font.render(url, 1, (255, 255, 255))
                screen.blit(text, (135, 268))

                movie, movie_screen = changeVideo(os.path.join(VIDEO_PATH), 342, 238)
                qr_code_path = os.path.join(ROOT_PATH,'img', 'code.png')
                img = scale(pygame.image.load(qr_code_path), (50,50))
                screen.blit(img,(350,255))
                #pygame.display.update()
                #clock.tick(60)
                postproduction_flag.clear()
                last_event = pygame.time.get_ticks()
                show_qr_code = True

        # if in recoding state then update timer image
        if (recording_flag.is_set()):

            # repaint last frame
            image = ALL_FRAMES[frame - 1]
            img = scale(pygame.image.load(image), (WIDTH,HEIGHT))
            screen.blit(img,(0,0))

            # paint timer icon
            timespent = (pygame.time.get_ticks() - first_rec_timestamp)/1000
            image = TIMER_IMGS[timespent%61 - 1]
            img = pygame.image.load(image)
            screen.blit(img,(0,0))

        # do this all the time
        pygame.display.update()
        clock.tick(60)

    if GPIO_ACTIVE:
        GPIO.cleanup()
<commit_msg>sorry<commit_after># -*- coding: utf-8 -*-

import pygame

import os
import time

from multiprocessing import Process, Lock, Event
from datetime import datetime

import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open('config.cfg'))

GPIO_ACTIVE = config.getboolean('System','gpio')

if GPIO_ACTIVE:
    import RPi.GPIO as GPIO
    GPIO.setmode(GPIO.BOARD)

    GPIO.setup(13, GPIO.OUT)
    GPIO.output(13, GPIO.HIGH)

    # GPIO.setup(15, GPIO.OUT)
    # GPIO.output(15, GPIO.HIGH)

    # GPIO.setup(29, GPIO.OUT)
    # GPIO.output(29, GPIO.HIGH)

    # GPIO.setup(31, GPIO.OUT)
    # GPIO.output(31, GPIO.HIGH)

    # GPIO.setup(33, GPIO.OUT)
    # GPIO.output(33, GPIO.HIGH)

    # GPIO.setup(35, GPIO.OUT)
    # GPIO.output(35, GPIO.HIGH)

from post_production import clearRecFolder, buildVideo, uploadToDropbox as upload, generateQrCode
from recorder import recordFrames

play_status_movie = True

frame = 1
stop_recording_event = Event()
rec_timestamps = [0]
first_rec_timestamp = -1

def postproduction(stop_recording_event, rec_timeout_flag, rec_truly_started_event, postproduction_flag, postproduction_finished_flag):
    stop_recording_event.set()
    rec_timeout_flag.clear()
    rec_truly_started_event.clear()

    print('{} - Start building video'.format(datetime.now()))
    buildVideo(RECORD_PATH, VIDEO_PATH, rec_timestamps, REC_FPS)
    print('{} - Finished building video'.format(datetime.now()))
    print('{} - Start uploading'.format(datetime.now()))
    url = upload(VIDEO_PATH)
    print('{} - Finished uploading'.format(datetime.now()))

    f = open('url', 'w')
    f.write(url)
    f.close()

    generateQrCode(url, os.path.join(ROOT_PATH,'img') )

    postproduction_finished_flag.set()

    return pygame.time.get_ticks()

def toggleRelais(pin):
    GPIO.output(pin, not GPIO.input(pin))

def changeVideo(path, width, height):
    print('changeVideo', path, width, height)
    movie = pygame.movie.Movie(path)
    print(movie.get_size())
    movie_screen = pygame.Surface(movie.get_size()).convert()
    movie.set_display(movie_screen)
    movie.play()

    return movie, movie_screen

# not sure alexa stole this code from the internets
def scale(img,(bx,by)):
    """ Scales 'img' to fit into box bx/by.
     This method will retain the original image's aspect ratio """
    ix,iy = img.get_size()
    if ix > iy:
        # fit to width
        scale_factor = bx/float(ix)
        sy = scale_factor * iy
        if sy > by:
            scale_factor = by/float(iy)
            sx = scale_factor * ix
            sy = by
        else:
            sx = bx
    else:
        # fit to height
        scale_factor = by/float(iy)
        sx = scale_factor * ix
        if sx > bx:
            scale_factor = bx/float(ix)
            sx = bx
            sy = scale_factor * iy
        else:
            sy = by

    return pygame.transform.scale(img, (int(sx),int(sy)))

if __name__ == '__main__':

    WIDTH  = config.getint('Screen','width')
    HEIGHT = config.getint('Screen','height')
    FULLSCREEN = config.getboolean('Screen','fullscreen')

    ROOT_PATH = config.get('System', 'root_folder')
    RECORD_PATH = config.get('System', 'rec_folder')
    VIDEO_PATH = config.get('System','video_path')

    TIMEOUT = config.getint('System', 'timeout')
    QR_TIMEOUT = config.getint('System', 'qr_timeout')

    FRAMES_PATH = config.get('System','frames_folder')
    ALL_FRAMES = [os.path.join(FRAMES_PATH,f) for f in sorted(os.listdir(FRAMES_PATH))]

    path = os.path.join(ROOT_PATH, 'img', 'timer')
    TIMER_IMGS = [os.path.join(path,f) for f in sorted(os.listdir(path))]

    REC_PATH = config.get('System','rec_folder')
    REC_DURATION = config.getint('Recorder','rec_duration') # seconds
    REC_FPS = config.getint('Recorder','rec_fps')

    rec_timestamps = [0]
    first_rec_timestamp = -1
    last_frame = -1

    print('{} - start magicKurbelKamera\nresolution: {} x {}'.format(datetime.now(), WIDTH,HEIGHT))

    # Lock Object for recording, only one recording should take place at once
    rec_lock = Lock()
    recording_flag = Event()
    stop_recording_event = Event()
    rec_timeout_flag = Event()
    rec_truly_started_event = Event()
    reset_flag = Event()

    postproduction_flag = Event()
    postproduction_finished_flag = Event()

    # Playing movie state
    play_status_movie = True
    show_qr_code = False

    # pygame setup
    pygame.init()
    clock = pygame.time.Clock()
    pygame.display.set_caption('MagicKurbelKamera') # set a window title
    if FULLSCREEN:
        screen = pygame.display.set_mode((WIDTH,HEIGHT), pygame.FULLSCREEN)
        pygame.mouse.set_visible(0)
    else:
        screen = pygame.display.set_mode((WIDTH,HEIGHT))

    background = pygame.Surface(screen.get_size()) # dunno
    background.fill((0,0,0)) # black background
    background = background.convert() # dunno
    # end of pygame setup

    # load init video
    movie, movie_screen = changeVideo(os.path.join(ROOT_PATH,'img','magic.mp4'), WIDTH, HEIGHT)

    frame = 1
    ticks = 0
    mainloop = True
    last_event = pygame.time.get_ticks()

    # magic ahead
    while mainloop:
        diff = pygame.time.get_ticks() - last_event

        # Normal timeout after some time
        if diff > (TIMEOUT * 1000) and not show_qr_code and not play_status_movie:
            reset_flag.set()
            last_event = pygame.time.get_ticks()

        # Recoding is timed out
        elif rec_timeout_flag.is_set():
            postproduction_flag.set()
            Process(target=postproduction,
                    args = (stop_recording_event,
                            rec_timeout_flag,
                            rec_truly_started_event,
                            postproduction_flag,
                            postproduction_finished_flag)).start()

        # A little longer timeout so you have time to scan the qr code
        elif diff > (QR_TIMEOUT * 1000) and show_qr_code:
            reset_flag.set()
            last_event = pygame.time.get_ticks()

        if (reset_flag.is_set()):
            movie, movie_screen = changeVideo(os.path.join(ROOT_PATH,'img','magic.mp4'), WIDTH, HEIGHT)
            play_status_movie = True
            show_qr_code = False
            movie.stop()
            frame = 1
            stop_recording_event.set()
            rec_timestamps = []
            first_rec_timestamp = -1
            clearRecFolder(REC_PATH, rec_timestamps, REC_FPS)
            postproduction_finished_flag.clear()

            GPIO.setwarnings(False)
            GPIO.setup( 7, GPIO.OUT)
            GPIO.setup( 11, GPIO.OUT)

            GPIO.output(7,GPIO.HIGH) # turn on led
            GPIO.output(11,GPIO.HIGH) # turn on led

            reset_flag.clear()

        # display video
        if play_status_movie:
            #screen.blit(background,(0,0))
            screen.blit(movie_screen,(0,0))
            pygame.display.update()
            clock.tick(60)

            if movie.get_busy() == 0:
                movie.rewind()
                movie.play()

        # if we are not in postproduction state
        if not postproduction_flag.is_set() and not show_qr_code:

            for event in pygame.event.get():

                # pygame window closed by user
                if event.type == pygame.QUIT: mainloop = False

                # key is released
                elif event.type == pygame.KEYUP:
                    last_event = pygame.time.get_ticks()

                    # left and right arrow
                    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

                        play_status_movie = False

                        if GPIO_ACTIVE:
                            toggleRelais(13) # klick klack

                        # increment or decrement the actual frame number
                        if event.key == pygame.K_LEFT:   frame -= 1
                        elif event.key == pygame.K_RIGHT: frame += 1

                        frame = (frame % len(ALL_FRAMES))
                        if frame == 0: frame = len(ALL_FRAMES)

                        image = ALL_FRAMES[frame - 1]
                        img = scale(pygame.image.load(image), (WIDTH,HEIGHT))
                        screen.blit(img,(0,0))
                        # pygame.display.update() # pygame.display.flip()
                        # clock.tick(60)

                        # count ms since first tick
                        if recording_flag.is_set() and not first_rec_timestamp == -1:
                            rec_truly_started_event.set()
                            rec_timestamps.append(pygame.time.get_ticks() - first_rec_timestamp)
                        else:
                            first_rec_timestamp = pygame.time.get_ticks()
                            rec_timestamps = [0]

                        ticks += 1

                        if ticks % 50 == 5:
                            clearRecFolder(REC_PATH, rec_timestamps, REC_FPS)

                    # escape
                    elif event.key == pygame.K_ESCAPE:
                        mainloop = False # user pressed ESC

                    # Record Button
                    elif event.key == pygame.K_r and config.getboolean('System','camera'):
                        if not recording_flag.is_set():

                            play_status_movie = False
                            stop_recording_event.clear()

                            first_rec_timestamp = pygame.time.get_ticks()
                            rec_timestamps = []

                            # show pre_rec screen
                            bg = pygame.image.load(os.path.join(ROOT_PATH,'img','pre_rec.png'))
                            screen.blit(bg,(0,0))
                            pygame.display.update()
                            clock.tick(60)

                            rec_timeout_flag.clear()
                            Process(target=recordFrames,
                                    args=(rec_lock,
                                          recording_flag,
                                          # WIDTH, HEIGHT,
                                          342, 238,
                                          REC_DURATION,
                                          REC_FPS,
                                          stop_recording_event,
                                          rec_timeout_flag,
                                          rec_truly_started_event)).start()

                        else:
                            # start postproduction
                            postproduction_flag.set()
                            Process(target=postproduction,
                                    args = (stop_recording_event,
                                            rec_timeout_flag,
                                            rec_truly_started_event,
                                            postproduction_flag,
                                            postproduction_finished_flag)).start()

                    # this is the bin button
                    elif event.key == pygame.K_e:
                        reset_flag.set()

                    # escape
                    elif event.key == pygame.K_ESCAPE:
                        mainloop = False # user pressed ESC

                    elif event.key == pygame.K_h:
                        os.system('sudo halt')

        elif not postproduction_flag.is_set() and show_qr_code:
            # if we are showing the qr code then it is possible to press X
            for event in pygame.event.get():
                if event.key == pygame.K_e:
                    print "real"
                    reset_flag.set()

        else:
            # ignore keyboard-Events while in postproduction
            for event in pygame.event.get():
                pass

            # here we are in postproduction state
            if not postproduction_finished_flag.is_set():

                if not play_status_movie:
                    movie, movie_screen = changeVideo(os.path.join(ROOT_PATH,'img','development.mp4'), WIDTH, HEIGHT)
                    play_status_movie = True

            elif postproduction_finished_flag.is_set() and not show_qr_code:
                bg = pygame.image.load(os.path.join(ROOT_PATH,'img','deinvideo.png'))
                screen.blit(bg,(0,0))
                #show shortlink
                f = open('url', 'r')
                url = f.read()
                font = pygame.font.Font(None, 24)
                text = font.render(url, 1, (255, 255, 255))
                screen.blit(text, (135, 268))

                movie, movie_screen = changeVideo(os.path.join(VIDEO_PATH), 342, 238)
                qr_code_path = os.path.join(ROOT_PATH,'img', 'code.png')
                img = scale(pygame.image.load(qr_code_path), (50,50))
                screen.blit(img,(350,255))
                #pygame.display.update()
                #clock.tick(60)
                postproduction_flag.clear()
                last_event = pygame.time.get_ticks()
                show_qr_code = True

        # if in recoding state then update timer image
        if (recording_flag.is_set()):

            # repaint last frame
            image = ALL_FRAMES[frame - 1]
            img = scale(pygame.image.load(image), (WIDTH,HEIGHT))
            screen.blit(img,(0,0))

            # paint timer icon
            timespent = (pygame.time.get_ticks() - first_rec_timestamp)/1000
            image = TIMER_IMGS[timespent%61 - 1]
            img = pygame.image.load(image)
            screen.blit(img,(0,0))

        # do this all the time
        pygame.display.update()
        clock.tick(60)

    if GPIO_ACTIVE:
        GPIO.cleanup()
<|endoftext|>


<commit_before>"""Tests for 'site'.

Tests assume the initial paths in sys.path once the interpreter has begun
executing have not been removed.

"""
import unittest
from test.support import run_unittest, TESTFN, EnvironmentVarGuard
import builtins
import os
import sys
import encodings
import subprocess
import sysconfig
from copy import copy

# Need to make sure to not import 'site' if someone specified ``-S`` at the
# command-line.  Detect this by just making sure 'site' has not been imported
# already.
if "site" in sys.modules:
    import site
else:
    raise unittest.SkipTest("importation of site.py suppressed")

if not os.path.isdir(site.USER_SITE):
    # need to add user site directory for tests
    os.makedirs(site.USER_SITE)
    site.addsitedir(site.USER_SITE)

class HelperFunctionsTests(unittest.TestCase):
    """Tests for helper functions.

    The setting of the encoding (set using sys.setdefaultencoding) used by
    the Unicode implementation is not tested.

    """

    def setUp(self):
        """Save a copy of sys.path"""
        self.sys_path = sys.path[:]
        self.old_base = site.USER_BASE
        self.old_site = site.USER_SITE
        self.old_prefixes = site.PREFIXES
        self.old_vars = copy(sysconfig._CONFIG_VARS)

    def tearDown(self):
        """Restore sys.path"""
        sys.path[:] = self.sys_path
        site.USER_BASE = self.old_base
        site.USER_SITE = self.old_site
        site.PREFIXES = self.old_prefixes
        sysconfig._CONFIG_VARS = self.old_vars

    def test_makepath(self):
        # Test makepath() have an absolute path for its first return value
        # and a case-normalized version of the absolute path for its
        # second value.
        path_parts = ("Beginning", "End")
        original_dir = os.path.join(*path_parts)
        abs_dir, norm_dir = site.makepath(*path_parts)
        self.assertEqual(os.path.abspath(original_dir), abs_dir)
        if original_dir == os.path.normcase(original_dir):
            self.assertEqual(abs_dir, norm_dir)
        else:
            self.assertEqual(os.path.normcase(abs_dir), norm_dir)

    def test_init_pathinfo(self):
        dir_set = site._init_pathinfo()
        for entry in [site.makepath(path)[1] for path in sys.path
                        if path and os.path.isdir(path)]:
            self.assertIn(entry, dir_set,
                          "%s from sys.path not found in set returned "
                          "by _init_pathinfo(): %s" % (entry, dir_set))

    def pth_file_tests(self, pth_file):
        """Contain common code for testing results of reading a .pth file"""
        self.assertIn(pth_file.imported, sys.modules,
                      "%s not in sys.modules" % pth_file.imported)
        self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
        self.assertFalse(os.path.exists(pth_file.bad_dir_path))

    def test_addpackage(self):
        # Make sure addpackage() imports if the line starts with 'import',
        # adds directories to sys.path for any line in the file that is not a
        # comment or import that is a valid directory name for where the .pth
        # file resides; invalid directories are not added
        pth_file = PthFile()
        pth_file.cleanup(prep=True)  # to make sure that nothing is
                                      # pre-existing that shouldn't be
        try:
            pth_file.create()
            site.addpackage(pth_file.base_dir, pth_file.filename, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup()

    def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup()

    def test_s_option(self):
        usersite = site.USER_SITE
        self.assertIn(usersite, sys.path)

        rc = subprocess.call([sys.executable, '-c',
            'import sys; sys.exit(%r in sys.path)' % usersite])
        self.assertEqual(rc, 1)

        rc = subprocess.call([sys.executable, '-s', '-c',
            'import sys; sys.exit(%r in sys.path)' % usersite])
        self.assertEqual(rc, 0)

        env = os.environ.copy()
        env["PYTHONNOUSERSITE"] = "1"
        rc = subprocess.call([sys.executable, '-c',
            'import sys; sys.exit(%r in sys.path)' % usersite],
            env=env)
        self.assertEqual(rc, 0)

        env = os.environ.copy()
        env["PYTHONUSERBASE"] = "/tmp"
        rc = subprocess.call([sys.executable, '-c',
            'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
            env=env)
        self.assertEqual(rc, 1)

    def test_getuserbase(self):
        site.USER_BASE = None
        user_base = site.getuserbase()

        # the call sets site.USER_BASE
        self.assertEquals(site.USER_BASE, user_base)

        # let's set PYTHONUSERBASE and see if it uses it
        site.USER_BASE = None
        import sysconfig
        sysconfig._CONFIG_VARS = None

        with EnvironmentVarGuard() as environ:
            environ['PYTHONUSERBASE'] = 'xoxo'
            self.assertTrue(site.getuserbase().startswith('xoxo'),
                            site.getuserbase())

    def test_getusersitepackages(self):
        site.USER_SITE = None
        site.USER_BASE = None
        user_site = site.getusersitepackages()

        # the call sets USER_BASE *and* USER_SITE
        self.assertEquals(site.USER_SITE, user_site)
        self.assertTrue(user_site.startswith(site.USER_BASE), user_site)

    def test_getsitepackages(self):
        site.PREFIXES = ['xoxo']
        dirs = site.getsitepackages()

        if sys.platform in ('os2emx', 'riscos'):
            self.assertEqual(len(dirs), 1)
            wanted = os.path.join('xoxo', 'Lib', 'site-packages')
            self.assertEquals(dirs[0], wanted)
        elif os.sep == '/':
            self.assertTrue(len(dirs), 2)
            wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
                                  'site-packages')
            self.assertEquals(dirs[0], wanted)
            wanted = os.path.join('xoxo', 'lib', 'site-python')
            self.assertEquals(dirs[1], wanted)
        else:
            self.assertTrue(len(dirs), 2)
            self.assertEquals(dirs[0], 'xoxo')
            wanted = os.path.join('xoxo', 'lib', 'site-packages')
            self.assertEquals(dirs[1], wanted)

        # let's try the specific Apple location
        if (sys.platform == "darwin" and
            sysconfig.get_config_var("PYTHONFRAMEWORK")):
            site.PREFIXES = ['Python.framework']
            dirs = site.getsitepackages()
            self.assertEqual(len(dirs), 3)
            wanted = os.path.join('/Library', 'Python', sys.version[:3],
                                  'site-packages')
            self.assertEquals(dirs[2], wanted)

class PthFile(object):
    """Helper class for handling testing of .pth files"""

    def __init__(self, filename_base=TESTFN, imported="time",
                    good_dirname="__testdir__", bad_dirname="__bad"):
        """Initialize instance variables"""
        self.filename = filename_base + ".pth"
        self.base_dir = os.path.abspath('')
        self.file_path = os.path.join(self.base_dir, self.filename)
        self.imported = imported
        self.good_dirname = good_dirname
        self.bad_dirname = bad_dirname
        self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
        self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)

    def create(self):
        """Create a .pth file with a comment, blank lines, an ``import
        <self.imported>``, a line with self.good_dirname, and a line with
        self.bad_dirname.

        Creation of the directory for self.good_dir_path (based off of
        self.good_dirname) is also performed.

        Make sure to call self.cleanup() to undo anything done by this method.

        """
        FILE = open(self.file_path, 'w')
        try:
            print("#import @bad module name", file=FILE)
            print("\n", file=FILE)
            print("import %s" % self.imported, file=FILE)
            print(self.good_dirname, file=FILE)
            print(self.bad_dirname, file=FILE)
        finally:
            FILE.close()
        os.mkdir(self.good_dir_path)

    def cleanup(self, prep=False):
        """Make sure that the .pth file is deleted, self.imported is not in
        sys.modules, and that both self.good_dirname and self.bad_dirname are
        not existing directories."""
        if os.path.exists(self.file_path):
            os.remove(self.file_path)
        if prep:
            self.imported_module = sys.modules.get(self.imported)
            if self.imported_module:
                del sys.modules[self.imported]
        else:
            if self.imported_module:
                sys.modules[self.imported] = self.imported_module
        if os.path.exists(self.good_dir_path):
            os.rmdir(self.good_dir_path)
        if os.path.exists(self.bad_dir_path):
            os.rmdir(self.bad_dir_path)

class ImportSideEffectTests(unittest.TestCase):
    """Test side-effects from importing 'site'."""

    def setUp(self):
        """Make a copy of sys.path"""
        self.sys_path = sys.path[:]

    def tearDown(self):
        """Restore sys.path"""
        sys.path[:] = self.sys_path

    def test_abs_paths(self):
        # Make sure all imported modules have their __file__ and __cached__
        # attributes as absolute paths.  Arranging to put the Lib directory on
        # PYTHONPATH would cause the os module to have a relative path for
        # __file__ if abs_paths() does not get run.  sys and builtins (the
        # only other modules imported before site.py runs) do not have
        # __file__ or __cached__ because they are built-in.
        parent = os.path.relpath(os.path.dirname(os.__file__))
        env = os.environ.copy()
        env['PYTHONPATH'] = parent
        code = ('import os, sys',
            # use ASCII to avoid locale issues with non-ASCII directories
            'os_file = os.__file__.encode("ascii", "backslashreplace")',
            r'sys.stdout.buffer.write(os_file + b"\n")',
            'os_cached = os.__cached__.encode("ascii", "backslashreplace")',
            r'sys.stdout.buffer.write(os_cached + b"\n")')
        command = '\n'.join(code)
        # First, prove that with -S (no 'import site'), the paths are
        # relative.
        proc = subprocess.Popen([sys.executable, '-S', '-c', command],
                                env=env,
                                stdout=subprocess.PIPE)
        stdout, stderr = proc.communicate()

        self.assertEqual(proc.returncode, 0)
        os__file__, os__cached__ = stdout.splitlines()[:2]
        self.assertFalse(os.path.isabs(os__file__))
        self.assertFalse(os.path.isabs(os__cached__))
        # Now, with 'import site', it works.
        proc = subprocess.Popen([sys.executable, '-c', command],
                                env=env,
                                stdout=subprocess.PIPE)
        stdout, stderr = proc.communicate()
        self.assertEqual(proc.returncode, 0)
        os__file__, os__cached__ = stdout.splitlines()[:2]
        self.assertTrue(os.path.isabs(os__file__))
        self.assertTrue(os.path.isabs(os__cached__))

    def test_no_duplicate_paths(self):
        # No duplicate paths should exist in sys.path
        # Handled by removeduppaths()
        site.removeduppaths()
        seen_paths = set()
        for path in sys.path:
            self.assertNotIn(path, seen_paths)
            seen_paths.add(path)

    def test_add_build_dir(self):
        # Test that the build directory's Modules directory is used when it
        # should be.
        # XXX: implement
        pass

    def test_setting_quit(self):
        # 'quit' and 'exit' should be injected into builtins
        self.assertTrue(hasattr(builtins, "quit"))
        self.assertTrue(hasattr(builtins, "exit"))

    def test_setting_copyright(self):
        # 'copyright' and 'credits' should be in builtins
        self.assertTrue(hasattr(builtins, "copyright"))
        self.assertTrue(hasattr(builtins, "credits"))

    def test_setting_help(self):
        # 'help' should be set in builtins
        self.assertTrue(hasattr(builtins, "help"))

    def test_aliasing_mbcs(self):
        if sys.platform == "win32":
            import locale
            if locale.getdefaultlocale()[1].startswith('cp'):
                for value in encodings.aliases.aliases.values():
                    if value == "mbcs":
                        break
                else:
                    self.fail("did not alias mbcs")

    def test_setdefaultencoding_removed(self):
        # Make sure sys.setdefaultencoding is gone
        self.assertTrue(not hasattr(sys, "setdefaultencoding"))

    def test_sitecustomize_executed(self):
        # If sitecustomize is available, it should have been imported.
        if "sitecustomize" not in sys.modules:
            try:
                import sitecustomize
            except ImportError:
                pass
            else:
                self.fail("sitecustomize not imported automatically")

def test_main():
    run_unittest(HelperFunctionsTests, ImportSideEffectTests)

if __name__ == "__main__":
    test_main()
<commit_msg>Correct a couple of assert* methods. Patch by Dave Malcolm.<commit_after>"""Tests for 'site'.

Tests assume the initial paths in sys.path once the interpreter has begun
executing have not been removed.

"""
import unittest
from test.support import run_unittest, TESTFN, EnvironmentVarGuard
import builtins
import os
import sys
import encodings
import subprocess
import sysconfig
from copy import copy

# Need to make sure to not import 'site' if someone specified ``-S`` at the
# command-line.  Detect this by just making sure 'site' has not been imported
# already.
if "site" in sys.modules:
    import site
else:
    raise unittest.SkipTest("importation of site.py suppressed")

if not os.path.isdir(site.USER_SITE):
    # need to add user site directory for tests
    os.makedirs(site.USER_SITE)
    site.addsitedir(site.USER_SITE)

class HelperFunctionsTests(unittest.TestCase):
    """Tests for helper functions.

    The setting of the encoding (set using sys.setdefaultencoding) used by
    the Unicode implementation is not tested.

    """

    def setUp(self):
        """Save a copy of sys.path"""
        self.sys_path = sys.path[:]
        self.old_base = site.USER_BASE
        self.old_site = site.USER_SITE
        self.old_prefixes = site.PREFIXES
        self.old_vars = copy(sysconfig._CONFIG_VARS)

    def tearDown(self):
        """Restore sys.path"""
        sys.path[:] = self.sys_path
        site.USER_BASE = self.old_base
        site.USER_SITE = self.old_site
        site.PREFIXES = self.old_prefixes
        sysconfig._CONFIG_VARS = self.old_vars

    def test_makepath(self):
        # Test makepath() have an absolute path for its first return value
        # and a case-normalized version of the absolute path for its
        # second value.
        path_parts = ("Beginning", "End")
        original_dir = os.path.join(*path_parts)
        abs_dir, norm_dir = site.makepath(*path_parts)
        self.assertEqual(os.path.abspath(original_dir), abs_dir)
        if original_dir == os.path.normcase(original_dir):
            self.assertEqual(abs_dir, norm_dir)
        else:
            self.assertEqual(os.path.normcase(abs_dir), norm_dir)

    def test_init_pathinfo(self):
        dir_set = site._init_pathinfo()
        for entry in [site.makepath(path)[1] for path in sys.path
                        if path and os.path.isdir(path)]:
            self.assertIn(entry, dir_set,
                          "%s from sys.path not found in set returned "
                          "by _init_pathinfo(): %s" % (entry, dir_set))

    def pth_file_tests(self, pth_file):
        """Contain common code for testing results of reading a .pth file"""
        self.assertIn(pth_file.imported, sys.modules,
                      "%s not in sys.modules" % pth_file.imported)
        self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
        self.assertFalse(os.path.exists(pth_file.bad_dir_path))

    def test_addpackage(self):
        # Make sure addpackage() imports if the line starts with 'import',
        # adds directories to sys.path for any line in the file that is not a
        # comment or import that is a valid directory name for where the .pth
        # file resides; invalid directories are not added
        pth_file = PthFile()
        pth_file.cleanup(prep=True)  # to make sure that nothing is
                                      # pre-existing that shouldn't be
        try:
            pth_file.create()
            site.addpackage(pth_file.base_dir, pth_file.filename, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup()

    def test_addsitedir(self):
        # Same tests for test_addpackage since addsitedir() essentially just
        # calls addpackage() for every .pth file in the directory
        pth_file = PthFile()
        pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
                                    # that is tested for
        try:
            pth_file.create()
            site.addsitedir(pth_file.base_dir, set())
            self.pth_file_tests(pth_file)
        finally:
            pth_file.cleanup()

    def test_s_option(self):
        usersite = site.USER_SITE
        self.assertIn(usersite, sys.path)

        rc = subprocess.call([sys.executable, '-c',
            'import sys; sys.exit(%r in sys.path)' % usersite])
        self.assertEqual(rc, 1)

        rc = subprocess.call([sys.executable, '-s', '-c',
            'import sys; sys.exit(%r in sys.path)' % usersite])
        self.assertEqual(rc, 0)

        env = os.environ.copy()
        env["PYTHONNOUSERSITE"] = "1"
        rc = subprocess.call([sys.executable, '-c',
            'import sys; sys.exit(%r in sys.path)' % usersite],
            env=env)
        self.assertEqual(rc, 0)

        env = os.environ.copy()
        env["PYTHONUSERBASE"] = "/tmp"
        rc = subprocess.call([sys.executable, '-c',
            'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
            env=env)
        self.assertEqual(rc, 1)

    def test_getuserbase(self):
        site.USER_BASE = None
        user_base = site.getuserbase()

        # the call sets site.USER_BASE
        self.assertEquals(site.USER_BASE, user_base)

        # let's set PYTHONUSERBASE and see if it uses it
        site.USER_BASE = None
        import sysconfig
        sysconfig._CONFIG_VARS = None

        with EnvironmentVarGuard() as environ:
            environ['PYTHONUSERBASE'] = 'xoxo'
            self.assertTrue(site.getuserbase().startswith('xoxo'),
                            site.getuserbase())

    def test_getusersitepackages(self):
        site.USER_SITE = None
        site.USER_BASE = None
        user_site = site.getusersitepackages()

        # the call sets USER_BASE *and* USER_SITE
        self.assertEquals(site.USER_SITE, user_site)
        self.assertTrue(user_site.startswith(site.USER_BASE), user_site)

    def test_getsitepackages(self):
        site.PREFIXES = ['xoxo']
        dirs = site.getsitepackages()

        if sys.platform in ('os2emx', 'riscos'):
            self.assertEqual(len(dirs), 1)
            wanted = os.path.join('xoxo', 'Lib', 'site-packages')
            self.assertEquals(dirs[0], wanted)
        elif os.sep == '/':
            self.assertEqual(len(dirs), 2)
            wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
                                  'site-packages')
            self.assertEquals(dirs[0], wanted)
            wanted = os.path.join('xoxo', 'lib', 'site-python')
            self.assertEquals(dirs[1], wanted)
        else:
            self.assertEqual(len(dirs), 2)
            self.assertEquals(dirs[0], 'xoxo')
            wanted = os.path.join('xoxo', 'lib', 'site-packages')
            self.assertEquals(dirs[1], wanted)

        # let's try the specific Apple location
        if (sys.platform == "darwin" and
            sysconfig.get_config_var("PYTHONFRAMEWORK")):
            site.PREFIXES = ['Python.framework']
            dirs = site.getsitepackages()
            self.assertEqual(len(dirs), 3)
            wanted = os.path.join('/Library', 'Python', sys.version[:3],
                                  'site-packages')
            self.assertEquals(dirs[2], wanted)

class PthFile(object):
    """Helper class for handling testing of .pth files"""

    def __init__(self, filename_base=TESTFN, imported="time",
                    good_dirname="__testdir__", bad_dirname="__bad"):
        """Initialize instance variables"""
        self.filename = filename_base + ".pth"
        self.base_dir = os.path.abspath('')
        self.file_path = os.path.join(self.base_dir, self.filename)
        self.imported = imported
        self.good_dirname = good_dirname
        self.bad_dirname = bad_dirname
        self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
        self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)

    def create(self):
        """Create a .pth file with a comment, blank lines, an ``import
        <self.imported>``, a line with self.good_dirname, and a line with
        self.bad_dirname.

        Creation of the directory for self.good_dir_path (based off of
        self.good_dirname) is also performed.

        Make sure to call self.cleanup() to undo anything done by this method.

        """
        FILE = open(self.file_path, 'w')
        try:
            print("#import @bad module name", file=FILE)
            print("\n", file=FILE)
            print("import %s" % self.imported, file=FILE)
            print(self.good_dirname, file=FILE)
            print(self.bad_dirname, file=FILE)
        finally:
            FILE.close()
        os.mkdir(self.good_dir_path)

    def cleanup(self, prep=False):
        """Make sure that the .pth file is deleted, self.imported is not in
        sys.modules, and that both self.good_dirname and self.bad_dirname are
        not existing directories."""
        if os.path.exists(self.file_path):
            os.remove(self.file_path)
        if prep:
            self.imported_module = sys.modules.get(self.imported)
            if self.imported_module:
                del sys.modules[self.imported]
        else:
            if self.imported_module:
                sys.modules[self.imported] = self.imported_module
        if os.path.exists(self.good_dir_path):
            os.rmdir(self.good_dir_path)
        if os.path.exists(self.bad_dir_path):
            os.rmdir(self.bad_dir_path)

class ImportSideEffectTests(unittest.TestCase):
    """Test side-effects from importing 'site'."""

    def setUp(self):
        """Make a copy of sys.path"""
        self.sys_path = sys.path[:]

    def tearDown(self):
        """Restore sys.path"""
        sys.path[:] = self.sys_path

    def test_abs_paths(self):
        # Make sure all imported modules have their __file__ and __cached__
        # attributes as absolute paths.  Arranging to put the Lib directory on
        # PYTHONPATH would cause the os module to have a relative path for
        # __file__ if abs_paths() does not get run.  sys and builtins (the
        # only other modules imported before site.py runs) do not have
        # __file__ or __cached__ because they are built-in.
        parent = os.path.relpath(os.path.dirname(os.__file__))
        env = os.environ.copy()
        env['PYTHONPATH'] = parent
        code = ('import os, sys',
            # use ASCII to avoid locale issues with non-ASCII directories
            'os_file = os.__file__.encode("ascii", "backslashreplace")',
            r'sys.stdout.buffer.write(os_file + b"\n")',
            'os_cached = os.__cached__.encode("ascii", "backslashreplace")',
            r'sys.stdout.buffer.write(os_cached + b"\n")')
        command = '\n'.join(code)
        # First, prove that with -S (no 'import site'), the paths are
        # relative.
        proc = subprocess.Popen([sys.executable, '-S', '-c', command],
                                env=env,
                                stdout=subprocess.PIPE)
        stdout, stderr = proc.communicate()

        self.assertEqual(proc.returncode, 0)
        os__file__, os__cached__ = stdout.splitlines()[:2]
        self.assertFalse(os.path.isabs(os__file__))
        self.assertFalse(os.path.isabs(os__cached__))
        # Now, with 'import site', it works.
        proc = subprocess.Popen([sys.executable, '-c', command],
                                env=env,
                                stdout=subprocess.PIPE)
        stdout, stderr = proc.communicate()
        self.assertEqual(proc.returncode, 0)
        os__file__, os__cached__ = stdout.splitlines()[:2]
        self.assertTrue(os.path.isabs(os__file__))
        self.assertTrue(os.path.isabs(os__cached__))

    def test_no_duplicate_paths(self):
        # No duplicate paths should exist in sys.path
        # Handled by removeduppaths()
        site.removeduppaths()
        seen_paths = set()
        for path in sys.path:
            self.assertNotIn(path, seen_paths)
            seen_paths.add(path)

    def test_add_build_dir(self):
        # Test that the build directory's Modules directory is used when it
        # should be.
        # XXX: implement
        pass

    def test_setting_quit(self):
        # 'quit' and 'exit' should be injected into builtins
        self.assertTrue(hasattr(builtins, "quit"))
        self.assertTrue(hasattr(builtins, "exit"))

    def test_setting_copyright(self):
        # 'copyright' and 'credits' should be in builtins
        self.assertTrue(hasattr(builtins, "copyright"))
        self.assertTrue(hasattr(builtins, "credits"))

    def test_setting_help(self):
        # 'help' should be set in builtins
        self.assertTrue(hasattr(builtins, "help"))

    def test_aliasing_mbcs(self):
        if sys.platform == "win32":
            import locale
            if locale.getdefaultlocale()[1].startswith('cp'):
                for value in encodings.aliases.aliases.values():
                    if value == "mbcs":
                        break
                else:
                    self.fail("did not alias mbcs")

    def test_setdefaultencoding_removed(self):
        # Make sure sys.setdefaultencoding is gone
        self.assertTrue(not hasattr(sys, "setdefaultencoding"))

    def test_sitecustomize_executed(self):
        # If sitecustomize is available, it should have been imported.
        if "sitecustomize" not in sys.modules:
            try:
                import sitecustomize
            except ImportError:
                pass
            else:
                self.fail("sitecustomize not imported automatically")

def test_main():
    run_unittest(HelperFunctionsTests, ImportSideEffectTests)

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


<commit_before>from datetime import date

from django import forms

from tower import ugettext_lazy as _lazy

class AnnouncementForm(forms.Form):
    """Form for collecting information about an announcement.

    This is not a ModelForm, and does not include the group or locale fields,
    because it should only be used in a context where the group or locale is
    implicit, and should not be user controllable. If you need a user
    controllable locale or group, use the admin interface.

    """
    content = forms.CharField(label=_lazy('Content'), max_length=10000,
                              widget=forms.Textarea)
    show_after = forms.DateField(label=_lazy('Show after'), initial=date.today,
                                 input_formats=['%Y-%m-%d'])
    show_until = forms.DateField(label=_lazy('Show until'), required=False,
                                 input_formats=['%Y-%m-%d'])
<commit_msg>Unicode fix in announcements form.<commit_after>from datetime import date

from django import forms

from tower import ugettext_lazy as _lazy

class AnnouncementForm(forms.Form):
    """Form for collecting information about an announcement.

    This is not a ModelForm, and does not include the group or locale fields,
    because it should only be used in a context where the group or locale is
    implicit, and should not be user controllable. If you need a user
    controllable locale or group, use the admin interface.

    """
    content = forms.CharField(label=_lazy(u'Content'), max_length=10000,
                              widget=forms.Textarea)
    show_after = forms.DateField(label=_lazy(u'Show after'), initial=date.today,
                                 input_formats=['%Y-%m-%d'])
    show_until = forms.DateField(label=_lazy(u'Show until'), required=False,
                                 input_formats=['%Y-%m-%d'])
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import json
import requests
from six.moves.urllib.parse import urlencode

import frappe
from frappe.model.document import Document
from frappe import _
from frappe.utils import get_url, call_hook_method, cint, flt, cstr
from frappe.integrations.utils import make_get_request, make_post_request, create_request_log, create_payment_gateway
from frappe.utils import get_request_site_address
from frappe.integrations.doctype.paytm_settings.checksum import generate_checksum, verify_checksum
from frappe.utils.password import get_decrypted_password

class PaytmSettings(Document):
	supported_currencies = ["INR"]

	def validate(self):
		create_payment_gateway('Paytm')
		call_hook_method('payment_gateway_enabled', gateway='Paytm')
		if not self.flags.ignore_mandatory:
			self.validate_paytm_credentails()

	def validate_paytm_credentails(self):
		if self.merchant_id and self.merchant_key:
			pass
			# header = {"Authorization": "Bearer {0}".format(self.get_password(fieldname="secret_key", raise_exception=False))}
			# try:
			# 	make_get_request(url="https://api.stripe.com/v1/charges", headers=header)
			# except Exception:
			# 	frappe.throw(_("Seems Publishable Key or Secret Key is wrong !!!"))

	def validate_transaction_currency(self, currency):
		if currency not in self.supported_currencies:
			frappe.throw(_("Please select another payment method. Stripe does not support transactions in currency '{0}'").format(currency))

	def get_payment_url(self, **kwargs):
		'''Return payment url with several params'''
		# create unique order id by making it equal to the integration request
		integration_request = create_request_log(kwargs, "Host", "Paytm")
		kwargs.update(dict(order_id=integration_request.name))

		return get_url("./integrations/paytm_checkout?{0}".format(urlencode(kwargs)))

def get_paytm_config():
	''' Returns paytm config '''

	paytm_config = frappe.db.get_singles_dict('Paytm Settings')
	paytm_config.update(dict(merchant_key=get_decrypted_password('Paytm Settings', 'Paytm Settings', 'merchant_key')))

	if cint(paytm_config.staging):
		paytm_config.update(dict(
			website="WEBSTAGING",
			url='https://securegw-stage.paytm.in/order/process',
			transaction_status_url='https://securegw-stage.paytm.in/order/status',
			industry_type_id='RETAIL'
		))
	else:
		paytm_config.update(dict(
			url='https://securegw.paytm.in/order/process',
			transaction_status_url='https://securegw.paytm.in/order/status',
		))
	return paytm_config

def get_paytm_params(payment_details, order_id, paytm_config):

	# initialize a dictionary
	paytm_params = dict()

	# redirect_uri = get_request_site_address(True) + "/api/method/frappe.integrations.doctype.paytm_settings.paytm_settings.get_transaction_status"
	redirect_uri = "http://cf9b2bb1.ngrok.io/api/method/frappe.integrations.doctype.paytm_settings.paytm_settings.get_transaction_status"

	paytm_params.update({
		"MID" : paytm_config.merchant_id,
		"WEBSITE" : paytm_config.website,
		"INDUSTRY_TYPE_ID" : paytm_config.industry_type_id,
		"CHANNEL_ID" : "WEB",
		"ORDER_ID" : order_id,
		"CUST_ID" : payment_details['payer_email'],
		"EMAIL" : payment_details['payer_email'],
		"TXN_AMOUNT" : cstr(flt(payment_details['amount'], 2)),
		"CALLBACK_URL" : redirect_uri,
	})

	checksum = generate_checksum(paytm_params, paytm_config.merchant_key)

	paytm_params.update({
		"CHECKSUMHASH" : checksum
	})

	return paytm_params

@frappe.whitelist(allow_guest=True)
def verify_transaction(**kwargs):
	'''Verify checksum for received data in the callback and then verify the transaction'''
	paytm_config = get_paytm_config()
	received_data = frappe._dict(kwargs)

	paytm_params = {}
	for key, value in received_data.items():
		if key == 'CHECKSUMHASH':
			paytm_checksum = value
		else:
			paytm_params[key] = value

	# Verify checksum
	is_valid_checksum = verify_checksum(paytm_params, paytm_config.merchant_key, paytm_checksum)

	if is_valid_checksum and received_data['RESPCODE'] == '01':
		verify_transaction_status(paytm_config, received_data['ORDERID'])
	else:
		frappe.respond_as_web_page("Payment Failed",
			"Transaction failed to complete. Don't worry, in case of failure amount will get refunded to your account.",
			http_status_code=401, indicator_color='red')
		frappe.log_error("Order unsuccessful, received data:"+received_data, 'Paytm Payment Failed')

def verify_transaction_status(paytm_config, order_id):
	'''Verify transaction completion after checksum has been verified'''
	paytm_params=dict(
		MID=paytm_config.merchant_id,
		ORDERID= order_id
	)

	checksum = generate_checksum(paytm_params, paytm_config.merchant_key)
	paytm_params["CHECKSUMHASH"] = checksum

	post_data = json.dumps(paytm_params)
	url = paytm_config.transaction_status_url

	response = requests.post(url, data = post_data, headers = {"Content-type": "application/json"}).json()
	finalize_request(order_id, response)

def finalize_request(order_id, transaction_response):
	request = frappe.db.get_value('Integration Request', order_id)
	redirect_to = request.data.get('redirect_to') or None
	redirect_message = request.data.get('redirect_message') or None

	if transaction_response['STATUS'] == "TXN_SUCCESS":
		if request.data.reference_doctype and request.data.reference_docname:
			custom_redirect_to = None
			try:
				custom_redirect_to = frappe.get_doc(request.data.reference_doctype,
					request.data.reference_docname).run_method("on_payment_authorized", request.flags.status_changed_to)
			except Exception:
				frappe.log_error(frappe.get_traceback())

			if custom_redirect_to:
				redirect_to = custom_redirect_to

			redirect_url = 'payment-success'
	else:
		redirect_url = 'payment-failed'

	if redirect_to:
		redirect_url += '?' + urlencode({'redirect_to': redirect_to})
	if redirect_message:
		redirect_url += '&' + urlencode({'redirect_message': redirect_message})

	frappe.local.response['type'] = 'redirect'
	frappe.local.response['location'] = 'redirect_url'

def get_gateway_controller(doctype, docname):
	reference_doc = frappe.get_doc(doctype, docname)
	gateway_controller = frappe.db.get_value("Payment Gateway", reference_doc.payment_gateway, "gateway_controller")
	return gateway_controller<commit_msg>fix: change status of integration request on completion<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import json
import requests
from six.moves.urllib.parse import urlencode

import frappe
from frappe.model.document import Document
from frappe import _
from frappe.utils import get_url, call_hook_method, cint, flt, cstr
from frappe.integrations.utils import make_get_request, make_post_request, create_request_log, create_payment_gateway
from frappe.utils import get_request_site_address
from frappe.integrations.doctype.paytm_settings.checksum import generate_checksum, verify_checksum
from frappe.utils.password import get_decrypted_password

class PaytmSettings(Document):
	supported_currencies = ["INR"]

	def validate(self):
		create_payment_gateway('Paytm')
		call_hook_method('payment_gateway_enabled', gateway='Paytm')
		if not self.flags.ignore_mandatory:
			self.validate_paytm_credentails()

	def validate_paytm_credentails(self):
		if self.merchant_id and self.merchant_key:
			pass
			# header = {"Authorization": "Bearer {0}".format(self.get_password(fieldname="secret_key", raise_exception=False))}
			# try:
			# 	make_get_request(url="https://api.stripe.com/v1/charges", headers=header)
			# except Exception:
			# 	frappe.throw(_("Seems Publishable Key or Secret Key is wrong !!!"))

	def validate_transaction_currency(self, currency):
		if currency not in self.supported_currencies:
			frappe.throw(_("Please select another payment method. Stripe does not support transactions in currency '{0}'").format(currency))

	def get_payment_url(self, **kwargs):
		'''Return payment url with several params'''
		# create unique order id by making it equal to the integration request
		integration_request = create_request_log(kwargs, "Host", "Paytm")
		kwargs.update(dict(order_id=integration_request.name))

		return get_url("./integrations/paytm_checkout?{0}".format(urlencode(kwargs)))

def get_paytm_config():
	''' Returns paytm config '''

	paytm_config = frappe.db.get_singles_dict('Paytm Settings')
	paytm_config.update(dict(merchant_key=get_decrypted_password('Paytm Settings', 'Paytm Settings', 'merchant_key')))

	if cint(paytm_config.staging):
		paytm_config.update(dict(
			website="WEBSTAGING",
			url='https://securegw-stage.paytm.in/order/process',
			transaction_status_url='https://securegw-stage.paytm.in/order/status',
			industry_type_id='RETAIL'
		))
	else:
		paytm_config.update(dict(
			url='https://securegw.paytm.in/order/process',
			transaction_status_url='https://securegw.paytm.in/order/status',
		))
	return paytm_config

def get_paytm_params(payment_details, order_id, paytm_config):

	# initialize a dictionary
	paytm_params = dict()

	# redirect_uri = get_request_site_address(True) + "/api/method/frappe.integrations.doctype.paytm_settings.paytm_settings.get_transaction_status"
	redirect_uri = "http://cf9b2bb1.ngrok.io/api/method/frappe.integrations.doctype.paytm_settings.paytm_settings.get_transaction_status"

	paytm_params.update({
		"MID" : paytm_config.merchant_id,
		"WEBSITE" : paytm_config.website,
		"INDUSTRY_TYPE_ID" : paytm_config.industry_type_id,
		"CHANNEL_ID" : "WEB",
		"ORDER_ID" : order_id,
		"CUST_ID" : payment_details['payer_email'],
		"EMAIL" : payment_details['payer_email'],
		"TXN_AMOUNT" : cstr(flt(payment_details['amount'], 2)),
		"CALLBACK_URL" : redirect_uri,
	})

	checksum = generate_checksum(paytm_params, paytm_config.merchant_key)

	paytm_params.update({
		"CHECKSUMHASH" : checksum
	})

	return paytm_params

@frappe.whitelist(allow_guest=True)
def verify_transaction(**kwargs):
	'''Verify checksum for received data in the callback and then verify the transaction'''
	paytm_config = get_paytm_config()
	received_data = frappe._dict(kwargs)

	paytm_params = {}
	for key, value in received_data.items():
		if key == 'CHECKSUMHASH':
			paytm_checksum = value
		else:
			paytm_params[key] = value

	# Verify checksum
	is_valid_checksum = verify_checksum(paytm_params, paytm_config.merchant_key, paytm_checksum)

	if is_valid_checksum and received_data['RESPCODE'] == '01':
		verify_transaction_status(paytm_config, received_data['ORDERID'])
	else:
		frappe.respond_as_web_page("Payment Failed",
			"Transaction failed to complete. Don't worry, in case of failure amount will get refunded to your account.",
			http_status_code=401, indicator_color='red')
		frappe.log_error("Order unsuccessful, received data:"+received_data, 'Paytm Payment Failed')

def verify_transaction_status(paytm_config, order_id):
	'''Verify transaction completion after checksum has been verified'''
	paytm_params=dict(
		MID=paytm_config.merchant_id,
		ORDERID= order_id
	)

	checksum = generate_checksum(paytm_params, paytm_config.merchant_key)
	paytm_params["CHECKSUMHASH"] = checksum

	post_data = json.dumps(paytm_params)
	url = paytm_config.transaction_status_url

	response = requests.post(url, data = post_data, headers = {"Content-type": "application/json"}).json()
	finalize_request(order_id, response)

def finalize_request(order_id, transaction_response):
	request = frappe.get_doc('Integration Request', order_id)
	redirect_to = request.data.get('redirect_to') or None
	redirect_message = request.data.get('redirect_message') or None

	if transaction_response['STATUS'] == "TXN_SUCCESS":
		if request.data.reference_doctype and request.data.reference_docname:
			custom_redirect_to = None
			try:
				custom_redirect_to = frappe.get_doc(request.data.reference_doctype,
					request.data.reference_docname).run_method("on_payment_authorized", 'Completed')
				request.db_set('status', 'Completed')
			except Exception:
				request.db_set('status', 'Failed')
				frappe.log_error(frappe.get_traceback())

			if custom_redirect_to:
				redirect_to = custom_redirect_to

			redirect_url = 'payment-success'
	else:
		request.db_set('status', 'Failed')
		redirect_url = 'payment-failed'

	if redirect_to:
		redirect_url += '?' + urlencode({'redirect_to': redirect_to})
	if redirect_message:
		redirect_url += '&' + urlencode({'redirect_message': redirect_message})

	frappe.local.response['type'] = 'redirect'
	frappe.local.response['location'] = 'redirect_url'

def get_gateway_controller(doctype, docname):
	reference_doc = frappe.get_doc(doctype, docname)
	gateway_controller = frappe.db.get_value("Payment Gateway", reference_doc.payment_gateway, "gateway_controller")
	return gateway_controller<|endoftext|>