<commit_before>#!/usr/bin/env python

import rospy
from jsk_apc2016_common.msg import BinInfo, BinInfoArray
from jsk_recognition_msgs.msg import BoundingBox, BoundingBoxArray
from geometry_msgs.msg import Pose
import jsk_apc2016_common.segmentation_in_bin.\
        segmentation_in_bin_helper as helper
import jsk_apc2016_common
from std_msgs.msg import Header

import json
import os

class BinInfoArrayPublisher(object):
    def __init__(self):
        self.bbox_dict = {}
        self.bin_contents_dict = {}
        self.targets_dict = {}
        self.cam_direction_dict = {}
        self.json_file = None

        pub_bin_info_arr = rospy.Publisher('~bin_array', BinInfoArray, queue_size=1)
        pub_bbox_arr = rospy.Publisher('~bbox_array', BoundingBoxArray, queue_size=1)
        rate = rospy.Rate(rospy.get_param('rate', 1))
        while not rospy.is_shutdown():
            json = rospy.get_param('~json', None)

            # update bin_info_arr only when rosparam: json is changd
            if self.json_file != json:
                if not os.path.isfile(json) or json[-4:] != 'json':
                    rospy.logwarn('wrong json file name')
                    rate.sleep()
                    continue
                self.json_file = json

                # get bbox from rosparam
                self.from_shelf_param('upper')
                self.from_shelf_param('lower')

                # create bounding box array
                self.bbox_array = self.get_bounding_box_array(self.bbox_dict)

                # get contents of bin from json
                self.bin_contents_dict = jsk_apc2016_common.get_bin_contents(self.json_file)
                self.targets_dict = jsk_apc2016_common.get_work_order(self.json_file)

                # create bin_msg
                self.create_bin_info_arr()

            self.bbox_array.header.stamp = rospy.Time.now()
            self.bin_info_arr.header.stamp = rospy.Time.now()
            self.bbox_array.header.seq += 1
            self.bin_info_arr.header.seq += 1
            pub_bbox_arr.publish(self.bbox_array)
            pub_bin_info_arr.publish(self.bin_info_arr)
            rate.sleep()

    def get_bounding_box_array(self, bbox_dict):
        bbox_list = [bbox_dict[bin_] for bin_ in 'abcdefghijkl']
        bbox_array = BoundingBoxArray(boxes=bbox_list)
        bbox_array.header.stamp = rospy.Time(0)
        bbox_array.header.seq = 0
        bbox_array.header.frame_id = 'kiva_pod_base'
        return bbox_array

    def from_shelf_param(self, upper_lower):
        upper_lower = upper_lower + '_shelf'
        initial_pos_list = rospy.get_param(
                '~' + upper_lower + '/initial_pos_list')
        initial_quat_list = rospy.get_param(
                '~' + upper_lower + '/initial_quat_list')
        dimensions = rospy.get_param(
                '~' + upper_lower + '/dimensions')
        frame_id_list = rospy.get_param(
                '~' + upper_lower + '/frame_id_list')
        prefixes = rospy.get_param(
                '~' + upper_lower + '/prefixes')
        camera_directions = rospy.get_param(
                '~' + upper_lower + '/camera_directions')

        for i, bin_ in enumerate(prefixes):
            bin_ = bin_.split('_')[1].lower()  # bin_A -> a
            header = Header(
                    stamp=rospy.Time.now(),
                    frame_id=frame_id_list[i])
            self.bbox_dict[bin_] = BoundingBox(
                    header=header,
                    pose=Pose(
                            position=helper.point(initial_pos_list[i]),
                            orientation=helper.quaternion(initial_quat_list[i])),
                    dimensions=helper.vector3(dimensions[i]))
            self.cam_direction_dict[bin_] = camera_directions[i]

    def create_bin_info_arr(self):
        self.bin_info_arr = BinInfoArray()
        for bin_ in 'abcdefghijkl':
            self.bin_info_arr.array.append(BinInfo(
                    header=Header(
                            stamp=rospy.Time(0),
                            seq=0,
                            frame_id='bin_'+bin_),
                    name=bin_,
                    objects=self.bin_contents_dict[bin_],
                    target=self.targets_dict[bin_],
                    bbox=self.bbox_dict[bin_],
                    camera_direction=self.cam_direction_dict[bin_]))

if __name__ == '__main__':
    rospy.init_node('set_bin_param')
    bin_publisher = BinInfoArrayPublisher()
    rate = rospy.Rate(rospy.get_param('rate', 1))
    while not rospy.is_shutdown():
        rate.sleep()
<commit_msg>remove header sequence in publish_bin_info<commit_after>#!/usr/bin/env python

import rospy
from jsk_apc2016_common.msg import BinInfo, BinInfoArray
from jsk_recognition_msgs.msg import BoundingBox, BoundingBoxArray
from geometry_msgs.msg import Pose
import jsk_apc2016_common.segmentation_in_bin.\
        segmentation_in_bin_helper as helper
import jsk_apc2016_common
from std_msgs.msg import Header

import json
import os

class BinInfoArrayPublisher(object):
    def __init__(self):
        self.bbox_dict = {}
        self.bin_contents_dict = {}
        self.targets_dict = {}
        self.cam_direction_dict = {}
        self.json_file = None

        pub_bin_info_arr = rospy.Publisher('~bin_array', BinInfoArray, queue_size=1)
        pub_bbox_arr = rospy.Publisher('~bbox_array', BoundingBoxArray, queue_size=1)
        rate = rospy.Rate(rospy.get_param('rate', 1))
        while not rospy.is_shutdown():
            json = rospy.get_param('~json', None)

            # update bin_info_arr only when rosparam: json is changd
            if self.json_file != json:
                if not os.path.isfile(json) or json[-4:] != 'json':
                    rospy.logwarn('wrong json file name')
                    rate.sleep()
                    continue
                self.json_file = json

                # get bbox from rosparam
                self.from_shelf_param('upper')
                self.from_shelf_param('lower')

                # create bounding box array
                self.bbox_array = self.get_bounding_box_array(self.bbox_dict)

                # get contents of bin from json
                self.bin_contents_dict = jsk_apc2016_common.get_bin_contents(self.json_file)
                self.targets_dict = jsk_apc2016_common.get_work_order(self.json_file)

                # create bin_msg
                self.create_bin_info_arr()

            self.bbox_array.header.stamp = rospy.Time.now()
            self.bin_info_arr.header.stamp = rospy.Time.now()
            pub_bbox_arr.publish(self.bbox_array)
            pub_bin_info_arr.publish(self.bin_info_arr)
            rate.sleep()

    def get_bounding_box_array(self, bbox_dict):
        bbox_list = [bbox_dict[bin_] for bin_ in 'abcdefghijkl']
        bbox_array = BoundingBoxArray(boxes=bbox_list)
        bbox_array.header.stamp = rospy.Time(0)
        bbox_array.header.seq = 0
        bbox_array.header.frame_id = 'kiva_pod_base'
        return bbox_array

    def from_shelf_param(self, upper_lower):
        upper_lower = upper_lower + '_shelf'
        initial_pos_list = rospy.get_param(
                '~' + upper_lower + '/initial_pos_list')
        initial_quat_list = rospy.get_param(
                '~' + upper_lower + '/initial_quat_list')
        dimensions = rospy.get_param(
                '~' + upper_lower + '/dimensions')
        frame_id_list = rospy.get_param(
                '~' + upper_lower + '/frame_id_list')
        prefixes = rospy.get_param(
                '~' + upper_lower + '/prefixes')
        camera_directions = rospy.get_param(
                '~' + upper_lower + '/camera_directions')

        for i, bin_ in enumerate(prefixes):
            bin_ = bin_.split('_')[1].lower()  # bin_A -> a
            header = Header(
                    stamp=rospy.Time.now(),
                    frame_id=frame_id_list[i])
            self.bbox_dict[bin_] = BoundingBox(
                    header=header,
                    pose=Pose(
                            position=helper.point(initial_pos_list[i]),
                            orientation=helper.quaternion(initial_quat_list[i])),
                    dimensions=helper.vector3(dimensions[i]))
            self.cam_direction_dict[bin_] = camera_directions[i]

    def create_bin_info_arr(self):
        self.bin_info_arr = BinInfoArray()
        for bin_ in 'abcdefghijkl':
            self.bin_info_arr.array.append(BinInfo(
                    header=Header(
                            stamp=rospy.Time(0),
                            seq=0,
                            frame_id='bin_'+bin_),
                    name=bin_,
                    objects=self.bin_contents_dict[bin_],
                    target=self.targets_dict[bin_],
                    bbox=self.bbox_dict[bin_],
                    camera_direction=self.cam_direction_dict[bin_]))

if __name__ == '__main__':
    rospy.init_node('set_bin_param')
    bin_publisher = BinInfoArrayPublisher()
    rate = rospy.Rate(rospy.get_param('rate', 1))
    while not rospy.is_shutdown():
        rate.sleep()
<|endoftext|>


<commit_before>from django.core.cache import cache
from django.test import TestCase
from corehq.apps.app_manager.models import Application
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.receiverwrapper.util import get_version_from_build_id, get_submit_url
from corehq.form_processor.interfaces.dbaccessors import FormAccessors
from corehq.form_processor.tests.utils import run_with_all_backends
from couchforms.util import spoof_submission

class TestAppId(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.domain = 'alskdjfablasdkffsdlkfjabas'
        cls.project = create_domain(name=cls.domain)

        cls.app = Application(domain=cls.domain, version=4)
        cls.app.save()
        cls.app_id = cls.app.get_id

        cls.build = Application(domain=cls.domain, version=3)
        cls.build.copy_of = cls.app_id
        cls.build.save()
        cls.build_id = cls.build.get_id

        cache.clear()

    @classmethod
    def tearDownClass(cls):
        cls.project.delete()

    @run_with_all_backends
    def test(self):
        self._test(self.build_id, self.app_id, self.build_id)
        self._test(self.app_id, self.app_id, None)
        self._test('alskdjflaksdjf', 'alskdjflaksdjf', None)

        # does this work just as well for a deleted app?
        self.app.delete_app()
        cache.clear()
        self._test(self.build_id, self.app_id, self.build_id)

        # and deleted builds
        self.build.delete_app()
        cache.clear()
        form = self._test(self.build_id, self.app_id, self.build_id)

        self._test_app_version(self.domain, form, self.build)
        # (test cache hit)
        self._test_app_version(self.domain, form, self.build)

    def _test(self, submit_app_id, expected_app_id, expected_build_id):
        form_id = spoof_submission(
            get_submit_url(self.domain, submit_app_id),
            '<data xmlns="http://example.com/"><question1/></data>'
        )
        form = FormAccessors(self.domain).get_form(form_id)
        self.assertEqual(form.app_id, expected_app_id)
        self.assertEqual(form.build_id, expected_build_id)
        return form

    def _test_app_version(self, domain, form, build):
        self.assertEqual(build.version, 3)
        self.assertEqual(get_version_from_build_id(domain, form.build_id),
                         build.version)
<commit_msg>make TestAppId compatible with django 1.8<commit_after>from django.core.cache import cache
from django.test import TestCase
from corehq.apps.app_manager.models import Application
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.receiverwrapper.util import get_version_from_build_id, get_submit_url
from corehq.form_processor.interfaces.dbaccessors import FormAccessors
from corehq.form_processor.tests.utils import run_with_all_backends
from couchforms.util import spoof_submission

class TestAppId(TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestAppId, cls).setUpClass()
        cls.domain = 'alskdjfablasdkffsdlkfjabas'
        cls.project = create_domain(name=cls.domain)

        cls.app = Application(domain=cls.domain, version=4)
        cls.app.save()
        cls.app_id = cls.app.get_id

        cls.build = Application(domain=cls.domain, version=3)
        cls.build.copy_of = cls.app_id
        cls.build.save()
        cls.build_id = cls.build.get_id

        cache.clear()

    @classmethod
    def tearDownClass(cls):
        cls.project.delete()
        super(TestAppId, cls).tearDownClass()

    @run_with_all_backends
    def test(self):
        self._test(self.build_id, self.app_id, self.build_id)
        self._test(self.app_id, self.app_id, None)
        self._test('alskdjflaksdjf', 'alskdjflaksdjf', None)

        # does this work just as well for a deleted app?
        self.app.delete_app()
        cache.clear()
        self._test(self.build_id, self.app_id, self.build_id)

        # and deleted builds
        self.build.delete_app()
        cache.clear()
        form = self._test(self.build_id, self.app_id, self.build_id)

        self._test_app_version(self.domain, form, self.build)
        # (test cache hit)
        self._test_app_version(self.domain, form, self.build)

    def _test(self, submit_app_id, expected_app_id, expected_build_id):
        form_id = spoof_submission(
            get_submit_url(self.domain, submit_app_id),
            '<data xmlns="http://example.com/"><question1/></data>'
        )
        form = FormAccessors(self.domain).get_form(form_id)
        self.assertEqual(form.app_id, expected_app_id)
        self.assertEqual(form.build_id, expected_build_id)
        return form

    def _test_app_version(self, domain, form, build):
        self.assertEqual(build.version, 3)
        self.assertEqual(get_version_from_build_id(domain, form.build_id),
                         build.version)
<|endoftext|>


<commit_before>from __future__ import absolute_import, unicode_literals

import re

from corehq.apps.app_manager.app_schemas.case_properties import (
    get_parent_type_map,
)
from corehq.apps.app_manager.const import USERCASE_TYPE
from corehq.apps.app_manager.models import (
    AdvancedForm,
    AdvancedModule,
    Form,
    FormAction,
    FormActionCondition,
    Module,
)
from corehq.apps.data_dictionary.util import get_case_property_description_dict
from corehq.apps.reports.formdetails.readable import (
    AppCaseMetadata,
    FormQuestionResponse,
)

class AppCaseMetadataBuilder(object):
    def __init__(self, domain, app):
        self.domain = domain
        self.app = app
        self.meta = AppCaseMetadata()

    def case_metadata(self):
        self._build_case_relationships()
        self._add_module_contributions()
        self._add_form_contributions()
        self._add_case_property_descriptions()
        return self.meta

    def _build_case_relationships(self):
        case_relationships = get_parent_type_map(self.app)
        for case_type, relationships in case_relationships.items():
            self.meta.get_type(case_type).relationships = relationships

    def _add_module_contributions(self):
        for module in self.app.get_modules():
            if isinstance(module, (Module, AdvancedModule)):
                self._add_module_contribution(module)

    def _add_module_contribution(self, module):
        for column in module.case_details.long.columns:
            self.meta.add_property_detail('long', module.case_type, module.unique_id, column)
        for column in module.case_details.short.columns:
            self.meta.add_property_detail('short', module.case_type, module.unique_id, column)

    def _add_form_contributions(self):
        for module in self.app.get_modules():
            for form in module.get_forms():
                if isinstance(form, Form):
                    _RegularFormCaseMetadataBuilder(self.meta, form).add_form_contributions()
                elif isinstance(form, AdvancedForm):
                    _AdvancedFormCaseMetadataBuilder(self.meta, form).add_form_contributions()

    def _add_case_property_descriptions(self):
        descriptions_dict = get_case_property_description_dict(self.domain)
        for type_ in self.meta.case_types:
            for prop in type_.properties:
                prop.description = descriptions_dict.get(type_.name, {}).get(prop.name, '')

class _BaseFormCaseMetadataBuilder(object):
    def __init__(self, meta, form):
        self.meta = meta
        self.form = form
        self.questions = {q['value']: FormQuestionResponse(q) for q in self.form.cached_get_questions()}

    def add_form_contributions(self):
        self._add_save_to_case_questions()

    def _add_save_to_case_questions(self):
        # The information for save to case questions comes directly from Vellum
        form_id = self.form.unique_id
        for property_info in self.form.case_references_data.get_save_references():
            if not property_info.case_type:
                # If there is no case type given by the user, we can't really
                # infer what this save to case question refers to
                continue

            for property_name in property_info.properties:
                question = FormQuestionResponse.wrap({
                    "label": property_info.path,
                    "tag": property_info.path,
                    "value": property_info.path,
                    "repeat": None,
                    "group": None,
                    "type": 'SaveToCase',
                    "relevant": None,
                    "required": None,
                    "comment": None,
                    "hashtagValue": property_info.path,
                })
                self.meta.add_property_save(property_info.case_type, property_name, form_id, question)

            type_meta = self.meta.get_type(property_info.case_type)
            if property_info.create:
                type_meta.add_opener(form_id, FormActionCondition(type='always'))
            if property_info.close:
                type_meta.add_closer(form_id, FormActionCondition(type='always'))

    def _add_property_save(self, case_type, name, question_path, condition=None):
        try:
            question = self.questions[question_path]
        except KeyError:
            message = "%s is not a valid question" % question_path
            self.meta.add_property_error(case_type, name, self.form.unique_id, message)
        else:
            self.meta.add_property_save(case_type, name, self.form.unique_id, question, condition)

    def _add_property_load(self, case_type, name, question_path):
        try:
            question = self.questions[question_path]
        except KeyError:
            message = "%s is not a valid question" % question_path
            self.meta.add_property_error(case_type, name, self.form.unique_id, message)
        else:
            self.meta.add_property_load(case_type, name, self.form.unique_id, question)

class _RegularFormCaseMetadataBuilder(_BaseFormCaseMetadataBuilder):
    def __init__(self, meta, form):
        super(_RegularFormCaseMetadataBuilder, self).__init__(meta, form)
        self.case_type = self.form.get_module().case_type

    def add_form_contributions(self):
        super(_RegularFormCaseMetadataBuilder, self).add_form_contributions()
        self._add_form_actions()
        self._add_load_references()

    def _add_form_actions(self):
        for action_type, action in self.form.active_actions().items():
            if action_type == 'open_case':
                self._handle_open_case_action(action)

            if action_type == 'close_case':
                self._handle_close_case_action(action)

            if action_type in ('update_case', 'usercase_update'):
                self._handle_update_case_action(action_type, action)

            if action_type in ('case_preload', 'load_from_form', 'usercase_preload'):
                self._handle_load_action(action_type, action)

            if action_type == 'subcases':
                self._handle_subcase_actions(action)

    def _handle_open_case_action(self, action):
        type_meta = self.meta.get_type(self.case_type)
        type_meta.add_opener(self.form.unique_id, action.condition)
        self._add_property_save(self.case_type, 'name', action.name_path)

    def _handle_close_case_action(self, action):
        type_meta = self.meta.get_type(self.case_type)
        type_meta.add_closer(self.form.unique_id, action.condition)

    def _handle_update_case_action(self, action_type, action):
        case_type = USERCASE_TYPE if action_type == 'usercase_update' else self.case_type
        for name, question_path in FormAction.get_action_properties(action):
            self._add_property_save(case_type, name, question_path)

    def _handle_load_action(self, action_type, action):
        case_type = USERCASE_TYPE if action_type == 'usercase_update' else self.case_type
        for name, question_path in FormAction.get_action_properties(action):
            self._add_property_load(case_type, name, question_path)

    def _handle_subcase_actions(self, actions):
        active_actions = [a for a in actions if a.is_active()]
        for action in active_actions:
            sub_type_meta = self.meta.get_type(action.case_type)
            sub_type_meta.add_opener(self.form.unique_id, action.condition)
            if action.close_condition.is_active():
                sub_type_meta.add_closer(self.form.unique_id, action.close_condition)
            for name, question_path in FormAction.get_action_properties(action):
                self._add_property_save(action.case_type, name, question_path)

    def _add_load_references(self):
        for case_load_reference in self.form.case_references.get_load_references():
            for name in case_load_reference.properties:
                case_type, name = self._parse_case_type(name, self.case_type)
                name = re.sub("^grandparent/", "parent/parent/", name)
                self._add_property_load(case_type, name, case_load_reference.path)

    @staticmethod
    def _parse_case_type(full_name, module_case_type):
        if full_name.startswith("#case/"):
            return module_case_type, full_name.split("/", 1)[1]
        elif full_name.startswith("#user/"):
            return USERCASE_TYPE, full_name.split("/", 1)[1]
        else:
            return module_case_type, full_name

class _AdvancedFormCaseMetadataBuilder(_BaseFormCaseMetadataBuilder):
    def add_form_contributions(self):
        super(_AdvancedFormCaseMetadataBuilder, self).add_form_contributions()
        self._add_load_update_actions()
        self._add_open_actions()

    def _add_load_update_actions(self):
        for action in self.form.actions.load_update_cases:
            for name, question_path in action.case_properties.items():
                self._add_property_save(action.case_type, name, question_path)
            for question_path, name in action.preload.items():
                self._add_property_load(action.case_type, name, question_path)
            if action.close_condition.is_active():
                meta = self.meta.get_type(action.case_type)
                meta.add_closer(self.form.unique_id, action.close_condition)

    def _add_open_actions(self):
        for action in self.form.actions.open_cases:
            self._add_property_save(action.case_type, 'name', action.name_path, action.open_condition)
            for name, question_path in action.case_properties.items():
                self._add_property_save(action.case_type, name, question_path, action.open_condition)
            meta = self.meta.get_type(action.case_type)
            meta.add_opener(self.form.unique_id, action.open_condition)
            if action.close_condition.is_active():
                meta.add_closer(self.form.unique_id, action.close_condition)
<commit_msg>Use instance method instead of staticmethod<commit_after>from __future__ import absolute_import, unicode_literals

import re

from corehq.apps.app_manager.app_schemas.case_properties import (
    get_parent_type_map,
)
from corehq.apps.app_manager.const import USERCASE_TYPE
from corehq.apps.app_manager.models import (
    AdvancedForm,
    AdvancedModule,
    Form,
    FormAction,
    FormActionCondition,
    Module,
)
from corehq.apps.data_dictionary.util import get_case_property_description_dict
from corehq.apps.reports.formdetails.readable import (
    AppCaseMetadata,
    FormQuestionResponse,
)

class AppCaseMetadataBuilder(object):
    def __init__(self, domain, app):
        self.domain = domain
        self.app = app
        self.meta = AppCaseMetadata()

    def case_metadata(self):
        self._build_case_relationships()
        self._add_module_contributions()
        self._add_form_contributions()
        self._add_case_property_descriptions()
        return self.meta

    def _build_case_relationships(self):
        case_relationships = get_parent_type_map(self.app)
        for case_type, relationships in case_relationships.items():
            self.meta.get_type(case_type).relationships = relationships

    def _add_module_contributions(self):
        for module in self.app.get_modules():
            if isinstance(module, (Module, AdvancedModule)):
                self._add_module_contribution(module)

    def _add_module_contribution(self, module):
        for column in module.case_details.long.columns:
            self.meta.add_property_detail('long', module.case_type, module.unique_id, column)
        for column in module.case_details.short.columns:
            self.meta.add_property_detail('short', module.case_type, module.unique_id, column)

    def _add_form_contributions(self):
        for module in self.app.get_modules():
            for form in module.get_forms():
                if isinstance(form, Form):
                    _RegularFormCaseMetadataBuilder(self.meta, form).add_form_contributions()
                elif isinstance(form, AdvancedForm):
                    _AdvancedFormCaseMetadataBuilder(self.meta, form).add_form_contributions()

    def _add_case_property_descriptions(self):
        descriptions_dict = get_case_property_description_dict(self.domain)
        for type_ in self.meta.case_types:
            for prop in type_.properties:
                prop.description = descriptions_dict.get(type_.name, {}).get(prop.name, '')

class _BaseFormCaseMetadataBuilder(object):
    def __init__(self, meta, form):
        self.meta = meta
        self.form = form
        self.questions = {q['value']: FormQuestionResponse(q) for q in self.form.cached_get_questions()}

    def add_form_contributions(self):
        self._add_save_to_case_questions()

    def _add_save_to_case_questions(self):
        # The information for save to case questions comes directly from Vellum
        form_id = self.form.unique_id
        for property_info in self.form.case_references_data.get_save_references():
            if not property_info.case_type:
                # If there is no case type given by the user, we can't really
                # infer what this save to case question refers to
                continue

            for property_name in property_info.properties:
                question = FormQuestionResponse.wrap({
                    "label": property_info.path,
                    "tag": property_info.path,
                    "value": property_info.path,
                    "repeat": None,
                    "group": None,
                    "type": 'SaveToCase',
                    "relevant": None,
                    "required": None,
                    "comment": None,
                    "hashtagValue": property_info.path,
                })
                self.meta.add_property_save(property_info.case_type, property_name, form_id, question)

            type_meta = self.meta.get_type(property_info.case_type)
            if property_info.create:
                type_meta.add_opener(form_id, FormActionCondition(type='always'))
            if property_info.close:
                type_meta.add_closer(form_id, FormActionCondition(type='always'))

    def _add_property_save(self, case_type, name, question_path, condition=None):
        try:
            question = self.questions[question_path]
        except KeyError:
            message = "%s is not a valid question" % question_path
            self.meta.add_property_error(case_type, name, self.form.unique_id, message)
        else:
            self.meta.add_property_save(case_type, name, self.form.unique_id, question, condition)

    def _add_property_load(self, case_type, name, question_path):
        try:
            question = self.questions[question_path]
        except KeyError:
            message = "%s is not a valid question" % question_path
            self.meta.add_property_error(case_type, name, self.form.unique_id, message)
        else:
            self.meta.add_property_load(case_type, name, self.form.unique_id, question)

class _RegularFormCaseMetadataBuilder(_BaseFormCaseMetadataBuilder):
    def __init__(self, meta, form):
        super(_RegularFormCaseMetadataBuilder, self).__init__(meta, form)
        self.case_type = self.form.get_module().case_type

    def add_form_contributions(self):
        super(_RegularFormCaseMetadataBuilder, self).add_form_contributions()
        self._add_form_actions()
        self._add_load_references()

    def _add_form_actions(self):
        for action_type, action in self.form.active_actions().items():
            if action_type == 'open_case':
                self._handle_open_case_action(action)

            if action_type == 'close_case':
                self._handle_close_case_action(action)

            if action_type in ('update_case', 'usercase_update'):
                self._handle_update_case_action(action_type, action)

            if action_type in ('case_preload', 'load_from_form', 'usercase_preload'):
                self._handle_load_action(action_type, action)

            if action_type == 'subcases':
                self._handle_subcase_actions(action)

    def _handle_open_case_action(self, action):
        type_meta = self.meta.get_type(self.case_type)
        type_meta.add_opener(self.form.unique_id, action.condition)
        self._add_property_save(self.case_type, 'name', action.name_path)

    def _handle_close_case_action(self, action):
        type_meta = self.meta.get_type(self.case_type)
        type_meta.add_closer(self.form.unique_id, action.condition)

    def _handle_update_case_action(self, action_type, action):
        case_type = USERCASE_TYPE if action_type == 'usercase_update' else self.case_type
        for name, question_path in FormAction.get_action_properties(action):
            self._add_property_save(case_type, name, question_path)

    def _handle_load_action(self, action_type, action):
        case_type = USERCASE_TYPE if action_type == 'usercase_update' else self.case_type
        for name, question_path in FormAction.get_action_properties(action):
            self._add_property_load(case_type, name, question_path)

    def _handle_subcase_actions(self, actions):
        active_actions = [a for a in actions if a.is_active()]
        for action in active_actions:
            sub_type_meta = self.meta.get_type(action.case_type)
            sub_type_meta.add_opener(self.form.unique_id, action.condition)
            if action.close_condition.is_active():
                sub_type_meta.add_closer(self.form.unique_id, action.close_condition)
            for name, question_path in FormAction.get_action_properties(action):
                self._add_property_save(action.case_type, name, question_path)

    def _add_load_references(self):
        for case_load_reference in self.form.case_references.get_load_references():
            for name in case_load_reference.properties:
                case_type, name = self._parse_case_type(name)
                name = re.sub("^grandparent/", "parent/parent/", name)
                self._add_property_load(case_type, name, case_load_reference.path)

    def _parse_case_type(self, full_name):
        if full_name.startswith("#case/"):
            return self.case_type, full_name.split("/", 1)[1]
        elif full_name.startswith("#user/"):
            return USERCASE_TYPE, full_name.split("/", 1)[1]
        else:
            return self.case_type, full_name

class _AdvancedFormCaseMetadataBuilder(_BaseFormCaseMetadataBuilder):
    def add_form_contributions(self):
        super(_AdvancedFormCaseMetadataBuilder, self).add_form_contributions()
        self._add_load_update_actions()
        self._add_open_actions()

    def _add_load_update_actions(self):
        for action in self.form.actions.load_update_cases:
            for name, question_path in action.case_properties.items():
                self._add_property_save(action.case_type, name, question_path)
            for question_path, name in action.preload.items():
                self._add_property_load(action.case_type, name, question_path)
            if action.close_condition.is_active():
                meta = self.meta.get_type(action.case_type)
                meta.add_closer(self.form.unique_id, action.close_condition)

    def _add_open_actions(self):
        for action in self.form.actions.open_cases:
            self._add_property_save(action.case_type, 'name', action.name_path, action.open_condition)
            for name, question_path in action.case_properties.items():
                self._add_property_save(action.case_type, name, question_path, action.open_condition)
            meta = self.meta.get_type(action.case_type)
            meta.add_opener(self.form.unique_id, action.open_condition)
            if action.close_condition.is_active():
                meta.add_closer(self.form.unique_id, action.close_condition)
<|endoftext|>


<commit_before>import json
import logging
import traceback

import json_delta
import laboratory

diff_logger = logging.getLogger('ucr_diff')
exception_logger = logging.getLogger('ucr_exception')
timing_logger = logging.getLogger('ucr_timing')

class UCRExperiment(laboratory.Experiment):

    def publish(self, result):
        control = result.control
        candidate = result.observations[0]

        control_value = control.value
        try:
            candidate_value = candidate.value
        except AttributeError:
            self.log_exception(control_value, candidate.exc_info)
        else:
            self.log_timing(control, candidate)
            if cmp(control_value, candidate_value) != 0:
                self.log_diff(control_value, candidate_value)

    def log_exception(self, control_value, candidate_exc):
        formatted_exception = traceback.format_exception(
            candidate_exc[0], candidate_exc[1], candidate_exc[2]
        )
        exception_logger.info(
            "",
            extra={
                'domain': self.context['domain'],
                'report_config_id': self.context['report_config_id'],
                'filter_values': self.context['filter_values'],
                'candidate': formatted_exception,
            })

    def log_diff(self, control_value, candidate_value):
        diff = json_delta.diff(control_value, candidate_value, verbose=False)
        diff_logger.info(
            "",
            extra={
                'domain': self.context['domain'],
                'report_config_id': self.context['report_config_id'],
                'filter_values': self.context['filter_values'],
                'control': json.dumps(control_value),
                'diff': json.dumps(diff)
            })

    def log_timing(self, control, candidate):
        timing_logger.info(
            "",
            extra={
                'domain': self.context['domain'],
                'report_config_id': self.context['report_config_id'],
                'filter_values': self.context['filter_values'],
                'control_duration': control.duration,
                'candidate_duration': candidate.duration
            })
<commit_msg>use simplejson instead of json in laboratory<commit_after>import logging
import traceback

# use simplejson because it supports dumping of Decimal
import simplejson as json
import json_delta
import laboratory

diff_logger = logging.getLogger('ucr_diff')
exception_logger = logging.getLogger('ucr_exception')
timing_logger = logging.getLogger('ucr_timing')

class UCRExperiment(laboratory.Experiment):

    def publish(self, result):
        control = result.control
        candidate = result.observations[0]

        control_value = control.value
        try:
            candidate_value = candidate.value
        except AttributeError:
            self.log_exception(control_value, candidate.exc_info)
        else:
            self.log_timing(control, candidate)
            if cmp(control_value, candidate_value) != 0:
                self.log_diff(control_value, candidate_value)

    def log_exception(self, control_value, candidate_exc):
        formatted_exception = traceback.format_exception(
            candidate_exc[0], candidate_exc[1], candidate_exc[2]
        )
        exception_logger.info(
            "",
            extra={
                'domain': self.context['domain'],
                'report_config_id': self.context['report_config_id'],
                'filter_values': self.context['filter_values'],
                'candidate': formatted_exception,
            })

    def log_diff(self, control_value, candidate_value):
        diff = json_delta.diff(control_value, candidate_value, verbose=False)
        diff_logger.info(
            "",
            extra={
                'domain': self.context['domain'],
                'report_config_id': self.context['report_config_id'],
                'filter_values': self.context['filter_values'],
                'control': json.dumps(control_value),
                'diff': json.dumps(diff)
            })

    def log_timing(self, control, candidate):
        timing_logger.info(
            "",
            extra={
                'domain': self.context['domain'],
                'report_config_id': self.context['report_config_id'],
                'filter_values': self.context['filter_values'],
                'control_duration': control.duration,
                'candidate_duration': candidate.duration
            })
<|endoftext|>


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

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from yaml.constructor import Constructor
from ansible.parsing.yaml.objects import AnsibleMapping, AnsibleSequence, AnsibleUnicode

class AnsibleConstructor(Constructor):
    def __init__(self, file_name=None):
        self._ansible_file_name = file_name
        super(AnsibleConstructor, self).__init__()

    def construct_yaml_map(self, node):
        data = AnsibleMapping()
        yield data
        value = self.construct_mapping(node)
        data.update(value)
        data.ansible_pos = self._node_position_info(node)

    def construct_mapping(self, node, deep=False):
        ret = AnsibleMapping(super(Constructor, self).construct_mapping(node, deep))
        ret.ansible_pos = self._node_position_info(node)

        return ret

    def construct_yaml_str(self, node):
        # Override the default string handling function
        # to always return unicode objects
        value = self.construct_scalar(node)
        ret = AnsibleUnicode(value)

        ret.ansible_pos = self._node_position_info(node)

        return ret

    def construct_yaml_seq(self, node):
        data = AnsibleSequence()
        yield data
        data.extend(self.construct_sequence(node))
        data.ansible_pos = self._node_position_info(node)

    def _node_position_info(self, node):
        # the line number where the previous token has ended (plus empty lines)
        column = node.start_mark.column + 1
        line = node.start_mark.line + 1

        # in some cases, we may have pre-read the data and then
        # passed it to the load() call for YAML, in which case we
        # want to override the default datasource (which would be
        # '<string>') to the actual filename we read in
        datasource = self._ansible_file_name or node.start_mark.name

        return (datasource, line, column)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:map',
    AnsibleConstructor.construct_yaml_map)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:python/dict',
    AnsibleConstructor.construct_yaml_map)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:str',
    AnsibleConstructor.construct_yaml_str)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:python/unicode',
    AnsibleConstructor.construct_yaml_str)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:seq',
    AnsibleConstructor.construct_yaml_seq)
<commit_msg>Comment to clarify why we add one to the line and column recording<commit_after># (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from yaml.constructor import Constructor
from ansible.parsing.yaml.objects import AnsibleMapping, AnsibleSequence, AnsibleUnicode

class AnsibleConstructor(Constructor):
    def __init__(self, file_name=None):
        self._ansible_file_name = file_name
        super(AnsibleConstructor, self).__init__()

    def construct_yaml_map(self, node):
        data = AnsibleMapping()
        yield data
        value = self.construct_mapping(node)
        data.update(value)
        data.ansible_pos = self._node_position_info(node)

    def construct_mapping(self, node, deep=False):
        ret = AnsibleMapping(super(Constructor, self).construct_mapping(node, deep))
        ret.ansible_pos = self._node_position_info(node)

        return ret

    def construct_yaml_str(self, node):
        # Override the default string handling function
        # to always return unicode objects
        value = self.construct_scalar(node)
        ret = AnsibleUnicode(value)

        ret.ansible_pos = self._node_position_info(node)

        return ret

    def construct_yaml_seq(self, node):
        data = AnsibleSequence()
        yield data
        data.extend(self.construct_sequence(node))
        data.ansible_pos = self._node_position_info(node)

    def _node_position_info(self, node):
        # the line number where the previous token has ended (plus empty lines)
        # Add one so that the first line is line 1 rather than line 0
        column = node.start_mark.column + 1
        line = node.start_mark.line + 1

        # in some cases, we may have pre-read the data and then
        # passed it to the load() call for YAML, in which case we
        # want to override the default datasource (which would be
        # '<string>') to the actual filename we read in
        datasource = self._ansible_file_name or node.start_mark.name

        return (datasource, line, column)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:map',
    AnsibleConstructor.construct_yaml_map)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:python/dict',
    AnsibleConstructor.construct_yaml_map)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:str',
    AnsibleConstructor.construct_yaml_str)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:python/unicode',
    AnsibleConstructor.construct_yaml_str)

AnsibleConstructor.add_constructor(
    u'tag:yaml.org,2002:seq',
    AnsibleConstructor.construct_yaml_seq)
<|endoftext|>


<commit_before>from __future__ import absolute_import
from __future__ import unicode_literals
from datetime import datetime
from django.db.models import Min

from celery import current_task, current_app
from celery.schedules import crontab
from celery.task import periodic_task, task
from celery.signals import after_task_publish
from django.conf import settings
from casexml.apps.phone.cleanliness import set_cleanliness_flags_for_all_domains
from casexml.apps.phone.models import SyncLogSQL
from corehq.form_processor.backends.sql.dbaccessors import get_cursor

ASYNC_RESTORE_QUEUE = 'async_restore_queue'
ASYNC_RESTORE_SENT = "SENT"
SYNCLOG_RETENTION_DAYS = 9 * 7  # 63 days

@periodic_task(run_every=crontab(hour="2", minute="0", day_of_week="1"),
               queue='background_queue')
def update_cleanliness_flags():
    """
    Once a week go through all cleanliness flags and see if any dirty ones have become clean
    """
    set_cleanliness_flags_for_all_domains(force_full=False)

@periodic_task(run_every=crontab(hour="4", minute="0", day_of_month="5"),
               queue='background_queue')
def force_update_cleanliness_flags():
    """
    Once a month, go through all cleanliness flags without using hints
    """
    # the only reason this task is run is to use the soft_assert to validate
    # that there are no bugs in the weekly task.
    # If we haven't seen any issues by the end of 2015 (so 6 runs) we should remove this.
    set_cleanliness_flags_for_all_domains(force_full=True)

@task(serializer='pickle', queue=ASYNC_RESTORE_QUEUE)
def get_async_restore_payload(restore_config, domain=None, username=None):
    """
    Process an async restore
    domain and username: added for displaying restore request details on flower
    """
    response = restore_config.generate_payload(async_task=current_task)

    # delete the task id from the task, since the payload can now be fetched from the cache
    restore_config.async_restore_task_id_cache.invalidate()

    return response.name

@after_task_publish.connect
def update_celery_state(sender=None, body=None, **kwargs):
    """Updates the celery task progress to "SENT"

    When fetching an task from celery using the form AsyncResponse(task_id), if
    task_id never exists, celery will not throw an error, it will just hang.
    This function updates each task sent to celery to have a progress value of "SENT",
    which means we can check that a task exists with the following pattern:
    `AsyncResponse(task_id).status == "SENT"`

    See
    http://stackoverflow.com/questions/9824172/find-out-whether-celery-task-exists/10089358
    for more info

    """

    task = current_app.tasks.get(sender)
    backend = task.backend if task else current_app.backend

    backend.store_result(body['id'], None, ASYNC_RESTORE_SENT)

@periodic_task(
    run_every=crontab(hour="1", minute="0"),
    queue=getattr(settings, 'CELERY_PERIODIC_QUEUE', 'celery')
)
def prune_synclogs():
    """
    Drops all partition tables containing data that's older than 63 days (7 weeks)
    """
    oldest_synclog = SyncLogSQL.objects.aggregate(Min('date'))['date__min']
    while oldest_synclog and (datetime.today() - oldest_synclog).days > SYNCLOG_RETENTION_DAYS:
        year, week, _ = oldest_synclog.isocalendar()
        table_name = "{base_name}_y{year}w{week}".format(
            base_name=SyncLogSQL._meta.db_table,
            year=year,
            week="%02d" % week
        )
        drop_query = "DROP TABLE IF EXISTS {}".format(table_name)
        get_cursor(SyncLogSQL).execute(drop_query)
        oldest_synclog = SyncLogSQL.objects.aggregate(Min('date'))['date__min']
<commit_msg>Fetch task id from headers instead of body<commit_after>from __future__ import absolute_import
from __future__ import unicode_literals
from datetime import datetime
from django.db.models import Min

from celery import current_task, current_app
from celery.schedules import crontab
from celery.task import periodic_task, task
from celery.signals import after_task_publish
from django.conf import settings
from casexml.apps.phone.cleanliness import set_cleanliness_flags_for_all_domains
from casexml.apps.phone.models import SyncLogSQL
from corehq.form_processor.backends.sql.dbaccessors import get_cursor

ASYNC_RESTORE_QUEUE = 'async_restore_queue'
ASYNC_RESTORE_SENT = "SENT"
SYNCLOG_RETENTION_DAYS = 9 * 7  # 63 days

@periodic_task(run_every=crontab(hour="2", minute="0", day_of_week="1"),
               queue='background_queue')
def update_cleanliness_flags():
    """
    Once a week go through all cleanliness flags and see if any dirty ones have become clean
    """
    set_cleanliness_flags_for_all_domains(force_full=False)

@periodic_task(run_every=crontab(hour="4", minute="0", day_of_month="5"),
               queue='background_queue')
def force_update_cleanliness_flags():
    """
    Once a month, go through all cleanliness flags without using hints
    """
    # the only reason this task is run is to use the soft_assert to validate
    # that there are no bugs in the weekly task.
    # If we haven't seen any issues by the end of 2015 (so 6 runs) we should remove this.
    set_cleanliness_flags_for_all_domains(force_full=True)

@task(serializer='pickle', queue=ASYNC_RESTORE_QUEUE)
def get_async_restore_payload(restore_config, domain=None, username=None):
    """
    Process an async restore
    domain and username: added for displaying restore request details on flower
    """
    response = restore_config.generate_payload(async_task=current_task)

    # delete the task id from the task, since the payload can now be fetched from the cache
    restore_config.async_restore_task_id_cache.invalidate()

    return response.name

@after_task_publish.connect
def update_celery_state(sender=None, headers=None, **kwargs):
    """Updates the celery task progress to "SENT"

    When fetching an task from celery using the form AsyncResponse(task_id), if
    task_id never exists, celery will not throw an error, it will just hang.
    This function updates each task sent to celery to have a progress value of "SENT",
    which means we can check that a task exists with the following pattern:
    `AsyncResponse(task_id).status == "SENT"`

    See
    http://stackoverflow.com/questions/9824172/find-out-whether-celery-task-exists/10089358
    for more info

    """

    task = current_app.tasks.get(sender)
    backend = task.backend if task else current_app.backend

    backend.store_result(headers['id'], None, ASYNC_RESTORE_SENT)

@periodic_task(
    run_every=crontab(hour="1", minute="0"),
    queue=getattr(settings, 'CELERY_PERIODIC_QUEUE', 'celery')
)
def prune_synclogs():
    """
    Drops all partition tables containing data that's older than 63 days (7 weeks)
    """
    oldest_synclog = SyncLogSQL.objects.aggregate(Min('date'))['date__min']
    while oldest_synclog and (datetime.today() - oldest_synclog).days > SYNCLOG_RETENTION_DAYS:
        year, week, _ = oldest_synclog.isocalendar()
        table_name = "{base_name}_y{year}w{week}".format(
            base_name=SyncLogSQL._meta.db_table,
            year=year,
            week="%02d" % week
        )
        drop_query = "DROP TABLE IF EXISTS {}".format(table_name)
        get_cursor(SyncLogSQL).execute(drop_query)
        oldest_synclog = SyncLogSQL.objects.aggregate(Min('date'))['date__min']
<|endoftext|>


<commit_before>
import discord
from discord.ext import commands

import json

import clients
from modules import utilities
from utilities import checks

def setup(bot):
	bot.add_cog(Permissions(bot))

class Permissions:

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

	@commands.group(invoke_without_command = True)
	@checks.is_permitted()
	async def setpermission(self, ctx):
		'''Set a permission'''
		await ctx.invoke(self.bot.get_command("help"), ctx.invoked_with)

	@setpermission.command(name = "everyone")
	@commands.guild_only()
	@checks.is_permitted()
	async def setpermission_everyone(self, ctx, permission : str, setting : bool = None):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		permissions_data.setdefault("everyone", {})
		permissions_data["everyone"][command] = setting
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'w') as permissions_file:
			json.dump(permissions_data, permissions_file, indent = 4)
		await ctx.embed_reply("Permission updated\n{} set to {} for everyone".format(permission, setting))

	@setpermission.command(name = "role")
	@commands.guild_only()
	@checks.is_permitted()
	async def setpermission_role(self, ctx, role : str, permission : str, setting : bool = None):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		matches = [_role for _role in ctx.guild.roles if _role.name == role]
		if len(matches) > 1: return (await ctx.embed_reply("Error: multiple roles with the name, {}".format(role)))
		elif len(matches) == 0: return (await ctx.embed_reply('Error: role with name, "{}", not found'.format(role)))
		else: _role = matches[0]
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		permissions_data.setdefault("roles", {})
		permissions_data["roles"].setdefault(str(_role.id), {"name" : _role.name})
		permissions_data["roles"][str(_role.id)][command] = setting
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'w') as permissions_file:
			json.dump(permissions_data, permissions_file, indent = 4)
		await ctx.embed_reply("Permission updated\n{} set to {} for the {} role".format(permission, setting, _role.name))

	@setpermission.command(name = "user")
	@commands.guild_only()
	@checks.is_permitted()
	async def setpermission_user(self, ctx, user : str, permission : str, setting : bool = None):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		_user = await utilities.get_user(ctx, user)
		if not _user: return (await ctx.embed_reply("Error: user not found"))
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		permissions_data.setdefault("users", {})
		permissions_data["users"].setdefault(str(_user.id), {"name" : _user.name})
		permissions_data["users"][str(_user.id)][command] = setting
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'w') as permissions_file:
			json.dump(permissions_data, permissions_file, indent = 4)
		await ctx.embed_reply("Permission updated\n{} set to {} for {}".format(permission, setting, _user))

	@commands.group(invoke_without_command = True)
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermission(self, ctx, *options : str):
		'''Get a permission'''
		if len(options) == 2:
			if options[1] not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(options[1])))
			command = self.bot.all_commands[options[1]].name
			user = await utilities.get_user(ctx, options[0])
			if not user: return (await ctx.embed_reply("Error: user not found"))
			setting = utilities.get_permission(ctx, command, id = user.id)
			await ctx.embed_reply("{} is set to {} for {}".format(options[1], setting, user))
		else:
			await ctx.embed_reply(":no_entry: Invalid input\ngetpermission everyone|role|user or <user> <permission>") #options

	@getpermission.command(name = "everyone")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermission_everyone(self, ctx, permission : str):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		setting = utilities.get_permission(ctx, command, type = "everyone")
		await ctx.embed_reply("{} is set to {} for everyone".format(permission, setting))

	@getpermission.command(name = "role")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermission_role(self, ctx, role : str, permission : str):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		matches = [_role for _role in ctx.guild.roles if _role.name == role]
		if len(matches) > 1: return (await ctx.embed_reply("Error: multiple roles with the name, {}".format(role)))
		elif len(matches) == 0: return (await ctx.embed_reply('Error: role with name, "{}", not found'.format(role)))
		else: _role = matches[0]
		setting = utilities.get_permission(ctx, command, type = "role", id = _role.id)
		await ctx.embed_reply("{} is set to {} for the {} role".format(permission, setting, _role.name))

	@getpermission.command(name = "user")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermission_user(self, ctx, user : str, permission : str):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		_user = await utilities.get_user(ctx, user)
		if not _user: return (await ctx.embed_reply("Error: user not found"))
		setting = utilities.get_permission(ctx, command, id = _user.id)
		await ctx.embed_reply("{} is set to {} for {}".format(permission, setting, _user))

	@commands.group(invoke_without_command = True)
	@checks.is_permitted()
	async def getpermissions(self, ctx):
		await ctx.embed_reply(":no_entry: Invalid input\nSee {}help getpermissions".format(ctx.prefix))

	@getpermissions.command(name = "everyone")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermissions_everyone(self, ctx):
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		everyone_settings = permissions_data.get("everyone", {})
		output = "__Permissions for everyone__\n"
		for permission, setting in everyone_settings.items():
			output += "{}: {}\n".format(permission, str(setting))
		await self.bot.say(output)

	@getpermissions.command(name = "role")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermissions_role(self, ctx, role : str):
		matches = [_role for _role in ctx.guild.roles if _role.name == role]
		if len(matches) > 1: return (await ctx.embed_reply("Error: multiple roles with the name, {}".format(role)))
		elif len(matches) == 0: return (await ctx.embed_reply('Error: role with name, "{}", not found'.format(role)))
		else: _role = matches[0]
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		role_settings = permissions_data.get("roles", {}).get(str(_role.id), {})
		output = "__Permissions for {}__\n".format(_role.name)
		role_settings.pop("name", None)
		for permission, setting in role_settings.items():
			output += "{}: {}\n".format(permission, str(setting))
		await self.bot.say(output)

	@getpermissions.command(name = "user")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermissions_user(self, ctx, user : str):
		_user = await utilities.get_user(ctx, user)
		if not _user: return (await ctx.embed_reply("Error: user not found"))
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		user_settings = permissions_data.get("users", {}).get(str(_user.id), {})
		output = "__Permissions for {}__\n".format(_user.name)
		user_settings.pop("name", None)
		for permission, setting in user_settings.items():
			output += "{}: {}\n".format(permission, str(setting))
		await self.bot.say(output)

	@getpermissions.command(name = "command")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermissions_command(self, ctx, command : str):
		if command not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(command)))
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		output = "__Permissions for {}__\n".format(command)
		permissions_data.pop("name", None)
		if command in permissions_data.get("everyone", {}):
			output += "**Everyone**: {}\n".format(permissions_data.pop("everyone")[command])
		for type, objects in permissions_data.items():
			output += "**{}**\n".format(type.capitalize())
			for id, settings in objects.items():
				if command in settings:
					output += "{}: {}\n".format(settings["name"], str(settings[command]))
		await self.bot.say(output)

<commit_msg>Updated permissions system response methods<commit_after>
import discord
from discord.ext import commands

import json

import clients
from modules import utilities
from utilities import checks

def setup(bot):
	bot.add_cog(Permissions(bot))

class Permissions:

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

	@commands.group(invoke_without_command = True)
	@checks.is_permitted()
	async def setpermission(self, ctx):
		'''Set a permission'''
		await ctx.invoke(self.bot.get_command("help"), ctx.invoked_with)

	@setpermission.command(name = "everyone")
	@commands.guild_only()
	@checks.is_permitted()
	async def setpermission_everyone(self, ctx, permission : str, setting : bool = None):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		permissions_data.setdefault("everyone", {})
		permissions_data["everyone"][command] = setting
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'w') as permissions_file:
			json.dump(permissions_data, permissions_file, indent = 4)
		await ctx.embed_reply("Permission updated\n{} set to {} for everyone".format(permission, setting))

	@setpermission.command(name = "role")
	@commands.guild_only()
	@checks.is_permitted()
	async def setpermission_role(self, ctx, role : str, permission : str, setting : bool = None):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		matches = [_role for _role in ctx.guild.roles if _role.name == role]
		if len(matches) > 1: return (await ctx.embed_reply("Error: multiple roles with the name, {}".format(role)))
		elif len(matches) == 0: return (await ctx.embed_reply('Error: role with name, "{}", not found'.format(role)))
		else: _role = matches[0]
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		permissions_data.setdefault("roles", {})
		permissions_data["roles"].setdefault(str(_role.id), {"name" : _role.name})
		permissions_data["roles"][str(_role.id)][command] = setting
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'w') as permissions_file:
			json.dump(permissions_data, permissions_file, indent = 4)
		await ctx.embed_reply("Permission updated\n{} set to {} for the {} role".format(permission, setting, _role.name))

	@setpermission.command(name = "user")
	@commands.guild_only()
	@checks.is_permitted()
	async def setpermission_user(self, ctx, user : str, permission : str, setting : bool = None):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		_user = await utilities.get_user(ctx, user)
		if not _user: return (await ctx.embed_reply("Error: user not found"))
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		permissions_data.setdefault("users", {})
		permissions_data["users"].setdefault(str(_user.id), {"name" : _user.name})
		permissions_data["users"][str(_user.id)][command] = setting
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'w') as permissions_file:
			json.dump(permissions_data, permissions_file, indent = 4)
		await ctx.embed_reply("Permission updated\n{} set to {} for {}".format(permission, setting, _user))

	@commands.group(invoke_without_command = True)
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermission(self, ctx, *options : str):
		'''Get a permission'''
		if len(options) == 2:
			if options[1] not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(options[1])))
			command = self.bot.all_commands[options[1]].name
			user = await utilities.get_user(ctx, options[0])
			if not user: return (await ctx.embed_reply("Error: user not found"))
			setting = utilities.get_permission(ctx, command, id = user.id)
			await ctx.embed_reply("{} is set to {} for {}".format(options[1], setting, user))
		else:
			await ctx.embed_reply(":no_entry: Invalid input\ngetpermission everyone|role|user or <user> <permission>") #options

	@getpermission.command(name = "everyone")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermission_everyone(self, ctx, permission : str):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		setting = utilities.get_permission(ctx, command, type = "everyone")
		await ctx.embed_reply("{} is set to {} for everyone".format(permission, setting))

	@getpermission.command(name = "role")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermission_role(self, ctx, role : str, permission : str):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		matches = [_role for _role in ctx.guild.roles if _role.name == role]
		if len(matches) > 1: return (await ctx.embed_reply("Error: multiple roles with the name, {}".format(role)))
		elif len(matches) == 0: return (await ctx.embed_reply('Error: role with name, "{}", not found'.format(role)))
		else: _role = matches[0]
		setting = utilities.get_permission(ctx, command, type = "role", id = _role.id)
		await ctx.embed_reply("{} is set to {} for the {} role".format(permission, setting, _role.name))

	@getpermission.command(name = "user")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermission_user(self, ctx, user : str, permission : str):
		if permission not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(permission)))
		command = self.bot.all_commands[permission].name
		_user = await utilities.get_user(ctx, user)
		if not _user: return (await ctx.embed_reply("Error: user not found"))
		setting = utilities.get_permission(ctx, command, id = _user.id)
		await ctx.embed_reply("{} is set to {} for {}".format(permission, setting, _user))

	@commands.group(invoke_without_command = True)
	@checks.is_permitted()
	async def getpermissions(self, ctx):
		await ctx.embed_reply(":no_entry: Invalid input\nSee {}help getpermissions".format(ctx.prefix))

	@getpermissions.command(name = "everyone")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermissions_everyone(self, ctx):
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		everyone_settings = permissions_data.get("everyone", {})
		output = "__Permissions for everyone__\n"
		for permission, setting in everyone_settings.items():
			output += "{}: {}\n".format(permission, str(setting))
		await ctx.send(output)

	@getpermissions.command(name = "role")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermissions_role(self, ctx, role : str):
		matches = [_role for _role in ctx.guild.roles if _role.name == role]
		if len(matches) > 1: return (await ctx.embed_reply("Error: multiple roles with the name, {}".format(role)))
		elif len(matches) == 0: return (await ctx.embed_reply('Error: role with name, "{}", not found'.format(role)))
		else: _role = matches[0]
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		role_settings = permissions_data.get("roles", {}).get(str(_role.id), {})
		output = "__Permissions for {}__\n".format(_role.name)
		role_settings.pop("name", None)
		for permission, setting in role_settings.items():
			output += "{}: {}\n".format(permission, str(setting))
		await ctx.send(output)

	@getpermissions.command(name = "user")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermissions_user(self, ctx, user : str):
		_user = await utilities.get_user(ctx, user)
		if not _user: return (await ctx.embed_reply("Error: user not found"))
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		user_settings = permissions_data.get("users", {}).get(str(_user.id), {})
		output = "__Permissions for {}__\n".format(_user.name)
		user_settings.pop("name", None)
		for permission, setting in user_settings.items():
			output += "{}: {}\n".format(permission, str(setting))
		await ctx.send(output)

	@getpermissions.command(name = "command")
	@commands.guild_only()
	@checks.is_permitted()
	async def getpermissions_command(self, ctx, command : str):
		if command not in self.bot.all_commands: return (await ctx.embed_reply("Error: {} is not a command".format(command)))
		with open(clients.data_path + "/permissions/{}.json".format(ctx.guild.id), 'r') as permissions_file:
			permissions_data = json.load(permissions_file)
		output = "__Permissions for {}__\n".format(command)
		permissions_data.pop("name", None)
		if command in permissions_data.get("everyone", {}):
			output += "**Everyone**: {}\n".format(permissions_data.pop("everyone")[command])
		for type, objects in permissions_data.items():
			output += "**{}**\n".format(type.capitalize())
			for id, settings in objects.items():
				if command in settings:
					output += "{}: {}\n".format(settings["name"], str(settings[command]))
		await ctx.send(output)

<|endoftext|>


<commit_before>#!/usr/bin/python

import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_address = ('localhost', 10000)

sock.bind(server_address)

# listen for incoming connections
sock.listen(1)
print "done listening"

while True:
    # Wait for a connection
    connection, client_address = sock.accept()
    print "done waiting"

    try:
        while True:
            data = connection.recv(16)
            print 'received "%s"' % data
            if data:
                connection.sendall(data)
            else:
                break

    finally:
        connection.close()

exit(0)
<commit_msg>Changed IP address of server to be LAN address (hardcoded)<commit_after>#!/usr/bin/python

import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_address = ('192.168.1.10', 10000)

sock.bind(server_address)

# listen for incoming connections
sock.listen(1)
print "done listening"

while True:
    # Wait for a connection
    print "done waiting"
    connection, client_address = sock.accept()

    try:
        while True:
            data = connection.recv(16)
            print 'received "%s"' % data
            if data:
                connection.sendall(data)
            else:
                break

    finally:
        connection.close()

        # right here is where I need to handle writing the data to file or closing the file for the conversation

        # inform the user that they got a pimessage

exit(0)
<|endoftext|>