<commit_before>##################################################################################
#                                                                                #
# PkgTk: Tool kit for Python packages                                            #
#                                                                                #
# Homepage: pkgtk.readthedocs.io                                                 #
#                                                                                #
# Copyright (c) 2016 Pierre Fernique                                             #
#                                                                                #
# This software is distributed under the CeCILL-C license. You should have       #
# received a copy of the legalcode along with this work. If not, see             #
# <http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html>.                 #
#                                                                                #
# File authors: Pierre Fernique <pfernique@gmail.com> (10)                       #
#                                                                                #
##################################################################################

import os
import git
import unittest
import hashlib
try:
    import __builtin__
except:
    import builtins as __builtin__
import path

from tempfile import NamedTemporaryFile
from pkgtk.config import init_config
from pkgtk.license import init_license, load_license, dump_license

class TemplateRender(object):

    def __get__(self, obj, objtype, **kwargs):
        code = obj.code
        code = code.replace('\n    __M_caller = context.caller_stack._push_frame()', '', 1)
        code = code.replace('\n    __M_caller = context.caller_stack._push_frame()', '', 1)
        code = code.replace("        return ''\n    finally:\n        context.caller_stack._pop_frame()\n", "        return __M_string\n    except:\n        return ''", 1)
        code = code.replace("context,**pageargs", "**context", 1)
        code = code.replace("\n        __M_locals = __M_dict_builtin(pageargs=pageargs)", "", 1)
        code = code.replace("__M_writer = context.writer()", "__M_string = u''")
        code = code.replace("__M_writer(", "__M_string = operator.add(__M_string, ")
        code = "import operator\n" + code
        exec(code, globals())
        def __call__(**context):
            for builtin in dir(__builtin__):
                if not builtin in context:
                    context[builtin] = getattr(__builtin__, builtin)
            return globals()["render_body"](**context)
        return __call__

from mako.template import Template
Template.render = TemplateRender()

class TestLicence(unittest.TestCase):
    """Test functions related to licensing"""

    @classmethod
    def setUpClass(cls):
        cls.repository = '.'
        init_license(cls.repository, plugin='CeCILL-C')
        cls.config = init_config(cls.repository)
        cls.config['license']['exclude'] += path.path(cls.repository).walkfiles()
        os.rename(cls.repository + os.sep + '.pkgtk.yml', cls.repository + os.sep + '.pkgtk.back')
        os.rename(cls.repository + os.sep + cls.config['license']['basename'], cls.repository + os.sep + cls.config['license']['basename'] + '.back')

    def test_init(self):
        """Test `init_license` function of module `pkgtk.license`"""
        init_license(self.repository, plugin='CeCILL-C')
        init_license(self.repository)

    def test_load_dump_cecillc(self, plugin='CeCILL-C'):
        """Test `dump_license` function of module `pkgtk.license`"""
        load_license.plugin = plugin
        repo = git.Repo('.')
        commit = repo.commit('HEAD')
        author = git.Actor('John Doe', 'jdoe@host')
        tempfiles = []
        for suffix in ['.c', '.h', '.cpp', '.cxx', '.c++', '.hpp', '.hxx', '.h++', '.py', '.rst', '.yml', '.yaml']:
            with NamedTemporaryFile(suffix=suffix, mode='w', dir=self.repository, delete=False) as filehandler:
                tempfiles.append(filehandler.name)
        repo.index.add(tempfiles)
        repo.index.commit('Add ' + ', '.join(tempfile for tempfile in tempfiles), author=author)
        dump_license(self.repository, self.config)
        repo.index.add(tempfiles)
        author.name = ' '.join(reversed(author.name.split()))
        repo.index.commit('Add ' + ', '.join(tempfile for tempfile in tempfiles), author=author)
        dump_license(self.repository, self.config)
        md5sum = hashlib.md5()
        for tempfile in tempfiles:
            with open(tempfile, 'r') as filehandler:
                md5sum.update(filehandler.read().encode('utf-8'))
        dump_license(self.repository, self.config)
        _md5sum = hashlib.md5()
        for tempfile in tempfiles:
            with open(tempfile, 'r') as filehandler:
                _md5sum.update(filehandler.read().encode('utf-8'))
        repo.head.reference = commit
        repo.index.reset()
        for tempfile in tempfiles:
            os.unlink(tempfile)
        self.assertEqual(md5sum.digest(), _md5sum.digest())
        with open(self.repository + os.sep + self.config['license']['basename'], 'r') as filehandler:
            curr = filehandler.read().encode('utf-8')
        with open(self.repository + os.sep + self.config['license']['basename'] + '.back', 'r') as filehandler:
            prev = filehandler.read().encode('utf-8')
        self.assertIn(curr, prev)

    @classmethod
    def tearDownClass(cls):
        os.unlink(cls.repository + os.sep + '.pkgtk.yml')
        os.rename(cls.repository + os.sep + '.pkgtk.back', cls.repository + os.sep + '.pkgtk.yml')
        os.unlink(cls.repository + os.sep + cls.config['license']['basename'])
        os.rename(cls.repository + os.sep + cls.config['license']['basename'] + '.back', cls.repository + os.sep + cls.config['license']['basename'])
<commit_msg>Update test_license.py<commit_after>##################################################################################
#                                                                                #
# PkgTk: Tool kit for Python packages                                            #
#                                                                                #
# Homepage: pkgtk.readthedocs.io                                                 #
#                                                                                #
# Copyright (c) 2016 Pierre Fernique                                             #
#                                                                                #
# This software is distributed under the CeCILL-C license. You should have       #
# received a copy of the legalcode along with this work. If not, see             #
# <http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html>.                 #
#                                                                                #
# File authors: Pierre Fernique <pfernique@gmail.com> (10)                       #
#                                                                                #
##################################################################################

import os
import git
import unittest
import hashlib
import __builtin__
import path

from tempfile import NamedTemporaryFile
from pkgtk.config import init_config
from pkgtk.license import init_license, load_license, dump_license

class TemplateRender(object):

    def __get__(self, obj, objtype, **kwargs):
        code = obj.code
        code = code.replace('\n    __M_caller = context.caller_stack._push_frame()', '', 1)
        code = code.replace('\n    __M_caller = context.caller_stack._push_frame()', '', 1)
        code = code.replace("        return ''\n    finally:\n        context.caller_stack._pop_frame()\n", "        return __M_string\n    except:\n        return ''", 1)
        code = code.replace("context,**pageargs", "**context", 1)
        code = code.replace("\n        __M_locals = __M_dict_builtin(pageargs=pageargs)", "", 1)
        code = code.replace("__M_writer = context.writer()", "__M_string = u''")
        code = code.replace("__M_writer(", "__M_string = operator.add(__M_string, ")
        code = "import operator\n" + code
        exec(code, globals())
        def __call__(**context):
            for builtin in dir(__builtin__):
                if not builtin in context:
                    context[builtin] = getattr(__builtin__, builtin)
            return globals()["render_body"](**context)
        return __call__

from mako.template import Template
Template.render = TemplateRender()

class TestLicence(unittest.TestCase):
    """Test functions related to licensing"""

    @classmethod
    def setUpClass(cls):
        cls.repository = '.'
        init_license(cls.repository, plugin='CeCILL-C')
        cls.config = init_config(cls.repository)
        cls.config['license']['exclude'] += path.path(cls.repository).walkfiles()
        os.rename(cls.repository + os.sep + '.pkgtk.yml', cls.repository + os.sep + '.pkgtk.back')
        os.rename(cls.repository + os.sep + cls.config['license']['basename'], cls.repository + os.sep + cls.config['license']['basename'] + '.back')

    def test_init(self):
        """Test `init_license` function of module `pkgtk.license`"""
        init_license(self.repository, plugin='CeCILL-C')
        init_license(self.repository)

    def test_load_dump_cecillc(self, plugin='CeCILL-C'):
        """Test `dump_license` function of module `pkgtk.license`"""
        load_license.plugin = plugin
        repo = git.Repo('.')
        commit = repo.commit('HEAD')
        author = git.Actor('John Doe', 'jdoe@host')
        tempfiles = []
        for suffix in ['.c', '.h', '.cpp', '.cxx', '.c++', '.hpp', '.hxx', '.h++', '.py', '.rst', '.yml', '.yaml']:
            with NamedTemporaryFile(suffix=suffix, mode='w', dir=self.repository, delete=False) as filehandler:
                tempfiles.append(filehandler.name)
        repo.index.add(tempfiles)
        repo.index.commit('Add ' + ', '.join(tempfile for tempfile in tempfiles), author=author)
        dump_license(self.repository, self.config)
        repo.index.add(tempfiles)
        author.name = ' '.join(reversed(author.name.split()))
        repo.index.commit('Add ' + ', '.join(tempfile for tempfile in tempfiles), author=author)
        dump_license(self.repository, self.config)
        md5sum = hashlib.md5()
        for tempfile in tempfiles:
            with open(tempfile, 'r') as filehandler:
                md5sum.update(filehandler.read().encode('utf-8'))
        dump_license(self.repository, self.config)
        _md5sum = hashlib.md5()
        for tempfile in tempfiles:
            with open(tempfile, 'r') as filehandler:
                _md5sum.update(filehandler.read().encode('utf-8'))
        repo.head.reference = commit
        repo.index.reset()
        for tempfile in tempfiles:
            os.unlink(tempfile)
        self.assertEqual(md5sum.digest(), _md5sum.digest())
        with open(self.repository + os.sep + self.config['license']['basename'], 'r') as filehandler:
            curr = filehandler.read().encode('utf-8')
        with open(self.repository + os.sep + self.config['license']['basename'] + '.back', 'r') as filehandler:
            prev = filehandler.read().encode('utf-8')
        self.assertIn(curr, prev)

    @classmethod
    def tearDownClass(cls):
        os.unlink(cls.repository + os.sep + '.pkgtk.yml')
        os.rename(cls.repository + os.sep + '.pkgtk.back', cls.repository + os.sep + '.pkgtk.yml')
        os.unlink(cls.repository + os.sep + cls.config['license']['basename'])
        os.rename(cls.repository + os.sep + cls.config['license']['basename'] + '.back', cls.repository + os.sep + cls.config['license']['basename'])
<|endoftext|>


<commit_before>"""
Tools for using a fake implementation of Vuforia.
"""

import os
import re
from contextlib import ContextDecorator
from urllib.parse import urljoin

from typing import Optional  # noqa: F401 This is used in a type hint.
from typing import Any, Callable, Tuple, TypeVar, Pattern

from requests_mock.mocker import Mocker

from ._mock import MockVuforiaTargetAPI

_MOCK_VWS_TYPE = TypeVar('_MOCK_VWS_TYPE', bound='MockVWS')

def _target_endpoint_pattern(path_pattern: str) -> Pattern[str]:
    """
    Given a path pattern, return a regex which will match URLs to
    patch for the Target API.

    Args:
        path_pattern: A part of the url which can be matched for endpoints.
            For example `https://vws.vuforia.com/<this-part>`. This is
            compiled to be a regular expression, so it may be `/foo` or
            `/foo/.+` for example.
    """
    base = 'https://vws.vuforia.com/'  # type: str
    joined = urljoin(base=base, url=path_pattern + '$')
    return re.compile(joined)

class MockVWS(ContextDecorator):
    """
    Route requests to Vuforia's Web Service APIs to fakes of those APIs.

    This creates a mock which uses access keys from the environment.
    See the README to find which secrets to set.

    This can be used as a context manager or as a decorator.

    Examples:

        >>> @MockVWS()
        ... def test_vuforia_example():
        ...     pass

        or

        >>> def test_vuforia_example():
        ...     with MockVWS():
        ...         pass
    """

    def __init__(self, real_http: bool=False) -> None:
        """
        Args:
            real_http: Whether or not to forward requests to the real server if
                they are not handled by the mock.
                See
                http://requests-mock.readthedocs.io/en/latest/mocker.html#real-http-requests

        Attributes:
            real_http (bool): Whether or not to forward requests to the real
                server if they are not handled by the mock.
                See
                http://requests-mock.readthedocs.io/en/latest/mocker.html#real-http-requests
            mock: None or an `requests_mock` object used for mocking Vuforia.
        """
        super().__init__()
        self.real_http = real_http
        self.mock = None  # type: Optional[Mocker]

    def __call__(self, func: Callable[..., Any]) -> Any:
        """
        Override call to allow function to return any type.
        """
        return super().__call__(func)

    def __enter__(self: _MOCK_VWS_TYPE) -> _MOCK_VWS_TYPE:
        """
        Start an instance of a Vuforia mock with access keys set from
        environment variables.

        Returns:
            ``self``.
        """
        fake_target_api = MockVuforiaTargetAPI(
            database_name=os.environ['VUFORIA_TARGET_MANAGER_DATABASE_NAME'],
            access_key=os.environ['VUFORIA_SERVER_ACCESS_KEY'],
            secret_key=os.environ['VUFORIA_SERVER_SECRET_KEY'],
        )

        headers = {
            'Connection': 'keep-alive',
            'Content-Type': 'application/json',
            'Server': 'nginx',
        }
        with Mocker(real_http=self.real_http) as mock:
            for route in fake_target_api.routes:
                for http_method in route.methods:
                    mock.register_uri(
                        method=http_method,
                        url=_target_endpoint_pattern(route.path_pattern),
                        text=getattr(fake_target_api, route.route_name),
                        headers=headers,
                    )
        self.mock = mock
        self.mock.start()
        return self

    def __exit__(self, *exc: Tuple[None, None, None]) -> bool:
        """
        Stop the Vuforia mock.

        Returns:
            False
        """
        self.mock.stop()
        return False
<commit_msg>Better docstring<commit_after>"""
Tools for using a fake implementation of Vuforia.
"""

import os
import re
from contextlib import ContextDecorator
from urllib.parse import urljoin

from typing import Optional  # noqa: F401 This is used in a type hint.
from typing import Any, Callable, Tuple, TypeVar, Pattern

from requests_mock.mocker import Mocker

from ._mock import MockVuforiaTargetAPI

_MOCK_VWS_TYPE = TypeVar('_MOCK_VWS_TYPE', bound='MockVWS')

def _target_endpoint_pattern(path_pattern: str) -> Pattern[str]:
    """
    Given a path pattern, return a regex which will match URLs to
    patch for the Target API.

    Args:
        path_pattern: A part of the url which can be matched for endpoints.
            For example `https://vws.vuforia.com/<this-part>`. This is
            compiled to be a regular expression, so it may be `/foo` or
            `/foo/.+` for example.
    """
    base = 'https://vws.vuforia.com/'  # type: str
    joined = urljoin(base=base, url=path_pattern + '$')
    return re.compile(joined)

class MockVWS(ContextDecorator):
    """
    Route requests to Vuforia's Web Service APIs to fakes of those APIs.

    This creates a mock which uses access keys from the environment.
    See the README to find which secrets to set.

    This can be used as a context manager or as a decorator.

    Examples:

        >>> @MockVWS()
        ... def test_vuforia_example():
        ...     pass

        or

        >>> def test_vuforia_example():
        ...     with MockVWS():
        ...         pass
    """

    def __init__(self, real_http: bool=False) -> None:
        """
        Args:
            real_http: Whether or not to forward requests to the real server if
                they are not handled by the mock.
                See
                http://requests-mock.readthedocs.io/en/latest/mocker.html#real-http-requests

        Attributes:
            real_http (bool): Whether or not to forward requests to the real
                server if they are not handled by the mock.
                See
                http://requests-mock.readthedocs.io/en/latest/mocker.html#real-http-requests
            mock: None or an `requests_mock` object used for mocking Vuforia.
        """
        super().__init__()
        self.real_http = real_http
        self.mock = None  # type: Optional[Mocker]

    def __call__(self, func: Callable[..., Any]) -> Any:
        """
        Override call to allow a wrapped function to return any type.
        """
        return super().__call__(func)

    def __enter__(self: _MOCK_VWS_TYPE) -> _MOCK_VWS_TYPE:
        """
        Start an instance of a Vuforia mock with access keys set from
        environment variables.

        Returns:
            ``self``.
        """
        fake_target_api = MockVuforiaTargetAPI(
            database_name=os.environ['VUFORIA_TARGET_MANAGER_DATABASE_NAME'],
            access_key=os.environ['VUFORIA_SERVER_ACCESS_KEY'],
            secret_key=os.environ['VUFORIA_SERVER_SECRET_KEY'],
        )

        headers = {
            'Connection': 'keep-alive',
            'Content-Type': 'application/json',
            'Server': 'nginx',
        }
        with Mocker(real_http=self.real_http) as mock:
            for route in fake_target_api.routes:
                for http_method in route.methods:
                    mock.register_uri(
                        method=http_method,
                        url=_target_endpoint_pattern(route.path_pattern),
                        text=getattr(fake_target_api, route.route_name),
                        headers=headers,
                    )
        self.mock = mock
        self.mock.start()
        return self

    def __exit__(self, *exc: Tuple[None, None, None]) -> bool:
        """
        Stop the Vuforia mock.

        Returns:
            False
        """
        self.mock.stop()
        return False
<|endoftext|>


<commit_before>from decimal import Decimal
import unittest
import datetime

import rdflib  # needed for eval(repr(...)) below
from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, _XSD_BOOLEAN
from rdflib.namespace import XSD

class TestLiteral(unittest.TestCase):
    def setUp(self):
        pass

    def test_repr_apostrophe(self):
        a = rdflib.Literal("'")
        b = eval(repr(a))
        self.assertEqual(a, b)

    def test_repr_quote(self):
        a = rdflib.Literal('"')
        b = eval(repr(a))
        self.assertEqual(a, b)

    def test_backslash(self):
        d = r"""
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:foo="http://example.org/foo#">
    <rdf:Description>
      <foo:bar>a\b</foo:bar>
    </rdf:Description>
</rdf:RDF>
"""
        g = rdflib.Graph()
        g.parse(data=d, format="xml")
        a = rdflib.Literal("a\\b")
        b = list(g.objects())[0]
        self.assertEqual(a, b)

    def test_literal_from_bool(self):
        l = rdflib.Literal(True)
        self.assertEqual(l.datatype, rdflib.XSD["boolean"])

class TestNew(unittest.TestCase):
    def testCantPassLangAndDatatype(self):
        self.assertRaises(
            TypeError, Literal, "foo", lang="en", datatype=URIRef("http://example.com/")
        )

    def testCantPassInvalidLang(self):
        self.assertRaises(
            ValueError, Literal, "foo", lang="999"
        )

    def testFromOtherLiteral(self):
        l = Literal(1)
        l2 = Literal(l)
        self.assertTrue(isinstance(l.value, int))
        self.assertTrue(isinstance(l2.value, int))

        # change datatype
        l = Literal("1")
        l2 = Literal(l, datatype=rdflib.XSD.integer)
        self.assertTrue(isinstance(l2.value, int))

    def testDatatypeGetsAutoURIRefConversion(self):
        # drewp disapproves of this behavior, but it should be
        # represented in the tests
        x = Literal("foo", datatype="http://example.com/")
        self.assertTrue(isinstance(x.datatype, URIRef))

        x = Literal("foo", datatype=Literal("pennies"))
        self.assertEqual(x.datatype, URIRef("pennies"))

class TestRepr(unittest.TestCase):
    def testOmitsMissingDatatypeAndLang(self):
        self.assertEqual(repr(Literal("foo")), "rdflib.term.Literal('foo')")

    def testOmitsMissingDatatype(self):
        self.assertEqual(
            repr(Literal("foo", lang="en")),
            "rdflib.term.Literal('foo', lang='en')",
        )

    def testOmitsMissingLang(self):
        self.assertEqual(
            repr(Literal("foo", datatype=URIRef("http://example.com/"))),
            "rdflib.term.Literal('foo', datatype=rdflib.term.URIRef('http://example.com/'))",
        )

    def testSubclassNameAppearsInRepr(self):
        class MyLiteral(Literal):
            pass

        x = MyLiteral("foo")
        self.assertEqual(repr(x), "MyLiteral('foo')")

class TestDoubleOutput(unittest.TestCase):
    def testNoDanglingPoint(self):
        """confirms the fix for https://github.com/RDFLib/rdflib/issues/237"""
        vv = Literal("0.88", datatype=_XSD_DOUBLE)
        out = vv._literal_n3(use_plain=True)
        self.assertTrue(out in ["8.8e-01", "0.88"], out)

class TestParseBoolean(unittest.TestCase):
    """confirms the fix for https://github.com/RDFLib/rdflib/issues/913"""

    def testTrueBoolean(self):
        test_value = Literal("tRue", datatype=_XSD_BOOLEAN)
        self.assertTrue(test_value.value)
        test_value = Literal("1", datatype=_XSD_BOOLEAN)
        self.assertTrue(test_value.value)

    def testFalseBoolean(self):
        test_value = Literal("falsE", datatype=_XSD_BOOLEAN)
        self.assertFalse(test_value.value)
        test_value = Literal("0", datatype=_XSD_BOOLEAN)
        self.assertFalse(test_value.value)

    def testNonFalseBoolean(self):
        test_value = Literal("abcd", datatype=_XSD_BOOLEAN)
        self.assertRaises(UserWarning)
        self.assertFalse(test_value.value)
        test_value = Literal("10", datatype=_XSD_BOOLEAN)
        self.assertRaises(UserWarning)
        self.assertFalse(test_value.value)

class TestBindings(unittest.TestCase):
    def testBinding(self):
        class a:
            def __init__(self, v):
                self.v = v[3:-3]

            def __str__(self):
                return "<<<%s>>>" % self.v

        dtA = rdflib.URIRef("urn:dt:a")
        bind(dtA, a)

        va = a("<<<2>>>")
        la = Literal(va, normalize=True)
        self.assertEqual(la.value, va)
        self.assertEqual(la.datatype, dtA)

        la2 = Literal("<<<2>>>", datatype=dtA)
        self.assertTrue(isinstance(la2.value, a))
        self.assertEqual(la2.value.v, va.v)

        class b:
            def __init__(self, v):
                self.v = v[3:-3]

            def __str__(self):
                return "B%s" % self.v

        dtB = rdflib.URIRef("urn:dt:b")
        bind(dtB, b, None, lambda x: "<<<%s>>>" % x)

        vb = b("<<<3>>>")
        lb = Literal(vb, normalize=True)
        self.assertEqual(lb.value, vb)
        self.assertEqual(lb.datatype, dtB)

    def testSpecificBinding(self):
        def lexify(s):
            return "--%s--" % s

        def unlexify(s):
            return s[2:-2]

        datatype = rdflib.URIRef("urn:dt:mystring")

        # Datatype-specific rule
        bind(datatype, str, unlexify, lexify, datatype_specific=True)

        s = "Hello"
        normal_l = Literal(s)
        self.assertEqual(str(normal_l), s)
        self.assertEqual(normal_l.toPython(), s)
        self.assertEqual(normal_l.datatype, None)

        specific_l = Literal("--%s--" % s, datatype=datatype)
        self.assertEqual(str(specific_l), lexify(s))
        self.assertEqual(specific_l.toPython(), s)
        self.assertEqual(specific_l.datatype, datatype)

class TestXsdLiterals(unittest.TestCase):
    def test_make_literals(self):
        """
        Tests literal construction.
        """
        inputs = [
            # these literals do not get converted to Python types
            ("ABCD", XSD.integer, None),
            ("ABCD", XSD.gYear, None),
            ("-10000", XSD.gYear, None),
            ("-1921-00", XSD.gYearMonth, None),
            ("1921-00", XSD.gMonthDay, None),
            ("1921-13", XSD.gMonthDay, None),
            ("-1921-00", XSD.gMonthDay, None),
            ("10", XSD.gDay, None),
            ("-1", XSD.gDay, None),
            ("0000", XSD.gYear, None),
            ("0000-00-00", XSD.date, None),
            ("NOT A VALID HEX STRING", XSD.hexBinary, None),
            ("NOT A VALID BASE64 STRING", XSD.base64Binary, None),
            # these literals get converted to python types
            ("1921-05-01", XSD.date, datetime.date),
            ("1921-05-01T00:00:00", XSD.dateTime, datetime.datetime),
            ("1921-05", XSD.gYearMonth, datetime.date),
            ("0001-01", XSD.gYearMonth, datetime.date),
            ("0001-12", XSD.gYearMonth, datetime.date),
            ("2002-01", XSD.gYearMonth, datetime.date),
            ("9999-01", XSD.gYearMonth, datetime.date),
            ("9999-12", XSD.gYearMonth, datetime.date),
            ("1921", XSD.gYear, datetime.date),
            ("2000", XSD.gYear, datetime.date),
            ("0001", XSD.gYear, datetime.date),
            ("9999", XSD.gYear, datetime.date),
            ("1982", XSD.gYear, datetime.date),
            ("2002", XSD.gYear, datetime.date),
            ("1921-05-01T00:00:00+00:30", XSD.dateTime, datetime.datetime),
            ("1921-05-01T00:00:00-00:30", XSD.dateTime, datetime.datetime),
            ("abcdef0123", XSD.hexBinary, bytes),
            ("", XSD.hexBinary, bytes),
            ("UkRGTGli", XSD.base64Binary, bytes),
            ("", XSD.base64Binary, bytes),
            ("0.0000000000000000000000000000001", XSD.decimal, Decimal),
            ("0.1", XSD.decimal, Decimal),
            ("1", XSD.integer, int),
        ]
        self.check_make_literals(inputs)

    @unittest.expectedFailure
    def test_make_literals_ki(self):
        """
        Known issues with literal construction.
        """
        inputs = [
            ("1921-01Z", XSD.gYearMonth, datetime.date),
            ("1921Z", XSD.gYear, datetime.date),
            ("1921-00", XSD.gYearMonth, datetime.date),
            ("1921-05-01Z", XSD.date, datetime.date),
            ("1921-05-01+00:30", XSD.date, datetime.date),
            ("1921-05-01+00:30", XSD.date, datetime.date),
            ("1921-05-01+00:00", XSD.date, datetime.date),
            ("1921-05-01+00:00", XSD.date, datetime.date),
            ("1921-05-01T00:00:00Z", XSD.dateTime, datetime.datetime),
            ("1e-31", XSD.decimal, None),  # This is not a valid decimal value
        ]
        self.check_make_literals(inputs)

    def check_make_literals(self, inputs):
        for literal_pair in inputs:
            (lexical, _type, value_cls) = literal_pair
            with self.subTest(f"testing {literal_pair}"):
                literal = Literal(lexical, datatype=_type)
                if value_cls is not None:
                    self.assertIsInstance(literal.value, value_cls)
                else:
                    self.assertIsNone(literal.value)
                self.assertEqual(lexical, f"{literal}")

if __name__ == "__main__":
    unittest.main()
<commit_msg>Add boolean sample to round-trip test<commit_after>from decimal import Decimal
import unittest
import datetime

import rdflib  # needed for eval(repr(...)) below
from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, _XSD_BOOLEAN
from rdflib.namespace import XSD

class TestLiteral(unittest.TestCase):
    def setUp(self):
        pass

    def test_repr_apostrophe(self):
        a = rdflib.Literal("'")
        b = eval(repr(a))
        self.assertEqual(a, b)

    def test_repr_quote(self):
        a = rdflib.Literal('"')
        b = eval(repr(a))
        self.assertEqual(a, b)

    def test_backslash(self):
        d = r"""
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:foo="http://example.org/foo#">
    <rdf:Description>
      <foo:bar>a\b</foo:bar>
    </rdf:Description>
</rdf:RDF>
"""
        g = rdflib.Graph()
        g.parse(data=d, format="xml")
        a = rdflib.Literal("a\\b")
        b = list(g.objects())[0]
        self.assertEqual(a, b)

    def test_literal_from_bool(self):
        l = rdflib.Literal(True)
        self.assertEqual(l.datatype, rdflib.XSD["boolean"])

class TestNew(unittest.TestCase):
    def testCantPassLangAndDatatype(self):
        self.assertRaises(
            TypeError, Literal, "foo", lang="en", datatype=URIRef("http://example.com/")
        )

    def testCantPassInvalidLang(self):
        self.assertRaises(
            ValueError, Literal, "foo", lang="999"
        )

    def testFromOtherLiteral(self):
        l = Literal(1)
        l2 = Literal(l)
        self.assertTrue(isinstance(l.value, int))
        self.assertTrue(isinstance(l2.value, int))

        # change datatype
        l = Literal("1")
        l2 = Literal(l, datatype=rdflib.XSD.integer)
        self.assertTrue(isinstance(l2.value, int))

    def testDatatypeGetsAutoURIRefConversion(self):
        # drewp disapproves of this behavior, but it should be
        # represented in the tests
        x = Literal("foo", datatype="http://example.com/")
        self.assertTrue(isinstance(x.datatype, URIRef))

        x = Literal("foo", datatype=Literal("pennies"))
        self.assertEqual(x.datatype, URIRef("pennies"))

class TestRepr(unittest.TestCase):
    def testOmitsMissingDatatypeAndLang(self):
        self.assertEqual(repr(Literal("foo")), "rdflib.term.Literal('foo')")

    def testOmitsMissingDatatype(self):
        self.assertEqual(
            repr(Literal("foo", lang="en")),
            "rdflib.term.Literal('foo', lang='en')",
        )

    def testOmitsMissingLang(self):
        self.assertEqual(
            repr(Literal("foo", datatype=URIRef("http://example.com/"))),
            "rdflib.term.Literal('foo', datatype=rdflib.term.URIRef('http://example.com/'))",
        )

    def testSubclassNameAppearsInRepr(self):
        class MyLiteral(Literal):
            pass

        x = MyLiteral("foo")
        self.assertEqual(repr(x), "MyLiteral('foo')")

class TestDoubleOutput(unittest.TestCase):
    def testNoDanglingPoint(self):
        """confirms the fix for https://github.com/RDFLib/rdflib/issues/237"""
        vv = Literal("0.88", datatype=_XSD_DOUBLE)
        out = vv._literal_n3(use_plain=True)
        self.assertTrue(out in ["8.8e-01", "0.88"], out)

class TestParseBoolean(unittest.TestCase):
    """confirms the fix for https://github.com/RDFLib/rdflib/issues/913"""

    def testTrueBoolean(self):
        test_value = Literal("tRue", datatype=_XSD_BOOLEAN)
        self.assertTrue(test_value.value)
        test_value = Literal("1", datatype=_XSD_BOOLEAN)
        self.assertTrue(test_value.value)

    def testFalseBoolean(self):
        test_value = Literal("falsE", datatype=_XSD_BOOLEAN)
        self.assertFalse(test_value.value)
        test_value = Literal("0", datatype=_XSD_BOOLEAN)
        self.assertFalse(test_value.value)

    def testNonFalseBoolean(self):
        test_value = Literal("abcd", datatype=_XSD_BOOLEAN)
        self.assertRaises(UserWarning)
        self.assertFalse(test_value.value)
        test_value = Literal("10", datatype=_XSD_BOOLEAN)
        self.assertRaises(UserWarning)
        self.assertFalse(test_value.value)

class TestBindings(unittest.TestCase):
    def testBinding(self):
        class a:
            def __init__(self, v):
                self.v = v[3:-3]

            def __str__(self):
                return "<<<%s>>>" % self.v

        dtA = rdflib.URIRef("urn:dt:a")
        bind(dtA, a)

        va = a("<<<2>>>")
        la = Literal(va, normalize=True)
        self.assertEqual(la.value, va)
        self.assertEqual(la.datatype, dtA)

        la2 = Literal("<<<2>>>", datatype=dtA)
        self.assertTrue(isinstance(la2.value, a))
        self.assertEqual(la2.value.v, va.v)

        class b:
            def __init__(self, v):
                self.v = v[3:-3]

            def __str__(self):
                return "B%s" % self.v

        dtB = rdflib.URIRef("urn:dt:b")
        bind(dtB, b, None, lambda x: "<<<%s>>>" % x)

        vb = b("<<<3>>>")
        lb = Literal(vb, normalize=True)
        self.assertEqual(lb.value, vb)
        self.assertEqual(lb.datatype, dtB)

    def testSpecificBinding(self):
        def lexify(s):
            return "--%s--" % s

        def unlexify(s):
            return s[2:-2]

        datatype = rdflib.URIRef("urn:dt:mystring")

        # Datatype-specific rule
        bind(datatype, str, unlexify, lexify, datatype_specific=True)

        s = "Hello"
        normal_l = Literal(s)
        self.assertEqual(str(normal_l), s)
        self.assertEqual(normal_l.toPython(), s)
        self.assertEqual(normal_l.datatype, None)

        specific_l = Literal("--%s--" % s, datatype=datatype)
        self.assertEqual(str(specific_l), lexify(s))
        self.assertEqual(specific_l.toPython(), s)
        self.assertEqual(specific_l.datatype, datatype)

class TestXsdLiterals(unittest.TestCase):
    def test_make_literals(self):
        """
        Tests literal construction.
        """
        inputs = [
            # these literals do not get converted to Python types
            ("ABCD", XSD.integer, None),
            ("ABCD", XSD.gYear, None),
            ("-10000", XSD.gYear, None),
            ("-1921-00", XSD.gYearMonth, None),
            ("1921-00", XSD.gMonthDay, None),
            ("1921-13", XSD.gMonthDay, None),
            ("-1921-00", XSD.gMonthDay, None),
            ("10", XSD.gDay, None),
            ("-1", XSD.gDay, None),
            ("0000", XSD.gYear, None),
            ("0000-00-00", XSD.date, None),
            ("NOT A VALID HEX STRING", XSD.hexBinary, None),
            ("NOT A VALID BASE64 STRING", XSD.base64Binary, None),
            # these literals get converted to python types
            ("1921-05-01", XSD.date, datetime.date),
            ("1921-05-01T00:00:00", XSD.dateTime, datetime.datetime),
            ("1921-05", XSD.gYearMonth, datetime.date),
            ("0001-01", XSD.gYearMonth, datetime.date),
            ("0001-12", XSD.gYearMonth, datetime.date),
            ("2002-01", XSD.gYearMonth, datetime.date),
            ("9999-01", XSD.gYearMonth, datetime.date),
            ("9999-12", XSD.gYearMonth, datetime.date),
            ("1921", XSD.gYear, datetime.date),
            ("2000", XSD.gYear, datetime.date),
            ("0001", XSD.gYear, datetime.date),
            ("9999", XSD.gYear, datetime.date),
            ("1982", XSD.gYear, datetime.date),
            ("2002", XSD.gYear, datetime.date),
            ("1921-05-01T00:00:00+00:30", XSD.dateTime, datetime.datetime),
            ("1921-05-01T00:00:00-00:30", XSD.dateTime, datetime.datetime),
            ("true", XSD.boolean, bool),
            ("abcdef0123", XSD.hexBinary, bytes),
            ("", XSD.hexBinary, bytes),
            ("UkRGTGli", XSD.base64Binary, bytes),
            ("", XSD.base64Binary, bytes),
            ("0.0000000000000000000000000000001", XSD.decimal, Decimal),
            ("0.1", XSD.decimal, Decimal),
            ("1", XSD.integer, int),
        ]
        self.check_make_literals(inputs)

    @unittest.expectedFailure
    def test_make_literals_ki(self):
        """
        Known issues with literal construction.
        """
        inputs = [
            ("1921-01Z", XSD.gYearMonth, datetime.date),
            ("1921Z", XSD.gYear, datetime.date),
            ("1921-00", XSD.gYearMonth, datetime.date),
            ("1921-05-01Z", XSD.date, datetime.date),
            ("1921-05-01+00:30", XSD.date, datetime.date),
            ("1921-05-01+00:30", XSD.date, datetime.date),
            ("1921-05-01+00:00", XSD.date, datetime.date),
            ("1921-05-01+00:00", XSD.date, datetime.date),
            ("1921-05-01T00:00:00Z", XSD.dateTime, datetime.datetime),
            ("1e-31", XSD.decimal, None),  # This is not a valid decimal value
        ]
        self.check_make_literals(inputs)

    def check_make_literals(self, inputs):
        for literal_pair in inputs:
            (lexical, _type, value_cls) = literal_pair
            with self.subTest(f"testing {literal_pair}"):
                literal = Literal(lexical, datatype=_type)
                if value_cls is not None:
                    self.assertIsInstance(literal.value, value_cls)
                else:
                    self.assertIsNone(literal.value)
                self.assertEqual(lexical, f"{literal}")

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


<commit_before>"""
Sequence Annotations testing on the test data
"""

import unittest

import ga4gh.protocol as protocol
import ga4gh.frontend as frontend

import logging

class TestSequenceAnnotations(unittest.TestCase):
    exampleUrl = 'www.example.com'
    datasetId = "YnJjYTE"

    @classmethod
    def setUpClass(cls):
        config = {
            "DATA_SOURCE": "tests/data",
            "DEBUG" : False
        }
        logging.getLogger('ga4gh.frontend.cors').setLevel(logging.CRITICAL)
        frontend.reset()
        frontend.configure(
            baseConfig="TestConfig", extraConfig=config)
        cls.app = frontend.app.test_client()

    @classmethod
    def tearDownClass(cls):
        cls.app = None

    def sendSearchRequest(self, path, request, responseClass):
        """
        Sends the specified protocol request instance as JSON, and
        parses the result into an instance of the specified response.
        """
        response = self.sendJsonPostRequest(path, request.toJsonString())
        self.assertEqual(200, response.status_code)
        responseData = responseClass.fromJsonString(response.data)
        self.assertTrue(responseData.validate(responseData.toJsonDict()))
        return responseData

    def getAllDatasets(self):
        path = 'datasets/search'
        request = protocol.SearchDatasetsRequest()
        responseData = self.sendSearchRequest(
            path, request, protocol.SearchDatasetsResponse)
        return responseData.datasets

    def getAllFeatureSets(self):
        datasetId = self.getAllDatasets()[0].id
        path = 'featuresets/search'
        request = protocol.SearchFeatureSetsRequest()
        request.datasetId = datasetId
        responseData = self.sendSearchRequest(
            path, request, protocol.SearchFeatureSetsResponse)
        return responseData.featureSets

    def testSearchFeatures(self):
        datasetId = self.getAllDatasets()[0].id
        featureSets = self.getAllFeatureSets()
        for featureSet in featureSets:
            path = "features/search"
            request = protocol.SearchFeaturesRequest()
            request.datasetId = datasetId
            request.featureSetId = featureSet.id
            request.start = 0
            request.end = 2**16
            request.featureTypes = ["exon"]
            request.referenceName = "chr1"
            request.featureSetId = featureSet.id
            responseData = self.sendSearchRequest(
                path, request, protocol.SearchFeaturesResponse)
            for feature in responseData.features:
                self.assertIn(
                    feature.featureType.term,
                    request.featureTypes,
                    "Term should be present {} {} \n{}\n{}".format(
                        feature.featureType.term,
                        request.featureTypes,
                        feature, request))

            path = "features/search"
            request = protocol.SearchFeaturesRequest()
            request.datasetId = datasetId
            request.featureSetId = featureSet.id
            request.start = 0
            request.end = 2**16
            request.featureTypes = ["gene", "exon"]
            request.referenceName = "chr1"
            request.featureSetId = featureSet.id
            responseData = self.sendSearchRequest(
                path, request, protocol.SearchFeaturesResponse)
            for feature in responseData.features:
                self.assertIn(feature.featureType.term, request.featureTypes)

            request = protocol.SearchFeaturesRequest()
            request.datasetId = datasetId
            request.featureSetId = featureSet.id
            request.start = 0
            request.end = 2**16
            request.featureTypes = ["exon"]
            request.referenceName = "chr1"
            request.featureSetId = featureSet.id
            responseData = self.sendSearchRequest(
                path, request, protocol.SearchFeaturesResponse)
            for feature in responseData.features:
                self.assertIn(feature.featureType.term, request.featureTypes)

    def sendJsonPostRequest(self, path, data):
        """
        Sends a JSON request to the specified path with the specified data
        and returns the response.
        """
        return self.app.post(
            path, headers={'Content-type': 'application/json'},
            data=data)
<commit_msg>Fixed flake8 complaint.<commit_after>"""
Sequence Annotations testing on the test data
"""

import unittest

import ga4gh.protocol as protocol
import ga4gh.frontend as frontend

import logging

class TestSequenceAnnotations(unittest.TestCase):
    exampleUrl = 'www.example.com'
    datasetId = "YnJjYTE"

    @classmethod
    def setUpClass(cls):
        config = {
            "DATA_SOURCE": "tests/data",
            "DEBUG": False
        }
        logging.getLogger('ga4gh.frontend.cors').setLevel(logging.CRITICAL)
        frontend.reset()
        frontend.configure(
            baseConfig="TestConfig", extraConfig=config)
        cls.app = frontend.app.test_client()

    @classmethod
    def tearDownClass(cls):
        cls.app = None

    def sendSearchRequest(self, path, request, responseClass):
        """
        Sends the specified protocol request instance as JSON, and
        parses the result into an instance of the specified response.
        """
        response = self.sendJsonPostRequest(path, request.toJsonString())
        self.assertEqual(200, response.status_code)
        responseData = responseClass.fromJsonString(response.data)
        self.assertTrue(responseData.validate(responseData.toJsonDict()))
        return responseData

    def getAllDatasets(self):
        path = 'datasets/search'
        request = protocol.SearchDatasetsRequest()
        responseData = self.sendSearchRequest(
            path, request, protocol.SearchDatasetsResponse)
        return responseData.datasets

    def getAllFeatureSets(self):
        datasetId = self.getAllDatasets()[0].id
        path = 'featuresets/search'
        request = protocol.SearchFeatureSetsRequest()
        request.datasetId = datasetId
        responseData = self.sendSearchRequest(
            path, request, protocol.SearchFeatureSetsResponse)
        return responseData.featureSets

    def testSearchFeatures(self):
        datasetId = self.getAllDatasets()[0].id
        featureSets = self.getAllFeatureSets()
        for featureSet in featureSets:
            path = "features/search"
            request = protocol.SearchFeaturesRequest()
            request.datasetId = datasetId
            request.featureSetId = featureSet.id
            request.start = 0
            request.end = 2**16
            request.featureTypes = ["exon"]
            request.referenceName = "chr1"
            request.featureSetId = featureSet.id
            responseData = self.sendSearchRequest(
                path, request, protocol.SearchFeaturesResponse)
            for feature in responseData.features:
                self.assertIn(
                    feature.featureType.term,
                    request.featureTypes,
                    "Term should be present {} {} \n{}\n{}".format(
                        feature.featureType.term,
                        request.featureTypes,
                        feature, request))

            path = "features/search"
            request = protocol.SearchFeaturesRequest()
            request.datasetId = datasetId
            request.featureSetId = featureSet.id
            request.start = 0
            request.end = 2**16
            request.featureTypes = ["gene", "exon"]
            request.referenceName = "chr1"
            request.featureSetId = featureSet.id
            responseData = self.sendSearchRequest(
                path, request, protocol.SearchFeaturesResponse)
            for feature in responseData.features:
                self.assertIn(feature.featureType.term, request.featureTypes)

            request = protocol.SearchFeaturesRequest()
            request.datasetId = datasetId
            request.featureSetId = featureSet.id
            request.start = 0
            request.end = 2**16
            request.featureTypes = ["exon"]
            request.referenceName = "chr1"
            request.featureSetId = featureSet.id
            responseData = self.sendSearchRequest(
                path, request, protocol.SearchFeaturesResponse)
            for feature in responseData.features:
                self.assertIn(feature.featureType.term, request.featureTypes)

    def sendJsonPostRequest(self, path, data):
        """
        Sends a JSON request to the specified path with the specified data
        and returns the response.
        """
        return self.app.post(
            path, headers={'Content-type': 'application/json'},
            data=data)
<|endoftext|>


<commit_before>from __future__ import print_function

import numpy
import sys

import kernel_tuner
from .context import skip_if_no_cuda, skip_if_no_noodles

def test_random_sample():

    kernel_string = "float test_kernel(float *a) { return 1.0f; }"
    a = numpy.array([1,2,3]).astype(numpy.float32)

    tune_params = {"block_size_x": range(1,25)}
    print(tune_params)

    result, _ = kernel_tuner.tune_kernel("test_kernel", kernel_string, (1,1), [a], tune_params, sample_fraction=0.1)

    print(result)

    #check that number of benchmarked kernels is 10% (rounded up)
    assert len(result) == 3

    #check all returned results make sense
    for v in result:
        assert v['time'] == 1.0

@skip_if_no_noodles
@skip_if_no_cuda
def test_noodles_runner():

    kernel_string = """
    __global__ void vector_add(float *c, float *a, float *b, int n) {
        int i = blockIdx.x * block_size_x + threadIdx.x;
        if (i<n) {
            c[i] = a[i] + b[i];
        }
    }
    """

    size = 100
    a = numpy.random.randn(size).astype(numpy.float32)
    b = numpy.random.randn(size).astype(numpy.float32)
    c = numpy.zeros_like(b)
    n = numpy.int32(size)

    args = [c, a, b, n]
    tune_params = {"block_size_x": [128+64*i for i in range(15)]}

    result, _ = kernel_tuner.tune_kernel("vector_add", kernel_string, size, args, tune_params,
                            use_noodles=True, num_threads=4)

    assert len(result) == len(tune_params["block_size_x"])

def get_vector_add_args():
    size = int(1e6)
    a = numpy.random.randn(size).astype(numpy.float32)
    b = numpy.random.randn(size).astype(numpy.float32)
    c = numpy.zeros_like(b).astype(numpy.float32)
    n = numpy.int32(size)
    return c, a, b, n

@skip_if_no_cuda
def test_diff_evo():

    kernel_string = """
    __global__ void vector_add(float *c, float *a, float *b, int n) {
        int i = blockIdx.x * block_size_x + threadIdx.x;
        if (i<n) {
            c[i] = a[i] + b[i];
        }
    } """

    args = get_vector_add_args()
    tune_params = {"block_size_x": [128+64*i for i in range(5)]}

    result, _ = kernel_tuner.tune_kernel("vector_add", kernel_string, args[-1], args, tune_params,
                            method="diff_evo", verbose=True)

    print(result)
    assert len(result) > 0

@skip_if_no_cuda
def test_sequential_runner_alt_block_size_names():

    kernel_string = """__global__ void vector_add(float *c, float *a, float *b, int n) {
        int i = blockIdx.x * block_dim_x + threadIdx.x;
        if (i<n) {
            c[i] = a[i] + b[i];
        }
    }
    """

    c, a, b, n = get_vector_add_args()
    args = [c, a, b, n]
    tune_params = {"block_dim_x": [128+64*i for i in range(5)]}

    ref = (a+b).astype(numpy.float32)
    answer = [ref, None, None, None]

    block_size_names = ["block_dim_x"]

    result, _ = kernel_tuner.tune_kernel("vector_add", kernel_string, int(n), args,
                            tune_params, grid_div_x=["block_dim_x"], answer=answer,
                            block_size_names=block_size_names)

    assert len(result) == len(tune_params["block_dim_x"])

@skip_if_no_cuda
def test_sequential_runner_not_matching_answer1():
    kernel_string = """__global__ void vector_add(float *c, float *a, float *b, int n) {
            int i = blockIdx.x * block_size_x + threadIdx.x;
            if (i<n) {
                c[i] = a[i] + b[i];
            }
        } """
    args = get_vector_add_args()
    answer = [args[1] + args[2]]
    tune_params = {"block_size_x": [128 + 64 * i for i in range(5)]}

    try:
        result, _ = kernel_tuner.tune_kernel("vector_add", kernel_string, args[-1], args, tune_params,
                                         method="diff_evo", verbose=True, answer=answer)
        print("Expected a TypeError to be raised")
        assert False
    except TypeError as expected_error:
        print(str(expected_error))
        assert "The length of argument list and provided results do not match." == str(expected_error)
    except Exception:
        print("Expected a TypeError to be raised")
        assert False<commit_msg>Added a test to check the detection of different types in expected results and arguments.<commit_after>from __future__ import print_function

import numpy
import sys

import kernel_tuner
from .context import skip_if_no_cuda, skip_if_no_noodles

def test_random_sample():

    kernel_string = "float test_kernel(float *a) { return 1.0f; }"
    a = numpy.array([1,2,3]).astype(numpy.float32)

    tune_params = {"block_size_x": range(1,25)}
    print(tune_params)

    result, _ = kernel_tuner.tune_kernel("test_kernel", kernel_string, (1,1), [a], tune_params, sample_fraction=0.1)

    print(result)

    #check that number of benchmarked kernels is 10% (rounded up)
    assert len(result) == 3

    #check all returned results make sense
    for v in result:
        assert v['time'] == 1.0

@skip_if_no_noodles
@skip_if_no_cuda
def test_noodles_runner():

    kernel_string = """
    __global__ void vector_add(float *c, float *a, float *b, int n) {
        int i = blockIdx.x * block_size_x + threadIdx.x;
        if (i<n) {
            c[i] = a[i] + b[i];
        }
    }
    """

    size = 100
    a = numpy.random.randn(size).astype(numpy.float32)
    b = numpy.random.randn(size).astype(numpy.float32)
    c = numpy.zeros_like(b)
    n = numpy.int32(size)

    args = [c, a, b, n]
    tune_params = {"block_size_x": [128+64*i for i in range(15)]}

    result, _ = kernel_tuner.tune_kernel("vector_add", kernel_string, size, args, tune_params,
                            use_noodles=True, num_threads=4)

    assert len(result) == len(tune_params["block_size_x"])

def get_vector_add_args():
    size = int(1e6)
    a = numpy.random.randn(size).astype(numpy.float32)
    b = numpy.random.randn(size).astype(numpy.float32)
    c = numpy.zeros_like(b).astype(numpy.float32)
    n = numpy.int32(size)
    return c, a, b, n

@skip_if_no_cuda
def test_diff_evo():

    kernel_string = """
    __global__ void vector_add(float *c, float *a, float *b, int n) {
        int i = blockIdx.x * block_size_x + threadIdx.x;
        if (i<n) {
            c[i] = a[i] + b[i];
        }
    } """

    args = get_vector_add_args()
    tune_params = {"block_size_x": [128+64*i for i in range(5)]}

    result, _ = kernel_tuner.tune_kernel("vector_add", kernel_string, args[-1], args, tune_params,
                            method="diff_evo", verbose=True)

    print(result)
    assert len(result) > 0

@skip_if_no_cuda
def test_sequential_runner_alt_block_size_names():

    kernel_string = """__global__ void vector_add(float *c, float *a, float *b, int n) {
        int i = blockIdx.x * block_dim_x + threadIdx.x;
        if (i<n) {
            c[i] = a[i] + b[i];
        }
    }
    """

    c, a, b, n = get_vector_add_args()
    args = [c, a, b, n]
    tune_params = {"block_dim_x": [128+64*i for i in range(5)]}

    ref = (a+b).astype(numpy.float32)
    answer = [ref, None, None, None]

    block_size_names = ["block_dim_x"]

    result, _ = kernel_tuner.tune_kernel("vector_add", kernel_string, int(n), args,
                            tune_params, grid_div_x=["block_dim_x"], answer=answer,
                            block_size_names=block_size_names)

    assert len(result) == len(tune_params["block_dim_x"])

@skip_if_no_cuda
def test_sequential_runner_not_matching_answer1():
    kernel_string = """__global__ void vector_add(float *c, float *a, float *b, int n) {
            int i = blockIdx.x * block_size_x + threadIdx.x;
            if (i<n) {
                c[i] = a[i] + b[i];
            }
        } """
    args = get_vector_add_args()
    answer = [args[1] + args[2]]
    tune_params = {"block_size_x": [128 + 64 * i for i in range(5)]}

    try:
        result, _ = kernel_tuner.tune_kernel("vector_add", kernel_string, args[-1], args, tune_params,
                                         method="diff_evo", verbose=True, answer=answer)
        print("Expected a TypeError to be raised")
        assert False
    except TypeError as expected_error:
        print(str(expected_error))
        assert "The length of argument list and provided results do not match." == str(expected_error)
    except Exception:
        print("Expected a TypeError to be raised")
        assert False

@skip_if_no_cuda
def test_sequential_runner_not_matching_answer2():
    kernel_string = """__global__ void vector_add(float *c, float *a, float *b, int n) {
            int i = blockIdx.x * block_size_x + threadIdx.x;
            if (i<n) {
                c[i] = a[i] + b[i];
            }
        } """
    args = get_vector_add_args()
    answer = [numpy.ubyte([12]), None, None, None]
    tune_params = {"block_size_x": [128 + 64 * i for i in range(5)]}

    try:
        result, _ = kernel_tuner.tune_kernel("vector_add", kernel_string, args[-1], args, tune_params,
                                         method="diff_evo", verbose=True, answer=answer)
        print("Expected a TypeError to be raised")
        assert False
    except TypeError as expected_error:
        print(str(expected_error))
        assert "Element 0" in str(expected_error)
    except Exception:
        print("Expected a TypeError to be raised")
        assert False<|endoftext|>


<commit_before>"""Tests priority rules."""
from __future__ import absolute_import
import unittest
import tests.util

PRE_CONFIG = """
report _SCORE_
report _TESTS_
"""

CONFIG = r"""
# Rule definitions here

body UNLIMITED /unlimited/i
score UNLIMITED 10.45

body EMAIL /email/
score EMAIL -12.15
priority EMAIL 5

body SPAM_TEST /spam_test/i
score SPAM_TEST 55.12

body SPAM_ON /spam_on/i
score SPAM_ON 25.73

body HAM_TEST /ham_test/
score HAM_TEST -75.3

body HAM_ON /ham_on/i
score HAM_ON -35.01

body TEST_RULE /abcd/
score TEST_RULE 2.5
priority TEST_RULE 2
"""

MSG = """Received: from 127.0.0.1  (EHLO m1.util24.eu) [213.133.99.176]
  by mta1329.mail.bf1.yahoo.com with SMTPS; Fri, 29 Jan 2016 05:57:28 +0000
Subject: Test Message.
X-Header: match_header

This is a test message for the Short Circuit Plugin.
Adjust the body for your case: %s
"""

class TestPriorityRules(tests.util.TestBase):
    @unittest.skip("This test fails at the moment and the code should be"
                   "fixed")
    def test_priority_rule_match(self):
        """Test the priority for rules"""
        self.setup_conf(config=CONFIG, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "abcd email")
        print(result)
        self.check_report(result, -8.7, ["EMAIL", "TEST_RULE"])
<commit_msg>Refs #42. Add more functional tests for priority option<commit_after>"""Tests priority rules."""
from __future__ import absolute_import

import tests.util
import unittest

PRE_CONFIG = """
report _SCORE_
report _TESTS_
"""

CONFIG_BODY = r"""
body UNLIMITED /unlimited/i
priority UNLIMITED 3

body EMAIL /email/
priority EMAIL -5

body SPAM_TEST /spam_test/i
priority SPAM_TEST -5

body SPAM_ON /spam_on/i
priority SPAM_ON -6

body HAM_TEST /ham_test/
priority HAM_TEST 2
priority HAM_TEST 1

body HAM_ON /ham_on/i
priority HAM_ON 2

body TEST_RULE /abcd/
priority TEST_RULE 2

body TEST_OUT /test_out/
priority TEST_OUT  3
priority TEST_OUT -1

body MONEY /money/
"""

CONFIG_HEADER = r"""
header      LOOK_FOR_SUBJECT   Subject =~ /subject/
priority LOOK_FOR_SUBJECT 2

header      LOOK_FOR_SUBJECT_SPAM   Subject =~ /spam/
priority LOOK_FOR_SUBJECT_SPAM 3

header      LOOK_FOR_SUBJECT_HAM   Subject =~ /ham/
"""

CONFIG_FULL = r"""
full        NULL_IN_MESSAGES       /\x00/
priority    NULL_IN_MESSAGES 2

full        NULL_IN_MESSAGES_SECOND       /\x000/
priority    NULL_IN_MESSAGES_SECOND 2
"""

CONFIG_MIMEHEADER = r"""
mimeheader  HAS_PDF_ATTACHMENT  Content-Type =~ /^application\/pdf/i
priority  HAS_PDF_ATTACHMENT 2

mimeheader  HAS_JPEG_ATTACHMENT  Content-Type =~ /^application\/jpeg/i
priority HAS_JPEG_ATTACHMENT 3
"""

CONFIG_URI = r"""
uri         HAS_EXAMPLE_HTTPS   /^https:\/\/example.com$/\
priority HAS_EXAMPLE_HTTPS 2

uri         HAS_EXAMPLE_HTTPS_SECOND   /^https:\/\/example.com$/\z
priority HAS_EXAMPLE_HTTPS_SECOND 3
"""

CONFIG_META = r"""
meta        NO_EXAMPLE_DKIM         __EXAMPLE_COM_SENDER && !__DKIM_EXISTS
priority NO_EXAMPLE_DKIM 2

meta        EXAMPLE_URL_SENDER      __EXAMPLE_COM_SENDER || __HAS_EXAMPLE_HTTPS
priority EXAMPLE_URL_SENDER 3

meta        NO_DKIM_AND_URL         EXAMPLE_URL_SENDER && NO_EXAMPLE_DKIM
priority NO_DKIM_AND_URL 4
"""

CONFIG_EVAL = r"""
full        PYZOR_CHECK     eval:check_pyzor()
priority PYZOR_CHECK 2

full        PYZOR_CHECK_SECOND    seval:check_pyzor()
priority PYZOR_CHECK_SECOND 3
"""

MSG = """Received: from 127.0.0.1  (EHLO m1.util24.eu) [213.133.99.176]
  by mta1329.mail.bf1.yahoo.com with SMTPS; Fri, 29 Jan 2016 05:57:28 +0000
Subject: Test Message.
X-Header: match_header

This is a test message.
Adjust the body for your case: %s
"""

MSG_MIMEHEADER = """Received: from 127.0.0.1  (EHLO m1.util24.eu) [21.13.99.1]
  by mta1329.mail.bf1.yahoo.com with SMTPS; Fri, 29 Jan 2016 05:57:28 +0000
Subject: Test Message.
Content-Type: application/pdf; name="file.pdf"
Content-Type: application/jpeg; name="picture.jpeg"

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_MIME_BOUNDARY_000_36205"

------=_MIME_BOUNDARY_000_36205
Content-Type: text/plain

This is a test mailing
------=_MIME_BOUNDARY_000_36205
Content-Type: application/octet-stream; name="fisier.pdf"
Content-Description: fisier.pdf
Content-Disposition: attachment; filename="fisier.pdf"
Content-Transfer-Encoding: BASE64

------=_MIME_BOUNDARY_000_36205--

This is a test message.
Adjust the body for your case: %s
"""

class TestPriorityRules(tests.util.TestBase):

    def test_priority_rule_both_negative_priority(self):
        """Test two rules with two negative priority"""
        self.setup_conf(config=CONFIG_BODY, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "SPAM_ON email ")
        self.check_report(result, 2.0, ["EMAIL", "SPAM_ON"])

    def test_priority_rule_negative_and_positive(self):
        """Test two rules with negative and positive priority"""
        self.setup_conf(config=CONFIG_BODY, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "abcd email")
        self.check_report(result, 2.0, ["TEST_RULE", "EMAIL"])

    def test_priority_rule_same_priority_positive(self):
        """Test two rules with same priority positive"""
        self.setup_conf(config=CONFIG_BODY, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "HAM_ON abcd")
        self.check_report(result, 2.0, ["HAM_ON", "TEST_RULE"])

    def test_priority_rule_same_priority_negative(self):
        """Test two rules with same priority negative"""
        self.setup_conf(config=CONFIG_BODY, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "SPAM_TEST email")
        self.check_report(result, 2.0, ["EMAIL", "SPAM_TEST"])

    def test_priority_for_one_rule_two_priority_positive(self):
        """Test for one rule with two different priority positives"""
        self.setup_conf(config=CONFIG_BODY, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "ham_test email")
        self.check_report(result, 2.0, ["HAM_TEST", "EMAIL"])

    def test_priority_for_one_rule_two_priority_negative(self):
        """Test for one rule with two different priority negatives"""
        self.setup_conf(config=CONFIG_BODY, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "test_out email")
        self.check_report(result, 2.0, ["TEST_OUT", "EMAIL"])

    def test_priority_for_one_rule_with_two_priority_combined(self):
        """Test for one rule with two different priority positive and"""
        """negative"""
        self.setup_conf(config=CONFIG_BODY, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "test_out email")
        self.check_report(result, 2.0, ["TEST_OUT", "EMAIL"])

    def test_priority_multiple_rules(self):
        """Test three rules, one with priorities `3`, `-5`, `-5`"""
        self.setup_conf(config=CONFIG_BODY, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "UNLIMITED email SPAM_TEST")
        self.check_report(result, 3.0, ["UNLIMITED", "SPAM_TEST", "EMAIL"])

    def test_priority_rule_with_default(self):
        """Test three rules, one with priorities default, `2`, `-5`"""
        self.setup_conf(config=CONFIG_BODY, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "money email abcd")
        self.check_report(result, 3.0, ["MONEY", "TEST_RULE", "EMAIL"])

    @unittest.skip("This test fails at the moment and the code should be"
                   "fixed")
    def test_priority_rule_for_header_rules(self):
        """Test two rules with two negative priority"""
        self.setup_conf(config=CONFIG_HEADER, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "subject spam", debug=True)
        print(result)
        self.check_report(result, 2.0, ["LOOK_FOR_SUBJECT_SPAM",
                                        "LOOK_FOR_SUBJECT"])

    @unittest.skip("This is not finished and the test fails")
    def test_priority_rule_for_full_rules(self):
        """Test two rules with two negative priority"""
        self.setup_conf(config=CONFIG_FULL, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "\x00 \x000", debug=True)
        print(result)
        self.check_report(result, 2.0, ["NULL_IN_MESSAGES",
                                        "NULL_IN_MESSAGES_SECOND"])

    @unittest.skip("This is not finished and the test fails")
    def test_priority_rule_for_mimeheader_rules(self):
        """Test two rules with two negative priority"""
        self.setup_conf(config=CONFIG_MIMEHEADER, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG_MIMEHEADER % "test", debug=True)
        print(result)
        self.check_report(result, 3.0, ["HAS_JPEG_ATTACHMENT",
                                        "HAS_PDF_ATTACHMENT"])

    @unittest.skip("This is not finished and the test fails")
    def test_priority_rule_for_url_rules(self):
        """Test two rules with two negative priority"""
        self.setup_conf(config=CONFIG_URI, pre_config=PRE_CONFIG)
        result = self.check_pad(
          MSG % "/^https:\/\/example.com$/\ /^https:\/\/example.com$/\second")
        print(result)
        self.check_report(result, 2.0, ["HAS_EXAMPLE_HTTPS_SECOND",
                                        "HAS_EXAMPLE_HTTPS"])

    @unittest.skip("This is not finished and the test fails")
    def test_priority_rule_for_meta_rules(self):
        """Test two rules with two negative priority"""
        self.setup_conf(config=CONFIG_META, pre_config=PRE_CONFIG)
        result = self.check_pad(
          MSG % "")
        print(result)
        self.check_report(result, 2.0, ["NO_DKIM_AND_URL",
                                        "EXAMPLE_URL_SENDER",
                                        "NO_EXAMPLE_DKIM"])

    @unittest.skip("This is not finished and the test fails")
    def test_priority_rule_for_eval_rules(self):
        """Test two rules with two negative priority"""
        self.setup_conf(config=CONFIG_EVAL, pre_config=PRE_CONFIG)
        result = self.check_pad(MSG % "eval:check_pyzor() seval:check_pyzor()")
        print(result)
        self.check_report(result, 2.0, ["PYZOR_CHECK_SECOND", "PYZOR_CHECK"])
<|endoftext|>


<commit_before>##################################################################################
#                                                                                #
# MngIt: Manage redundant information in software                                #
#                                                                                #
# Copyright (c) 2016 Pierre Fernique                                             #
#                                                                                #
# This software is distributed under the CeCILL-C license. You should have       #
# received a copy of the legalcode along with this work. If not, see             #
# <http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html>.                 #
#                                                                                #
# File authors: Pierre Fernique <pfernique@gmail.com> (29)                       #
#                                                                                #
##################################################################################

import os
import unittest
import subprocess

from pkgtk.config import load_config
from pkgtk.scripts import pkgtk

class TestScripts(unittest.TestCase):
    """Test functions related to scripts"""

    @classmethod
    def setUpClass(cls):
        cls.repository = '.'
        cls.config = load_config(cls.repository)
        os.rename(cls.repository + os.sep + '.pkgtk.back', cls.repository + os.sep + '.pkgtk.yml')
        if 'basename' in cls.config['authors']:
            os.rename(cls.repository + os.sep + cls.config['authors']['basename'], cls.repository + os.sep + cls.config['authors']['basename'] + '.back')

    def test_authors(self):
        """Test `pkgtk authors` script of module `pkgtk.console_scripts`"""
        pkgtk(['authors'])
        pkgtk(['authors']+['--' + key + '=' + str(value) for key, value in self.config['authors'].iteritems()])

    @classmethod
    def tearDownClass(cls):
        os.rename(cls.repository + os.sep + '.pkgtk.back', cls.repository + os.sep + '.pkgtk.yml')
        if 'basename' in cls.config['authors']:
            os.rename(cls.repository + os.sep + cls.config['authors']['basename'] + '.back', cls.repository + os.sep + cls.config['authors']['basename'])<commit_msg>Update scripts<commit_after>##################################################################################
#                                                                                #
# MngIt: Manage redundant information in software                                #
#                                                                                #
# Copyright (c) 2016 Pierre Fernique                                             #
#                                                                                #
# This software is distributed under the CeCILL-C license. You should have       #
# received a copy of the legalcode along with this work. If not, see             #
# <http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html>.                 #
#                                                                                #
# File authors: Pierre Fernique <pfernique@gmail.com> (29)                       #
#                                                                                #
##################################################################################

import os
import unittest
import subprocess

from pkgtk.config import load_config
from pkgtk.scripts import pkgtk

class TestScripts(unittest.TestCase):
    """Test functions related to scripts"""

    @classmethod
    def setUpClass(cls):
        cls.repository = '.'
        cls.config = load_config(cls.repository)
        os.rename(cls.repository + os.sep + '.pkgtk.yml', cls.repository + os.sep + '.pkgtk.back')
        if 'basename' in cls.config['authors']:
            os.rename(cls.repository + os.sep + cls.config['authors']['basename'], cls.repository + os.sep + cls.config['authors']['basename'] + '.back')

    def test_authors(self):
        """Test `pkgtk authors` script of module `pkgtk.console_scripts`"""
        pkgtk(['authors'])
        pkgtk(['authors']+['--' + key + '=' + str(value) for key, value in self.config['authors'].iteritems()])

    @classmethod
    def tearDownClass(cls):
        os.rename(cls.repository + os.sep + '.pkgtk.back', cls.repository + os.sep + '.pkgtk.yml')
        if 'basename' in cls.config['authors']:
            os.rename(cls.repository + os.sep + cls.config['authors']['basename'] + '.back', cls.repository + os.sep + cls.config['authors']['basename'])<|endoftext|>


<commit_before>#!/usr/bin/env python
# (c) 2009 Andrey V. Scopenco andrey@scopenco.net
# $Id$
# xml processing module

import os
import sys
import xml.sax
import logging

class xml_object:
  """ Superclass for xml object. Init base methods for all sub classes. """

  def _validate_attributes(self, tag, attributes, goodattributes, config):
    """ Makes sure 'attributes' does not contain any attribute not
        listed in 'goodattributes'. """

    all_good = True
    for attr in attributes.keys():
      if not attr in goodattributes:
        logging.critical("unknown %s attribute '%s' in role %s" % (tag, attr, config))
        sys.exit(1)
        all_good = False
    return all_good
#end def _validate_attributes

class xml_handler(xml.sax.handler.ContentHandler):
  """
  SAX handler. Find tags 'project' 'role' 'package' and append.
  look at xml project and get list roles and packages
  user classes xml_project xml_package xml_role
  """

  def __init__(self, path, config):

    #parse config
    self.path         = path
    self.config       = config

    self.projects         = []                  # SAX: get project name
    self.roles            = []                  # SAX: get project type
    self.packages         = []                  # SAX: get project vendor
    self.repositories     = []                  # SAX: get project vendor
  #end def __init__

  def startElement(self, tag, attributes):
    """ SAX processing, called per node in the config stream. """

    if tag == 'project':
      self._check_before_append(self.projects, xml_project(attributes, self.path, self.config).output())
    elif tag == 'role':
      self._check_before_append(self.roles, xml_role(attributes, self.path, self.config).output())
    elif tag == 'package':
      self._check_before_append(self.packages, xml_package(attributes, self.path, self.config).output())
    elif tag == 'repository':
      self._check_before_append(self.repositories, xml_repository(attributes, self.path, self.config).output())
    else:
      logging.critical("unrecognized tag '%s' in role %s%s" % (tag, self.path, self.config))
      sys.exit(1)
  #end def startElement

  def _check_before_store(self, option, value):
    if value:
      option = value
  #end def _check_before_store

  def _check_before_append(self, option, value):
    if value:
      option.append(value)
  #end def _check_before_append
#end class ProjectHandler

class xml_project(xml_object):
  """ Class for xml object 'project'. """

  def __init__(self, attributes, path, config):

    #parse config
    self.path         = path
    self.config       = config

    # appent attrs like '_x_name' '_x_key'
    for attr in attributes.keys():
      setattr(self, '_x_%s' % attr, attributes.get(attr))
  #end def __init__

  def output(self):
    """ Convert attributs for output. """

    # dict used for output
    out = {}

    # ex: ['__doc__', '__module__', '_x_name', '_x_summary', 'config', 'path']
    attrlist = [method for method in dir(self) if not callable(getattr(self, method))]

    # get attrs like '_x_' and append to out
    for attr in attrlist:
      splited_attr = attr.split('_x_')
      if  len(splited_attr) == 2:
        out[splited_attr[1]] = getattr(self, attr)

    return out
  #end def output
#end class xml_project

class xml_role(xml_object):
  """ Class for xml object 'role'. """

  def __init__(self, attributes, path, config):

    #parse config
    self.path         = path
    self.config       = config

    #Attributes
    self._path  = None                # SAX: get role path
    self._title = None                # SAX: get role path

    if not self._validate_attributes('role', attributes, ('path','title'),  '%s/%s' % (self.path, self.config)):
      return

    self._path  = attributes.get('path')
    self._title = attributes.get('title')

    # Validate attributs
    if self._path:
      if not os.path.isfile("%s/%s" % (self.path, self._path)):
        logging.critical("can not locate role %s/%s in role %s/%s" % (self.path, self._path, self.path, self.config))
        sys.exit(1)
        self._path = None
    elif not self._title:
      logging.critical("role must have a 'path' or 'title' attribute in role %s/%s" % (self.path, self.config))
      sys.exit(1)
  #end def __init__

  def output(self):
    """
    convert attributs for output
    """
    return self._path
  #end def output
#end class XmlRole

class xml_package(xml_object):
  """ Xml object 'package' class """

  def __init__(self, attributes, path, config):

    #parse config
    self.path         = path
    self.config       = config

    #Attributes
    self._name         = None                # SAX: get project summary
    self._version      = None                # SAX: get project name
    self._release      = None                # SAX: get project type
    self._repository   = None                # SAX: get project vendor

    if not self._validate_attributes('package', attributes, ('name', 'version', 'release', 'repository'), '%s/%s' % (self.path, self.config)):
      return

    self._name         = attributes.get('name')
    self._version      = attributes.get('version')
    self._release      = attributes.get('release')
    self._repository   = attributes.get('repository')

    # Validate attributs
    if not self._name:
      logging.critical("package needs a 'name' attribute in role %s/%s" % (self.path, self.config))
      sys.exit(1)
  #end def __init__

  def output(self):
    """ convert attributs for output """

    return (self._name, self._version, self._release, self._repository)
  #end def output
#end class XmlProject

class xml_repository(xml_object):
  """ Xml object 'package' class """

  def __init__(self, attributes, path ,config):

    #parse config
    self.path         = path
    self.config       = config

    #Attributes
    self._name         = None
    self._description  = None
    self._baseurl      = None

    if not self._validate_attributes('repository', attributes, ('name', 'description', 'baseurl'), '%s/%s' % (self.path, self.config)):
      return

    self._name          = attributes.get('name')
    self._description   = attributes.get('description')
    self._baseurl       = attributes.get('baseurl')

    # Validate attributs
    if not self._name:
      logging.critical("repository needs a 'name' attribute in role %s/%s" % (self.path, self.config))
      sys.exit(1)
    if not self._description:
      logging.critical("repository needs a 'description' attribute in role %s/%s" % (self.path, self.config))
      sys.exit(1)
    if not self._baseurl:
      logging.critical("repository needs a 'baseurl' attribute in role %s/%s" % (self.path, self.config))
      sys.exit(1)
  #end def __init__

  def output(self):
    """ convert attributs for output """

    return  {'repoid':self._name, 'description':self._description, 'baseurl':self._baseurl}
  #end def output
#end class XmlProject
<commit_msg>fix pep8 syntax<commit_after>#!/usr/bin/env python
# (c) 2009 Andrey V. Scopenco andrey@scopenco.net
# $Id$
# xml processing module

import os
import sys
import xml.sax
import logging

class xml_object:
    """ Superclass for xml object. Init base methods for all sub classes. """

    def _validate_attributes(self, tag, attributes, goodattributes, config):
        """ Makes sure 'attributes' does not contain any attribute not
            listed in 'goodattributes'. """

        all_good = True
        for attr in attributes.keys():
            if not attr in goodattributes:
                logging.critical(
                    "unknown %s attribute '%s' in role %s" %
                    (tag, attr, config))
                sys.exit(1)
                all_good = False
        return all_good
#end def _validate_attributes

class xml_handler(xml.sax.handler.ContentHandler):
    """
    SAX handler. Find tags 'project' 'role' 'package' and append.
    look at xml project and get list roles and packages
    user classes xml_project xml_package xml_role
    """

    def __init__(self, path, config):

        #parse config
        self.path = path
        self.config = config

        self.projects = []
        self.roles = []
        self.packages = []
        self.repositories = []
    #end def __init__

    def startElement(self, tag, attributes):
        """ SAX processing, called per node in the config stream. """

        if tag == 'project':
            self._check_before_append(self.projects, xml_project(
                attributes, self.path, self.config).output())
        elif tag == 'role':
            self._check_before_append(self.roles, xml_role(
                attributes, self.path, self.config).output())
        elif tag == 'package':
            self._check_before_append(self.packages, xml_package(
                attributes, self.path, self.config).output())
        elif tag == 'repository':
            self._check_before_append(self.repositories, xml_repository(
                attributes, self.path, self.config).output())
        else:
            logging.critical(
                "unrecognized tag '%s' in role %s%s" %
                (tag, self.path, self.config))
            sys.exit(1)
    #end def startElement

    def _check_before_store(self, option, value):
        if value:
            option = value
    #end def _check_before_store

    def _check_before_append(self, option, value):
        if value:
            option.append(value)
    #end def _check_before_append
#end class ProjectHandler

class xml_project(xml_object):
    """ Class for xml object 'project'. """

    def __init__(self, attributes, path, config):

        #parse config
        self.path = path
        self.config = config

        # appent attrs like '_x_name' '_x_key'
        for attr in attributes.keys():
            setattr(self, '_x_%s' % attr, attributes.get(attr))
    #end def __init__

    def output(self):
        """ Convert attributs for output. """

        # dict used for output
        out = {}

        # ex: ['__doc__', '__module__', '_x_name',
        # '_x_summary', 'config', 'path']
        attrlist = [method for method in dir(self) if not callable(
            getattr(self, method))]

        # get attrs like '_x_' and append to out
        for attr in attrlist:
            splited_attr = attr.split('_x_')
            if len(splited_attr) == 2:
                out[splited_attr[1]] = getattr(self, attr)

        return out
    #end def output
#end class xml_project

class xml_role(xml_object):
    """ Class for xml object 'role'. """

    def __init__(self, attributes, path, config):

        #parse config
        self.path = path
        self.config = config

        #Attributes
        self._path = None
        self._title = None

        if not self._validate_attributes(
                'role', attributes, ('path', 'title'),
                '%s/%s' % (self.path, self.config)):
            return

        self._path = attributes.get('path')
        self._title = attributes.get('title')

        # Validate attributs
        if self._path:
            if not os.path.isfile("%s/%s" % (self.path, self._path)):
                logging.critical(
                    "can not locate role %s/%s in role %s/%s" %
                    (self.path, self._path, self.path, self.config))
                sys.exit(1)
                self._path = None
        elif not self._title:
            logging.critical(
                "role must have a 'path' or 'title' attribute in role %s/%s" %
                (self.path, self.config))
            sys.exit(1)
    #end def __init__

    def output(self):
        """
        convert attributs for output
        """
        return self._path
    #end def output
#end class XmlRole

class xml_package(xml_object):
    """ Xml object 'package' class """

    def __init__(self, attributes, path, config):

        #parse config
        self.path = path
        self.config = config

        #Attributes
        self._name = None
        self._version = None
        self._release = None
        self._repository = None

        if not self._validate_attributes(
                'package', attributes,
                ('name', 'version', 'release', 'repository'),
                '%s/%s' % (self.path, self.config)):
            return

        self._name = attributes.get('name')
        self._version = attributes.get('version')
        self._release = attributes.get('release')
        self._repository = attributes.get('repository')

        # Validate attributs
        if not self._name:
            logging.critical(
                "package needs a 'name' attribute in role %s/%s" %
                (self.path, self.config))
            sys.exit(1)
    #end def __init__

    def output(self):
        """ convert attributs for output """

        return (self._name, self._version, self._release, self._repository)
    #end def output
#end class XmlProject

class xml_repository(xml_object):
    """ Xml object 'package' class """

    def __init__(self, attributes, path, config):

        #parse config
        self.path = path
        self.config = config

        #Attributes
        self._name = None
        self._description = None
        self._baseurl = None

        if not self._validate_attributes(
                'repository', attributes,
                ('name', 'description', 'baseurl'),
                '%s/%s' % (self.path, self.config)):
            return

        self._name = attributes.get('name')
        self._description = attributes.get('description')
        self._baseurl = attributes.get('baseurl')

        # Validate attributs
        if not self._name:
            logging.critical(
                "repository needs a 'name' attribute in role %s/%s" %
                (self.path, self.config))
            sys.exit(1)
        if not self._description:
            logging.critical(
                "repository needs a 'description' attribute in role %s/%s" %
                (self.path, self.config))
            sys.exit(1)
        if not self._baseurl:
            logging.critical(
                "repository needs a 'baseurl' attribute in role %s/%s" %
                (self.path, self.config))
            sys.exit(1)
    #end def __init__

    def output(self):
        """ convert attributs for output """

        return {'repoid': self._name,
                'description': self._description,
                'baseurl': self._baseurl}
    #end def output
#end class XmlProject
<|endoftext|>


<commit_before>"""
33. Generic relations

Generic relations let an object have a foreign key to any object through a
content-type/object-id field. A generic foreign key can point to any object,
be it animal, vegetable, or mineral.

The cannonical example is tags (although this example implementation is *far*
from complete).
"""

from django.db import models
from django.contrib.contenttypes.models import ContentType

class TaggedItem(models.Model):
    """A tag on an item."""
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()

    content_object = models.GenericForeignKey()

    class Meta:
        ordering = ["tag"]

    def __str__(self):
        return self.tag

class Animal(models.Model):
    common_name = models.CharField(maxlength=150)
    latin_name = models.CharField(maxlength=150)

    tags = models.GenericRelation(TaggedItem)

    def __str__(self):
        return self.common_name

class Vegetable(models.Model):
    name = models.CharField(maxlength=150)
    is_yucky = models.BooleanField(default=True)

    tags = models.GenericRelation(TaggedItem)

    def __str__(self):
        return self.name

class Mineral(models.Model):
    name = models.CharField(maxlength=150)
    hardness = models.PositiveSmallIntegerField()

    # note the lack of an explicit GenericRelation here...

    def __str__(self):
        return self.name

API_TESTS = """
# Create the world in 7 lines of code...
>>> lion = Animal(common_name="Lion", latin_name="Panthera leo")
>>> platypus = Animal(common_name="Platypus", latin_name="Ornithorhynchus anatinus")
>>> eggplant = Vegetable(name="Eggplant", is_yucky=True)
>>> bacon = Vegetable(name="Bacon", is_yucky=False)
>>> quartz = Mineral(name="Quartz", hardness=7)
>>> for o in (lion, platypus, eggplant, bacon, quartz):
...     o.save()

# Objects with declared GenericRelations can be tagged directly -- the API
# mimics the many-to-many API
>>> lion.tags.create(tag="yellow")
<TaggedItem: yellow>
>>> lion.tags.create(tag="hairy")
<TaggedItem: hairy>
>>> bacon.tags.create(tag="fatty")
<TaggedItem: fatty>
>>> bacon.tags.create(tag="salty")
<TaggedItem: salty>

>>> lion.tags.all()
[<TaggedItem: hairy>, <TaggedItem: yellow>]
>>> bacon.tags.all()
[<TaggedItem: fatty>, <TaggedItem: salty>]

# You can easily access the content object like a foreign key
>>> t = TaggedItem.objects.get(tag="salty")
>>> t.content_object
<Vegetable: Bacon>

# Recall that the Mineral class doesn't have an explicit GenericRelation
# defined. That's OK since you can create TaggedItems explicitally.
>>> tag1 = TaggedItem(content_object=quartz, tag="shiny")
>>> tag2 = TaggedItem(content_object=quartz, tag="clearish")
>>> tag1.save()
>>> tag2.save()

# However, not having the convience takes a small toll when it comes
# to do lookups
>>> from django.contrib.contenttypes.models import ContentType
>>> ctype = ContentType.objects.get_for_model(quartz)
>>> TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id)
[<TaggedItem: clearish>, <TaggedItem: shiny>]

# You can set a generic foreign key in the way you'd expect
>>> tag1.content_object = platypus
>>> tag1.save()
>>> platypus.tags.all()
[<TaggedItem: shiny>]
>>> TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id)
[<TaggedItem: clearish>]
"""<commit_msg>Fixed some small typos in generic_relations model tests<commit_after>"""
33. Generic relations

Generic relations let an object have a foreign key to any object through a
content-type/object-id field. A generic foreign key can point to any object,
be it animal, vegetable, or mineral.

The cannonical example is tags (although this example implementation is *far*
from complete).
"""

from django.db import models
from django.contrib.contenttypes.models import ContentType

class TaggedItem(models.Model):
    """A tag on an item."""
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()

    content_object = models.GenericForeignKey()

    class Meta:
        ordering = ["tag"]

    def __str__(self):
        return self.tag

class Animal(models.Model):
    common_name = models.CharField(maxlength=150)
    latin_name = models.CharField(maxlength=150)

    tags = models.GenericRelation(TaggedItem)

    def __str__(self):
        return self.common_name

class Vegetable(models.Model):
    name = models.CharField(maxlength=150)
    is_yucky = models.BooleanField(default=True)

    tags = models.GenericRelation(TaggedItem)

    def __str__(self):
        return self.name

class Mineral(models.Model):
    name = models.CharField(maxlength=150)
    hardness = models.PositiveSmallIntegerField()

    # note the lack of an explicit GenericRelation here...

    def __str__(self):
        return self.name

API_TESTS = """
# Create the world in 7 lines of code...
>>> lion = Animal(common_name="Lion", latin_name="Panthera leo")
>>> platypus = Animal(common_name="Platypus", latin_name="Ornithorhynchus anatinus")
>>> eggplant = Vegetable(name="Eggplant", is_yucky=True)
>>> bacon = Vegetable(name="Bacon", is_yucky=False)
>>> quartz = Mineral(name="Quartz", hardness=7)
>>> for o in (lion, platypus, eggplant, bacon, quartz):
...     o.save()

# Objects with declared GenericRelations can be tagged directly -- the API
# mimics the many-to-many API.
>>> lion.tags.create(tag="yellow")
<TaggedItem: yellow>
>>> lion.tags.create(tag="hairy")
<TaggedItem: hairy>
>>> bacon.tags.create(tag="fatty")
<TaggedItem: fatty>
>>> bacon.tags.create(tag="salty")
<TaggedItem: salty>

>>> lion.tags.all()
[<TaggedItem: hairy>, <TaggedItem: yellow>]
>>> bacon.tags.all()
[<TaggedItem: fatty>, <TaggedItem: salty>]

# You can easily access the content object like a foreign key.
>>> t = TaggedItem.objects.get(tag="salty")
>>> t.content_object
<Vegetable: Bacon>

# Recall that the Mineral class doesn't have an explicit GenericRelation
# defined. That's OK, because you can create TaggedItems explicitly.
>>> tag1 = TaggedItem(content_object=quartz, tag="shiny")
>>> tag2 = TaggedItem(content_object=quartz, tag="clearish")
>>> tag1.save()
>>> tag2.save()

# However, excluding GenericRelations means your lookups have to be a bit more
# explicit.
>>> from django.contrib.contenttypes.models import ContentType
>>> ctype = ContentType.objects.get_for_model(quartz)
>>> TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id)
[<TaggedItem: clearish>, <TaggedItem: shiny>]

# You can set a generic foreign key in the way you'd expect.
>>> tag1.content_object = platypus
>>> tag1.save()
>>> platypus.tags.all()
[<TaggedItem: shiny>]
>>> TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id)
[<TaggedItem: clearish>]
"""
<|endoftext|>