<commit_before>from pyrsistent import freeze, inc, discard, rex, ny, field, PClass

def test_callable_command():
    m = freeze({'foo': {'bar': {'baz': 1}}})
    assert m.transform(['foo', 'bar', 'baz'], inc) == {'foo': {'bar': {'baz': 2}}}

def test_predicate():
    m = freeze({'foo': {'bar': {'baz': 1}, 'qux': {'baz': 1}}})
    assert m.transform(['foo', lambda x: x.startswith('b'), 'baz'], inc) == {'foo': {'bar': {'baz': 2}, 'qux': {'baz': 1}}}

def test_remove():
    m = freeze({'foo': {'bar': {'baz': 1}}})
    assert m.transform(['foo', 'bar', 'baz'], discard) == {'foo': {'bar': {}}}

def test_remove_pvector():
    m = freeze({'foo': [1, 2, 3]})
    assert m.transform(['foo', 1], discard) == {'foo': [1, 3]}

def test_remove_pclass():
    class MyClass(PClass):
        a = field()
        b = field()

    m = freeze({'foo': MyClass(a=1, b=2)})
    assert m.transform(['foo', 'b'], discard) == {'foo': MyClass(a=1)}

def test_predicate_no_match():
    m = freeze({'foo': {'bar': {'baz': 1}}})
    assert m.transform(['foo', lambda x: x.startswith('c'), 'baz'], inc) == m

def test_rex_predicate():
    m = freeze({'foo': {'bar': {'baz': 1},
                        'bof': {'baz': 1}}})
    assert m.transform(['foo', rex('^bo.*'), 'baz'], inc) == {'foo': {'bar': {'baz': 1},
                                                                      'bof': {'baz': 2}}}

def test_rex_with_non_string_key():
    m = freeze({'foo': 1, 5: 2})
    assert m.transform([rex(".*")], 5) == {'foo': 5, 5: 2}

def test_ny_predicated_matches_any_key():
    m = freeze({'foo': 1, 5: 2})
    assert m.transform([ny], 5) == {'foo': 5, 5: 5}

def test_new_elements_created_when_missing():
    m = freeze({})
    assert m.transform(['foo', 'bar', 'baz'], 7) == {'foo': {'bar': {'baz': 7}}}

def test_mixed_vector_and_map():
    m = freeze({'foo': [1, 2, 3]})
    assert m.transform(['foo', 1], 5) == freeze({'foo': [1, 5, 3]})

def test_vector_predicate_callable_command():
    v = freeze([1, 2, 3, 4, 5])
    assert v.transform([lambda i: 0 < i < 4], inc) == freeze(freeze([1, 3, 4, 5, 5]))

def test_vector_insert_map_one_step_beyond_end():
    v = freeze([1, 2])
    assert v.transform([2, 'foo'], 3) == freeze([1, 2, {'foo': 3}])

def test_multiple_transformations():
    v = freeze([1, 2])
    assert v.transform([2, 'foo'], 3, [2, 'foo'], inc) == freeze([1, 2, {'foo': 4}])

def test_no_transformation_returns_the_same_structure():
    v = freeze([{'foo': 1}, {'bar': 2}])
    assert v.transform([ny, ny], lambda x: x) is v

def test_discard_multiple_elements_in_pvector():
    assert freeze([0, 1, 2, 3, 4]).transform([lambda i: i % 2], discard) == freeze([0, 2, 4])
<commit_msg>New tests for the new desired behavior<commit_after>from pyrsistent import freeze, inc, discard, rex, ny, field, PClass

def test_callable_command():
    m = freeze({'foo': {'bar': {'baz': 1}}})
    assert m.transform(['foo', 'bar', 'baz'], inc) == {'foo': {'bar': {'baz': 2}}}

def test_predicate():
    m = freeze({'foo': {'bar': {'baz': 1}, 'qux': {'baz': 1}}})
    assert m.transform(['foo', lambda x: x.startswith('b'), 'baz'], inc) == {'foo': {'bar': {'baz': 2}, 'qux': {'baz': 1}}}

def test_broken_predicate():
    broken_predicates = [
        lambda: None,
        lambda a, b, c: None,
        lambda a, b, c, d=None: None,
        lambda *args: None,
        lambda **kwargs: None,
    ]
    for pred in broken_predicates:
        try:
            freeze({}).transform([pred], None)
            assert False
        except ValueError as e:
            assert str(e) == "callable in transform path must take 1 or 2 arguments"

def test_key_value_predicate():
    m = freeze({
        'foo': 1,
        'bar': 2,
    })
    assert m.transform([
        lambda k, v: (k, v) == ('foo', 1),
    ], lambda v: v * 3) == {"foo": 3, "bar": 2}

def test_remove():
    m = freeze({'foo': {'bar': {'baz': 1}}})
    assert m.transform(['foo', 'bar', 'baz'], discard) == {'foo': {'bar': {}}}

def test_remove_pvector():
    m = freeze({'foo': [1, 2, 3]})
    assert m.transform(['foo', 1], discard) == {'foo': [1, 3]}

def test_remove_pclass():
    class MyClass(PClass):
        a = field()
        b = field()

    m = freeze({'foo': MyClass(a=1, b=2)})
    assert m.transform(['foo', 'b'], discard) == {'foo': MyClass(a=1)}

def test_predicate_no_match():
    m = freeze({'foo': {'bar': {'baz': 1}}})
    assert m.transform(['foo', lambda x: x.startswith('c'), 'baz'], inc) == m

def test_rex_predicate():
    m = freeze({'foo': {'bar': {'baz': 1},
                        'bof': {'baz': 1}}})
    assert m.transform(['foo', rex('^bo.*'), 'baz'], inc) == {'foo': {'bar': {'baz': 1},
                                                                      'bof': {'baz': 2}}}

def test_rex_with_non_string_key():
    m = freeze({'foo': 1, 5: 2})
    assert m.transform([rex(".*")], 5) == {'foo': 5, 5: 2}

def test_ny_predicated_matches_any_key():
    m = freeze({'foo': 1, 5: 2})
    assert m.transform([ny], 5) == {'foo': 5, 5: 5}

def test_new_elements_created_when_missing():
    m = freeze({})
    assert m.transform(['foo', 'bar', 'baz'], 7) == {'foo': {'bar': {'baz': 7}}}

def test_mixed_vector_and_map():
    m = freeze({'foo': [1, 2, 3]})
    assert m.transform(['foo', 1], 5) == freeze({'foo': [1, 5, 3]})

def test_vector_predicate_callable_command():
    v = freeze([1, 2, 3, 4, 5])
    assert v.transform([lambda i: 0 < i < 4], inc) == freeze(freeze([1, 3, 4, 5, 5]))

def test_vector_insert_map_one_step_beyond_end():
    v = freeze([1, 2])
    assert v.transform([2, 'foo'], 3) == freeze([1, 2, {'foo': 3}])

def test_multiple_transformations():
    v = freeze([1, 2])
    assert v.transform([2, 'foo'], 3, [2, 'foo'], inc) == freeze([1, 2, {'foo': 4}])

def test_no_transformation_returns_the_same_structure():
    v = freeze([{'foo': 1}, {'bar': 2}])
    assert v.transform([ny, ny], lambda x: x) is v

def test_discard_multiple_elements_in_pvector():
    assert freeze([0, 1, 2, 3, 4]).transform([lambda i: i % 2], discard) == freeze([0, 2, 4])
<|endoftext|>


<commit_before>import web
import requests
import httplib, base64
urls = (
    '/(.*)', 'index'
)

render = web.template.render('templates/')

class index:
    def GET(self, user_id):
        pass

class twitter:
    def __init__(self):
        self.bearer = self.__get_bearer()
    #@staticmethod
    def __get_bearer(self):

        encode_string = "{!s}:{!s}".format("2nTaA2Uny9mZIOWrtNnO9Q","dqzpWUxiFjfNvrI2hNSNlKnPMSG8cMVU0e2yw9SDAM")
        encoded_string = base64.b64encode(encode_string)
        headers = {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "Authorization": "Basic {!s}".format(encoded_string)}
        body = "grant_type=client_credentials"

        r = requests.post("https://api.twitter.com/oauth2/token", data=body, headers=headers,verify=False)
        return r.json()['access_token']

    def get_friends(self, screen_name):
        """
        Returns: A list of ids that are this person's friends
        include an Authorization header with the value of Bearer <base64 bearer token value from step 2>
        """
        headers = {"Authorization": "Bearer {!s}".format(self.bearer)}
        parameters = {"screen_name": screen_name}
        r = requests.get("https://api.twitter.com/1.1/friends/ids.json", params=parameters,headers=headers,verify=False)
        if r.status_code != requests.codes.ok:
            raise 'Error in API call. Twitter returned:' + r.text
        return r.json()['ids']

if __name__ == "__main__":
    #app = web.application(urls, globals())
    #app.run()
    twit = twitter()
    print twit.get_friends('himerzi')
<commit_msg>added some kind of exception handling<commit_after>import web
import requests
import httplib, base64
urls = (
    '/(.*)', 'index'
)

render = web.template.render('templates/')

class index:
    def GET(self, user_id):
        pass

class twitter:
    def __init__(self):
        self.bearer = self.__get_bearer()
    #@staticmethod
    def __get_bearer(self):

        encode_string = "{!s}:{!s}".format("2nTaA2Uny9mZIOWrtNnO9Q","dqzpWUxiFjfNvrI2hNSNlKnPMSG8cMVU0e2yw9SDAM")
        encoded_string = base64.b64encode(encode_string)
        headers = {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "Authorization": "Basic {!s}".format(encoded_string)}
        body = "grant_type=client_credentials"

        r = requests.post("https://api.twitter.com/oauth2/token", data=body, headers=headers,verify=False)
        return r.json()['access_token']

    def get_friends(self, screen_name):
        """
        Returns: A list of ids that are this person's friends
        include an Authorization header with the value of Bearer <base64 bearer token value from step 2>
        """
        headers = {"Authorization": "Bearer {!s}".format(self.bearer)}
        parameters = {"screen_name": screen_name}
        r = requests.get("https://api.twitter.com/1.1/friends/ids.json", params=parameters,headers=headers,verify=False)
        if r.status_code != requests.codes.ok:
            raise Exception('Error in API call. Twitter returned:' + r.text)
        return r.json()['ids']

if __name__ == "__main__":
    #app = web.application(urls, globals())
    #app.run()
    twit = twitter()
    print twit.get_friends('himerzi')
<|endoftext|>


<commit_before><commit_msg>add contact details for find leg page<commit_after><|endoftext|>


<commit_before>from __future__ import unicode_literals

from multiprocessing import Process
import socket
import subprocess

from fabric.api import env, hosts, task
from fabric.colors import green
from fabric.utils import puts

from fh_fablib import run_local, require_services

def own_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(('feinheit.ch', 80))
    return s.getsockname()[0]

@task(default=True)
@hosts('')
@require_services
def dev(host='127.0.0.1', port=8000):
    """Runs the development server, SCSS watcher and backend services if they
    are not running already"""
    if host == 'net':
        host = own_ip()
        puts(green(
            'Starting dev server on http://%s:%s/' % (host, port),
            bold=True))

    jobs = [
        lambda: run_local(
            'venv/bin/python -Wonce manage.py runserver 0.0.0.0:%s' % (
                port,
            ),
        ),
        lambda: run_local('HOST=%s yarn run dev' % host),
    ]
    jobs = [Process(target=j) for j in jobs]
    [j.start() for j in jobs]
    [j.join() for j in jobs]

@task
@hosts('')
def makemessages():
    """Wrapper around the ``makemessages`` management command which excludes
    dependencies (virtualenv, bower components, node modules)"""
    run_local(
        'venv/bin/python manage.py makemessages -a'
        ' -i app/cms'
        ' -i bower_components'
        ' -i node_modules'
        ' -i venv')

    """Also statici18n ``makemessages`` command will be executed"""
    run_local(
        'venv/bin/python manage.py makemessages -d djangojs -a'
        ' -e jsx,js'
        ' -i app/static/jsi18n'
        ' -i app/cms'
        ' -i app/templates/elephantblog'
        ' -i bower_components'
        ' -i node_modules'
        ' -i venv')

@task
@hosts('')
def compilemessages():
    """Wrapper around ``compilemessages`` which does not descend into
    venv"""
    run_local(
        '. venv/bin/activate && for dir in '
        '$(find . -name venv -prune -or -name locale -print)'
        '; do (cd $dir; cd ..; django-admin.py compilemessages); done')

@task
@hosts('')
@require_services
def services():
    """Starts all required background services"""
    pass

@task
@hosts('')
def kill():
    """Send SIGTERM to postgres and redis-server"""
    subprocess.call(
        "ps -ef | awk '/(postgres|redis)/ {print $2}' | xargs kill",
        shell=True)

@task(aliases=['prettier'])
@hosts('')
def prettify():
    """Prettifies JS and SCSS code using prettier"""
    for cmd in env['box_prettify']:
        run_local(cmd)
<commit_msg>Always print dev server URL<commit_after>from __future__ import unicode_literals

from multiprocessing import Process
import socket
import subprocess

from fabric.api import env, hosts, task
from fabric.colors import green
from fabric.utils import puts

from fh_fablib import run_local, require_services

def own_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(('feinheit.ch', 80))
    return s.getsockname()[0]

@task(default=True)
@hosts('')
@require_services
def dev(host='127.0.0.1', port=8000):
    """Runs the development server, SCSS watcher and backend services if they
    are not running already"""
    if host == 'net':
        host = own_ip()

    puts(green(
        'Starting dev server on http://%s:%s/' % (host, port),
        bold=True))

    jobs = [
        lambda: run_local(
            'venv/bin/python -Wonce manage.py runserver 0.0.0.0:%s' % (
                port,
            ),
        ),
        lambda: run_local('HOST=%s yarn run dev' % host),
    ]
    jobs = [Process(target=j) for j in jobs]
    [j.start() for j in jobs]
    [j.join() for j in jobs]

@task
@hosts('')
def makemessages():
    """Wrapper around the ``makemessages`` management command which excludes
    dependencies (virtualenv, bower components, node modules)"""
    run_local(
        'venv/bin/python manage.py makemessages -a'
        ' -i app/cms'
        ' -i bower_components'
        ' -i node_modules'
        ' -i venv')

    """Also statici18n ``makemessages`` command will be executed"""
    run_local(
        'venv/bin/python manage.py makemessages -d djangojs -a'
        ' -e jsx,js'
        ' -i app/static/jsi18n'
        ' -i app/cms'
        ' -i app/templates/elephantblog'
        ' -i bower_components'
        ' -i node_modules'
        ' -i venv')

@task
@hosts('')
def compilemessages():
    """Wrapper around ``compilemessages`` which does not descend into
    venv"""
    run_local(
        '. venv/bin/activate && for dir in '
        '$(find . -name venv -prune -or -name locale -print)'
        '; do (cd $dir; cd ..; django-admin.py compilemessages); done')

@task
@hosts('')
@require_services
def services():
    """Starts all required background services"""
    pass

@task
@hosts('')
def kill():
    """Send SIGTERM to postgres and redis-server"""
    subprocess.call(
        "ps -ef | awk '/(postgres|redis)/ {print $2}' | xargs kill",
        shell=True)

@task(aliases=['prettier'])
@hosts('')
def prettify():
    """Prettifies JS and SCSS code using prettier"""
    for cmd in env['box_prettify']:
        run_local(cmd)
<|endoftext|>


<commit_before># Copyright 2018 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark.
#
# 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.

"""Provide a class for medium definitions."""

import json
import logging

from goodtables import validate
from importlib_resources import open_text

import memote.experimental.schemata
# The following import is necessary in order to register the custom check.
from memote.experimental.checks import UnknownIdentifier  # noqa: F401
from memote.experimental.tabular import read_tabular

__all__ = ("ExperimentalBase",)

LOGGER = logging.getLogger(__name__)

class ExperimentalBase(object):
    """Represent a specific medium condition."""

    SCHEMA = None
    TRUTHY = {"true", "True", "TRUE", "1", "yes", "Yes", "YES"}

    def __init__(self, identifier, obj, filename, **kwargs):
        """
        Initialize a medium.

        Parameters
        ----------
        identifier : str
        obj : dict
        filename : str or pathlib.Path
            The full file path. May be a compressed file.
        kwargs

        """
        super(ExperimentalBase, self).__init__(**kwargs)
        self.id = identifier
        self.label = obj.get("label")
        if self.label is None:
            self.label = ""
        self.filename = filename
        self.data = None
        self.schema = None

    def load(self, dtype_conversion=None):
        """
        Load the data table and corresponding validation schema.

        Parameters
        ----------
        dtype_conversion : dict
            Column names as keys and corresponding type for loading the data.
            Please take a look at the `pandas documentation
            <https://pandas.pydata.org/pandas-docs/stable/io.html#specifying-column-data-types>`__
            for detailed explanations.

        """
        self.data = read_tabular(self.filename, dtype_conversion)
        with open_text(
            memote.experimental.schemata, self.SCHEMA, encoding="utf-8"
        ) as file_handle:
            self.schema = json.load(file_handle)

    def validate(self, model, checks=None):
        """Use a defined schema to validate the given table."""
        if checks is None:
            checks = []
        records = self.data.to_dict("records")
        self.evaluate_report(
            validate(
                records,
                headers=list(records[0]),
                preset="table",
                schema=self.schema,
                order_fields=True,
                checks=checks,
            )
        )

    @staticmethod
    def evaluate_report(report):
        """Iterate over validation errors."""
        if report["valid"]:
            return
        for warn in report["warnings"]:
            LOGGER.warning(warn)
        # We only ever test one table at a time.
        for err in report["tables"][0]["errors"]:
            LOGGER.error(err["message"])
        raise ValueError("Invalid data file. Please see errors above.")
<commit_msg>style: make isort and black happy<commit_after># Copyright 2018 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark.
#
# 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.

"""Provide a class for medium definitions."""

import json
import logging

from goodtables import validate
from importlib_resources import open_text

# Importing the checks is necessary in order to register them.
import memote.experimental.schemata
from memote.experimental.checks import UnknownIdentifier  # noqa: F401
from memote.experimental.tabular import read_tabular

__all__ = ("ExperimentalBase",)

LOGGER = logging.getLogger(__name__)

class ExperimentalBase(object):
    """Represent a specific medium condition."""

    SCHEMA = None
    TRUTHY = {"true", "True", "TRUE", "1", "yes", "Yes", "YES"}

    def __init__(self, identifier, obj, filename, **kwargs):
        """
        Initialize a medium.

        Parameters
        ----------
        identifier : str
        obj : dict
        filename : str or pathlib.Path
            The full file path. May be a compressed file.
        kwargs

        """
        super(ExperimentalBase, self).__init__(**kwargs)
        self.id = identifier
        self.label = obj.get("label")
        if self.label is None:
            self.label = ""
        self.filename = filename
        self.data = None
        self.schema = None

    def load(self, dtype_conversion=None):
        """
        Load the data table and corresponding validation schema.

        Parameters
        ----------
        dtype_conversion : dict
            Column names as keys and corresponding type for loading the data.
            Please take a look at the `pandas documentation
            <https://pandas.pydata.org/pandas-docs/stable/io.html#specifying-column-data-types>`__
            for detailed explanations.

        """
        self.data = read_tabular(self.filename, dtype_conversion)
        with open_text(
            memote.experimental.schemata, self.SCHEMA, encoding="utf-8"
        ) as file_handle:
            self.schema = json.load(file_handle)

    def validate(self, model, checks=None):
        """Use a defined schema to validate the given table."""
        if checks is None:
            checks = []
        records = self.data.to_dict("records")
        self.evaluate_report(
            validate(
                records,
                headers=list(records[0]),
                preset="table",
                schema=self.schema,
                order_fields=True,
                checks=checks,
            )
        )

    @staticmethod
    def evaluate_report(report):
        """Iterate over validation errors."""
        if report["valid"]:
            return
        for warn in report["warnings"]:
            LOGGER.warning(warn)
        # We only ever test one table at a time.
        for err in report["tables"][0]["errors"]:
            LOGGER.error(err["message"])
        raise ValueError("Invalid data file. Please see errors above.")
<|endoftext|>


<commit_before>import maec_bundle_3_0 as maecbundle
import cybox.win_executable_file_object_1_3 as cybox_win_executable_file_object

class win_executable_file_object:
    def __init__(self, id):
        self.id = id

    def build_object(self, win_executable_file_attributes):
        cybox_object = maecbundle.cybox_core_1_0.AssociatedObjectType(id=self.generator.generate_obj_id(), type_='File')
        win_executable_file_obj = cybox_win_executable_file_object.WindowsExecutableFileObjectType()
        win_executable_file_obj.set_anyAttributes_({'xsi:type' : 'WinExecutableFileObj:WindowsExecutableFileObjectType'})
        pe_attributes = cybox_win_executable_file_object.PEAttributesType()

        for key, value in win_executable_file_attributes.items():
            if key.lower() == 'peak_code_entropy' and self.__value_test(value):
                entropytype = cybox_win_executable_file_object.EntropyType()
                for entropy_key, entropy_value in value.items():
                    if entropy_key.lower() == 'value' and self.__value_test(entropy_value):
                        entropytype.set_Value(maecbundle.cybox_common_types_1_0.FloatObjectAttributeType(datatype='Float',valueOf_=entropy_value))
                    elif entropy_key.lower() == 'min' and self.__value_test(entropy_value):
                        entropytype.set_Min(maecbundle.cybox_common_types_1_0.FloatObjectAttributeType(datatype='Float',valueOf_=entropy_value))
                    elif entropy_key.lower() == 'max' and self.__value_test(entropy_value):
                        entropytype.set_Max(maecbundle.cybox_common_types_1_0.FloatObjectAttributeType(datatype='Float',valueOf_=entropy_value))
                if entropytype.hasContent_():
                    win_executable_file_obj.set_Peak_Code_Entropy(entropytype)
            elif key.lower() == 'pe_attributes' and self.__value_test(value):
                pe_attributes = cybox_win_executable_file_object.PEAttributesType()
                for pe_attributes_key, pe_attributes_value in value.items():
                    if pe_attributes_key.lower() == 'base_address' and self.__value_test(pe_attributes_value):
                        pe_attributes.set_Base_Address(maecbundle.cybox_common_types_1_0.HexBinaryObjectAttributeType(datatype='hexBinary',valueOf_=pe_attributes_value))
                    elif pe_attributes_key.lower() == 'exports' and self.__value_test(pe_attributes_value):
                        exports = cybox_win_executable_file_object.PEExportsType()
                        for export_key, export_value in pe_attributes_value.items():
                            if export_key.lower() == 'exported_functions' and self.__value_test(export_value):
                                exported_functions = cybox_win_executable_file_object.PEExportedFunctionsType()
                                for exported_function in export_value:
                                    xported_function = cybox_win_executable_file_object.PEExportedFunctionType()
                                    for exported_function_key, exported_function_value in exported_function.items():
                                        if exported_function_key.lower() == 'function_name' and self.__value_test(exported_function_value):
                                            xported_function.set_Function_Name(maecbundle.cybox_common_types_1_0.StringObjectAttributeType(datatype='String',valueOf_=maecbundle.quote_xml(exported_function_value)))
                                        elif exported_function_key.lower() == 'entry_point' and self.__value_test(exported_function_value):
                                            xported_function.set_Entry_Point(maecbundle.cybox_common_types_1_0.HexBinaryObjectAttributeType(datatype='hexBinary',valueOf_=exported_function_value))
                                        elif exported_function_key.lower() == 'ordinal' and self.__value_test(exported_function_value):
                                            xported_function.set_Ordinal(maecbundle.cybox_common_types_1_0.NonNegativeIntegerObjectAttributeType(datatype='NonNegativeInteger',valueOf_=exported_function_value))
                                    if xported_function.hasContent_():
                                        exported_functions.add_Exported_Function(xported_function)
                                if exported_functions.hasContent_():
                                    exports.set_Exported_Functions(exported_functions)
                            elif export_key.lower() == 'exports_time_stamp' and self.__value_test(export_value):
                                exports.set_Exports_Time_stamp(maecbundle.cybox_common_types_1_0.DateTimeObjectAttributeType(datatype='DateTime',valueOf_=exported_function_value))
                            elif export_key.lower() == 'number_of_addresses' and self.__value_test(export_value):
                                exports.set_Number_Of_Addresses(maecbundle.cybox_common_types_1_0.LongObjectAttributeType(datatype='Long',valueOf_=exported_function_value))
                            elif export_key.lower() == 'number_of_names' and self.__value_test(export_value):
                                exports.set_Number_Of_Names(maecbundle.cybox_common_types_1_0.LongObjectAttributeType(datatype='Long',valueOf_=exported_function_value))
                        if exports.hasContent_():
                            pe_attributes.set_Exports(exports)
            elif key == 'association':
                cybox_object.set_association_type(value)

        if pe_attributes.hasContent_():
            win_executable_file_obj.set_PE_Attributes(pe_attributes)
        if win_executable_file_obj.hasContent_():
            cybox_object.set_Defined_Object(win_executable_file_obj)

        return cybox_object<commit_msg>Updated win_executable_file_object to encompass to conform with current naming/style conventions<commit_after>import cybox.utils as utils
import cybox.bindings.cybox_common_types_binding_1_0 as common_types_binding_binding
import cybox.bindings.win_executable_file_object_1_3 as win_executable_file_binding
from cybox.common.baseobjectattribute import baseobjectattribute
from cybox.objects.win_file_object import Win_File

class Win_Executable_File:
    def __init__(self):
        pass

    @classmethod
    def object_from_dict(cls, win_executable_file_dict):
        """Create the Win Executable File Object object representation from an input dictionary"""
        win_executable_file_obj = Win_File.object_from_dict(win_executable_file_dict, win_executable_file_binding.WindowsExecutableFileObjectType())
        win_executable_file_obj.set_anyAttributes_({'xsi:type' : 'WinExecutableFileObj:WindowsExecutableFileObjectType'})

        for key, value in win_executable_file_dict.items():
            if key == 'peak_code_entropy':
                entropy_obj = win_executable_file_binding.EntropyType()
                for entropy_key, entropy_value in value.items():
                    if entropy_key == 'value' and utils.test_value(entropy_value):
                        entropy_obj.set_Value(baseobjectattribute.object_from_dict(common_types_binding.FloatObjectAttributeType(datatype='Float'), entropy_value))
                    elif entropy_key == 'min' and utils.test_value(entropy_value):
                        entropy_obj.set_Min(baseobjectattribute.object_from_dict(common_types_binding.FloatObjectAttributeType(datatype='Float'), entropy_value))
                    elif entropy_key == 'max' and utils.test_value(entropy_value):
                        entropy_obj.set_Max(baseobjectattribute.object_from_dict(common_types_binding.FloatObjectAttributeType(datatype='Float'), entropy_value))
                if entropy_obj.hasContent_():
                    win_executable_file_obj.set_Peak_Code_Entropy(entropy_obj)
            elif key == 'pe_attributes':
                pe_attributes_obj = cls.__pe_attributes_obj_from_dict(value)
                if pe_attributes_obj.hasContent_() : win_executable_file_obj.set_PE_Attributes(pe_attributes_obj)

        return win_executable_file_obj

    @classmethod
    def dict_from_object(cls, win_executable_file_obj):
        """Parse and return a dictionary for a Win Executable File Object object"""
        win_executable_file_dict = {}
        if win_executable_file_obj.get_Peak_Code_Entropy() is not None:
            entropy_obj = win_executable_file_obj.get_Peak_Code_Entropy()
            entropy_dict = {}
            if entropy_obj.get_Value() is not None: entropy_dict['value'] = baseobjectattribute.dict_from_object(entropy_obj.get_Value())
            if entropy_obj.get_Max() is not None: entropy_dict['max'] = baseobjectattribute.dict_from_object(entropy_obj.get_Max())
            if entropy_obj.get_Min() is not None: entropy_dict['min'] = baseobjectattribute.dict_from_object(entropy_obj.get_Min())
            win_executable_file_dict['peak_code_entropy'] = entropy_dict
        if win_executable_file_obj.get_PE_Attributes() is not None: win_executable_file_dict['pe_attributes'] = cls.__pe_attributes_dict_from_obj(win_executable_file_obj.get_PE_Attributes())
        return win_executable_file_dict

    @classmethod
    def __pe_attributes_dict_from_obj(cls, pe_attributes_obj):
        pe_attributes_dict = {}
        if pe_attributes_obj.get_Base_Address() is not None: pe_attributes_dict['base_address'] = baseobjectattribute.dict_from_object(pe_attributes_obj.get_Base_Address())
        if pe_attributes_obj.get_Exports() is not None: pe_attributes_dict['exports'] = cls.__exports_dict_from_obj(pe_attributes_obj.get_Exports())
        return pe_attributes_dict

    @classmethod
    def __pe_attributes_obj_from_dict(cls, pe_attributes_dict):
        pe_attributes_obj = win_executable_file_binding.PEAttributesType()
        for pe_attributes_key, pe_attributes_value in value.items():
            if pe_attributes_key == 'base_address' and utils.test_value(pe_attributes_value):
                pe_attributes.set_Base_Address(baseobjectattribute.object_from_dict(common_types_binding.HexBinaryObjectAttributeType(datatype='hexBinary'), pe_attributes_value))
            elif pe_attributes_key == 'exports':
                exports_obj = cls.__exports_obj_from_dict(value)
                if exports_obj.hasContent_() : pe_attributes.set_Exports(exports_obj)
        return pe_attributes_obj

    @classmethod
    def __exports_obj_from_dict(cls, exports_dict):
        exports_obj = win_executable_file_binding.PEExportsType()
        for key, value in exports_dict.items():
            if key == 'exported_functions':
                exported_functions = win_executable_file_binding.PEExportedFunctionsType()
                for exported_function in value:
                    xported_function = win_executable_file_binding.PEExportedFunctionType()
                    for exported_function_key, exported_function_value in exported_function.items():
                        if exported_function_key == 'function_name' and utils.test_value(exported_function_value):
                            xported_function.set_Function_Name(baseobjectattribute.object_from_dict(common_types_binding.StringObjectAttributeType(datatype='String'), exported_function_value))
                        elif exported_function_key == 'entry_point' and utils.test_value(exported_function_value):
                            xported_function.set_Entry_Point(cbaseobjectattribute.object_from_dict(ommon_types_binding.HexBinaryObjectAttributeType(datatype='hexBinary'), exported_function_value))
                        elif exported_function_key == 'ordinal' and utils.test_value(exported_function_value):
                            xported_function.set_Ordinal(baseobjectattribute.object_from_dict(common_types_binding.NonNegativeIntegerObjectAttributeType(datatype='NonNegativeInteger'), exported_function_value))
                    if xported_function.hasContent_():
                        exported_functions.add_Exported_Function(xported_function)
                if exported_functions.hasContent_():
                    exports.set_Exported_Functions(exported_functions)
            elif key == 'exports_time_stamp' and utils.test_value(value):
                exports.set_Exports_Time_stamp(baseobjectattribute.object_from_dict(common_types_binding.DateTimeObjectAttributeType(datatype='DateTime'), exported_function_value))
            elif key == 'number_of_addresses' and utils.test_value(value):
                exports.set_Number_Of_Addresses(baseobjectattribute.object_from_dict(common_types_binding.LongObjectAttributeType(datatype='Long'), exported_function_value))
            elif key == 'number_of_names' and utils.test_value(value):
                exports.set_Number_Of_Names(baseobjectattribute.object_from_dict(common_types_binding.LongObjectAttributeType(datatype='Long'), exported_function_value))
        return exports_obj

    @classmethod
    def __exports_dict_from_obj(cls, exports_obj):
        exports_dict = {}
        if exports_obj.get_Exported_Functions() is not None:
            exported_functions = []
            for exported_function_obj in exports_obj.get_Exported_Functions().get_Exported_Function():
                exported_function_dict = {}
                if exported_function_obj.get_Function_Name() is not None: exported_function_dict['function_name'] = baseobjectattribute.dict_from_object(exported_function_obj.get_Function_Name())
                if exported_function_obj.get_Entry_Point() is not None: exported_function_dict['entry_point'] = baseobjectattribute.dict_from_object(exported_function_obj.get_Entry_Point())
                if exported_function_obj.get_Ordinal() is not None: exported_function_dict['ordinal'] = baseobjectattribute.dict_from_object(exported_function_obj.get_Ordinal())
                exported_functions.append(exported_function_dict)
            exports_dict['exported_functions'] = exported_functions
        if exports_obj.get_Exports_Time_Stamp() is not None: exports_dict['exports_time_stamp'] = baseobjectattribute.dict_from_object(exports_obj.get_Exports_Time_Stamp())
        if exports_obj.get_Number_Of_Addresses() is not None: exports_dict['number_of_addresses'] = baseobjectattribute.dict_from_object(exports_obj.get_Number_Of_Addresses())
        if exports_obj.get_Number_Of_Names() is not None: exports_dict['number_of_names'] = baseobjectattribute.dict_from_object(exports_obj.get_Number_Of_Names())
        return exports_dict<|endoftext|>


<commit_before>from __future__ import absolute_import
from __future__ import unicode_literals

from dateutil.rrule import rrule, DAILY, MO, TU, WE, TH, FR, SA
from sqlagg.columns import SimpleColumn
from sqlagg.filters import EQ
from sqlagg.sorting import OrderBy
from django.db.models.aggregates import Count

from corehq.apps.reports.datatables import DataTablesHeader, DataTablesColumn
from corehq.apps.reports.generic import GenericTabularReport
from corehq.apps.reports.sqlreport import SqlData, DatabaseColumn
from corehq.apps.reports.standard import CustomProjectReport, DatespanMixin
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _

from corehq.apps.reports.filters.dates import DatespanFilter
from corehq.apps.sms.models import SMS, INCOMING, MessagingSubEvent, MessagingEvent
from corehq.apps.userreports.util import get_table_name
from custom.abt.reports.filters import UsernameFilter, CountryFilter, LevelOneFilter, LevelTwoFilter, \
    LevelThreeFilter, LevelFourFilter, SubmissionStatusFilter

class LatePMTUsers(SqlData):
    engine_id = 'default'

    @property
    def table_name(self):
        return get_table_name(self.domain, "static-late-pmt")

    @property
    def domain(self):
        return self.config['domain']

    @property
    def filters(self):
        filters = []
        filter_fields = [
            'country',
            'level_1',
            'level_2',
            'level_3',
            'level_4',
        ]
        for filter_field in filter_fields:
            if filter_field in self.config and self.config[filter_field]:
                filters.append(EQ(filter_field, filter_field))
        if 'user_id' in self.config and self.config['user_id']:
            filters.append(EQ('doc_id', 'user_id'))
        return filters

    @property
    def group_by(self):
        return [
            'user_id',
            'username',
            'phone_number',
            'country',
            'level_1',
            'level_2',
            'level_3',
            'level_4',
        ]

    @property
    def order_by(self):
        return [OrderBy('username')]

    @property
    def columns(self):
        return [
            DatabaseColumn('user_id', SimpleColumn('doc_id', alias='user_id')),
            DatabaseColumn('username', SimpleColumn('username')),
            DatabaseColumn('phone_number', SimpleColumn('phone_number')),
            DatabaseColumn('country', SimpleColumn('country')),
            DatabaseColumn('level_1', SimpleColumn('level_1')),
            DatabaseColumn('level_2', SimpleColumn('level_2')),
            DatabaseColumn('level_3', SimpleColumn('level_3')),
            DatabaseColumn('level_4', SimpleColumn('level_4'))
        ]

class LatePmtReport(GenericTabularReport, CustomProjectReport, DatespanMixin):
    report_title = "Late PMT"
    slug = 'late_pmt'
    name = "Late PMT"

    fields = [
        DatespanFilter,
        UsernameFilter,
        CountryFilter,
        LevelOneFilter,
        LevelTwoFilter,
        LevelThreeFilter,
        LevelFourFilter,
        SubmissionStatusFilter,
    ]

    @property
    def report_config(self):
        return {
            'domain': self.domain,
            'startdate': self.startdate,
            'enddate': self.enddate,
            'user_id': self.request.GET.get('user_id', ''),
            'country': self.request.GET.get('country', ''),
            'level_1': self.request.GET.get('level_1', ''),
            'level_2': self.request.GET.get('level_2', ''),
            'level_3': self.request.GET.get('level_3', ''),
            'level_4': self.request.GET.get('level_4', ''),
            'submission_status': self.request.GET.get('submission_status', '')
        }

    @property
    def startdate(self):
        return self.request.datespan.startdate

    @property
    def enddate(self):
        return self.request.datespan.end_of_end_day

    @property
    def headers(self):
        return DataTablesHeader(
            DataTablesColumn(_("Date Submitted")),
            DataTablesColumn(_("Username")),
            DataTablesColumn(_("Phone Number")),
            DataTablesColumn(_("Country")),
            DataTablesColumn(_("Level 1")),
            DataTablesColumn(_("Level 2")),
            DataTablesColumn(_("Level 3")),
            DataTablesColumn(_("Level 4")),
            DataTablesColumn(_("Submission Status")),
        )

    @cached_property
    def get_users_in_group_a(self):
        data = SMS.objects.filter(
            domain=self.domain,
            couch_recipient_doc_type='CommCareUser',
            direction=INCOMING,
            couch_recipient__in=self.get_user_ids,
            date__range=(
                self.startdate,
                self.enddate
            )
        ).values('date', 'couch_recipient').annotate(
            number_of_sms=Count('couch_recipient')
        )
        return [(user['date'].date(), user['couch_recipient']) for user in data]

    @cached_property
    def get_users_in_group_b(self):
        data = MessagingSubEvent.objects.filter(
            parent__domain=self.domain,
            parent__recipient_type=MessagingEvent.RECIPIENT_MOBILE_WORKER,
            parent__source=MessagingEvent.SOURCE_KEYWORD,
            xforms_session__isnull=False,
            xforms_session__submission_id__isnull=False,
            recipient_id__in=self.get_user_ids,
            date__range=(
                self.startdate,
                self.enddate
            )
        ).values('date', 'recipient_id')
        return [(user['date'].date(), user['recipient_id']) for user in data]

    @cached_property
    def get_user_ids(self):
        return [user['user_id'] for user in self.get_users]

    @cached_property
    def get_users(self):
        return LatePMTUsers(config=self.report_config).get_data()

    @property
    def rows(self):
        def _to_report_format(date, user, group):
            return [
                date.strftime("%Y-%m-%d"),
                user['username'].split('@')[0],
                user['phone_number'],
                user['country'],
                user['level_1'],
                user['level_2'],
                user['level_3'],
                user['level_4'],
                group
            ]

        def not_in_group(key, group):
            return key not in group

        users = self.get_users
        dates = rrule(
            DAILY,
            dtstart=self.startdate,
            until=self.enddate,
            byweekday=(MO, TU, WE, TH, FR, SA)
        )
        rows = []
        sub_status = self.report_config['submission_status']
        if users:
            group_a = self.get_users_in_group_a
            group_b = self.get_users_in_group_b

            for date in dates:
                for user in users:
                    key = (date.date(), user['user_id'])
                    if not_in_group(key, group_a) and sub_status != 'group_b':
                        group = 'No PMT data Submitted'
                    elif not_in_group(key, group_b) and not not_in_group(key, group_a) and sub_status != 'group_a':
                        group = 'Incorrect PMT data Submitted'
                    else:
                        continue
                    rows.append(_to_report_format(date, user, group))
        return rows
<commit_msg>[VectorLink] don't show data before welcome sms<commit_after>from __future__ import absolute_import
from __future__ import unicode_literals

from dateutil.rrule import rrule, DAILY, MO, TU, WE, TH, FR, SA
from sqlagg.columns import SimpleColumn
from sqlagg.filters import EQ
from sqlagg.sorting import OrderBy
from django.db.models.aggregates import Count

from corehq.apps.reports.datatables import DataTablesHeader, DataTablesColumn
from corehq.apps.reports.generic import GenericTabularReport
from corehq.apps.reports.sqlreport import SqlData, DatabaseColumn
from corehq.apps.reports.standard import CustomProjectReport, DatespanMixin
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _

from corehq.apps.reports.filters.dates import DatespanFilter
from corehq.apps.sms.models import SMS, INCOMING, MessagingSubEvent, MessagingEvent, OUTGOING
from corehq.apps.userreports.util import get_table_name
from custom.abt.reports.filters import UsernameFilter, CountryFilter, LevelOneFilter, LevelTwoFilter, \
    LevelThreeFilter, LevelFourFilter, SubmissionStatusFilter

class LatePMTUsers(SqlData):
    engine_id = 'default'

    @property
    def table_name(self):
        return get_table_name(self.domain, "static-late-pmt")

    @property
    def domain(self):
        return self.config['domain']

    @property
    def filters(self):
        filters = []
        filter_fields = [
            'country',
            'level_1',
            'level_2',
            'level_3',
            'level_4',
        ]
        for filter_field in filter_fields:
            if filter_field in self.config and self.config[filter_field]:
                filters.append(EQ(filter_field, filter_field))
        if 'user_id' in self.config and self.config['user_id']:
            filters.append(EQ('doc_id', 'user_id'))
        return filters

    @property
    def group_by(self):
        return [
            'user_id',
            'username',
            'phone_number',
            'country',
            'level_1',
            'level_2',
            'level_3',
            'level_4',
        ]

    @property
    def order_by(self):
        return [OrderBy('username')]

    @property
    def columns(self):
        return [
            DatabaseColumn('user_id', SimpleColumn('doc_id', alias='user_id')),
            DatabaseColumn('username', SimpleColumn('username')),
            DatabaseColumn('phone_number', SimpleColumn('phone_number')),
            DatabaseColumn('country', SimpleColumn('country')),
            DatabaseColumn('level_1', SimpleColumn('level_1')),
            DatabaseColumn('level_2', SimpleColumn('level_2')),
            DatabaseColumn('level_3', SimpleColumn('level_3')),
            DatabaseColumn('level_4', SimpleColumn('level_4'))
        ]

class LatePmtReport(GenericTabularReport, CustomProjectReport, DatespanMixin):
    report_title = "Late PMT"
    slug = 'late_pmt'
    name = "Late PMT"

    fields = [
        DatespanFilter,
        UsernameFilter,
        CountryFilter,
        LevelOneFilter,
        LevelTwoFilter,
        LevelThreeFilter,
        LevelFourFilter,
        SubmissionStatusFilter,
    ]

    @property
    def report_config(self):
        return {
            'domain': self.domain,
            'startdate': self.startdate,
            'enddate': self.enddate,
            'user_id': self.request.GET.get('user_id', ''),
            'country': self.request.GET.get('country', ''),
            'level_1': self.request.GET.get('level_1', ''),
            'level_2': self.request.GET.get('level_2', ''),
            'level_3': self.request.GET.get('level_3', ''),
            'level_4': self.request.GET.get('level_4', ''),
            'submission_status': self.request.GET.get('submission_status', '')
        }

    @property
    def startdate(self):
        return self.request.datespan.startdate

    @property
    def enddate(self):
        return self.request.datespan.end_of_end_day

    @property
    def headers(self):
        return DataTablesHeader(
            DataTablesColumn(_("Date Submitted")),
            DataTablesColumn(_("Username")),
            DataTablesColumn(_("Phone Number")),
            DataTablesColumn(_("Country")),
            DataTablesColumn(_("Level 1")),
            DataTablesColumn(_("Level 2")),
            DataTablesColumn(_("Level 3")),
            DataTablesColumn(_("Level 4")),
            DataTablesColumn(_("Submission Status")),
        )

    @cached_property
    def welcome_smses(self):
        data = SMS.objects.filter(
            domain=self.domain,
            couch_recipient_doc_type='CommCareUser',
            couch_recipient__in=self.get_user_ids,
            direction=OUTGOING,
            text__startswith="Welcome to"
        ).values('date', 'couch_recipient')
        return {user['couch_recipient']: user['date'].date() for user in data}

    @cached_property
    def get_users_in_group_a(self):
        data = SMS.objects.filter(
            domain=self.domain,
            couch_recipient_doc_type='CommCareUser',
            direction=INCOMING,
            couch_recipient__in=self.get_user_ids,
            date__range=(
                self.startdate,
                self.enddate
            )
        ).values('date', 'couch_recipient').annotate(
            number_of_sms=Count('couch_recipient')
        )
        return [(user['date'].date(), user['couch_recipient']) for user in data]

    @cached_property
    def get_users_in_group_b(self):
        data = MessagingSubEvent.objects.filter(
            parent__domain=self.domain,
            parent__recipient_type=MessagingEvent.RECIPIENT_MOBILE_WORKER,
            parent__source=MessagingEvent.SOURCE_KEYWORD,
            xforms_session__isnull=False,
            xforms_session__submission_id__isnull=False,
            recipient_id__in=self.get_user_ids,
            date__range=(
                self.startdate,
                self.enddate
            )
        ).values('date', 'recipient_id')
        return [(user['date'].date(), user['recipient_id']) for user in data]

    @cached_property
    def get_user_ids(self):
        return [user['user_id'] for user in self.get_users]

    @cached_property
    def get_users(self):
        return LatePMTUsers(config=self.report_config).get_data()

    @property
    def rows(self):
        def _to_report_format(date, user, group):
            return [
                date.strftime("%Y-%m-%d"),
                user['username'].split('@')[0],
                user['phone_number'],
                user['country'],
                user['level_1'],
                user['level_2'],
                user['level_3'],
                user['level_4'],
                group
            ]

        def not_in_group(key, group):
            return key not in group

        users = self.get_users
        dates = rrule(
            DAILY,
            dtstart=self.startdate,
            until=self.enddate,
            byweekday=(MO, TU, WE, TH, FR, SA)
        )
        rows = []
        sub_status = self.report_config['submission_status']
        if users:
            group_a = self.get_users_in_group_a
            group_b = self.get_users_in_group_b

            for date in dates:
                for user in users:
                    key = (date.date(), user['user_id'])
                    welcome_sms_date = self.welcome_smses.get(user['user_id'])
                    if not welcome_sms_date or date.date() <= welcome_sms_date:
                        continue

                    if not_in_group(key, group_a) and sub_status != 'group_b':
                        group = 'No PMT data Submitted'
                    elif not_in_group(key, group_b) and not not_in_group(key, group_a) and sub_status != 'group_a':
                        group = 'Incorrect PMT data Submitted'
                    else:
                        continue
                    rows.append(_to_report_format(date, user, group))
        return rows
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    autodoc.tests.test_requests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Tests for `requests` client.

    :copyright: (c) 2014 Shinya Ohyanagi, All rights reserved.
    :license: BSD, see LICENSE for more details.
"""
import os
import shutil
from urllib.parse import urlencode
from requests import Request, Session, Response
from unittest import TestCase
from mock import patch
from autodoc import autodoc
from autodoc._compat import iteritems

root_path = os.path.dirname(os.path.abspath(__file__))
var_path = os.path.join(root_path, 'var')

os.environ['PYAUTODOC'] = '1'

def dummy_response(m, request, params, filename=None):
    response = Response()
    response.status_code = 200
    response.headers['Content-Type'] = 'application/json'
    response.request = request
    response.request.body = urlencode(params)
    response.request.path_url = '/'
    if filename is None:
        response._content = ''
    else:
        root_path = os.path.dirname(os.path.abspath(__file__))
        file_path = root_path + '/' + filename
        with open(file_path, 'r') as f:
            data = f.read()
            response._content = data

    m.return_value = response

class TestRequestsResponse(TestCase):
    def create_request(self, url, method, data=None, headers=None):
        request = Request(method, url, data=data, headers=headers)

        return request

    @patch('requests.sessions.Session.send')
    def send(self, request, params, file_path, m):
        dummy_response(m, request, params, file_path)

        session = Session()
        res = session.send(request.prepare())

        return res

    def setUp(self):
        self.root_path = root_path
        if os.path.exists(var_path):
            shutil.rmtree(var_path)
            os.mkdir(var_path)

    def test_parse_response(self):
        """ Should parse requests response. """
        params = {'message': 'foo'}
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='POST',
                                  data=params,
                                  headers=headers)

        res = self.send(req, params, 'data/post.json')
        autodoc.parse('POST /', res)

        var = {
            'describe': 'POST /',
            'describe_separators': '======',
            'target_url': 'http://localhost:5000/',
            'status_code': 200,
            'request': 'POST /',
            'response_body': '{"response": "create"}\n',
            'response_content_type': 'application/json',
            'params': '{\n  "message": "foo"\n}'
        }
        for k, v in iteritems(autodoc.vars[0]):
            self.assertEqual(v, var[k])

        autodoc.clear()

    def test_parse_responses(self):
        """ Should stack responses. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')
        autodoc.parse('GET /', res)
        autodoc.parse('GET /', res)
        var = {
            'response_content_type': 'application/json',
            'response_body': '{"response": "index"}\n',
            'describe': 'GET /',
            'request': 'GET /',
            'params': {},
            'status_code': 200,
            'target_url': 'http://localhost:5000/',
            'describe_separators': '====='
        }
        vars = [var, var]
        self.assertEqual(autodoc.vars, vars)
        autodoc.clear()

    def test_clear_responses(self):
        """ Should clear stacked WebTest responses. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')

        autodoc.parse('GET /', res)
        autodoc.parse('GET /', res)
        autodoc.clear()
        self.assertEqual(autodoc.vars, [])
<commit_msg>Fix import source.<commit_after># -*- coding: utf-8 -*-
"""
    autodoc.tests.test_requests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Tests for `requests` client.

    :copyright: (c) 2014 Shinya Ohyanagi, All rights reserved.
    :license: BSD, see LICENSE for more details.
"""
import os
import shutil
from requests import Request, Session, Response
from unittest import TestCase
from mock import patch
from autodoc import Autodoc
from autodoc._compat import iteritems, urlencode

root_path = os.path.dirname(os.path.abspath(__file__))
var_path = os.path.join(root_path, 'var')

os.environ['PYAUTODOC'] = '1'

def dummy_response(m, request, params, filename=None):
    response = Response()
    response.status_code = 200
    response.headers['Content-Type'] = 'application/json'
    response.request = request
    response.request.body = urlencode(params)
    response.request.path_url = '/'
    if filename is None:
        response._content = ''
    else:
        root_path = os.path.dirname(os.path.abspath(__file__))
        file_path = root_path + '/' + filename
        with open(file_path, 'r') as f:
            data = f.read()
            response._content = data

    m.return_value = response

class TestRequestsResponse(TestCase):
    def create_request(self, url, method, data=None, headers=None):
        request = Request(method, url, data=data, headers=headers)

        return request

    @patch('requests.sessions.Session.send')
    def send(self, request, params, file_path, m):
        dummy_response(m, request, params, file_path)

        session = Session()
        res = session.send(request.prepare())

        return res

    def setUp(self):
        self.root_path = root_path
        if os.path.exists(var_path):
            shutil.rmtree(var_path)
            os.mkdir(var_path)

    def test_parse_response(self):
        """ Should parse requests response. """
        params = {'message': 'foo'}
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='POST',
                                  data=params,
                                  headers=headers)

        res = self.send(req, params, 'data/post.json')
        autodoc.parse('POST /', res)

        var = {
            'describe': 'POST /',
            'describe_separators': '======',
            'target_url': 'http://localhost:5000/',
            'status_code': 200,
            'request': 'POST /',
            'response_body': '{"response": "create"}\n',
            'response_content_type': 'application/json',
            'params': '{\n  "message": "foo"\n}'
        }
        for k, v in iteritems(autodoc.vars[0]):
            self.assertEqual(v, var[k])

        autodoc.clear()

    def test_parse_responses(self):
        """ Should stack responses. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')
        autodoc.parse('GET /', res)
        autodoc.parse('GET /', res)
        var = {
            'response_content_type': 'application/json',
            'response_body': '{"response": "index"}\n',
            'describe': 'GET /',
            'request': 'GET /',
            'params': {},
            'status_code': 200,
            'target_url': 'http://localhost:5000/',
            'describe_separators': '====='
        }
        vars = [var, var]
        self.assertEqual(autodoc.vars, vars)
        autodoc.clear()

    def test_clear_responses(self):
        """ Should clear stacked WebTest responses. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')

        autodoc.parse('GET /', res)
        autodoc.parse('GET /', res)
        autodoc.clear()
        self.assertEqual(autodoc.vars, [])
<|endoftext|>


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

import nfc
import time
import signal
import sys
import sqlite3
import RPi.GPIO as GPIO
import syslog

def output(message):
    print(message)
    syslog.syslog(message)

def light_on(colour):
    colours = {'red': [1,0,0], 'green': [0,1,0], 'blue': [0,0,1],\
            'yellow': [1,1,0], 'cyan': [0,1,1], 'magenta': [1,0,1],\
            'white': [1,1,1], True: [1,1,1],\
            'black': [0,0,0], 'off': [0,0,0], False: [0,0,0], None: [0,0,0]}
    pins = {'red': 18, 'green': 23, 'blue': 24}
    if colour in colours:
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pins['red'], GPIO.OUT)
        GPIO.setup(pins['green'], GPIO.OUT)
        GPIO.setup(pins['blue'], GPIO.OUT)

        GPIO.output(pins['red'], colours[colour][0])
        GPIO.output(pins['green'], colours[colour][1])
        GPIO.output(pins['blue'], colours[colour][2])
    else:
        output("Not a valid colour.")

def clean_up():
    nfc.close(pnd)
    nfc.exit(context)
    conn.close()

def signal_handler(signal, frame):
    clean_up()
    sys.exit(0)

sleep_time = 2

# Ensure ^C doesn't corrupt/leave anything broken.
signal.signal(signal.SIGINT, signal_handler)

conn = sqlite3.connect('/var/db/assurpid/assurpid.db')
output('opened database successfully')

output('libnfc version: ', nfc.__version__)

context = nfc.init()
pnd = nfc.open(context)
if pnd is None:
    output('ERROR: Unable to detect RFID sensor (run this as root).')
    exit()

if nfc.initiator_init(pnd) < 0:
    nfc.perror(pnd, "nfc_initiator_init")
    output('ERROR: Unable to init RFID sensor.')
    exit()

output('NFC reader: %s opened' % nfc.device_get_name(pnd))

nmMifare = nfc.modulation()
nmMifare.nmt = nfc.NMT_ISO14443A
nmMifare.nbr = nfc.NBR_106

nt = nfc.target()

admin_switch = False

while True:
    if not admin_switch:
        GPIO.cleanup()

    output()

    # Wait for tag
    ret = nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt)

    # Convert raw uid to hex, then trim to length (2*nt.nti.nai.szUidLen)
    tag = ''.join(format(x, '02x') for x in nt.nti.nai.abtUid)[:2*nt.nti.nai.szUidLen]
    output('Found tag with UID of', tag)
    cursor = conn.cursor()
    cursor.execute("SELECT UID, ADMIN, ACCESS_ROOM1, INSIDE_ROOM1 FROM MAIN WHERE UID = ?", (tag,))
    details = cursor.fetchone()

    if details is None:
        output("Tag is unrecognised")
        light_on('red')
        if admin_switch:
            conn.execute("INSERT INTO MAIN(UID, ADMIN, ACCESS_ROOM1, INSIDE_ROOM1) VALUES(?, 0, 1, 0)", (tag,))
            conn.commit()
            admin_switch = False
            light_on('green')
            output("Added tag to database")
        time.sleep(sleep_time)
        continue
    else:
        output("Tag is recognised")
        if admin_switch and details[1] == 0:
            conn.execute("DELETE FROM MAIN WHERE UID = ?", (tag,))
            conn.commit()
            admin_switch = False
            light_on('red')
            output("Deleted tag from database")
            time.sleep(sleep_time)
            continue

    if details[1] == 1:
        output("Tag is admin.")
        light_on('magenta')
        admin_switch = True
        time.sleep(sleep_time)
        continue

    if details[3] == 1:
        output("Tag is inside Room 1.")
    else:
        output("Tag is outside Room 1.")

    if details[2] == 1:
        output("Tag can open door to Room 1.")
        light_on('green')
    else:
        output("Tag cannot open door to Room 1.")
        light_on('blue')

    if details[2] == 1 and details[3]:
        conn.execute("UPDATE MAIN SET INSIDE_ROOM1 = 0 WHERE UID = ?", (tag,))
        output("Door opened from inside, tag now outside.")

    if details[2] == 1 and details[3] == 0:
        conn.execute("UPDATE MAIN SET INSIDE_ROOM1 = 1 WHERE UID = ?", (tag,))
        output("Door opened from outside, tag now inside.")
    time.sleep(sleep_time)

clean_up()
sys.exit(0)
<commit_msg>fix output()<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import nfc
import time
import signal
import sys
import sqlite3
import RPi.GPIO as GPIO
import syslog

def output(message):
    print(message)
    syslog.syslog(message)

def light_on(colour):
    colours = {'red': [1,0,0], 'green': [0,1,0], 'blue': [0,0,1],\
            'yellow': [1,1,0], 'cyan': [0,1,1], 'magenta': [1,0,1],\
            'white': [1,1,1], True: [1,1,1],\
            'black': [0,0,0], 'off': [0,0,0], False: [0,0,0], None: [0,0,0]}
    pins = {'red': 18, 'green': 23, 'blue': 24}
    if colour in colours:
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pins['red'], GPIO.OUT)
        GPIO.setup(pins['green'], GPIO.OUT)
        GPIO.setup(pins['blue'], GPIO.OUT)

        GPIO.output(pins['red'], colours[colour][0])
        GPIO.output(pins['green'], colours[colour][1])
        GPIO.output(pins['blue'], colours[colour][2])
    else:
        output("Not a valid colour.")

def clean_up():
    nfc.close(pnd)
    nfc.exit(context)
    conn.close()

def signal_handler(signal, frame):
    clean_up()
    sys.exit(0)

sleep_time = 2

# Ensure ^C doesn't corrupt/leave anything broken.
signal.signal(signal.SIGINT, signal_handler)

conn = sqlite3.connect('/var/db/assurpid/assurpid.db')
output('opened database successfully')

output('libnfc version: ' + nfc.__version__)

context = nfc.init()
pnd = nfc.open(context)
if pnd is None:
    output('ERROR: Unable to detect RFID sensor (run this as root).')
    exit()

if nfc.initiator_init(pnd) < 0:
    nfc.perror(pnd, "nfc_initiator_init")
    output('ERROR: Unable to init RFID sensor.')
    exit()

output('NFC reader: ' + nfc.device_get_name(pnd) + 'opened')

nmMifare = nfc.modulation()
nmMifare.nmt = nfc.NMT_ISO14443A
nmMifare.nbr = nfc.NBR_106

nt = nfc.target()

admin_switch = False

while True:
    if not admin_switch:
        GPIO.cleanup()

    # Wait for tag
    ret = nfc.initiator_select_passive_target(pnd, nmMifare, 0, 0, nt)

    # Convert raw uid to hex, then trim to length (2*nt.nti.nai.szUidLen)
    tag = ''.join(format(x, '02x') for x in nt.nti.nai.abtUid)[:2*nt.nti.nai.szUidLen]
    output('Found tag with UID of' + tag)
    cursor = conn.cursor()
    cursor.execute("SELECT UID, ADMIN, ACCESS_ROOM1, INSIDE_ROOM1 FROM MAIN WHERE UID = ?", (tag,))
    details = cursor.fetchone()

    if details[1] == 1:
        output("Tag is admin.")
        light_on('magenta')
        admin_switch = True
        time.sleep(sleep_time)
        continue

    if details is None:
        output("Tag is unrecognised")
        light_on('red')
        if admin_switch:
            conn.execute("INSERT INTO MAIN(UID, ADMIN, ACCESS_ROOM1, INSIDE_ROOM1) VALUES(?, 0, 1, 0)", (tag,))
            conn.commit()
            admin_switch = False
            light_on('green')
            output("Added tag to database")
        time.sleep(sleep_time)
        continue
    else:
        output("Tag is recognised")
        if admin_switch and details[1] == 0:
            conn.execute("DELETE FROM MAIN WHERE UID = ?", (tag,))
            conn.commit()
            admin_switch = False
            light_on('red')
            output("Deleted tag from database")
            time.sleep(sleep_time)
            continue

    if details[3] == 1:
        output("Tag is inside Room 1.")
    else:
        output("Tag is outside Room 1.")

    if details[2] == 1:
        output("Tag can open door to Room 1.")
        light_on('green')
    else:
        output("Tag cannot open door to Room 1.")
        light_on('blue')

    if details[2] == 1 and details[3]:
        conn.execute("UPDATE MAIN SET INSIDE_ROOM1 = 0 WHERE UID = ?", (tag,))
        output("Door opened from inside, tag now outside.")

    if details[2] == 1 and details[3] == 0:
        conn.execute("UPDATE MAIN SET INSIDE_ROOM1 = 1 WHERE UID = ?", (tag,))
        output("Door opened from outside, tag now inside.")
    time.sleep(sleep_time)

clean_up()
sys.exit(0)
<|endoftext|>


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

# pyvows testing engine
# https://github.com/heynemann/pyvows

# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Bernardo Heynemann heynemann@gmail.com

import sys
import re
import traceback

from lxml import etree
from colorama import init, Fore, Style

from pyvows.core import VowsAssertionError

PROGRESS_SIZE = 50

class VowsDefaultReporter(object):
    honored = Fore.GREEN + Style.BRIGHT + '✓' + Fore.RESET + Style.RESET_ALL
    broken = Fore.RED + Style.BRIGHT + '✗' + Fore.RESET + Style.RESET_ALL

    def __init__(self, result):
        init(autoreset=True)
        self.result = result
        self.tab = ' ' * 2
        self.indent = 1

    def camel_split(self, string):
        return re.sub('((?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z])|(?=[0-9]\b))', ' ', string).strip()

    def under_split(self, string):
        return ' '.join(string.split('_'))

    @classmethod
    def handle_success(cls, vow):
        sys.stdout.write(cls.honored)

    @classmethod
    def handle_error(cls, vow):
        sys.stdout.write(cls.broken)

    def pretty_print(self):
        print
        print
        if not self.result:
            print ' %s No vows found! » 0 honored • 0 broken (0.0s)' % self.broken
            return

        for context in self.result.contexts:
            self.print_context(context['name'], context)

        print
        print '%s%s OK » %d honored • %d broken (%.6fs)' % (
            self.tab * self.indent,
            self.honored if self.result.successful else self.broken,
            self.result.successful_tests,
            self.result.errored_tests,
            self.result.ellapsed_time
        )

    def humanized_print(self, msg):
        msg = self.under_split(msg)
        msg = self.camel_split(msg)
        print (self.tab * self.indent) + msg.capitalize()

    def format_traceback(self, traceback_list, indentation):
        def indent(msg):
            if msg.startswith('  File'):
                return msg.replace('\n ', '\n %s' % indentation)
            return msg

        return map(indent, traceback_list)

    def print_context(self, name, context):
        self.humanized_print(name)
        self.indent += 1

        indentation2 = self.tab * (self.indent + 2)

        for test in context['tests']:
            if test['succeeded']:
                self.humanized_print(VowsDefaultReporter.honored + ' ' + test['name'])
            else:
                self.humanized_print(VowsDefaultReporter.broken + ' ' + test['name'])

                if isinstance(test['topic'], Exception) and \
                   'context_instance' in test and \
                   hasattr(test['context_instance'], 'topic_error') and \
                   not isinstance(test['context_instance'].topic_error[1], AssertionError):
                    exc_type, exc_value, exc_traceback = test['context_instance'].topic_error
                else:
                    error = test['error']
                    exc_type, exc_value, exc_traceback = error['type'], error['value'], error['traceback']

                if isinstance(exc_value, VowsAssertionError):
                    exc_values_args = tuple(map(lambda arg: Fore.RESET + arg + Fore.RED, exc_value.args))
                    error_msg = exc_value.msg % exc_values_args
                else:
                    error_msg = unicode(exc_value)

                traceback_msg = traceback.format_exception(exc_type, exc_value, exc_traceback)
                traceback_msg = self.format_traceback(traceback_msg, indentation2)
                traceback_msg = indentation2.join(traceback_msg)

                print indentation2 + Fore.RED + error_msg + Fore.RESET
                print
                print indentation2 + traceback_msg

                if 'file' in test:
                    print indentation2 + Fore.RED + '(found in %s at line %s)' % (test['file'], test['lineno']) + Fore.RESET
                    print

        for context in context['contexts']:
            self.print_context(context['name'], context)

        self.indent -= 1

    def print_coverage(self, xml, cover_threshold):
        write_blue = lambda msg: Fore.BLUE + Style.BRIGHT + str(msg) + Style.RESET_ALL + Fore.RESET
        write_white = lambda msg: Fore.WHITE + Style.BRIGHT + str(msg) + Style.RESET_ALL + Fore.RESET

        root = etree.fromstring(xml)

        klasses = root.xpath('//class')
        names = ['.'.join([klass.getparent().getparent().attrib['name'], klass.attrib['name']]) for klass in klasses]
        max_length = max([len(klass_name) for klass_name in names])

        print ' ' + '=' * len('Code Coverage')
        print Fore.GREEN + Style.BRIGHT + ' Code Coverage' + Style.RESET_ALL + Fore.RESET
        print ' ' + '=' * len('Code Coverage')
        print

        klasses = sorted(klasses, key=lambda klass: float(klass.attrib['line-rate']))

        max_coverage = 0
        for klass in klasses:
            package_name = klass.getparent().getparent().attrib['name']
            klass_name = '.'.join([package_name, klass.attrib['name']])
            coverage = float(klass.attrib['line-rate']) * 100
            if coverage < cover_threshold:
                cover_character = self.broken
            else:
                cover_character = self.honored

            if coverage > max_coverage and max_coverage < 100.0:
                max_coverage = coverage
                if max_coverage == 100.0:
                    print

            uncovered_lines = [line.attrib['number'] for line in klass.find('lines') if line.attrib['hits'] == '0']

            coverage = int(round(coverage, 0))
            progress = int(round(coverage / 100.0 * PROGRESS_SIZE, 0))
            offset = coverage == 0 and 2 or (coverage < 10 and 1 or 0)

            if coverage == 0 and not uncovered_lines:
                continue

            print ' %s %s%s\t%s%s%%%s %s' % (cover_character,
                                        write_blue(klass_name),
                                        ' ' * (max_length - len(klass_name)),
                                        '•' * progress,
                                        write_white((coverage > 0 and ' ' or '') + '%.2f' % coverage),
                                        ' ' * (PROGRESS_SIZE - progress + offset),
                                        self.get_uncovered_lines(uncovered_lines))

        print
        total_coverage = float(root.xpath('//coverage')[0].attrib['line-rate']) * 100
        progress = int(round(total_coverage / 100.0 * PROGRESS_SIZE, 0))
        print ' %s %s%s\t%s %s%%' % ((total_coverage >= cover_threshold) and self.honored or self.broken,
                                    write_blue('OVERALL'),
                                    ' ' * (max_length - len('OVERALL')),
                                    '•' * progress,
                                    write_white('%.2f' % total_coverage))

        print

    def get_uncovered_lines(self, uncovered_lines, number_of=3):
        if len(uncovered_lines) > number_of:
            template_str = []
            for i in range(number_of):
                template_str.append(uncovered_lines[i])
                if not i == number_of - 1:
                    template_str += ' ,'

            template_str.append(' and %d more' % (len(uncovered_lines) - number_of))

            return ''.join(template_str)

        return ', '.join(uncovered_lines)

<commit_msg>improving error message while theres an error inside the topic<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# pyvows testing engine
# https://github.com/heynemann/pyvows

# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Bernardo Heynemann heynemann@gmail.com

import sys
import re
import traceback

from lxml import etree
from colorama import init, Fore, Style

from pyvows.core import VowsAssertionError

PROGRESS_SIZE = 50

class VowsDefaultReporter(object):
    honored = Fore.GREEN + Style.BRIGHT + '✓' + Fore.RESET + Style.RESET_ALL
    broken = Fore.RED + Style.BRIGHT + '✗' + Fore.RESET + Style.RESET_ALL

    def __init__(self, result):
        init(autoreset=True)
        self.result = result
        self.tab = ' ' * 2
        self.indent = 1

    def camel_split(self, string):
        return re.sub('((?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z])|(?=[0-9]\b))', ' ', string).strip()

    def under_split(self, string):
        return ' '.join(string.split('_'))

    @classmethod
    def handle_success(cls, vow):
        sys.stdout.write(cls.honored)

    @classmethod
    def handle_error(cls, vow):
        sys.stdout.write(cls.broken)

    def pretty_print(self):
        print
        print
        if not self.result:
            print ' %s No vows found! » 0 honored • 0 broken (0.0s)' % self.broken
            return

        for context in self.result.contexts:
            self.print_context(context['name'], context)

        print
        print '%s%s OK » %d honored • %d broken (%.6fs)' % (
            self.tab * self.indent,
            self.honored if self.result.successful else self.broken,
            self.result.successful_tests,
            self.result.errored_tests,
            self.result.ellapsed_time
        )

    def humanized_print(self, msg):
        msg = self.under_split(msg)
        msg = self.camel_split(msg)
        print (self.tab * self.indent) + msg.capitalize()

    def format_traceback(self, traceback_list, indentation):
        def indent(msg):
            if msg.startswith('  File'):
                return msg.replace('\n ', '\n %s' % indentation)
            return msg

        return map(indent, traceback_list)

    def print_traceback(self, exc_type, exc_value, exc_traceback, indentation):
        if isinstance(exc_value, VowsAssertionError):
            exc_values_args = tuple(map(lambda arg: Fore.RESET + arg + Fore.RED, exc_value.args))
            error_msg = exc_value.msg % exc_values_args
        else:
            error_msg = unicode(exc_value)

        traceback_msg = traceback.format_exception(exc_type, exc_value, exc_traceback)
        traceback_msg = self.format_traceback(traceback_msg, indentation)
        traceback_msg = indentation.join(traceback_msg)

        print indentation + Fore.RED + error_msg + Fore.RESET
        print
        print indentation + traceback_msg

    def print_context(self, name, context):
        self.humanized_print(name)
        self.indent += 1

        indentation2 = self.tab * (self.indent + 2)

        for test in context['tests']:
            if test['succeeded']:
                self.humanized_print(VowsDefaultReporter.honored + ' ' + test['name'])
            else:
                self.humanized_print(VowsDefaultReporter.broken + ' ' + test['name'])

                if isinstance(test['topic'], Exception) and \
                   'context_instance' in test and \
                   hasattr(test['context_instance'], 'topic_error'):
                    exc_type, exc_value, exc_traceback = test['context_instance'].topic_error
                    self.print_traceback(exc_type, exc_value, exc_traceback, indentation2)

                error = test['error']
                exc_type, exc_value, exc_traceback = error['type'], error['value'], error['traceback']

                self.print_traceback(exc_type, exc_value, exc_traceback, indentation2)

                if 'file' in test:
                    print indentation2 + Fore.RED + '(found in %s at line %s)' % (test['file'], test['lineno']) + Fore.RESET
                    print

        for context in context['contexts']:
            self.print_context(context['name'], context)

        self.indent -= 1

    def print_coverage(self, xml, cover_threshold):
        write_blue = lambda msg: Fore.BLUE + Style.BRIGHT + str(msg) + Style.RESET_ALL + Fore.RESET
        write_white = lambda msg: Fore.WHITE + Style.BRIGHT + str(msg) + Style.RESET_ALL + Fore.RESET

        root = etree.fromstring(xml)

        klasses = root.xpath('//class')
        names = ['.'.join([klass.getparent().getparent().attrib['name'], klass.attrib['name']]) for klass in klasses]
        max_length = max([len(klass_name) for klass_name in names])

        print ' ' + '=' * len('Code Coverage')
        print Fore.GREEN + Style.BRIGHT + ' Code Coverage' + Style.RESET_ALL + Fore.RESET
        print ' ' + '=' * len('Code Coverage')
        print

        klasses = sorted(klasses, key=lambda klass: float(klass.attrib['line-rate']))

        max_coverage = 0
        for klass in klasses:
            package_name = klass.getparent().getparent().attrib['name']
            klass_name = '.'.join([package_name, klass.attrib['name']])
            coverage = float(klass.attrib['line-rate']) * 100
            if coverage < cover_threshold:
                cover_character = self.broken
            else:
                cover_character = self.honored

            if coverage > max_coverage and max_coverage < 100.0:
                max_coverage = coverage
                if max_coverage == 100.0:
                    print

            uncovered_lines = [line.attrib['number'] for line in klass.find('lines') if line.attrib['hits'] == '0']

            coverage = int(round(coverage, 0))
            progress = int(round(coverage / 100.0 * PROGRESS_SIZE, 0))
            offset = coverage == 0 and 2 or (coverage < 10 and 1 or 0)

            if coverage == 0 and not uncovered_lines:
                continue

            print ' %s %s%s\t%s%s%%%s %s' % (cover_character,
                                        write_blue(klass_name),
                                        ' ' * (max_length - len(klass_name)),
                                        '•' * progress,
                                        write_white((coverage > 0 and ' ' or '') + '%.2f' % coverage),
                                        ' ' * (PROGRESS_SIZE - progress + offset),
                                        self.get_uncovered_lines(uncovered_lines))

        print
        total_coverage = float(root.xpath('//coverage')[0].attrib['line-rate']) * 100
        progress = int(round(total_coverage / 100.0 * PROGRESS_SIZE, 0))
        print ' %s %s%s\t%s %s%%' % ((total_coverage >= cover_threshold) and self.honored or self.broken,
                                    write_blue('OVERALL'),
                                    ' ' * (max_length - len('OVERALL')),
                                    '•' * progress,
                                    write_white('%.2f' % total_coverage))

        print

    def get_uncovered_lines(self, uncovered_lines, number_of=3):
        if len(uncovered_lines) > number_of:
            template_str = []
            for i in range(number_of):
                template_str.append(uncovered_lines[i])
                if not i == number_of - 1:
                    template_str += ' ,'

            template_str.append(' and %d more' % (len(uncovered_lines) - number_of))

            return ''.join(template_str)

        return ', '.join(uncovered_lines)

<|endoftext|>


<commit_before>import re
from dimagi.utils.dates import force_to_datetime
from corehq.apps.commtrack.models import CommTrackUser
from corehq.apps.locations.models import Location
from corehq.apps.reports.datatables import DataTablesHeader, DataTablesColumnGroup, DataTablesColumn
from corehq.apps.reports.sqlreport import DataFormatter, DictDataFormat
from corehq.fluff.calculators.xform import FormPropertyFilter, IN
from corehq.util.translation import localize
from custom.intrahealth.reports.fiche_consommation_report import FicheConsommationReport
from custom.intrahealth.reports.recap_passage_report import RecapPassageReport
from custom.intrahealth.reports.tableu_de_board_report import TableuDeBoardReport

from django.utils.dateformat import format

INTRAHEALTH_DOMAINS = ('ipm-senegal', 'testing-ipm-senegal', 'ct-apr')

OPERATEUR_XMLNSES = (
    'http://openrosa.org/formdesigner/7330597b92db84b1a33c7596bb7b1813502879be',
)

COMMANDE_XMLNSES = (
    'http://openrosa.org/formdesigner/9ED66735-752D-4C69-B9C8-77CEDAAA0348',
    'http://openrosa.org/formdesigner/12b412390011cb9b13406030ab10447ffd99bdf8',
)

RAPTURE_XMLNSES = (
    'http://openrosa.org/formdesigner/AD88DE3E-6AFC-48A5-8BEC-092419C1D45A',
)

LIVRAISON_XMLNSES = ("http://openrosa.org/formdesigner/182649A1-A3BB-4F56-988C-2C103DBAA6D7", )

CUSTOM_REPORTS = (
    ('INFORMED PUSH MODEL REPORTS', (
        TableuDeBoardReport,
        FicheConsommationReport,
        RecapPassageReport
    )),
)

PRODUCT_MAPPING = {
    "collier": "Collier",
    "cu": "CU",
    "depoprovera": "Depo-Provera",
    "diu": "DIU",
    "jadelle": "Jadelle",
    "microgynon": "Microgynon/Lof.",
    "microlut": "Microlut/Ovrette",
    "preservatif_feminin": "Preservatif Feminin",
    "preservatif_masculin": "Preservatif Masculin"
}

def get_products(form, property):
    products = []
    if 'products' in form.form:
        for product in form.form['products']:
            if property in product:
                products.append(product[property])
    return products

def get_rupture_products(form):
    result = []
    for k, v in form.form.iteritems():
        if re.match("^rupture.*hv$", k):
            result.append(PRODUCT_MAPPING[k[8:-3]])
    return result

def _get_location(form):
    if 'location_id' in form.form:
        loc_id = form.form['location_id']
        loc = Location.get(loc_id)
    else:
        user_id = form['auth_context']['user_id']
        if not user_id:
            return None
        user = CommTrackUser.get(user_id)
        loc = user.location
    return loc

def get_domain(form):
    return form.domain

def get_prod_info(prod, property):
    return prod[property]

def get_location_id(form):
    return _get_location(form)._id

def get_location_id_by_type(form, type):
    loc = get_location_by_type(form, type)
    return loc._id if loc else None

def get_location_by_type(form, type):
    loc = _get_location(form)
    if not loc:
        return None

    for loc_id in loc.lineage:
        loc = Location.get(loc_id)
        if unicode(loc.location_type).lower().replace(" ", "") == type:
            return loc

def get_real_date(form):
    date = ""
    if 'real_date' in form.form:
        date = form.form['real_date']
    return date

def format_date_string(value):
    if not value:
        return value
    with localize('fr'):
        return format(force_to_datetime(value), 'd E')

def get_pps_name(form):
    pps_name = form.form.get('PPS_name', None)

    if not pps_name:
        loc = _get_location(form)
        if not loc:
            return None
        return loc.name
    else:
        return pps_name

def get_district_name(form):
    district_name = form.form.get('district_name', None)
    if not district_name:
        loc = get_location_by_type(form, 'district')
        if not loc:
            return None
        return loc.name
    else:
        return district_name

def get_month(form, prop):
    value = form.form.get(prop, '')
    if value:
        with localize('fr'):
            return format(force_to_datetime(value), 'E')
    else:
        return value

class IsExistFormPropertyFilter(FormPropertyFilter):

    def __init__(self, xmlns=None, property_path=None, property_value=None):
        super(IsExistFormPropertyFilter, self).__init__(xmlns=xmlns, property_path=property_path,
                                                        property_value=property_value, operator=IN)

    def filter(self, form):
        return (
            form.xmlns == self.xmlns and (
                self.property_path is None or
                self.operator(self.property_value, form.xpath(self.property_path))
            )
        )<commit_msg>IntraHealth: Added handling for deleted Locations in fluff calcs.<commit_after>import logging
import re
from dimagi.utils.dates import force_to_datetime
from couchdbkit.exceptions import ResourceNotFound
from corehq.apps.commtrack.models import CommTrackUser
from corehq.apps.locations.models import Location
from corehq.apps.reports.datatables import DataTablesHeader, DataTablesColumnGroup, DataTablesColumn
from corehq.apps.reports.sqlreport import DataFormatter, DictDataFormat
from corehq.fluff.calculators.xform import FormPropertyFilter, IN
from corehq.util.translation import localize
from custom.intrahealth.reports.fiche_consommation_report import FicheConsommationReport
from custom.intrahealth.reports.recap_passage_report import RecapPassageReport
from custom.intrahealth.reports.tableu_de_board_report import TableuDeBoardReport

from django.utils.dateformat import format

INTRAHEALTH_DOMAINS = ('ipm-senegal', 'testing-ipm-senegal', 'ct-apr')

OPERATEUR_XMLNSES = (
    'http://openrosa.org/formdesigner/7330597b92db84b1a33c7596bb7b1813502879be',
)

COMMANDE_XMLNSES = (
    'http://openrosa.org/formdesigner/9ED66735-752D-4C69-B9C8-77CEDAAA0348',
    'http://openrosa.org/formdesigner/12b412390011cb9b13406030ab10447ffd99bdf8',
)

RAPTURE_XMLNSES = (
    'http://openrosa.org/formdesigner/AD88DE3E-6AFC-48A5-8BEC-092419C1D45A',
)

LIVRAISON_XMLNSES = ("http://openrosa.org/formdesigner/182649A1-A3BB-4F56-988C-2C103DBAA6D7", )

CUSTOM_REPORTS = (
    ('INFORMED PUSH MODEL REPORTS', (
        TableuDeBoardReport,
        FicheConsommationReport,
        RecapPassageReport
    )),
)

PRODUCT_MAPPING = {
    "collier": "Collier",
    "cu": "CU",
    "depoprovera": "Depo-Provera",
    "diu": "DIU",
    "jadelle": "Jadelle",
    "microgynon": "Microgynon/Lof.",
    "microlut": "Microlut/Ovrette",
    "preservatif_feminin": "Preservatif Feminin",
    "preservatif_masculin": "Preservatif Masculin"
}

def get_products(form, property):
    products = []
    if 'products' in form.form:
        for product in form.form['products']:
            if property in product:
                products.append(product[property])
    return products

def get_rupture_products(form):
    result = []
    for k, v in form.form.iteritems():
        if re.match("^rupture.*hv$", k):
            result.append(PRODUCT_MAPPING[k[8:-3]])
    return result

def _get_location(form):
    loc = None
    if 'location_id' in form.form:
        loc_id = form.form['location_id']
        try:
            loc = Location.get(loc_id)
        except ResourceNotFound:
            logging.info('Location %s Not Found.' % loc_id)
    else:
        user_id = form['auth_context']['user_id']
        if not user_id:
            return None
        user = CommTrackUser.get(user_id)
        try:
            loc = user.location
        except ResourceNotFound:
            logging.info('Location %s Not Found.' % loc)

    return loc

def get_domain(form):
    return form.domain

def get_prod_info(prod, property):
    return prod[property]

def get_location_id(form):
    loc = _get_location(form)
    if not loc:
        return None
    return loc._id

def get_location_id_by_type(form, type):
    loc = get_location_by_type(form, type)
    return loc._id if loc else None

def get_location_by_type(form, type):
    loc = _get_location(form)
    if not loc:
        return None

    for loc_id in loc.lineage:
        loc = Location.get(loc_id)
        if unicode(loc.location_type).lower().replace(" ", "") == type:
            return loc

def get_real_date(form):
    date = ""
    if 'real_date' in form.form:
        date = form.form['real_date']
    return date

def format_date_string(value):
    if not value:
        return value
    with localize('fr'):
        return format(force_to_datetime(value), 'd E')

def get_pps_name(form):
    pps_name = form.form.get('PPS_name', None)

    if not pps_name:
        loc = _get_location(form)
        if not loc:
            return None
        return loc.name
    else:
        return pps_name

def get_district_name(form):
    district_name = form.form.get('district_name', None)
    if not district_name:
        loc = get_location_by_type(form, 'district')
        if not loc:
            return None
        return loc.name
    else:
        return district_name

def get_month(form, prop):
    value = form.form.get(prop, '')
    if value:
        with localize('fr'):
            return format(force_to_datetime(value), 'E')
    else:
        return value

class IsExistFormPropertyFilter(FormPropertyFilter):

    def __init__(self, xmlns=None, property_path=None, property_value=None):
        super(IsExistFormPropertyFilter, self).__init__(xmlns=xmlns, property_path=property_path,
                                                        property_value=property_value, operator=IN)

    def filter(self, form):
        return (
            form.xmlns == self.xmlns and (
                self.property_path is None or
                self.operator(self.property_value, form.xpath(self.property_path))
            )
        )<|endoftext|>