<commit_before>import ast

def add_attribute_decorator(attr, value):
    def wrapper(function_or_class):
        setattr(function_or_class, attr, value)
        return function_or_class
    return wrapper

class TransformToSpecsNodeTransformer(ast.NodeTransformer):
    PENDING_EXAMPLE_GROUPS = ('_description', '_context', '_describe')
    FOCUSED_EXAMPLE_GROUPS = ('fdescription', 'fcontext', 'fdescribe')
    SHARED_EXAMPLE_GROUPS = ('shared_context', )
    INCLUDED_EXAMPLE_GROUPS = ('included_context', )
    EXAMPLE_GROUPS = ('description', 'context', 'describe') + PENDING_EXAMPLE_GROUPS + FOCUSED_EXAMPLE_GROUPS + SHARED_EXAMPLE_GROUPS
    FOCUSED_EXAMPLE = ('fit', )
    PENDING_EXAMPLE = ('_it', )
    EXAMPLES = ('it',) + PENDING_EXAMPLE + FOCUSED_EXAMPLE
    FOCUSED = FOCUSED_EXAMPLE_GROUPS + FOCUSED_EXAMPLE
    HOOKS = ('before', 'after')

    sequence = 1

    def visit_Module(self, node):
        self.has_focused_examples = False

        super(TransformToSpecsNodeTransformer, self).generic_visit(node)

        node.body.insert(0, ast.ImportFrom(
            module='mamba.nodetransformers',
            names=[ast.alias(name='add_attribute_decorator')],
            level=0
        ))
        node.body.append(ast.Assign(
            targets=[ast.Name(id='__mamba_has_focused_examples', ctx=ast.Store())],
            value=ast.Name(id=str(self.has_focused_examples), ctx=ast.Load())),
        )

        return node

    def visit_With(self, node):
        super(TransformToSpecsNodeTransformer, self).generic_visit(node)

        name = self._get_name(node)

        if name in self.FOCUSED:
            self.has_focused_examples = True

        if name in self.INCLUDED_EXAMPLE_GROUPS:
            return self._get_shared_example_group(node)
        if name in self.EXAMPLE_GROUPS:
            return self._transform_to_example_group(node, name)
        if name in self.EXAMPLES:
            return self._transform_to_example(node, name)
        if name in self.HOOKS:
            return self._transform_to_hook(node, name)

        return node

    def _get_name(self, node):
        context_expr = self._context_expr_for(node)

        if isinstance(context_expr, ast.Call):
            if hasattr(context_expr.func, 'value'):
                return context_expr.func.value.id
            return context_expr.func.id

        if isinstance(context_expr, ast.Attribute):
            return context_expr.value.id

    def _context_expr_for(self, node):
        return node.context_expr

    def _transform_to_example_group(self, node, name):
        context_expr = self._context_expr_for(node)
        example_name = self._human_readable_context_expr(context_expr)
        return ast.copy_location(
            ast.ClassDef(
                name=self._prefix_with_sequence(example_name),
                bases=[],
                keywords=[],
                body=node.body,
                decorator_list=[
                    self._set_attribute('_example_group', True),
                    self._set_attribute('_example_name', example_name),
                    self._set_attribute('_tags', self._tags_from(context_expr, name)),
                    self._set_attribute('_pending', name in self.PENDING_EXAMPLE_GROUPS),
                    self._set_attribute('_shared', name in self.SHARED_EXAMPLE_GROUPS)
                ]
            ),
            node
        )

    def _human_readable_context_expr(self, context_expr):
        if isinstance(context_expr.args[0], ast.Str):
            return context_expr.args[0].s
        elif isinstance(context_expr.args[0], ast.Attribute):
            return context_expr.args[0].attr
        else:
            return context_expr.args[0].id

    def _tags_from(self, context_expr, method_name):
        tags = []
        if method_name in self.FOCUSED:
            tags.append('focus')
        if len(context_expr.args) > 1:
            tags.extend([arg.s for arg in context_expr.args[1:]])

        return tags

    def _transform_to_example(self, node, name):
        context_expr = self._context_expr_for(node)

        example_name = '{0} {1}'.format(name, context_expr.args[0].s)

        return ast.copy_location(
            ast.FunctionDef(
                name=self._prefix_with_sequence(example_name),
                args=self._generate_argument('self'),
                body=node.body,
                decorator_list=[
                    self._set_attribute('_example', True),
                    self._set_attribute('_example_name', example_name),
                    self._set_attribute('_tags', self._tags_from(context_expr, name)),
                    self._set_attribute('_pending', name in self.PENDING_EXAMPLE)
                ]
            ),
            node
        )

    def _prefix_with_sequence(self, name):
        result = '{0:08d}__{1}'.format(self.sequence, name)
        self.sequence += 1
        return result

    def _generate_argument(self, name):
        return ast.arguments(args=[ast.Name(id=name, ctx=ast.Param())], vararg=None, kwarg=None, defaults=[])

    def _transform_to_hook(self, node, name):
        when = self._context_expr_for(node).attr
        return ast.copy_location(
            ast.FunctionDef(
                name=name + '_' + when,
                args=self._generate_argument('self'),
                body=node.body,
                decorator_list=[]
            ),
            node
        )

    def _get_shared_example_group(self, node):
        context_expr = self._context_expr_for(node)
        example_name = self._human_readable_context_expr(context_expr)

        return ast.copy_location(
            ast.ClassDef(
                name=self._prefix_with_sequence(example_name),
                bases=[],
                keywords=[],
                body=node.body,
                decorator_list=[
                    self._set_attribute('_example_group', True),
                    self._set_attribute('_example_name', example_name),
                    self._set_attribute('_tags', self._tags_from(context_expr, 'context')),
                    self._set_attribute('_pending', False),
                    self._set_attribute('_shared', False)
                ]
            ),
            node
        )

    def _set_attribute(self, attr, value):
        return ast.Call(
            func=ast.Name(id='add_attribute_decorator', ctx=ast.Load()),
            args=[ast.Str(attr), self._convert_value(value)],
            keywords=[]
        )

    def _convert_value(self, value):
        if isinstance(value, bool):
            return ast.Str(str(value) if value else '')
        elif isinstance(value, list):
            return ast.List(elts=list(map(self._convert_value, value)), ctx=ast.Load())
        else:
            return ast.Str(str(value))

class TransformToSpecsPython3NodeTransformer(TransformToSpecsNodeTransformer):

    def _context_expr_for(self, node):
        return node.items[0].context_expr

    def _generate_argument(self, name):
        return ast.arguments(
            args=[ast.arg(arg=name, annotation=None)],
            vararg=None,
            kwonlyargs=[],
            kw_defaults=[],
            kwarg=None,
            defaults=[]
        )
<commit_msg>Insert examples from shared context into included contexts<commit_after>import ast

def add_attribute_decorator(attr, value):
    def wrapper(function_or_class):
        setattr(function_or_class, attr, value)
        return function_or_class
    return wrapper

class TransformToSpecsNodeTransformer(ast.NodeTransformer):
    PENDING_EXAMPLE_GROUPS = ('_description', '_context', '_describe')
    FOCUSED_EXAMPLE_GROUPS = ('fdescription', 'fcontext', 'fdescribe')
    SHARED_EXAMPLE_GROUPS = ('shared_context', )
    INCLUDED_EXAMPLE_GROUPS = ('included_context', )
    EXAMPLE_GROUPS = ('description', 'context', 'describe') + PENDING_EXAMPLE_GROUPS + FOCUSED_EXAMPLE_GROUPS + SHARED_EXAMPLE_GROUPS
    FOCUSED_EXAMPLE = ('fit', )
    PENDING_EXAMPLE = ('_it', )
    EXAMPLES = ('it',) + PENDING_EXAMPLE + FOCUSED_EXAMPLE
    FOCUSED = FOCUSED_EXAMPLE_GROUPS + FOCUSED_EXAMPLE
    HOOKS = ('before', 'after')

    sequence = 1

    def visit_Module(self, node):
        self.has_focused_examples = False
        self.shared_contexts = {}

        super(TransformToSpecsNodeTransformer, self).generic_visit(node)

        node.body.insert(0, ast.ImportFrom(
            module='mamba.nodetransformers',
            names=[ast.alias(name='add_attribute_decorator')],
            level=0
        ))
        node.body.append(ast.Assign(
            targets=[ast.Name(id='__mamba_has_focused_examples', ctx=ast.Store())],
            value=ast.Name(id=str(self.has_focused_examples), ctx=ast.Load())),
        )

        return node

    def visit_With(self, node):
        super(TransformToSpecsNodeTransformer, self).generic_visit(node)

        name = self._get_name(node)

        if name in self.FOCUSED:
            self.has_focused_examples = True

        if name in self.INCLUDED_EXAMPLE_GROUPS:
            return self._get_shared_example_group(node)
        if name in self.EXAMPLE_GROUPS:
            return self._transform_to_example_group(node, name)
        if name in self.EXAMPLES:
            return self._transform_to_example(node, name)
        if name in self.HOOKS:
            return self._transform_to_hook(node, name)

        return node

    def _get_name(self, node):
        context_expr = self._context_expr_for(node)

        if isinstance(context_expr, ast.Call):
            if hasattr(context_expr.func, 'value'):
                return context_expr.func.value.id
            return context_expr.func.id

        if isinstance(context_expr, ast.Attribute):
            return context_expr.value.id

    def _context_expr_for(self, node):
        return node.context_expr

    def _transform_to_example_group(self, node, name):
        context_expr = self._context_expr_for(node)
        example_name = self._human_readable_context_expr(context_expr)

        if name in self.SHARED_EXAMPLE_GROUPS:
            self.shared_contexts[example_name] = node.body

        return ast.copy_location(
            ast.ClassDef(
                name=self._prefix_with_sequence(example_name),
                bases=[],
                keywords=[],
                body=node.body,
                decorator_list=[
                    self._set_attribute('_example_group', True),
                    self._set_attribute('_example_name', example_name),
                    self._set_attribute('_tags', self._tags_from(context_expr, name)),
                    self._set_attribute('_pending', name in self.PENDING_EXAMPLE_GROUPS),
                    self._set_attribute('_shared', name in self.SHARED_EXAMPLE_GROUPS)
                ]
            ),
            node
        )

    def _human_readable_context_expr(self, context_expr):
        if isinstance(context_expr.args[0], ast.Str):
            return context_expr.args[0].s
        elif isinstance(context_expr.args[0], ast.Attribute):
            return context_expr.args[0].attr
        else:
            return context_expr.args[0].id

    def _tags_from(self, context_expr, method_name):
        tags = []
        if method_name in self.FOCUSED:
            tags.append('focus')
        if len(context_expr.args) > 1:
            tags.extend([arg.s for arg in context_expr.args[1:]])

        return tags

    def _transform_to_example(self, node, name):
        context_expr = self._context_expr_for(node)

        example_name = '{0} {1}'.format(name, context_expr.args[0].s)

        return ast.copy_location(
            ast.FunctionDef(
                name=self._prefix_with_sequence(example_name),
                args=self._generate_argument('self'),
                body=node.body,
                decorator_list=[
                    self._set_attribute('_example', True),
                    self._set_attribute('_example_name', example_name),
                    self._set_attribute('_tags', self._tags_from(context_expr, name)),
                    self._set_attribute('_pending', name in self.PENDING_EXAMPLE)
                ]
            ),
            node
        )

    def _prefix_with_sequence(self, name):
        result = '{0:08d}__{1}'.format(self.sequence, name)
        self.sequence += 1
        return result

    def _generate_argument(self, name):
        return ast.arguments(args=[ast.Name(id=name, ctx=ast.Param())], vararg=None, kwarg=None, defaults=[])

    def _transform_to_hook(self, node, name):
        when = self._context_expr_for(node).attr
        return ast.copy_location(
            ast.FunctionDef(
                name=name + '_' + when,
                args=self._generate_argument('self'),
                body=node.body,
                decorator_list=[]
            ),
            node
        )

    def _get_shared_example_group(self, node):
        context_expr = self._context_expr_for(node)
        example_name = self._human_readable_context_expr(context_expr)

        return ast.copy_location(
            ast.ClassDef(
                name=self._prefix_with_sequence(example_name),
                bases=[],
                keywords=[],
                body=self.shared_contexts[example_name] + node.body,
                decorator_list=[
                    self._set_attribute('_example_group', True),
                    self._set_attribute('_example_name', example_name),
                    self._set_attribute('_tags', self._tags_from(context_expr, 'context')),
                    self._set_attribute('_pending', False),
                    self._set_attribute('_shared', False)
                ]
            ),
            node
        )

    def _set_attribute(self, attr, value):
        return ast.Call(
            func=ast.Name(id='add_attribute_decorator', ctx=ast.Load()),
            args=[ast.Str(attr), self._convert_value(value)],
            keywords=[]
        )

    def _convert_value(self, value):
        if isinstance(value, bool):
            return ast.Str(str(value) if value else '')
        elif isinstance(value, list):
            return ast.List(elts=list(map(self._convert_value, value)), ctx=ast.Load())
        else:
            return ast.Str(str(value))

class TransformToSpecsPython3NodeTransformer(TransformToSpecsNodeTransformer):

    def _context_expr_for(self, node):
        return node.items[0].context_expr

    def _generate_argument(self, name):
        return ast.arguments(
            args=[ast.arg(arg=name, annotation=None)],
            vararg=None,
            kwonlyargs=[],
            kw_defaults=[],
            kwarg=None,
            defaults=[]
        )
<|endoftext|>


<commit_before>#!/usr/bin/python
# coding: utf-8

# Copyright (c) 2013 Mountainstorm
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from amdevice import *
from afc import *

class AFCRoot(AFC):
	def __init__(self, amdevice):
		s = amdevice.start_service(u'com.apple.afc2')
		if s is None:
			raise RuntimeError(u'Unable to launch:', u'com.apple.afc2')
		AFC.__init__(self, s)

if __name__ == u'__main__':
	import sys

	def printdir(afc, path):
		for name in afc.listdir(path):
			isdir = u''
			if afc.lstat(path + name).st_ifmt == stat.S_IFDIR:
				isdir = u'/'
			print path + name + isdir
			if afc.lstat(path + name).st_ifmt == stat.S_IFDIR:
				printdir(afc, path + name + isdir)

	def factory(dev):
		d = AMDevice(dev)
		d.connect()
		afc = AFCRoot(d)

		printdir(afc, u'/') # recursive print of all files visible

		afc.disconnect()
		return d

	handle_devices(factory)

<commit_msg>Fixed string type for amdevice.start_service<commit_after>#!/usr/bin/python
# coding: utf-8

# Copyright (c) 2013 Mountainstorm
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from amdevice import *
from afc import *

class AFCRoot(AFC):
	def __init__(self, amdevice):
		s = amdevice.start_service(CFSTR(u'com.apple.afc2'))
		if s is None:
			raise RuntimeError(u'Unable to launch:', u'com.apple.afc2')
		AFC.__init__(self, s)

if __name__ == u'__main__':
	import sys

	def printdir(afc, path):
		for name in afc.listdir(path):
			isdir = u''
			if afc.lstat(path + name).st_ifmt == stat.S_IFDIR:
				isdir = u'/'
			print path + name + isdir
			if afc.lstat(path + name).st_ifmt == stat.S_IFDIR:
				printdir(afc, path + name + isdir)

	def factory(dev):
		d = AMDevice(dev)
		d.connect()
		afc = AFCRoot(d)

		printdir(afc, u'/System/Library/Caches/') # recursive print of all files visible

		afc.disconnect()
		return d

	handle_devices(factory)

<|endoftext|>


<commit_before># Copyright (c) 2010-2013, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

"""
Functions for exporting risk artifacts from the database.
"""

import os

from openquake.db import models
from openquake.export import core
from nrml.risk import writers

LOSS_CURVE_FILENAME_FMT = 'loss-curves-%(loss_curve_id)s.xml'
LOSS_MAP_FILENAME_FMT = 'loss-maps-%(loss_map_id)s-poe-%(poe)s.xml'
BCR_FILENAME_FMT = 'bcr-distribution-%(bcr_distribution_id)s.xml'

def export(output_id, target_dir):
    """
    Export the given risk calculation output from the database to the
    specified directory. See `openquake.export.hazard.export` for more
    details.
    """
    output = models.Output.objects.get(id=output_id)
    export_fn = _export_fn_map().get(
        output.output_type, core._export_fn_not_implemented)

    return export_fn(output, os.path.expanduser(target_dir))

def _export_fn_map():
    """
    Creates a mapping from output type to risk export function
    """
    fn_map = {
        'agg_loss_curve': export_agg_loss_curve,
        'loss_curve': export_loss_curve,
        'ins_loss_curve': export_loss_curve,
        'loss_map': export_loss_map,
        'bcr_distribution': export_bcr_distribution
    }
    return fn_map

def _export_common(output):
    """
    Returns a dict containing the common arguments used by nrml
    serializers to serialize the risk calculation `output`.
    """
    risk_calculation = output.oq_job.risk_calculation
    investigation_time = risk_calculation.hazard_calculation.investigation_time
    statistics, quantile_value = risk_calculation.hazard_statistics

    source_model_tree_path, gsim_tree_path = None, None
    if not statistics:
        lt_paths = risk_calculation.hazard_logic_tree_paths

        if lt_paths:
            source_model_tree_path, gsim_tree_path = [
                core.LT_PATH_JOIN_TOKEN.join(x) for x in lt_paths]

    unit = risk_calculation.exposure_model.stco_unit

    return dict(investigation_time=investigation_time,
                statistics=statistics,
                quantile_value=quantile_value,
                source_model_tree_path=source_model_tree_path,
                gsim_tree_path=gsim_tree_path,
                unit=unit)

@core.makedirs
def export_agg_loss_curve(output, target_dir):
    """
    Export `output` to `target_dir` by using a nrml loss curves
    serializer
    """
    args = _export_common(output)
    args['path'] = os.path.join(target_dir, LOSS_CURVE_FILENAME_FMT % {
        'loss_curve_id': output.losscurve.id})
    writers.AggregateLossCurveXMLWriter(**args).serialize(
        output.losscurve.aggregatelosscurvedata)
    return [args['path']]

@core.makedirs
def export_loss_curve(output, target_dir):
    """
    Export `output` to `target_dir` by using a nrml loss curves
    serializer
    """
    args = _export_common(output)
    args['path'] = os.path.join(target_dir, LOSS_CURVE_FILENAME_FMT % {
        'loss_curve_id': output.losscurve.id})
    if output.losscurve.insured:
        args['insured'] = True

    writers.LossCurveXMLWriter(**args).serialize(
        output.losscurve.losscurvedata_set.all().order_by('asset_ref'))
    return [args['path']]

@core.makedirs
def export_loss_map(output, target_dir):
    """
    Export `output` to `target_dir` by using a nrml loss map
    serializer
    """
    risk_calculation = output.oq_job.risk_calculation
    args = _export_common(output)
    args.update(
        dict(path=os.path.join(target_dir, LOSS_MAP_FILENAME_FMT % {
            'loss_map_id': output.lossmap.id,
            'poe': output.lossmap.poe}),
            poe=output.lossmap.poe,
            loss_category=risk_calculation.exposure_model.category))
    writers.LossMapXMLWriter(**args).serialize(
        output.lossmap.lossmapdata_set.all().order_by('asset_ref'))
    return [args['path']]

@core.makedirs
def export_bcr_distribution(output, target_dir):
    """
    Export `output` to `target_dir` by using a nrml bcr distribution
    serializer
    """
    risk_calculation = output.oq_job.risk_calculation
    args = _export_common(output)

    args.update(
        dict(path=os.path.join(target_dir, BCR_FILENAME_FMT % {
            'bcr_distribution_id': output.bcrdistribution.id}),
            interest_rate=risk_calculation.interest_rate,
            asset_life_expectancy=risk_calculation.asset_life_expectancy))
    del args['investigation_time']

    writers.BCRMapXMLWriter(**args).serialize(
        output.bcrdistribution.bcrdistributiondata_set.all().order_by(
            'asset_ref'))
    return [args['path']]
<commit_msg>Preliminary work for the scenario_damage export<commit_after># Copyright (c) 2010-2013, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

"""
Functions for exporting risk artifacts from the database.
"""

import os

from openquake.db import models
from openquake.export import core
from nrml.risk import writers

LOSS_CURVE_FILENAME_FMT = 'loss-curves-%(loss_curve_id)s.xml'
LOSS_MAP_FILENAME_FMT = 'loss-maps-%(loss_map_id)s-poe-%(poe)s.xml'
BCR_FILENAME_FMT = 'bcr-distribution-%(bcr_distribution_id)s.xml'

def export(output_id, target_dir):
    """
    Export the given risk calculation output from the database to the
    specified directory. See `openquake.export.hazard.export` for more
    details.
    """
    output = models.Output.objects.get(id=output_id)
    export_fn = _export_fn_map().get(
        output.output_type, core._export_fn_not_implemented)

    return export_fn(output, os.path.expanduser(target_dir))

def _export_fn_map():
    """
    Creates a mapping from output type to risk export function
    """
    fn_map = {
        'agg_loss_curve': export_agg_loss_curve,
        'loss_curve': export_loss_curve,
        'ins_loss_curve': export_loss_curve,
        'loss_map': export_loss_map,
        'bcr_distribution': export_bcr_distribution
    }
    return fn_map

def _export_common(output):
    """
    Returns a dict containing the common arguments used by nrml
    serializers to serialize the risk calculation `output`.
    """
    risk_calculation = output.oq_job.risk_calculation
    investigation_time = risk_calculation.hazard_calculation.investigation_time
    statistics, quantile_value = risk_calculation.hazard_statistics

    source_model_tree_path, gsim_tree_path = None, None
    if not statistics:
        lt_paths = risk_calculation.hazard_logic_tree_paths

        if lt_paths:
            source_model_tree_path, gsim_tree_path = [
                core.LT_PATH_JOIN_TOKEN.join(x) for x in lt_paths]

    unit = risk_calculation.exposure_model.stco_unit

    return dict(investigation_time=investigation_time,
                statistics=statistics,
                quantile_value=quantile_value,
                source_model_tree_path=source_model_tree_path,
                gsim_tree_path=gsim_tree_path,
                unit=unit)

@core.makedirs
def export_agg_loss_curve(output, target_dir):
    """
    Export `output` to `target_dir` by using a nrml loss curves
    serializer
    """
    args = _export_common(output)
    args['path'] = os.path.join(target_dir, LOSS_CURVE_FILENAME_FMT % {
        'loss_curve_id': output.losscurve.id})
    writers.AggregateLossCurveXMLWriter(**args).serialize(
        output.losscurve.aggregatelosscurvedata)
    return [args['path']]

@core.makedirs
def export_loss_curve(output, target_dir):
    """
    Export `output` to `target_dir` by using a nrml loss curves
    serializer
    """
    args = _export_common(output)
    args['path'] = os.path.join(target_dir, LOSS_CURVE_FILENAME_FMT % {
        'loss_curve_id': output.losscurve.id})
    if output.losscurve.insured:
        args['insured'] = True

    writers.LossCurveXMLWriter(**args).serialize(
        output.losscurve.losscurvedata_set.all().order_by('asset_ref'))
    return [args['path']]

@core.makedirs
def export_loss_map(output, target_dir):
    """
    Export `output` to `target_dir` by using a nrml loss map
    serializer
    """
    risk_calculation = output.oq_job.risk_calculation
    args = _export_common(output)
    args.update(
        dict(path=os.path.join(target_dir, LOSS_MAP_FILENAME_FMT % {
            'loss_map_id': output.lossmap.id,
            'poe': output.lossmap.poe}),
            poe=output.lossmap.poe,
            loss_category=risk_calculation.exposure_model.category))
    writers.LossMapXMLWriter(**args).serialize(
        output.lossmap.lossmapdata_set.all().order_by('asset_ref'))
    return [args['path']]

@core.makedirs
def export_bcr_distribution(output, target_dir):
    """
    Export `output` to `target_dir` by using a nrml bcr distribution
    serializer
    """
    risk_calculation = output.oq_job.risk_calculation
    args = _export_common(output)

    args.update(
        dict(path=os.path.join(target_dir, BCR_FILENAME_FMT % {
            'bcr_distribution_id': output.bcrdistribution.id}),
            interest_rate=risk_calculation.interest_rate,
            asset_life_expectancy=risk_calculation.asset_life_expectancy))
    del args['investigation_time']

    writers.BCRMapXMLWriter(**args).serialize(
        output.bcrdistribution.bcrdistributiondata_set.all().order_by(
            'asset_ref'))
    return [args['path']]

def make_dmg_dist_export(damagecls, writercls, filename):
    @core.makedirs
    def export_dmg_dist(output, target_dir):
        """
        Export the damage distribution identified
        by the given output to the `target_dir`.

        :param output: db output record which identifies the distribution.
        :type output: :py:class:`openquake.db.models.Output`
        :param target_dir: destination directory of the exported file.
        :type target_dir: string
        """
        file_path = os.path.join(target_dir, filename % output.oq_job.id)
        dmg_states = models.DmgState.objects.get(output=output)
        writer = writercls(file_path, [ds.damage_state for ds in dmg_states])
        data = [damagecls.objects.filter(dmg_state=ds) for ds in dmg_states]
        writer.serialize(data)
        return [file_path]

    return export_dmg_dist

export_dmg_dist_per_asset = make_dmg_dist_export(
    models.DmgDistPerAsset, writers.DmgDistPerAssetXMLWriter,
    "dmg-dist-asset-%s.xml")

export_dmg_dist_per_taxonomy = make_dmg_dist_export(
    models.DmgDistPerTaxonomy, writers.DmgDistPerTaxonomyXMLWriter,
    "dmg-dist-taxonomy-%s.xml")

export_dmg_dist_total = make_dmg_dist_export(
    models.DmgDistTotal, writers.DmgDistTotalXMLWriter,
    "dmg-dist-total-%s.xml")

# this is a special case of DmgDistPerAsset for the outmost damage state
@core.makedirs
def export_collapse_map(output, target_dir):
    """
    Export the collapse map identified
    by the given output to the `target_dir`.

    :param output: db output record which identifies the distribution.
    :type output: :py:class:`openquake.db.models.Output`
    :param target_dir: destination directory of the exported file.
    :type target_dir: string
    """
    file_name = "collapse-map-%s.xml" % output.oq_job.id
    file_path = os.path.join(target_dir, file_name)
    dmg_states = models.DmgState.objects.get(output=output).order_by('lsi')
    collapse = dmg_states[-1]  # the bigger one
    writer = writers.CollapseMapXMLWriter(file_path)
    data = models.DmgDistPerAsset.objects.filter(dmg_state=collapse)
    writer.serialize(data)
    return [file_path]
<|endoftext|>


<commit_before># This file defines workaround implementation for
# NumPy-compatibility functions that fall back to NumPy/CuPy functions
# for native/cuda devices respecitvely.
# The workaround does not support backprop, and also requires external
# libraries mentioned above.
# Functions defined in this file should be considered to have high priority for
# genuine implementations.
import numpy

import chainerx

try:
    import cupy
except Exception:
    cupy = None

class _DummyContext:
    def __enter__(self):
        pass

    def __exit__(self, type, value, traceback):
        pass

_dummy_context = _DummyContext()

def _to_numpy(array):
    assert isinstance(array, chainerx.ndarray)
    return chainerx.to_numpy(array, copy=False)

def _from_numpy(array):
    assert isinstance(array, numpy.ndarray)
    return chainerx.array(array, copy=False)

def _to_cupy(array):
    assert cupy is not None
    # Convert to cupy.ndarray on the same device as source array
    return chainerx._to_cupy(array)

def _from_cupy(array):
    assert cupy is not None
    assert isinstance(array, cupy.ndarray)
    device = chainerx.get_device('cuda', array.device.id)
    return chainerx._core._fromrawpointer(
        array.data.mem.ptr,
        array.shape,
        array.dtype,
        array.strides,
        device,
        array.data.ptr - array.data.mem.ptr,
        array)

def _from_chx(array, check_backprop=True):
    # Converts chainerx.ndarray to numpy/cupy.ndarray.
    # Objects with other types are kept intact.
    # Returns a pair: (xp, cupy device or dummy context, numpy/cupy.ndarray).
    if not isinstance(array, chainerx.ndarray):
        if (isinstance(array, numpy.ndarray)
                or (cupy and isinstance(array, cupy.ndarray))):
            raise TypeError(
                'ChainerX function fallback using NumPy/CuPy arrays '
                'is not supported.')
        # _from_chx is also called for slice and tuple objects
        # Used to index a chx array
        return None, _dummy_context, array
    if check_backprop and array.is_backprop_required():
        raise RuntimeError(
            'ChainerX function fallback using NumPy/CuPy is not '
            'supported for arrays that are connected to a graph.')
    backend_name = array.device.backend.name
    if backend_name == 'native':
        return numpy, _dummy_context, _to_numpy(array)
    if backend_name == 'cuda':
        if cupy is None:
            raise RuntimeError(
                'ChainerX fallback implementation for cuda backend requires '
                'cupy to be installed.')
        array_cupy = _to_cupy(array)
        return cupy, array_cupy.device, array_cupy
    raise RuntimeError(
        'ChainerX fallback implementation only supports native or cuda '
        'backends.')

def _to_chx(array):
    # Converts numpy/cupy.ndarray to chainerx.ndarray.
    # Objects with other types are kept intact.
    if isinstance(array, numpy.ndarray):
        return _from_numpy(array)
    elif cupy is not None and isinstance(array, cupy.ndarray):
        return _from_cupy(array)
    return array

def _populate_module_functions():

    def _fix(arr):
        xp, dev, arr = _from_chx(arr)
        with dev:
            ret = xp.fix(arr)
            ret = xp.asarray(ret)
        return _to_chx(ret)

    chainerx.fix = _fix

def _populate_ndarray():
    ndarray = chainerx.ndarray

    # __getitem__ with advanced indexing
    old_getitem = ndarray.__getitem__

    def __getitem__(arr, key):
        if not isinstance(key, chainerx.ndarray):
            return old_getitem(arr, key)

        is_backprop_required = arr.is_backprop_required()

        xp, dev, arr = _from_chx(arr, check_backprop=False)
        # The elements used for indexing the array might be
        # also ChainerX arrays. _from_chx ignores
        # other types and return them as-is
        if isinstance(key, tuple):
            key = tuple([_from_chx(k, check_backprop=False)[2] for k in key])
        else:
            _, _, key = _from_chx(key, check_backprop=False)

        with dev:
            ret = arr[key]

        # Doing this check after the fallback __getitem__ because the error
        # which caused the fallback might not be due to advanced indexing.
        # In such case the fallback __getitem__ should also raise the error.

        if is_backprop_required:
            raise RuntimeError(
                'ChainerX getitem fallback for advanced indexing is not '
                'supported for arrays that are connected to a graph.')

        return _to_chx(ret)

    # __setitem__ with advanced indexing
    def __setitem__(self, key, value):
        if self.is_backprop_required():
            raise RuntimeError(
                'ChainerX setitem fallback for advanced indexing is not '
                'supported for arrays that are connected to a graph.')

        xp, dev, self = _from_chx(self)
        if isinstance(key, tuple):
            key = tuple([_from_chx(k)[2] for k in key])
        else:
            _, _, key = _from_chx(key)
        _, _, value = _from_chx(value)

        with dev:
            self[key] = value

    ndarray.__setitem__ = __setitem__
    ndarray.__getitem__ = __getitem__

    def tolist(arr):
        _, dev, arr = _from_chx(arr)
        with dev:
            ret = arr.tolist()
        return ret

    ndarray.tolist = tolist

def populate():
    _populate_module_functions()
    _populate_ndarray()
<commit_msg>adding code from #7960<commit_after># This file defines workaround implementation for
# NumPy-compatibility functions that fall back to NumPy/CuPy functions
# for native/cuda devices respecitvely.
# The workaround does not support backprop, and also requires external
# libraries mentioned above.
# Functions defined in this file should be considered to have high priority for
# genuine implementations.
import numpy

import chainerx

try:
    import cupy
except Exception:
    cupy = None

class _DummyContext:
    def __enter__(self):
        pass

    def __exit__(self, type, value, traceback):
        pass

_dummy_context = _DummyContext()

def _to_numpy(array):
    assert isinstance(array, chainerx.ndarray)
    return chainerx.to_numpy(array, copy=False)

def _from_numpy(array):
    assert isinstance(array, numpy.ndarray)
    return chainerx.array(array, copy=False)

def _to_cupy(array):
    assert cupy is not None
    # Convert to cupy.ndarray on the same device as source array
    return chainerx._to_cupy(array)

def _from_cupy(array):
    assert cupy is not None
    assert isinstance(array, cupy.ndarray)
    device = chainerx.get_device('cuda', array.device.id)
    return chainerx._core._fromrawpointer(
        array.data.mem.ptr,
        array.shape,
        array.dtype,
        array.strides,
        device,
        array.data.ptr - array.data.mem.ptr,
        array)

def _from_chx(array, check_backprop=True):
    # Converts chainerx.ndarray to numpy/cupy.ndarray.
    # Objects with other types are kept intact.
    # Returns a pair: (xp, cupy device or dummy context, numpy/cupy.ndarray).
    if not isinstance(array, chainerx.ndarray):
        if (isinstance(array, numpy.ndarray)
                or (cupy and isinstance(array, cupy.ndarray))):
            raise TypeError(
                'ChainerX function fallback using NumPy/CuPy arrays '
                'is not supported.')
        # _from_chx is also called for slice and tuple objects
        # Used to index a chx array
        return None, _dummy_context, array
    if check_backprop and array.is_backprop_required():
        raise RuntimeError(
            'ChainerX function fallback using NumPy/CuPy is not '
            'supported for arrays that are connected to a graph.')
    backend_name = array.device.backend.name
    if backend_name == 'native':
        return numpy, _dummy_context, _to_numpy(array)
    if backend_name == 'cuda':
        if cupy is None:
            raise RuntimeError(
                'ChainerX fallback implementation for cuda backend requires '
                'cupy to be installed.')
        array_cupy = _to_cupy(array)
        return cupy, array_cupy.device, array_cupy
    raise RuntimeError(
        'ChainerX fallback implementation only supports native or cuda '
        'backends.')

def _to_chx(array):
    # Converts numpy/cupy.ndarray to chainerx.ndarray.
    # Objects with other types are kept intact.
    if isinstance(array, numpy.ndarray):
        return _from_numpy(array)
    elif cupy is not None and isinstance(array, cupy.ndarray):
        return _from_cupy(array)
    return array

def _populate_module_functions():

    def _fix(arr):
        xp, dev, arr = _from_chx(arr)
        with dev:
            ret = xp.fix(arr)
            ret = xp.asarray(ret)
        return _to_chx(ret)

    def _broadcast_arrays(*args):
        xps, devs, arrs = zip(*(_from_chx(arr) for arr in args))
        backend = xps[0]
        if not all([xp is backend for xp in xps]):
            raise TypeError(
                'ChainerX function fallback using mixed NumPy/CuPy '
                'arrays is not supported.')
        bcasted = backend.broadcast_arrays(*arrs)
        return [_to_chx(ret) for ret in bcasted]

    chainerx.fix = _fix
    chainerx.broadcast_arrays = _broadcast_arrays

def _populate_ndarray():
    ndarray = chainerx.ndarray

    # __getitem__ with advanced indexing
    old_getitem = ndarray.__getitem__

    def __getitem__(arr, key):
        if not isinstance(key, chainerx.ndarray):
            return old_getitem(arr, key)

        is_backprop_required = arr.is_backprop_required()

        xp, dev, arr = _from_chx(arr, check_backprop=False)
        # The elements used for indexing the array might be
        # also ChainerX arrays. _from_chx ignores
        # other types and return them as-is
        if isinstance(key, tuple):
            key = tuple([_from_chx(k, check_backprop=False)[2] for k in key])
        else:
            _, _, key = _from_chx(key, check_backprop=False)

        with dev:
            ret = arr[key]

        # Doing this check after the fallback __getitem__ because the error
        # which caused the fallback might not be due to advanced indexing.
        # In such case the fallback __getitem__ should also raise the error.

        if is_backprop_required:
            raise RuntimeError(
                'ChainerX getitem fallback for advanced indexing is not '
                'supported for arrays that are connected to a graph.')

        return _to_chx(ret)

    # __setitem__ with advanced indexing
    def __setitem__(self, key, value):
        if self.is_backprop_required():
            raise RuntimeError(
                'ChainerX setitem fallback for advanced indexing is not '
                'supported for arrays that are connected to a graph.')

        xp, dev, self = _from_chx(self)
        if isinstance(key, tuple):
            key = tuple([_from_chx(k)[2] for k in key])
        else:
            _, _, key = _from_chx(key)
        _, _, value = _from_chx(value)

        with dev:
            self[key] = value

    ndarray.__setitem__ = __setitem__
    ndarray.__getitem__ = __getitem__

    def tolist(arr):
        _, dev, arr = _from_chx(arr)
        with dev:
            ret = arr.tolist()
        return ret

    ndarray.tolist = tolist

def populate():
    _populate_module_functions()
    _populate_ndarray()
<|endoftext|>


<commit_before>from django.contrib import admin
from polymorphic.admin import (
    PolymorphicChildModelAdmin,
    PolymorphicChildModelFilter,
    PolymorphicParentModelAdmin,
)

from projects.models.files import File
from projects.models.nodes import Node
from projects.models.projects import Project, ProjectAgent, ProjectEvent
from projects.models.snapshots import Snapshot
from projects.models.sources import (
    ElifeSource,
    GithubSource,
    GoogleDocsSource,
    GoogleDriveSource,
    PlosSource,
    Source,
    UploadSource,
    UrlSource,
)

@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    """Admin interface for projects."""

    list_display = [
        "name",
        "account",
        "creator",
        "created",
        "temporary",
        "public",
    ]
    list_select_related = ["account", "creator"]

@admin.register(ProjectAgent)
class ProjectAgentAdmin(admin.ModelAdmin):
    """Admin interface for projects agents."""

    list_display = ["id", "project", "user_id", "team_id", "role"]
    list_select_related = ["project"]

@admin.register(ProjectEvent)
class ProjectEventAdmin(admin.ModelAdmin):
    """
    Admin interface for projects events.

    Because there are many events the list display is kept
    simple and uses related ids rather than selecting replated objects.
    """

    list_display = ["id", "time", "project_id", "user_id", "source_id"]

@admin.register(Source)
class SourceParentAdmin(PolymorphicParentModelAdmin):
    """Base admin class for source classes."""

    base_model = Source
    child_models = (
        ElifeSource,
        GithubSource,
        GoogleDocsSource,
        UploadSource,
        UrlSource,
    )

    list_display = [
        "id",
        "project_id",
        "path",
        "creator_id",
        "created",
        "updated",
        "polymorphic_ctype",
    ]
    list_select_related = ["polymorphic_ctype"]
    list_filter = [PolymorphicChildModelFilter]
    show_full_result_count = False

class SourceChildAdmin(PolymorphicChildModelAdmin):
    """Base admin class for all child models of Source."""

# If an admin class is not declared for each source type a 403
# error is thrown when trying to edit that source in the admin interface.

@admin.register(ElifeSource)
class ElifeSourceAdmin(SourceChildAdmin):
    """Admin interface for eLife sources."""

    base_model = ElifeSource
    show_in_index = True

@admin.register(GithubSource)
class GithubSourceAdmin(SourceChildAdmin):
    """Admin interface for GitHub sources."""

    base_model = GithubSource
    show_in_index = True

@admin.register(GoogleDocsSource)
class GoogleDocsSourceAdmin(SourceChildAdmin):
    """Admin interface for GoogleDocs sources."""

    base_model = GoogleDocsSource
    show_in_index = True

@admin.register(GoogleDriveSource)
class GoogleDriveSourceAdmin(SourceChildAdmin):
    """Admin interface for GoogleDrive sources."""

    base_model = GoogleDriveSource
    show_in_index = True

@admin.register(PlosSource)
class PlosSourceAdmin(SourceChildAdmin):
    """Admin interface for PLOS sources."""

    base_model = PlosSource
    show_in_index = True

@admin.register(UploadSource)
class UploadSourceAdmin(SourceChildAdmin):
    """Admin interface for upload sources."""

    base_model = UploadSource
    show_in_index = True

@admin.register(UrlSource)
class UrlSourceAdmin(SourceChildAdmin):
    """Admin interface for URL sources."""

    base_model = UrlSource
    show_in_index = True

@admin.register(File)
class FileAdmin(admin.ModelAdmin):
    """Admin interface for project files."""

    list_display = [
        "id",
        "project",
        "path",
        "current",
        "created",
        "updated",
        "mimetype",
        "size",
    ]
    list_select_related = ["project"]
    list_filter = ["project"]

@admin.register(Snapshot)
class SnapshotAdmin(admin.ModelAdmin):
    """Admin interface for project snapshots."""

    list_display = ["id", "project", "number", "creator", "created"]
    list_select_related = ["project", "creator", "job"]

@admin.register(Node)
class NodeAdmin(admin.ModelAdmin):
    """Admin interface for project nodes."""

    list_display = ["id", "key", "creator", "created", "project", "app", "host"]
    list_select_related = ["creator", "project"]
    list_filter = ["app"]
    show_full_result_count = False

    def get_queryset(self, request):
        """Get custom queryset that does not fetch the `json` field (which can be large)."""
        queryset = super().get_queryset(request)
        return queryset.defer("json")
<commit_msg>fix(Admin): Register child models<commit_after>from django.contrib import admin
from polymorphic.admin import (
    PolymorphicChildModelAdmin,
    PolymorphicChildModelFilter,
    PolymorphicParentModelAdmin,
)

from projects.models.files import File
from projects.models.nodes import Node
from projects.models.projects import Project, ProjectAgent, ProjectEvent
from projects.models.snapshots import Snapshot
from projects.models.sources import (
    ElifeSource,
    GithubSource,
    GoogleDocsSource,
    GoogleDriveSource,
    PlosSource,
    Source,
    UploadSource,
    UrlSource,
)

@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    """Admin interface for projects."""

    list_display = [
        "name",
        "account",
        "creator",
        "created",
        "temporary",
        "public",
    ]
    list_select_related = ["account", "creator"]

@admin.register(ProjectAgent)
class ProjectAgentAdmin(admin.ModelAdmin):
    """Admin interface for projects agents."""

    list_display = ["id", "project", "user_id", "team_id", "role"]
    list_select_related = ["project"]

@admin.register(ProjectEvent)
class ProjectEventAdmin(admin.ModelAdmin):
    """
    Admin interface for projects events.

    Because there are many events the list display is kept
    simple and uses related ids rather than selecting replated objects.
    """

    list_display = ["id", "time", "project_id", "user_id", "source_id"]

@admin.register(Source)
class SourceParentAdmin(PolymorphicParentModelAdmin):
    """Base admin class for source classes."""

    base_model = Source

    # If an admin class is not declared for each source type a 403
    # error is thrown when trying to edit that source in the admin interface.
    child_models = (
        ElifeSource,
        GithubSource,
        GoogleDocsSource,
        GoogleDriveSource,
        PlosSource,
        UploadSource,
        UrlSource,
    )

    list_display = [
        "id",
        "project_id",
        "path",
        "creator_id",
        "created",
        "updated",
        "polymorphic_ctype",
    ]
    list_select_related = ["polymorphic_ctype"]
    list_filter = [PolymorphicChildModelFilter]
    show_full_result_count = False

class SourceChildAdmin(PolymorphicChildModelAdmin):
    """Base admin class for all child models of Source."""

@admin.register(ElifeSource)
class ElifeSourceAdmin(SourceChildAdmin):
    """Admin interface for eLife sources."""

    base_model = ElifeSource
    show_in_index = True

@admin.register(GithubSource)
class GithubSourceAdmin(SourceChildAdmin):
    """Admin interface for GitHub sources."""

    base_model = GithubSource
    show_in_index = True

@admin.register(GoogleDocsSource)
class GoogleDocsSourceAdmin(SourceChildAdmin):
    """Admin interface for GoogleDocs sources."""

    base_model = GoogleDocsSource
    show_in_index = True

@admin.register(GoogleDriveSource)
class GoogleDriveSourceAdmin(SourceChildAdmin):
    """Admin interface for GoogleDrive sources."""

    base_model = GoogleDriveSource
    show_in_index = True

@admin.register(PlosSource)
class PlosSourceAdmin(SourceChildAdmin):
    """Admin interface for PLOS sources."""

    base_model = PlosSource
    show_in_index = True

@admin.register(UploadSource)
class UploadSourceAdmin(SourceChildAdmin):
    """Admin interface for upload sources."""

    base_model = UploadSource
    show_in_index = True

@admin.register(UrlSource)
class UrlSourceAdmin(SourceChildAdmin):
    """Admin interface for URL sources."""

    base_model = UrlSource
    show_in_index = True

@admin.register(File)
class FileAdmin(admin.ModelAdmin):
    """Admin interface for project files."""

    list_display = [
        "id",
        "project",
        "path",
        "current",
        "created",
        "updated",
        "mimetype",
        "size",
    ]
    list_select_related = ["project"]
    list_filter = ["project"]

@admin.register(Snapshot)
class SnapshotAdmin(admin.ModelAdmin):
    """Admin interface for project snapshots."""

    list_display = ["id", "project", "number", "creator", "created"]
    list_select_related = ["project", "creator", "job"]

@admin.register(Node)
class NodeAdmin(admin.ModelAdmin):
    """Admin interface for project nodes."""

    list_display = ["id", "key", "creator", "created", "project", "app", "host"]
    list_select_related = ["creator", "project"]
    list_filter = ["app"]
    show_full_result_count = False

    def get_queryset(self, request):
        """Get custom queryset that does not fetch the `json` field (which can be large)."""
        queryset = super().get_queryset(request)
        return queryset.defer("json")
<|endoftext|>


<commit_before># WARNING: this is a namespace package. Do not add anything to this file!
__import__('pkg_resources').declare_namespace(__name__)
from ._version import __version__
<commit_msg>Silence the version import error<commit_after># WARNING: this is a namespace package. Do not add anything to this file!
__import__('pkg_resources').declare_namespace(__name__)
from ._version import __version__  # NOQA
<|endoftext|>


<commit_before>import sys
import trueskill

sys.argv.pop(0)

teams = [[trueskill.Rating(mu=float(sys.argv[a]), sigma=float(sys.argv[a+1]))] for a in range(0, len(sys.argv), 2)]

newRatings = trueskill.rate(teams)
for rating in newRatings:
    print(str(rating[0].mu) + " " + str(rating[0].sigma))
<commit_msg>Delete updateTruskill.py<commit_after><|endoftext|>


<commit_before>"""a WSGI application filter for CherryPy"""

# by Christian Wyglendowski

import sys

import cherrypy
from cherrypy.filters.basefilter import BaseFilter
from cherrypy.lib import cptools
from cherrypy._cputil import get_object_trail

class NoWSGIEnvironmentError(Exception):
    pass

# is this sufficient for start_response?
def start_response(status, response_headers, exc_info=None):
    cherrypy.response.status = status
    headers_dict = dict(response_headers)
    cherrypy.response.headers.update(headers_dict)

def get_path_components(path):
    """returns (script_name, path_info)

    determines what part of the path belongs to cp (script_name)
    and what part belongs to the wsgi application (path_info)
    """
    no_parts = ['']
    object_trail = get_object_trail(path)
    root = object_trail.pop(0)
    if not path.endswith('/index'):
        object_trail.pop()
    script_name_parts = [""]
    path_info_parts = [""]
    for (pc,obj) in object_trail:
        if obj:
            script_name_parts.append(pc)
        else:
            path_info_parts.append(pc)
    script_name = "/".join(script_name_parts)
    path_info = "/".join(path_info_parts)
    if len(script_name) > 1 and path.endswith('/'):
        path_info = path_info + '/'

    if script_name and not script_name.startswith('/'):
        script_name = '/' + script_name
    if path_info and not path_info.startswith('/'):
        path_info = '/' + path_info

    return script_name, path_info

def make_environ():
    """grabbed some of below from _cpwsgiserver.py

    for compatibility with the 2.2beta release of CP
    """

    script_name, path_info = get_path_components(cherrypy.request.path)

    # create and populate the wsgi environment
    environ = dict()
    environ["wsgi.version"] = (1,0)
    environ["wsgi.url_scheme"] = cherrypy.request.scheme
    environ["wsgi.input"] = cherrypy.request.rfile
    environ["wsgi.errors"] = sys.stderr
    environ["wsgi.multithread"] = True
    environ["wsgi.multiprocess"] = False
    environ["wsgi.run_once"] = False
    environ["REQUEST_METHOD"] = cherrypy.request.method
    environ["SCRIPT_NAME"] = script_name
    environ["PATH_INFO"] = path_info
    environ["QUERY_STRING"] = cherrypy.request.queryString
    environ["SERVER_PROTOCOL"] = cherrypy.request.version
    server_name = getattr(cherrypy.server.httpserver, 'server_name', "None")
    environ["SERVER_NAME"] = server_name
    environ["SERVER_PORT"] = cherrypy.config.get('server.socketPort')
    environ["REMOTE_HOST"] = cherrypy.request.remoteHost
    environ["REMOTE_ADDR"] = cherrypy.request.remoteAddr
    environ["REMOTE_PORT"] = cherrypy.request.remotePort
    # then all the http headers
    headers = cherrypy.request.headers
    environ["CONTENT_TYPE"] = headers.get("Content-type", "")
    environ["CONTENT_LENGTH"] = headers.get("Content-length", "")
    for (k, v) in headers.iteritems():
        envname = "HTTP_" + k.upper().replace("-","_")
        environ[envname] = v
    return environ

class WSGIAppFilter(BaseFilter):
    """A filter for running any WSGI middleware/application within CP.

    It can be used through the config system or by adding it to
    a classes _cp_filters special attribute.  Here are the parameters:

    wsgi_app (wsgiapp_filter.app) - any wsgi application callable
    env_update (wsgiapp_filter.env_update) - a dictionary with
    arbitrary keys and values to be merged with the WSGI
    environment dictionary.

    Examples:

    # using _cp_filters
    class Whatever:
        _cp_filters = [WSGIAppFilter(some_app)]

    # using a config file
    [/hosted/app]
    wsgiapp_filter.on = True
    wsgiapp_filter.app = 'package.module.wsgi_app_callable'
    wsgiapp_filter.env_update = {'running_in_cp':True}
    """

    def __init__(self, wsgi_app=None, env_update=None):
        self.app = wsgi_app
        self.env_update = env_update or {}

    def before_request_body(self):
        request = cherrypy.request
        enabled = cherrypy.config.get('wsgiapp_filter.on', False)
        request.wsgi_app = self.app
        request.wsgi_env_update = self.env_update
        request.wsgiapp_filter_on = enabled or bool(self.app)

        if not request.wsgiapp_filter_on:
            return

        if not request.wsgi_app:
            app = cherrypy.config.get('wsgiapp_filter.app', None)
            if app and isinstance(app, basestring):
                app = cptools.attributes(app)
            request.wsgi_app = app

        if not request.wsgi_env_update:
            env_update = cherrypy.config.get('wsgiapp_filter.env_update', {})
            request.wsgi_env_update = env_update

        # keep the request body intact so the wsgi app
        # can have its way with it
        cherrypy.request.processRequestBody = False

    def before_main(self):
        """run the wsgi_app and set response.body to its output
        """

        request = cherrypy.request

        if not request.wsgiapp_filter_on:
            return

        # if the static filter is on for this path and
        # request.execute_main is False, assume that the
        # static filter has already taken care of this request
        staticfilter_on = cherrypy.config.get('static_filter.on', False)
        if staticfilter_on and not request.execute_main:
            return

        try:
            environ = request.wsgi_environ
            sn, pi = get_path_components(request.path)
            environ['SCRIPT_NAME'] = sn
            environ['PATH_INFO'] = pi
        except AttributeError:
            environ = make_environ()

        # update the environ with the dict passed to the filter's
        # constructor
        environ.update(request.wsgi_env_update)

        # run the wsgi app and have it set response.body
        cherrypy.response.body = request.wsgi_app(environ, start_response)

        # tell CP not to handle the request further
        request.execute_main = False

class WSGIApp(object):
    """a convenience class used to easily mount a wsgi app.

    example:
    cherrypy.root = SomeRoot()
    cherrypy.root.otherapp = WSGIApp(other_wsgi_app)
    """
    def __init__(self, app, env_update=None):
        self._cpFilterList = [WSGIAppFilter(app, env_update)]

if __name__ == '__main__':

    def my_app(environ, start_response):
        status = '200 OK'
        response_headers = [('Content-type', 'text/plain')]
        start_response(status, response_headers)
        yield 'Hello, world!\n'
        yield 'This is a wsgi app running within CherryPy!\n\n'
        keys = environ.keys()
        keys.sort()
        for k in keys:
            yield '%s: %s\n' % (k,environ[k])

    class Root(object):
        @cherrypy.expose
        def index(self):
            yield "
Hi, from CherryPy!
"
            yield "A non-CP WSGI app
"
            yield "
"
            yield "SCRIPT_NAME and PATH_INFO get set "
            yield "properly"

    # mount standard CherryPy app
    cherrypy.root = Root()
    # mount the WSGI app
    cherrypy.root.app = WSGIApp(my_app, {'cherrypy.wsgi_filter':True})

    cherrypy.server.start()

<commit_msg>Fix for #484.<commit_after>"""a WSGI application filter for CherryPy"""

# by Christian Wyglendowski

import sys

import cherrypy
from cherrypy.filters.basefilter import BaseFilter
from cherrypy.lib import cptools
from cherrypy._cputil import get_object_trail

class NoWSGIEnvironmentError(Exception):
    pass

# is this sufficient for start_response?
def start_response(status, response_headers, exc_info=None):
    cherrypy.response.status = status
    headers_dict = dict(response_headers)
    cherrypy.response.headers.update(headers_dict)

def get_path_components(path):
    """returns (script_name, path_info)

    determines what part of the path belongs to cp (script_name)
    and what part belongs to the wsgi application (path_info)
    """
    no_parts = ['']
    object_trail = get_object_trail(path)
    root = object_trail.pop(0)
    if not path.endswith('/index'):
        object_trail.pop()
    script_name_parts = [""]
    path_info_parts = [""]
    for (pc,obj) in object_trail:
        if obj:
            script_name_parts.append(pc)
        else:
            path_info_parts.append(pc)
    script_name = "/".join(script_name_parts)
    path_info = "/".join(path_info_parts)
    if len(script_name) > 1 and path.endswith('/'):
        path_info = path_info + '/'

    if script_name and not script_name.startswith('/'):
        script_name = '/' + script_name
    if path_info and not path_info.startswith('/'):
        path_info = '/' + path_info

    return script_name, path_info

def make_environ():
    """grabbed some of below from _cpwsgiserver.py

    for compatibility with the 2.2beta release of CP
    """

    script_name, path_info = get_path_components(cherrypy.request.path)

    # create and populate the wsgi environment
    environ = dict()
    environ["wsgi.version"] = (1,0)
    environ["wsgi.url_scheme"] = cherrypy.request.scheme
    environ["wsgi.input"] = cherrypy.request.rfile
    environ["wsgi.errors"] = sys.stderr
    environ["wsgi.multithread"] = True
    environ["wsgi.multiprocess"] = False
    environ["wsgi.run_once"] = False
    environ["REQUEST_METHOD"] = cherrypy.request.method
    environ["SCRIPT_NAME"] = script_name
    environ["PATH_INFO"] = path_info
    environ["QUERY_STRING"] = cherrypy.request.queryString
    environ["SERVER_PROTOCOL"] = cherrypy.request.version
    server_name = getattr(cherrypy.server.httpserver, 'server_name', "None")
    environ["SERVER_NAME"] = server_name
    environ["SERVER_PORT"] = cherrypy.config.get('server.socketPort')
    environ["REMOTE_HOST"] = cherrypy.request.remoteHost
    environ["REMOTE_ADDR"] = cherrypy.request.remoteAddr
    environ["REMOTE_PORT"] = cherrypy.request.remotePort
    # then all the http headers
    headers = cherrypy.request.headers
    environ["CONTENT_TYPE"] = headers.get("Content-type", "")
    environ["CONTENT_LENGTH"] = headers.get("Content-length", "")
    for (k, v) in headers.iteritems():
        envname = "HTTP_" + k.upper().replace("-","_")
        environ[envname] = v
    return environ

class WSGIAppFilter(BaseFilter):
    """A filter for running any WSGI middleware/application within CP.

    It can be used through the config system or by adding it to
    a classes _cp_filters special attribute.  Here are the parameters:

    wsgi_app (wsgiapp_filter.app) - any wsgi application callable
    env_update (wsgiapp_filter.env_update) - a dictionary with
    arbitrary keys and values to be merged with the WSGI
    environment dictionary.

    Examples:

    # using _cp_filters
    class Whatever:
        _cp_filters = [WSGIAppFilter(some_app)]

    # using a config file
    [/hosted/app]
    wsgiapp_filter.on = True
    wsgiapp_filter.app = 'package.module.wsgi_app_callable'
    wsgiapp_filter.env_update = {'running_in_cp':True}
    """

    def __init__(self, wsgi_app=None, env_update=None):
        self.app = wsgi_app
        self.env_update = env_update or {}

    def before_request_body(self):
        request = cherrypy.request
        enabled = cherrypy.config.get('wsgiapp_filter.on', False)
        request.wsgi_app = self.app
        request.wsgi_env_update = self.env_update
        request.wsgiapp_filter_on = enabled or bool(self.app)

        if not request.wsgiapp_filter_on:
            return

        if not request.wsgi_app:
            app = cherrypy.config.get('wsgiapp_filter.app', None)
            if app and isinstance(app, basestring):
                app = cptools.attributes(app)
            request.wsgi_app = app

        if not request.wsgi_env_update:
            env_update = cherrypy.config.get('wsgiapp_filter.env_update', {})
            request.wsgi_env_update = env_update

        # keep the request body intact so the wsgi app
        # can have its way with it
        cherrypy.request.processRequestBody = False

    def before_main(self):
        """run the wsgi_app and set response.body to its output
        """

        request = cherrypy.request

        if not request.wsgiapp_filter_on:
            return

        # if the static filter is on for this path and
        # request.execute_main is False, assume that the
        # static filter has already taken care of this request
        staticfilter_on = cherrypy.config.get('static_filter.on', False)
        if staticfilter_on and not request.execute_main:
            return

        try:
            environ = request.wsgi_environ
            sn, pi = get_path_components(request.path)
            environ['SCRIPT_NAME'] = sn
            environ['PATH_INFO'] = pi
        except AttributeError:
            environ = make_environ()

        # update the environ with the dict passed to the filter's
        # constructor
        environ.update(request.wsgi_env_update)

        # run the wsgi app and have it set response.body
        cherrypy.response.body = request.wsgi_app(environ, start_response)

        # tell CP not to handle the request further
        request.execute_main = False

class WSGIApp(object):
    """a convenience class used to easily mount a wsgi app.

    example:
    cherrypy.root = SomeRoot()
    cherrypy.root.otherapp = WSGIApp(other_wsgi_app)
    """
    def __init__(self, app, env_update=None):
        self._cpFilterList = [WSGIAppFilter(app, env_update)]

if __name__ == '__main__':

    def my_app(environ, start_response):
        status = '200 OK'
        response_headers = [('Content-type', 'text/plain')]
        start_response(status, response_headers)
        yield 'Hello, world!\n'
        yield 'This is a wsgi app running within CherryPy!\n\n'
        keys = environ.keys()
        keys.sort()
        for k in keys:
            yield '%s: %s\n' % (k,environ[k])

    class Root(object):
        def index(self):
            yield "
Hi, from CherryPy!
"
            yield "A non-CP WSGI app
"
            yield "
"
            yield "SCRIPT_NAME and PATH_INFO get set "
            yield "properly"
        index.exposed = True

    # mount standard CherryPy app
    cherrypy.root = Root()
    # mount the WSGI app
    cherrypy.root.app = WSGIApp(my_app, {'cherrypy.wsgi_filter':True})

    cherrypy.server.start()

<|endoftext|>


<commit_before># This file is part of the MapProxy project.
# Copyright (C) 2011 Omniscale <http://omniscale.de>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import with_statement, absolute_import

import sys
import time
import threading
from cStringIO import StringIO

from mapproxy.grid import tile_grid
from mapproxy.image import ImageSource
from mapproxy.image.opts import ImageOptions
from mapproxy.layer import MapExtent, DefaultMapExtent, BlankImage, MapLayer
from mapproxy.source import  SourceError
from mapproxy.client.log import log_request
from mapproxy.util.py import reraise_exception
from mapproxy.util.async import run_non_blocking

try:
    import mapnik
    mapnik
except ImportError:
    try:
        # for 2.0 alpha/rcs and first 2.0 release
        import mapnik2 as mapnik
    except ImportError:
        mapnik = None

# fake 2.0 API for older versions
if mapnik and not hasattr(mapnik, 'Box2d'):
    mapnik.Box2d = mapnik.Envelope

import logging
log = logging.getLogger(__name__)

class MapnikSource(MapLayer):
    supports_meta_tiles = True
    def __init__(self, mapfile, layers=None, image_opts=None, coverage=None,
        res_range=None, lock=None, reuse_map_objects=False):
        MapLayer.__init__(self, image_opts=image_opts)
        self.mapfile = mapfile
        self.coverage = coverage
        self.res_range = res_range
        self.layers = set(layers) if layers else None
        self.lock = lock
        self._map_objs = {}
        self._map_objs_lock = threading.Lock()
        self._cache_map_obj = reuse_map_objects
        if self.coverage:
            self.extent = MapExtent(self.coverage.bbox, self.coverage.srs)
        else:
            self.extent = DefaultMapExtent()

    def get_map(self, query):
        if self.res_range and not self.res_range.contains(query.bbox, query.size,
                                                          query.srs):
            raise BlankImage()
        if self.coverage and not self.coverage.intersects(query.bbox, query.srs):
            raise BlankImage()

        try:
            resp = self.render(query)
        except RuntimeError, ex:
            log.error('could not render Mapnik map: %s', ex)
            reraise_exception(SourceError(ex.args[0]), sys.exc_info())
        resp.opacity = self.opacity
        return resp

    def render(self, query):
        mapfile = self.mapfile
        if '%(webmercator_level)' in mapfile:
            _bbox, level = tile_grid(3857).get_affected_bbox_and_level(
                query.bbox, query.size, req_srs=query.srs)
            mapfile = mapfile % {'webmercator_level': level}

        if self.lock:
            with self.lock():
                return self.render_mapfile(mapfile, query)
        else:
            return self.render_mapfile(mapfile, query)

    def map_obj(self, mapfile):
        if not self._cache_map_obj:
            m = mapnik.Map(0, 0)
            mapnik.load_map(m, str(mapfile))
            return m

        # cache loaded map objects
        # only works when a single proc/thread accesses this object
        # (forking the render process doesn't work because of open database
        #  file handles that gets passed to the child)
        if mapfile not in self._map_objs:
            with self._map_objs_lock:
                if mapfile not in self._map_objs:
                    m = mapnik.Map(0, 0)
                    mapnik.load_map(m, str(mapfile))
                    self._map_objs[mapfile] = m

        return self._map_objs[mapfile]

    def render_mapfile(self, mapfile, query):
        return run_non_blocking(self._render_mapfile, (mapfile, query))

    def _render_mapfile(self, mapfile, query):
        start_time = time.time()

        m = self.map_obj(mapfile)
        m.resize(query.size[0], query.size[1])
        m.srs = '+init=%s' % str(query.srs.srs_code.lower())
        envelope = mapnik.Box2d(*query.bbox)
        m.zoom_to_box(envelope)

        try:
            if self.layers:
                i = 0
                for layer in m.layers[:]:
                    if layer.name != 'Unkown' and layer.name not in self.layers:
                        del m.layers[i]
                    else:
                        i += 1

            img = mapnik.Image(query.size[0], query.size[1])
            mapnik.render(m, img)
            data = img.tostring(str(query.format))
        finally:
            size = None
            if data:
                size = len(data)
            log_request('%s:%s:%s:%s' % (mapfile, query.bbox, query.srs.srs_code, query.size),
                status='200' if data else '500', size=size, method='API', duration=time.time()-start_time)

        return ImageSource(StringIO(data), size=query.size,
            image_opts=ImageOptions(transparent=self.transparent, format=query.format))
<commit_msg>fix error in expection handling for mapnik source<commit_after># This file is part of the MapProxy project.
# Copyright (C) 2011 Omniscale <http://omniscale.de>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import with_statement, absolute_import

import sys
import time
import threading
from cStringIO import StringIO

from mapproxy.grid import tile_grid
from mapproxy.image import ImageSource
from mapproxy.image.opts import ImageOptions
from mapproxy.layer import MapExtent, DefaultMapExtent, BlankImage, MapLayer
from mapproxy.source import  SourceError
from mapproxy.client.log import log_request
from mapproxy.util.py import reraise_exception
from mapproxy.util.async import run_non_blocking

try:
    import mapnik
    mapnik
except ImportError:
    try:
        # for 2.0 alpha/rcs and first 2.0 release
        import mapnik2 as mapnik
    except ImportError:
        mapnik = None

# fake 2.0 API for older versions
if mapnik and not hasattr(mapnik, 'Box2d'):
    mapnik.Box2d = mapnik.Envelope

import logging
log = logging.getLogger(__name__)

class MapnikSource(MapLayer):
    supports_meta_tiles = True
    def __init__(self, mapfile, layers=None, image_opts=None, coverage=None,
        res_range=None, lock=None, reuse_map_objects=False):
        MapLayer.__init__(self, image_opts=image_opts)
        self.mapfile = mapfile
        self.coverage = coverage
        self.res_range = res_range
        self.layers = set(layers) if layers else None
        self.lock = lock
        self._map_objs = {}
        self._map_objs_lock = threading.Lock()
        self._cache_map_obj = reuse_map_objects
        if self.coverage:
            self.extent = MapExtent(self.coverage.bbox, self.coverage.srs)
        else:
            self.extent = DefaultMapExtent()

    def get_map(self, query):
        if self.res_range and not self.res_range.contains(query.bbox, query.size,
                                                          query.srs):
            raise BlankImage()
        if self.coverage and not self.coverage.intersects(query.bbox, query.srs):
            raise BlankImage()

        try:
            resp = self.render(query)
        except RuntimeError, ex:
            log.error('could not render Mapnik map: %s', ex)
            reraise_exception(SourceError(ex.args[0]), sys.exc_info())
        resp.opacity = self.opacity
        return resp

    def render(self, query):
        mapfile = self.mapfile
        if '%(webmercator_level)' in mapfile:
            _bbox, level = tile_grid(3857).get_affected_bbox_and_level(
                query.bbox, query.size, req_srs=query.srs)
            mapfile = mapfile % {'webmercator_level': level}

        if self.lock:
            with self.lock():
                return self.render_mapfile(mapfile, query)
        else:
            return self.render_mapfile(mapfile, query)

    def map_obj(self, mapfile):
        if not self._cache_map_obj:
            m = mapnik.Map(0, 0)
            mapnik.load_map(m, str(mapfile))
            return m

        # cache loaded map objects
        # only works when a single proc/thread accesses this object
        # (forking the render process doesn't work because of open database
        #  file handles that gets passed to the child)
        if mapfile not in self._map_objs:
            with self._map_objs_lock:
                if mapfile not in self._map_objs:
                    m = mapnik.Map(0, 0)
                    mapnik.load_map(m, str(mapfile))
                    self._map_objs[mapfile] = m

        return self._map_objs[mapfile]

    def render_mapfile(self, mapfile, query):
        return run_non_blocking(self._render_mapfile, (mapfile, query))

    def _render_mapfile(self, mapfile, query):
        start_time = time.time()

        m = self.map_obj(mapfile)
        m.resize(query.size[0], query.size[1])
        m.srs = '+init=%s' % str(query.srs.srs_code.lower())
        envelope = mapnik.Box2d(*query.bbox)
        m.zoom_to_box(envelope)
        data = None

        try:
            if self.layers:
                i = 0
                for layer in m.layers[:]:
                    if layer.name != 'Unkown' and layer.name not in self.layers:
                        del m.layers[i]
                    else:
                        i += 1

            img = mapnik.Image(query.size[0], query.size[1])
            mapnik.render(m, img)
            data = img.tostring(str(query.format))
        finally:
            size = None
            if data:
                size = len(data)
            log_request('%s:%s:%s:%s' % (mapfile, query.bbox, query.srs.srs_code, query.size),
                status='200' if data else '500', size=size, method='API', duration=time.time()-start_time)

        return ImageSource(StringIO(data), size=query.size,
            image_opts=ImageOptions(transparent=self.transparent, format=query.format))
<|endoftext|>


<commit_before>import unittest
import json
import out
from concourse_common import testutil
from unittest.mock import patch

class TestOutput(unittest.TestCase):

    def test_json_output_wrong_command(self):
        testutil.put_stdin(json.dumps({"source": {"SLACK_BOT_TOKEN": "test"}, "params": {"version": "1.1.0",
                                                                                "command": "1.1.0",
                                                                                "channel": "reports",
                                                                                "directory": "1.1.0"}}))
        self.assertEquals(out.execute("/"), -1)

    @patch("out.open")
    @patch("out.json")
    @patch("out.slack_post")
    def test_correct_slack_method_was_called(self, mock_io, mock2, mock3):
        testutil.put_stdin(json.dumps({"source": {"SLACK_BOT_TOKEN": "test"}, "params": {"version": "1.1.0",
                                                                                "command": "success",
                                                                                "channel": "reports"}}))
        out.execute("/")
        mock_io.post_success_message.assert_called_with(unittest.mock.ANY, unittest.mock.ANY, unittest.mock.ANY)

    @patch("out.open")
    @patch("out.json")
    @patch("out.slack_post")
    def test_correct_slack_failure_method_was_called(self, mock_io, mock2, mock3):
        testutil.put_stdin(json.dumps({"source": {"SLACK_BOT_TOKEN": "test"}, "params": {"version": "1.1.0",
                                                                                "command": "failure",
                                                                                "channel": "reports"}}))
        out.execute("/")
        mock_io.post_failure_message.assert_called_with(unittest.mock.ANY, unittest.mock.ANY, unittest.mock.ANY)

if __name__ == '__main__':
    unittest.main()<commit_msg>added tests<commit_after>import unittest
import json
import out
from concourse_common import testutil
from unittest.mock import patch

class TestOutput(unittest.TestCase):

    def test_json_output_wrong_command(self):
        testutil.put_stdin(json.dumps({"source": {"SLACK_BOT_TOKEN": "test"}, "params": {"version": "1.1.0",
                                                                                "command": "1.1.0",
                                                                                "channel": "reports",
                                                                                "directory": "1.1.0"}}))
        self.assertEquals(out.execute("/"), -1)

    def test_json_output_no_source(self):
        testutil.put_stdin(json.dumps({ "params": {"version": "1.1.0",
                                                         "command": "1.1.0",
                                                         "channel": "reports",
                                                         "directory": "1.1.0"}}))
        self.assertEquals(out.execute("/"), -1)

    def test_json_output_no_directory(self):
        testutil.put_stdin(json.dumps({"source": {"SLACK_BOT_TOKEN": "test"}, "params": {"version": "1.1.0",
                                                                                "command": "1.1.0",
                                                                                "channel": "reports"}}))
        self.assertEquals(out.execute("/"), -1)

    def test_json_output_missing_version(self):
        testutil.put_stdin(json.dumps({"source": {"SLACK_BOT_TOKEN": "test"}, "params": {"command": "1.1.0",
                                                                                "channel": "reports",
                                                                                "directory": "1.1.0"}}))
        self.assertEquals(out.execute("/"), -1)

    @patch("out.open")
    @patch("out.json")
    @patch("out.slack_post")
    def test_correct_slack_method_was_called(self, mock_io, mock2, mock3):
        testutil.put_stdin(json.dumps({"source": {"SLACK_BOT_TOKEN": "test"}, "params": {"version": "1.1.0",
                                                                                "command": "success",
                                                                                "channel": "reports"}}))
        out.execute("/")
        mock_io.post_success_message.assert_called_with(unittest.mock.ANY, unittest.mock.ANY, unittest.mock.ANY)

    @patch("out.open")
    @patch("out.json")
    @patch("out.slack_post")
    def test_correct_slack_failure_method_was_called(self, mock_io, mock2, mock3):
        testutil.put_stdin(json.dumps({"source": {"SLACK_BOT_TOKEN": "test"}, "params": {"version": "1.1.0",
                                                                                "command": "failure",
                                                                                "channel": "reports"}}))
        out.execute("/")
        mock_io.post_failure_message.assert_called_with(unittest.mock.ANY, unittest.mock.ANY, unittest.mock.ANY)

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