<commit_before># -*- coding: utf-8 -*-
# TODO: move tests one out of src to project root.
# TODO: travis has numpy on their workers. Maybe add tests?

"""Helpers for testing."""

import ctypes
import os
import sys
import sysconfig

import pytest
import clr

# Add path for `Python.Test`
cwd = os.path.dirname(__file__)
fixtures_path = os.path.join(cwd, "fixtures")

BUILD_TEST = True
if BUILD_TEST:
    from subprocess import check_call
    test_proj_path = os.path.join(cwd, "..", "testing")
    check_call(["dotnet", "build", test_proj_path, "-o", fixtures_path])

sys.path.append(fixtures_path)

# Add References for tests
clr.AddReference("Python.Test")
clr.AddReference("System.Collections")
clr.AddReference("System.Data")

def pytest_report_header(config):
    """Generate extra report headers"""
    # FIXME: https://github.com/pytest-dev/pytest/issues/2257
    is_64bits = sys.maxsize > 2**32
    arch = "x64" if is_64bits else "x86"
    ucs = ctypes.sizeof(ctypes.c_wchar)
    libdir = sysconfig.get_config_var("LIBDIR")
    shared = bool(sysconfig.get_config_var("Py_ENABLE_SHARED"))

    header = ("Arch: {arch}, UCS: {ucs}, LIBDIR: {libdir}, "
              "Py_ENABLE_SHARED: {shared}".format(**locals()))
    return header

@pytest.fixture()
def filepath():
    """Returns full filepath for file in `fixtures` directory."""

    def make_filepath(filename):
        # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
        return os.path.join(fixtures_path, filename)

    return make_filepath
<commit_msg>Do the clr import later to not interfere with other test imports<commit_after># -*- coding: utf-8 -*-
# TODO: move tests one out of src to project root.
# TODO: travis has numpy on their workers. Maybe add tests?

"""Helpers for testing."""

import ctypes
import os
import sys
import sysconfig

import pytest

# Add path for `Python.Test`
cwd = os.path.dirname(__file__)
fixtures_path = os.path.join(cwd, "fixtures")

BUILD_TEST = True
if BUILD_TEST:
    from subprocess import check_call
    test_proj_path = os.path.join(cwd, "..", "testing")
    check_call(["dotnet", "build", test_proj_path, "-o", fixtures_path])

sys.path.append(fixtures_path)

import clr

# Add References for tests
clr.AddReference("Python.Test")
clr.AddReference("System.Collections")
clr.AddReference("System.Data")

def pytest_report_header(config):
    """Generate extra report headers"""
    # FIXME: https://github.com/pytest-dev/pytest/issues/2257
    is_64bits = sys.maxsize > 2**32
    arch = "x64" if is_64bits else "x86"
    ucs = ctypes.sizeof(ctypes.c_wchar)
    libdir = sysconfig.get_config_var("LIBDIR")
    shared = bool(sysconfig.get_config_var("Py_ENABLE_SHARED"))

    header = ("Arch: {arch}, UCS: {ucs}, LIBDIR: {libdir}, "
              "Py_ENABLE_SHARED: {shared}".format(**locals()))
    return header

@pytest.fixture()
def filepath():
    """Returns full filepath for file in `fixtures` directory."""

    def make_filepath(filename):
        # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
        return os.path.join(fixtures_path, filename)

    return make_filepath
<|endoftext|>


<commit_before>from UnitTest import UnitTest

from ClassTest import PassMeAClass
from ClassTest import ExampleChildClass
from ClassTest import ExampleMultiSuperclassParent1
import Factory2

import imports.decors # must be in this form
import imports.enum.Late

class Handler:

    def __init__(self, x):
        self._x = x

    def handle(self, y):
        return self._x is y

class Callable(object):
    def __call__(self):
        return 5

def aProcedure():
    x = 1
    if x is 2:
        # a return statement which is not reached
        return "something"
    #this is a comment

def aFunctionWithOnlyDoc():
    """Only a doc string"""

def aFunctionReturningNone():
    return None

def aFunctionReturningParam(param):
    return param

def aFunctionReturningFunction():
    return aFunctionReturningParam

def aFunctionReturningGlobalX1():
    return x

def aFunctionReturningGlobalX2():
    return x

def aFunctionReturningGlobalX3():
    a = x
    return a

def aFunctionReturningLocalX():
    x = 'local test'
    return x

def aFunctionReturningArgX(x):
    return x

x = 'global test'

name = 'mapping-test'
def call(default, arguments, this, label=None):
    return (name, default, arguments, this, label)

def functionDefaults(s = "", l = []):
    n = len(l)
    s = s + "%d" % n
    l.append(n)
    return s, l

class FunctionTest(UnitTest):

    def testLambda(self):
        f = lambda x: x
        self.assertEqual(f(1), 1)
        f = lambda x=1: x
        self.assertEqual(f(), 1)
        self.assertEqual(f(2), 2)

        f = lambda x, y: x*y
        self.assertEqual(f(2,3), 6)
        f = lambda x, y=4: x*y
        self.assertEqual(f(2), 8)
        h = Handler(5)
        f = lambda x: h.handle(x)
        self.assertTrue(f(5))
        self.assertFalse(f(4))

        f = lambda a, b=1, **kw: (a,b,kw)
        v = f(b = 2, c = 3, a = 1)
        self.assertEqual(v[0], 1)
        self.assertEqual(v[1], 2)
        self.assertEqual(v[2]['c'], 3)

        f = lambda a, b=1, *args: (a, b, args)
        v = f(1,2,3,4)
        self.assertEqual(v[2][0], 3)
        self.assertEqual(v[2][1], 4)

        try:
            class ClassWithLambdas1:
                f1 = [lambda *args: args[0]]
        except:
            self.fail("issue #385 - lambda in class definition")
        else:
            c = ClassWithLambdas1()
            self.assertEqual(c.f1[0](1), 1, 'issue #385 - lambda function called as bound method')

        try:
            class ClassWithLambdas2:
                f2 = lambda *args: args[0]
        except:
            self.fail("issue #385 - lambda in class definition")
        else:
            c = ClassWithLambdas2()
            self.assertEqual(c.f2(1), c, 'issue #385 - bound method lambda called as function')

    def test_callable(self):
        try:
            self.assertEqual(Callable()(), 5)
        except:
            self.fail("Bug #572 Callable()() __call__ not supported")
        self.assertTrue(callable(Callable))
        self.assertTrue(
            callable(Callable()),
            "Bug #572 callable(Callable()) __call__ not supported",
        )

    def testProcedure(self):
        self.assertTrue(aFunctionReturningNone() is None,
                        "Function should return None")
        self.assertTrue(aProcedure() is None,
                        "Procedures should always return None")

    def testVariableFunction(self):
        self.assertEqual((aFunctionReturningParam)("foo"), "foo")
        self.assertEqual(aFunctionReturningFunction()("foo"), "foo")

    def testLookup(self):
        expected_result1 = 'global test'
        expected_result2 = 'local test'
        self.assertEqual(aFunctionReturningGlobalX1(), expected_result1)
        self.assertEqual(aFunctionReturningGlobalX2(), expected_result1)
        self.assertEqual(aFunctionReturningGlobalX3(), expected_result1)
        self.assertEqual(aFunctionReturningLocalX(), expected_result2)
        self.assertEqual(aFunctionReturningArgX('test'), 'test')

    def testLookupLate(self):
        self.assertEqual(late_global, 'late_global')

        def local_lookup1():
            self.assertEqual(late_local, 'late_local')
            self.assertTrue(callable(local_lookup2))
            self.assertEqual(local_lookup2(), 'late_local')
            try:
                local_lookup3()
                self.fail("lookup3")
            except NotImplementedError, e:
                self.assertTrue(True)

        def local_lookup2():
            return late_local

        def local_lookup3():
            raise NotImplementedError

        late_local = 'late_local'
        local_lookup1()

        late = imports.enum.Late.getLate()
        self.assertEqual(late.value, 'late')

    def testNameMapping(self):
        r = call(1, 2, this=3, label=4)
        self.assertEqual(r[0], 'mapping-test')
        self.assertEqual(r[1], 1)
        self.assertEqual(r[2], 2)
        self.assertEqual(r[3], 3)
        self.assertEqual(r[4], 4)
        try:
            self.assertEqual(Text('foo'), 'Text: foo')
        except:
            self.fail("Bug #574: javascript keywords")

    def testFactory(self):

        Factory2.gregister("passme", PassMeAClass)
        Factory2.gregister("exchild", ExampleChildClass)
        Factory2.gregister("mscp1", ExampleMultiSuperclassParent1)

        pmc = Factory2.ggetObject("passme")
        self.assertEqual(pmc.foo(), "foo in PassMeAClass")

        try:
            pmc = Factory2.ggetObject("mscp1", 5)
        except:
            self.assertEqual(False, True, "Exception indicates bug in compiler: 'Error: uncaught exception: ExampleMultiSuperclassParent1() arguments after ** must be a dictionary 5'")
        else:
            self.assertEqual(pmc.x, 5)
        try:
            pmc = Factory2.ggetObject("exchild", 5, 7) # 5 is ignored
        except:
            self.assertEqual(False, True, "Exception indicates bug in compiler: 'Error: uncaught exception: ExampleChildClass() arguments after ** must be a dictionary 7'")
        else:
            self.assertEqual(pmc.prop_a, 1)
            self.assertEqual(pmc.prop_b, 7)

    def testSliceFunc(self):
        s = "123 "
        s = s[1:].rstrip()
        self.assertEqual(s, "23")

    def testFunctionDefaults(self):
        s, l = functionDefaults()
        self.assertEqual(s, '0')
        self.assertTrue(l == [0], "First mutable default mismatch")

        s, l = functionDefaults()
        #self.assertEqual(s, '1') # can be enabled when the next line is fixed
        self.assertTrue(l == [0, 1], "Second mutable default mismatch bug #214")

        inittest = 1
        def f(inittest = inittest):
            return inittest
        self.assertEqual(f(), inittest)

    def testKwargs(self):
        def f(**kwargs):
            return kwargs

        self.assertEqual(f(), {})
        self.assertEqual(f(a=1), dict(a=1))

    def testFunctionDecorating(self):
        log = []
        def deco1(f):
            def fn(*args, **kwargs):
                log.append("deco1 begin")
                res = f(*args, **kwargs)
                log.append("deco1 end")
                return res
            return fn

        def deco2(f):
            def fn(*args, **kwargs):
                log.append("deco2 begin")
                res = f(*args, **kwargs)
                log.append("deco2 end")
                return res
            return fn

        @deco1
        def fn1(a, b = 0):
            return a, b

        @deco1
        @deco2
        def fn2(a, b = 0):
            return a, b

        res = fn1(1,2)
        self.assertEqual(res[0], 1)
        self.assertEqual(res[1], 2)
        self.assertEqual(len(log), 2)
        self.assertEqual(log[0], "deco1 begin")
        self.assertEqual(log[1], "deco1 end")

        log = []
        res = fn2(a=3)
        self.assertEqual(res[0], 3)
        self.assertEqual(res[1], 0)
        self.assertEqual(len(log), 4)
        self.assertEqual(log[0], "deco1 begin")
        self.assertEqual(log[1], "deco2 begin")
        self.assertEqual(log[2], "deco2 end")
        self.assertEqual(log[3], "deco1 end")

        @imports.decors.othermoduledeco1
        def fn3(x):
            return "b"

        self.assertEqual(fn3("b"), "abc")

    def testTopLevelContionalFunction(self):
        self.assertEqual(imports.conditional_func(), "overridden")

late_global = 'late_global'

# Text is a reserved javascript word
def Text(x):
    return str("Text: %s" % x)
<commit_msg>Tests for issue #369<commit_after>from UnitTest import UnitTest

from ClassTest import PassMeAClass
from ClassTest import ExampleChildClass
from ClassTest import ExampleMultiSuperclassParent1
import Factory2

import imports.decors # must be in this form
import imports.enum.Late

class Handler:

    def __init__(self, x):
        self._x = x

    def handle(self, y):
        return self._x is y

class Callable(object):
    def __call__(self):
        return 5

def aProcedure():
    x = 1
    if x is 2:
        # a return statement which is not reached
        return "something"
    #this is a comment

def aFunctionWithOnlyDoc():
    """Only a doc string"""

def aFunctionReturningNone():
    return None

def aFunctionReturningParam(param):
    return param

def aFunctionReturningFunction():
    return aFunctionReturningParam

def aFunctionReturningGlobalX1():
    return x

def aFunctionReturningGlobalX2():
    return x

def aFunctionReturningGlobalX3():
    a = x
    return a

def aFunctionReturningLocalX():
    x = 'local test'
    return x

def aFunctionReturningArgX(x):
    return x

x = 'global test'

name = 'mapping-test'
def call(default, arguments, this, label=None):
    return (name, default, arguments, this, label)

def functionDefaults(s = "", l = []):
    n = len(l)
    s = s + "%d" % n
    l.append(n)
    return s, l

class FunctionTest(UnitTest):

    def testLambda(self):
        f = lambda x: x
        self.assertEqual(f(1), 1)
        f = lambda x=1: x
        self.assertEqual(f(), 1)
        self.assertEqual(f(2), 2)

        f = lambda x, y: x*y
        self.assertEqual(f(2,3), 6)
        f = lambda x, y=4: x*y
        self.assertEqual(f(2), 8)
        h = Handler(5)
        f = lambda x: h.handle(x)
        self.assertTrue(f(5))
        self.assertFalse(f(4))

        f = lambda a, b=1, **kw: (a,b,kw)
        v = f(b = 2, c = 3, a = 1)
        self.assertEqual(v[0], 1)
        self.assertEqual(v[1], 2)
        self.assertEqual(v[2]['c'], 3)

        f = lambda a, b=1, *args: (a, b, args)
        v = f(1,2,3,4)
        self.assertEqual(v[2][0], 3)
        self.assertEqual(v[2][1], 4)

        try:
            class ClassWithLambdas1:
                f1 = [lambda *args: args[0]]
        except:
            self.fail("issue #385 - lambda in class definition")
        else:
            c = ClassWithLambdas1()
            self.assertEqual(c.f1[0](1), 1, 'issue #385 - lambda function called as bound method')

        try:
            class ClassWithLambdas2:
                f2 = lambda *args: args[0]
        except:
            self.fail("issue #385 - lambda in class definition")
        else:
            c = ClassWithLambdas2()
            self.assertEqual(c.f2(1), c, 'issue #385 - bound method lambda called as function')

    def test_callable(self):
        try:
            self.assertEqual(Callable()(), 5)
        except:
            self.fail("Bug #572 Callable()() __call__ not supported")
        self.assertTrue(callable(Callable))
        self.assertTrue(
            callable(Callable()),
            "Bug #572 callable(Callable()) __call__ not supported",
        )

    def testProcedure(self):
        self.assertTrue(aFunctionReturningNone() is None,
                        "Function should return None")
        self.assertTrue(aProcedure() is None,
                        "Procedures should always return None")

    def testVariableFunction(self):
        self.assertEqual((aFunctionReturningParam)("foo"), "foo")
        self.assertEqual(aFunctionReturningFunction()("foo"), "foo")

    def testLookup(self):
        expected_result1 = 'global test'
        expected_result2 = 'local test'
        self.assertEqual(aFunctionReturningGlobalX1(), expected_result1)
        self.assertEqual(aFunctionReturningGlobalX2(), expected_result1)
        self.assertEqual(aFunctionReturningGlobalX3(), expected_result1)
        self.assertEqual(aFunctionReturningLocalX(), expected_result2)
        self.assertEqual(aFunctionReturningArgX('test'), 'test')

    def testLookupLate(self):
        self.assertEqual(late_global, 'late_global')

        def local_lookup1():
            self.assertEqual(late_local, 'late_local')
            self.assertTrue(callable(local_lookup2))
            self.assertEqual(local_lookup2(), 'late_local')
            try:
                local_lookup3()
                self.fail("lookup3")
            except NotImplementedError, e:
                self.assertTrue(True)

        def local_lookup2():
            return late_local

        def local_lookup3():
            raise NotImplementedError

        late_local = 'late_local'
        local_lookup1()

        late = imports.enum.Late.getLate()
        self.assertEqual(late.value, 'late')

    def testNameMapping(self):
        r = call(1, 2, this=3, label=4)
        self.assertEqual(r[0], 'mapping-test')
        self.assertEqual(r[1], 1)
        self.assertEqual(r[2], 2)
        self.assertEqual(r[3], 3)
        self.assertEqual(r[4], 4)
        try:
            self.assertEqual(Text('foo'), 'Text: foo')
        except:
            self.fail("Bug #574: javascript keywords")

    def testFactory(self):

        Factory2.gregister("passme", PassMeAClass)
        Factory2.gregister("exchild", ExampleChildClass)
        Factory2.gregister("mscp1", ExampleMultiSuperclassParent1)

        pmc = Factory2.ggetObject("passme")
        self.assertEqual(pmc.foo(), "foo in PassMeAClass")

        try:
            pmc = Factory2.ggetObject("mscp1", 5)
        except:
            self.assertEqual(False, True, "Exception indicates bug in compiler: 'Error: uncaught exception: ExampleMultiSuperclassParent1() arguments after ** must be a dictionary 5'")
        else:
            self.assertEqual(pmc.x, 5)
        try:
            pmc = Factory2.ggetObject("exchild", 5, 7) # 5 is ignored
        except:
            self.assertEqual(False, True, "Exception indicates bug in compiler: 'Error: uncaught exception: ExampleChildClass() arguments after ** must be a dictionary 7'")
        else:
            self.assertEqual(pmc.prop_a, 1)
            self.assertEqual(pmc.prop_b, 7)

    def testSliceFunc(self):
        s = "123 "
        s = s[1:].rstrip()
        self.assertEqual(s, "23")

    def testFunctionDefaults(self):
        s, l = functionDefaults()
        self.assertEqual(s, '0')
        self.assertTrue(l == [0], "First mutable default mismatch")

        s, l = functionDefaults()
        #self.assertEqual(s, '1') # can be enabled when the next line is fixed
        self.assertTrue(l == [0, 1], "Second mutable default mismatch bug #214")

        inittest = 1
        def f(inittest = inittest):
            return inittest
        self.assertEqual(f(), inittest)

    def testKwargs(self):
        def f(**kwargs):
            return kwargs

        self.assertEqual(f(), {})
        self.assertEqual(f(a=1), dict(a=1))

    def testFunctionDecorating(self):
        log = []
        def deco1(f):
            def fn(*args, **kwargs):
                log.append("deco1 begin")
                res = f(*args, **kwargs)
                log.append("deco1 end")
                return res
            return fn

        def deco2(f):
            def fn(*args, **kwargs):
                log.append("deco2 begin")
                res = f(*args, **kwargs)
                log.append("deco2 end")
                return res
            return fn

        @deco1
        def fn1(a, b = 0):
            return a, b

        @deco1
        @deco2
        def fn2(a, b = 0):
            return a, b

        res = fn1(1,2)
        self.assertEqual(res[0], 1)
        self.assertEqual(res[1], 2)
        self.assertEqual(len(log), 2)
        self.assertEqual(log[0], "deco1 begin")
        self.assertEqual(log[1], "deco1 end")

        log = []
        res = fn2(a=3)
        self.assertEqual(res[0], 3)
        self.assertEqual(res[1], 0)
        self.assertEqual(len(log), 4)
        self.assertEqual(log[0], "deco1 begin")
        self.assertEqual(log[1], "deco2 begin")
        self.assertEqual(log[2], "deco2 end")
        self.assertEqual(log[3], "deco1 end")

        @imports.decors.othermoduledeco1
        def fn3(x):
            return "b"

        self.assertEqual(fn3("b"), "abc")

        def shiftdecorator(si):
            def wrapper(fn):
                def decorated(*args, **kw):
                    return fn(*args, **kw) + si
                return decorated
            return wrapper

        def fn4(v):
            return v

        @shiftdecorator(1)
        def fn4d1(v):
            return v
        @shiftdecorator(2)
        def fn4d2(v):
            return v
        fn4d3 = shiftdecorator(2)(fn4)

        self.assertEqual(fn4d1(1), 2)
        self.assertEqual(fn4d2(1), 3)
        self.assertEqual(fn4d3(1), 3)

    def testTopLevelContionalFunction(self):
        self.assertEqual(imports.conditional_func(), "overridden")

late_global = 'late_global'

# Text is a reserved javascript word
def Text(x):
    return str("Text: %s" % x)
<|endoftext|>


<commit_before>from harpoon.ship.progress_stream import Failure, Unknown
from harpoon.ship.builders.base import BuilderBase
from harpoon.errors import FailedImage
from harpoon import helpers as hp

from itertools import chain
import logging

log = logging.getLogger("harpoon.ship.builders.normal")

class NormalBuilder(BuilderBase):
    def __init__(self, image_name=None):
        self.image_name = image_name

    def build(self, conf, context, stream):
        image_name = self.image_name
        if image_name is None:
            image_name = conf.image_name

        context.close()
        self.log_context_size(context, conf)

        # Login into the correct registry
        current_tags = chain.from_iterable(image["RepoTags"] for image in conf.harpoon.docker_context.images())
        if conf.commands.parent_image not in current_tags:
            conf.login(conf.commands.parent_image, is_pushing=False)

        lines = conf.harpoon.docker_context.build(
              tag = image_name
            , fileobj = context.tmpfile
            , custom_context = True

            , rm = True
            , pull = False
            , stream = True
            )

        for line in lines:
            try:
                stream.feed(line)
            except Failure as error:
                raise FailedImage("Failed to build an image", image=conf.name, msg=error)
            except Unknown as error:
                log.warning("Unknown line\tline=%s", error)

            for part in stream.printable():
                hp.write_to(conf.harpoon.stdout, part)
            conf.harpoon.stdout.flush()

        return stream.cached

<commit_msg>Fix bug where we try to authenticate when we don't need to<commit_after>from harpoon.ship.progress_stream import Failure, Unknown
from harpoon.ship.builders.base import BuilderBase
from harpoon.errors import FailedImage
from harpoon import helpers as hp

from itertools import chain
import logging

log = logging.getLogger("harpoon.ship.builders.normal")

class NormalBuilder(BuilderBase):
    def __init__(self, image_name=None):
        self.image_name = image_name

    def build(self, conf, context, stream):
        image_name = self.image_name
        if image_name is None:
            image_name = conf.image_name

        context.close()
        self.log_context_size(context, conf)

        # Login into the correct registry
        current_tags = list(chain.from_iterable(image["RepoTags"] for image in conf.harpoon.docker_context.images()))
        parent_image = conf.commands.parent_image
        if ":" not in parent_image:
            parent_image = "{0}:latest".format(parent_image)

        if parent_image not in current_tags:
            conf.login(conf.commands.parent_image, is_pushing=False)

        lines = conf.harpoon.docker_context.build(
              tag = image_name
            , fileobj = context.tmpfile
            , custom_context = True

            , rm = True
            , pull = False
            , stream = True
            )

        for line in lines:
            try:
                stream.feed(line)
            except Failure as error:
                raise FailedImage("Failed to build an image", image=conf.name, msg=error)
            except Unknown as error:
                log.warning("Unknown line\tline=%s", error)

            for part in stream.printable():
                hp.write_to(conf.harpoon.stdout, part)
            conf.harpoon.stdout.flush()

        return stream.cached

<|endoftext|>


<commit_before>import logging

from ckan_api_client.syncing import SynchronizationClient

from harvester.ext.importers.base import ImporterPluginBase

class CkanImporter(ImporterPluginBase):
    logger = logging.getLogger(__name__)

    def sync_data(self, storage):
        self.logger.info("Starting data synchronization to ckan")

        api_key = self.conf['api_key']
        source_name = self.conf['source_name']
        client = SynchronizationClient(self.url, api_key)

        # todo: load all data from the storage, or make it accessible as a dict
        # todo: we need to preserve names on update
        # todo: improve the sync client to make it more extensible

        data = {'dataset': {}, 'group': {}, 'organization': {}}

        for obj_id in storage.list_objects('dataset'):
            obj = storage.get_object('dataset', obj_id)
            data['dataset'][obj_id] = obj

        for obj_id in storage.list_objects('group'):
            obj = storage.get_object('group', obj_id)
            data['group'][obj_id] = obj

        for obj_id in storage.list_objects('organization'):
            obj = storage.get_object('organization', obj_id)
            data['organization'][obj_id] = obj

        client.sync(source_name, data)
<commit_msg>Adapted to use new-style storage<commit_after>import logging

from ckan_api_client.syncing import SynchronizationClient

from harvester.ext.importers.base import ImporterPluginBase

class CkanImporter(ImporterPluginBase):
    logger = logging.getLogger(__name__)

    options = [
        ('api_key', 'str', None),
        ('source_name', 'str', None),
    ]

    def sync_data(self, storage):
        self.logger.info("Starting data synchronization to ckan")

        api_key = self.conf['api_key']
        source_name = self.conf['source_name']
        client = SynchronizationClient(self.url, api_key)

        # todo: load all data from the storage, or make it accessible as a dict
        # todo: we need to preserve names on update
        # todo: improve the sync client to make it more extensible

        data = {'dataset': {}, 'group': {}, 'organization': {}}

        for obj_id in storage.documents['dataset']:
            obj = storage.documents['dataset'][obj_id]
            data['dataset'][obj_id] = obj

        for obj_id in storage.documents['group']:
            obj = storage.documents['group'][obj_id]
            data['group'][obj_id] = obj

        for obj_id in storage.documents['organization']:
            obj = storage.documents['organization'][obj_id]
            data['organization'][obj_id] = obj

        client.sync(source_name, data)
<|endoftext|>


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

"""Print classes which have a virtual method and non-virtual destructor."""

__author__ = 'nnorwitz@google.com (Neal Norwitz)'

import sys

from cpp import ast
from cpp import metrics
from cpp import utils

def _FindWarnings(filename, source, ast_list):
  for node in ast_list:
    if isinstance(node, ast.Class) and node.body:
      class_node = node
      has_virtuals = False
      for node in node.body:
        if (isinstance(node, ast.Function) and
            node.modifiers & ast.FUNCTION_VIRTUAL):
            has_virtuals = True
            if node.modifiers & ast.FUNCTION_DTOR:
              break
      else:
        if has_virtuals and not class_node.bases:
          lines = metrics.Metrics(source)
          print '%s:%d' % (filename, lines.GetLineNumber(class_node.start)),
          print class_node.name, 'has virtual methods without a virtual dtor'

def main(argv):
  for filename in argv[1:]:
    source = utils.ReadFile(filename)
    if source is None:
      continue

    print 'Processing', filename
    builder = ast.BuilderFromSource(source, filename)
    try:
      entire_ast = filter(None, builder.Generate())
      _FindWarnings(filename, source, entire_ast)
    except KeyboardInterrupt:
      return
    except:
      # An error message was already printed since we couldn't parse.
      pass

if __name__ == '__main__':
  main(sys.argv)
<commit_msg>Handle classes defined in other classes<commit_after>#!/usr/bin/env python
#
# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Print classes which have a virtual method and non-virtual destructor."""

__author__ = 'nnorwitz@google.com (Neal Norwitz)'

import sys

from cpp import ast
from cpp import metrics
from cpp import utils

def _FindWarnings(filename, source, ast_list):
  for node in ast_list:
    if isinstance(node, ast.Class) and node.body:
      class_node = node
      has_virtuals = False
      for node in node.body:
        if isinstance(node, ast.Class) and node.body:
          _FindWarnings(filename, source, node.body)
        elif (isinstance(node, ast.Function) and
            node.modifiers & ast.FUNCTION_VIRTUAL):
            has_virtuals = True
            if node.modifiers & ast.FUNCTION_DTOR:
              break
      else:
        if has_virtuals and not class_node.bases:
          lines = metrics.Metrics(source)
          print '%s:%d' % (filename, lines.GetLineNumber(class_node.start)),
          print class_node.name, 'has virtual methods without a virtual dtor'

def main(argv):
  for filename in argv[1:]:
    source = utils.ReadFile(filename)
    if source is None:
      continue

    print 'Processing', filename
    builder = ast.BuilderFromSource(source, filename)
    try:
      entire_ast = filter(None, builder.Generate())
      _FindWarnings(filename, source, entire_ast)
    except KeyboardInterrupt:
      return
    except:
      # An error message was already printed since we couldn't parse.
      pass

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


<commit_before>"""Import user-defined configuration files declared in settings.py"""
import sys

from django.conf import settings
sys.path.append(settings.HEALTH_MONITOR_CONFIG)

try:
    from dispatcher import get_dispatcher
except ImportError as e:
    raise ImportError('settings.HEALTH_MONITOR_CONFIG not properly set. See docs at https://django-health-monitor.readthedocs.io/en/latest/usage.html#configure-scoring-logic')

try:
    import scoring_logic
except ImportError as e:
    raise ImportError('settings.HEALTH_MONITOR_CONFIG not properly set. See docs at https://django-health-monitor.readthedocs.io/en/latest/usage.html#configure-scoring-logic')

"""
Functions accessing health/config/scoring_logic.py
"""

def get_score(test_name, **kwargs):
    """Calculate a score red=4, orange=3, yellow=2, green=1 based off of the scoring logic."""
    # for integrity purposes, if method_name is not in dispatcher, raise Exception
    dispatcher = get_dispatcher()
    if test_name not in dispatcher.keys():
        raise LookupError("test for '{}' not implemented in scoring_logic.py".format(test_name))

    method_name = dispatcher[test_name]['method']

    # get method based on method_name
    method = getattr(scoring_logic, method_name)

    # get params to pass to method
    # checks against dispatcher dictionary for integrity
    params = {}
    for key, value in kwargs.items():
        try:
            params[dispatcher[test_name]['params'][key]] = value
        except Exception:
            raise LookupError('param \'{}\' not implemented in test for \'{}\' in scoring_logic.py'.format(key, test_name))

    try:
        return method(**params)
    except Exception as e:
        raise LookupError(str(e))
        return 0

"""
Functions accessing health/config/dispatcher.py
"""

def get_legend(subscriber):
    """Return a tuple of tests and descriptions associated with each subscriber."""
    legend = {}
    dispatcher = get_dispatcher(original_dispatcher=True)
    health_keys = get_health_keys(subscriber)
    for key in health_keys:
        legend[key.decode('unicode-escape')] = dispatcher[key]['description'] if 'description' in dispatcher[key] else None

    return sorted(legend.items())

def calculate_severity(subscriber_state):
    """Return the highest score in state dict."""
    test_scores = [1, ]
    for test in subscriber_state.keys():
        if subscriber_state[test]['score']:
            test_scores.append(subscriber_state[test]['score'])

    return max(test_scores)

def get_health_keys(subscriber):
    """Return an array of tests associated with each subscriber."""
    dispatcher = get_dispatcher(original_dispatcher=True)
    health_keys = [x for x in dispatcher.keys() if subscriber in dispatcher[x]['subscriber']]

    return health_keys

def get_subscribers_list_for_test(test_name):
    """Return a list of subscribers for a given test."""
    dispatcher = get_dispatcher()
    return dispatcher[test_name]['subscriber']

# def get_latest_health_state(tui):
#     """Return the latest health state for a given TUI."""
#     health_state = {}
#     for subscriber in Subscriber.objects.values_list():
#         health_state[subscriber[1].lower()] = {}
#     dispatcher = get_dispatcher(original_dispatcher=True)
#
#     for test in dispatcher.keys():
#         score = get_latest_score(tui, test)
#         for subscriber in dispatcher[test]['subscriber']:
#             health_state[subscriber][test] = score
#
#     return health_state

def get_test_list(subscriber=None):
    """Get entire list of tests or those associated with a subscriber."""
    test_list = []
    dispatcher = get_dispatcher(original_dispatcher=True)

    if not subscriber:
        return sorted(dispatcher.keys())
    else:
        for key, value in dispatcher.items():
            if subscriber.lower() in value['subscriber']:
                test_list.append(key)

    return sorted(test_list)
<commit_msg>Pass ImportError directly.<commit_after>"""Import user-defined configuration files declared in settings.py"""
import sys

from django.conf import settings
sys.path.append(settings.HEALTH_MONITOR_CONFIG)

try:
    from dispatcher import get_dispatcher
except ImportError as e:
    raise ImportError(e)

try:
    import scoring_logic
except ImportError as e:
    raise ImportError(e)

"""
Functions accessing health/config/scoring_logic.py
"""

def get_score(test_name, **kwargs):
    """Calculate a score red=4, orange=3, yellow=2, green=1 based off of the scoring logic."""
    # for integrity purposes, if method_name is not in dispatcher, raise Exception
    dispatcher = get_dispatcher()
    if test_name not in dispatcher.keys():
        raise LookupError("test for '{}' not implemented in scoring_logic.py".format(test_name))

    method_name = dispatcher[test_name]['method']

    # get method based on method_name
    method = getattr(scoring_logic, method_name)

    # get params to pass to method
    # checks against dispatcher dictionary for integrity
    params = {}
    for key, value in kwargs.items():
        try:
            params[dispatcher[test_name]['params'][key]] = value
        except Exception:
            raise LookupError('param \'{}\' not implemented in test for \'{}\' in scoring_logic.py'.format(key, test_name))

    try:
        return method(**params)
    except Exception as e:
        raise LookupError(str(e))
        return 0

"""
Functions accessing health/config/dispatcher.py
"""

def get_legend(subscriber):
    """Return a tuple of tests and descriptions associated with each subscriber."""
    legend = {}
    dispatcher = get_dispatcher(original_dispatcher=True)
    health_keys = get_health_keys(subscriber)
    for key in health_keys:
        legend[key.decode('unicode-escape')] = dispatcher[key]['description'] if 'description' in dispatcher[key] else None

    return sorted(legend.items())

def calculate_severity(subscriber_state):
    """Return the highest score in state dict."""
    test_scores = [1, ]
    for test in subscriber_state.keys():
        if subscriber_state[test]['score']:
            test_scores.append(subscriber_state[test]['score'])

    return max(test_scores)

def get_health_keys(subscriber):
    """Return an array of tests associated with each subscriber."""
    dispatcher = get_dispatcher(original_dispatcher=True)
    health_keys = [x for x in dispatcher.keys() if subscriber in dispatcher[x]['subscriber']]

    return health_keys

def get_subscribers_list_for_test(test_name):
    """Return a list of subscribers for a given test."""
    dispatcher = get_dispatcher()
    return dispatcher[test_name]['subscriber']

# def get_latest_health_state(tui):
#     """Return the latest health state for a given TUI."""
#     health_state = {}
#     for subscriber in Subscriber.objects.values_list():
#         health_state[subscriber[1].lower()] = {}
#     dispatcher = get_dispatcher(original_dispatcher=True)
#
#     for test in dispatcher.keys():
#         score = get_latest_score(tui, test)
#         for subscriber in dispatcher[test]['subscriber']:
#             health_state[subscriber][test] = score
#
#     return health_state

def get_test_list(subscriber=None):
    """Get entire list of tests or those associated with a subscriber."""
    test_list = []
    dispatcher = get_dispatcher(original_dispatcher=True)

    if not subscriber:
        return sorted(dispatcher.keys())
    else:
        for key, value in dispatcher.items():
            if subscriber.lower() in value['subscriber']:
                test_list.append(key)

    return sorted(test_list)
<|endoftext|>


<commit_before>DEBUG = True
# GOOGLE_SERVICE_ACCOUNT_KEYFILE_PATH = '/home/vagrant/heltour/gspread.conf'
# SLACK_API_TOKEN_FILE_PATH = '/home/vagrant/heltour/slack-token.conf'

INTERNAL_IPS = ['127.0.0.1']
# JAVAFO_COMMAND = 'java -jar /home/vagrant/heltour/javafo.jar'
# STATIC_ROOT = '/home/vagrant/heltour/static'

# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
<commit_msg>Add config to local config to enable login to work<commit_after>DEBUG = True
# GOOGLE_SERVICE_ACCOUNT_KEYFILE_PATH = '/home/vagrant/heltour/gspread.conf'
# SLACK_API_TOKEN_FILE_PATH = '/home/vagrant/heltour/slack-token.conf'

INTERNAL_IPS = ['127.0.0.1']

LICHESS_CREDS_FILE_PATH = '/Users/colwem/workshop/heltour/.ignore/lichess.conf'
LINK_PROTOCOL = 'http'
SITE_ID = 2
# JAVAFO_COMMAND = 'java -jar /home/vagrant/heltour/javafo.jar'
# STATIC_ROOT = '/home/vagrant/heltour/static'

# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
<|endoftext|>


<commit_before>from __future__ import unicode_literals, absolute_import

# -*- coding: utf-8 -*-
__author__ = 'degibenz'

import logging

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

import json
import asyncio
import traceback

from aiohttp import web, MsgType

from configs.db import DB

from core.api import AbsView
from core.model import ObjectId

from models.chat import *
from models.client import Token, Client

from core.exceptions import *

__all__ = [
    'ChatWS'
]

loop = asyncio.get_event_loop()

# TODO было бы хорошо, сделать тут полноценное удержание соединения
database = DB(loop=loop)

def init_model():
    connection = database.hold_connect()
    return connection

class ChatWS(AbsView):
    ws = None

    chat = None
    client = None

    chat_pk = None
    client_pk = None

    agents = []

    db = None

    def __init__(self, request):
        self.db = init_model()

        self.chat_pk = ObjectId(request.match_info.get('id'))
        self.client_pk = ObjectId(request.match_info.get('client'))

        super(AbsView, self).__init__(request)

    async def check_receiver(self, receiver: ObjectId):
        client = Client(
            pk=ObjectId(receiver)
        )

        if not await client.get():
            raise ClientNotFound

        client_in_chat = ClientsInChatRoom()

        q = {
            'chat': self.chat_pk,
            'client': ObjectId(receiver)
        }

        if not await client_in_chat.objects.find_one(q):
            raise ClientNotFoundInChat

    async def prepare_msg(self):
        while True:
            msg = await self.ws.receive()

            if msg.tp == MsgType.text:

                if msg.data == 'close':

                    log.info("Client :: %s and exit from chat :: %s" % (self.client_pk, self.chat_pk))

                    await self.close_chat(for_me=True)

                else:
                    try:
                        data = json.loads(msg.data)

                        receiver = data.get('receiver', None)

                        if receiver:
                            await self.check_receiver(receiver)

                        msg_obj = MessagesFromClientInChat(
                            chat=self.chat.get('_id'),
                            client=self.client_pk,
                            msg=data.get('msg'),
                            receiver_message=receiver
                        )

                        await msg_obj.save()

                        try:
                            for client in self.agents:
                                await self.notify(client, msg_obj.message_content, receiver)
                        except(Exception,) as error:
                            log.error("%s" % error)

                    except(Exception,) as error:
                        log.error("prepare_msg :: %s" % error)

    async def check_client(self):
        token_in_header = self.request.__dict__.get('headers').get('AUTHORIZATION', None)

        if not token_in_header:
            raise TokeInHeadersNotFound

        token = Token()

        token_is = await token.objects.find_one({
            'token': token_in_header
        })

        if not token_is:
            raise TokenIsNotFound

        client = Client(
            pk=token_is.get('client')
        )

        self.client = await client.get()

        if not self.client:
            raise ClientNotFoundViaToken

    async def cache_ws(self):
        try:
            chat_root = ClientsInChatRoom(
                chat=self.chat_pk,
                client=self.client_pk,
            )

            q = {
                'chat': chat_root.chat,
                'client': chat_root.client,
                'online': True
            }

            if not await chat_root.objects.find_one(q):
                await chat_root.save()

        except(Exception,) as error:
            log.error("%s" % error)

    async def notify(self, item: dict, message: str, receiver=None):

        async def _notify():
            socket = item.get('socket')

            try:
                if not socket.closed:
                    await socket.send_str(
                        data="%s" % message
                    )

            except(Exception,) as error:
                log.error("notify :: %s" % error)

        if receiver:
            if item.get('client_uid') == receiver:
                message = "@%s" % receiver + message

        await _notify()

    async def close_chat(self, for_me=False):

        async def _del_it():
            try:
                await item.get('socket').close()
            except(Exception,)as error:
                pass
            finally:
                del item

        for item in self.agents:
            if for_me:
                if item.get('chat_uid') == self.chat_pk and item.get('client_uid') == self.client_pk:
                    await _del_it()
            else:
                await _del_it()

    async def get(self):
        try:

            chat = Chat(
                pk=self.chat_pk
            )

            self.chat = await chat.get()

            if not self.chat:
                raise ChatNotFound

            self.ws = web.WebSocketResponse()

            await self.ws.prepare(self.request)

            await self.cache_ws()

            self.agents.append(
                {
                    "client_uid": self.client_pk,
                    "chat_uid": self.chat_pk,
                    "socket": self.ws
                }
            )

            await asyncio.gather(self.prepare_msg())

        except(Exception,) as error:

            traceback.print_exc()

            self.response = {
                'status': False,
                'error': '%s' % error
            }

            log.error("%s" % self.response)
            data = {
                'client_uid': self.client_pk
            }
            self.notify(item=data, message="%s" % error)
        finally:

            return self.ws
<commit_msg>Fix in docker-compose<commit_after>from __future__ import unicode_literals, absolute_import

# -*- coding: utf-8 -*-
__author__ = 'degibenz'

import logging

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

import json
import asyncio
import traceback

from aiohttp import web, MsgType

from configs.db import DB

from core.api import AbsView
from core.model import ObjectId

from models.chat import *
from models.client import Token, Client

from core.exceptions import *

__all__ = [
    'ChatWS'
]

loop = asyncio.get_event_loop()

# TODO было бы хорошо, сделать тут полноценное удержание соединения
database = DB(loop=loop)

def init_model():
    connection = database.hold_connect()
    return connection

class ChatWS(AbsView):
    ws = None

    chat = None
    client = None

    chat_pk = None
    client_pk = None

    agents = []

    db = None

    def __init__(self, request):
        self.db = init_model()

        self.chat_pk = ObjectId(request.match_info.get('id'))
        self.client_pk = ObjectId(request.match_info.get('client'))

        super(AbsView, self).__init__(request)

    async def check_receiver(self, receiver: ObjectId):
        client = Client(
            pk=ObjectId(receiver)
        )

        if not await client.get():
            raise ClientNotFound

        client_in_chat = ClientsInChatRoom()

        q = {
            'chat': self.chat_pk,
            'client': ObjectId(receiver)
        }

        if not await client_in_chat.objects.find_one(q):
            raise ClientNotFoundInChat

    async def prepare_msg(self):
        while True:
            msg = await self.ws.receive()

            if msg.tp == MsgType.text:

                if msg.data == 'close':

                    log.info("Client :: %s and exit from chat :: %s" % (self.client_pk, self.chat_pk))

                    await self.close_chat(for_me=True)

                else:
                    try:
                        data = json.loads(msg.data)

                        receiver = data.get('receiver', None)

                        if receiver:
                            await self.check_receiver(receiver)

                        msg_obj = MessagesFromClientInChat(
                            chat=self.chat.get('_id'),
                            client=self.client_pk,
                            msg=data.get('msg'),
                            receiver_message=receiver
                        )

                        await msg_obj.save()

                        try:
                            for client in self.agents:
                                await self.notify(client, msg_obj.message_content, receiver)
                        except(Exception,) as error:
                            log.error("%s" % error)

                    except(Exception,) as error:
                        log.error("prepare_msg :: %s" % error)

    async def check_client(self):
        token_in_header = self.request.__dict__.get('headers').get('AUTHORIZATION', None)

        if not token_in_header:
            raise TokeInHeadersNotFound

        token = Token()

        token_is = await token.objects.find_one({
            'token': token_in_header
        })

        if not token_is:
            raise TokenIsNotFound

        client = Client(
            pk=token_is.get('client')
        )

        self.client = await client.get()

        if not self.client:
            raise ClientNotFoundViaToken

    async def cache_ws(self):
        try:
            chat_root = ClientsInChatRoom(
                chat=self.chat_pk,
                client=self.client_pk,
            )

            q = {
                'chat': chat_root.chat,
                'client': chat_root.client,
                'online': True
            }

            if not await chat_root.objects.find_one(q):
                await chat_root.save()

        except(Exception,) as error:
            log.error("%s" % error)

    async def notify(self, item: dict, message: str, receiver=None):

        async def _notify():
            socket = item.get('socket')

            try:
                if not socket.closed:
                    await socket.send_str(
                        data="%s" % message
                    )

            except(Exception,) as error:
                log.error("notify :: %s" % error)

        if receiver:
            if item.get('client_uid') == receiver:
                message = "@%s" % receiver + message

        await _notify()

    async def close_chat(self, for_me=False):

        async def _del_it():
            try:
                await item.get('socket').close()
            except(Exception,)as error:
                pass
            finally:
                del item

        for item in self.agents:
            if for_me:
                if item.get('chat_uid') == self.chat_pk and item.get('client_uid') == self.client_pk:
                    await _del_it()
            else:
                await _del_it()

    async def get(self):
        try:

            chat = Chat(
                pk=self.chat_pk
            )

            self.chat = await chat.get()

            if not self.chat:
                raise ChatNotFound

            self.ws = web.WebSocketResponse()

            await self.ws.prepare(self.request)

            await self.cache_ws()

            self.agents.append(
                {
                    "client_uid": self.client_pk,
                    "chat_uid": self.chat_pk,
                    "socket": self.ws
                }
            )

            await asyncio.gather(self.prepare_msg())

        except(Exception,) as error:

            traceback.print_exc()

            self.response = {
                'status': False,
                'error': '%s' % error
            }

            log.error("%s" % self.response)

            data = {
                'client_uid': self.client_pk
            }
            await self.notify(item=data, message="%s" % self.response)

        finally:
            return self.ws
<|endoftext|>


<commit_before>import hoomd
import numpy as np
import pytest
from copy import deepcopy
try:
    import gsd.hoomd
    skip_gsd = False
except ImportError:
    skip_gsd = True

skip_gsd = pytest.mark.skipif(skip_gsd,
                              reason="gsd Python package was not found.")

def make_gsd_snapshot(hoomd_snapshot):
    s = gsd.hoomd.Snapshot()
    for attr in dir(hoomd_snapshot):
        if attr[0] != '_' and attr not in ['exists', 'replicate']:
            for prop in dir(getattr(hoomd_snapshot, attr)):
                if prop[0] != '_':
                    # s.attr.prop = hoomd_snapshot.attr.prop
                    setattr(getattr(s, attr), prop,
                            getattr(getattr(hoomd_snapshot, attr), prop))
    return s

def set_types(s, inds, particle_types, particle_type):
    if s.exists:
        for i in inds:
            s.particles.typeid[i] = particle_types.index(particle_type)

def update_positions(snap):
    if snap.exists:
        noise = 0.01
        rs = np.random.RandomState(0)
        mean = [0] * 3
        var = noise * noise
        cov = np.diag([var, var, var])
        shape = snap.particles.position.shape
        snap.particles.position[:] += rs.multivariate_normal(mean,
                                                             cov,
                                                             size=shape[:-1])
    return snap

def assert_equivalent_snapshots(gsd_snap, hoomd_snap):
    for attr in dir(hoomd_snap):
        if attr[0] == '_' or attr in ['exists', 'replicate']:
            continue
        for prop in dir(getattr(hoomd_snap, attr)):
            if prop[0] == '_':
                continue
            elif prop == 'types':
                if hoomd_snap.exists:
                    assert getattr(getattr(gsd_snap, attr), prop) == \
                        getattr(getattr(hoomd_snap, attr), prop)
            else:
                if hoomd_snap.exists:
                    np.testing.assert_allclose(
                        getattr(getattr(gsd_snap, attr), prop),
                        getattr(getattr(hoomd_snap, attr), prop))

def random_inds(n):
    return np.random.choice(np.arange(n),
                            size=int(n * np.random.rand()),
                            replace=False)

def test_initialization(simulation_factory):
    with pytest.raises(TypeError):
        sim = hoomd.Simulation()

    sim = simulation_factory()  # noqa

def test_device_property(device):
    sim = hoomd.Simulation(device)
    assert sim.device is device

    with pytest.raises(ValueError):
        sim.device = device

def test_allows_compute_pressure(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory()
    assert sim.always_compute_pressure is False
    with pytest.raises(RuntimeError):
        sim.always_compute_pressure = True
    sim.create_state_from_snapshot(lattice_snapshot_factory())
    sim.always_compute_pressure = True
    assert sim.always_compute_pressure is True

def test_run(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory()
    with pytest.raises(RuntimeError):
        sim.run(1)  # Before setting state

    sim = simulation_factory(lattice_snapshot_factory())
    sim.run(1)

    assert sim.operations._scheduled

def test_tps(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory()
    assert sim.tps is None

    sim = simulation_factory(lattice_snapshot_factory())
    sim.run(100)

    assert sim.tps > 0

def test_timestep(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory()
    assert sim.timestep is None

    initial_steps = 10
    sim.timestep = initial_steps
    assert sim.timestep == initial_steps
    sim.create_state_from_snapshot(lattice_snapshot_factory())
    assert sim.timestep == initial_steps

    with pytest.raises(RuntimeError):
        sim.timestep = 20

def test_run_with_timestep(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory(lattice_snapshot_factory())

    steps = 0
    n_step_list = [1, 10, 100]
    for n_steps in n_step_list:
        steps += n_steps
        sim.run(n_steps)
        assert sim.timestep == steps

    assert sim.timestep == sum(n_step_list)

_state_args = [((10, ['A']), 10), ((5, ['A', 'B']), 20),
               ((8, ['A', 'B', 'C']), 4)]

@pytest.fixture(scope="function", params=_state_args)
def state_args(request):
    return deepcopy(request.param)

@skip_gsd
def test_state_from_gsd(simulation_factory, lattice_snapshot_factory,
                        state_args, tmp_path):
    snap_params, nsteps = state_args

    d = tmp_path / "sub"
    d.mkdir()
    filename = d / "temporary_test_file.gsd"
    with gsd.hoomd.open(name=filename, mode='wb+') as file:
        sim = simulation_factory(
            lattice_snapshot_factory(n=snap_params[0],
                                     particle_types=snap_params[1]))
        snap = sim.state.snapshot
        snapshot_dict = {}
        snapshot_dict[0] = snap
        file.append(make_gsd_snapshot(snap))
        box = sim.state.box
        for step in range(1, nsteps):
            particle_type = np.random.choice(snap_params[1])
            snap = update_positions(sim.state.snapshot)
            set_types(snap, random_inds(snap_params[0]), snap_params[1],
                      particle_type)
            file.append(make_gsd_snapshot(snap))
            snapshot_dict[step] = snap

    for step, snap in snapshot_dict.items():
        sim = simulation_factory()
        sim.create_state_from_gsd(filename, frame=step)
        assert box == sim.state.box
        assert_equivalent_snapshots(snap, sim.state.snapshot)

def test_writer_order(simulation_factory, two_particle_snapshot_factory):
    """Ensure that writers run at the end of the loop step."""

    class StepRecorder(hoomd.custom.Action):

        def __init__(self):
            self.steps = []

        def act(self, timestep):
            self.steps.append(timestep)

    record = StepRecorder()
    periodic = hoomd.trigger.Periodic(period=100, phase=0)
    writer = hoomd.write.CustomWriter(action=record, trigger=periodic)

    sim = simulation_factory(two_particle_snapshot_factory())
    sim.operations.writers.append(writer)

    sim.run(500)
    assert record.steps == [100, 200, 300, 400, 500]
    sim.run(500)
    assert record.steps == [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]

def test_writer_order_initial(simulation_factory,
                              two_particle_snapshot_factory):
    """Ensure that writers optionally run at the beginning of the loop."""

    class StepRecorder(hoomd.custom.Action):

        def __init__(self):
            self.steps = []

        def act(self, timestep):
            self.steps.append(timestep)

    record = StepRecorder()
    periodic = hoomd.trigger.Periodic(period=100, phase=0)
    writer = hoomd.write.CustomWriter(action=record, trigger=periodic)

    sim = simulation_factory(two_particle_snapshot_factory())
    sim.operations.writers.append(writer)

    sim.run(500, write_at_start=True)
    assert record.steps == [0, 100, 200, 300, 400, 500]
    sim.run(500, write_at_start=True)
    assert record.steps == [
        0,
        100,
        200,
        300,
        400,
        500,
        500,
        600,
        700,
        800,
        900,
        1000,
    ]

def test_large_timestep(simulation_factory, lattice_snapshot_factory):
    """Test that simluations suport large timestep values."""
    sim = simulation_factory()
    sim.timestep = 2**64 - 100
    sim.create_state_from_snapshot(lattice_snapshot_factory())

    sim.run(99)
    assert sim.timestep == 2**64 - 1

def test_timestep_wrap(simulation_factory, lattice_snapshot_factory):
    """Test that time steps wrap around."""
    sim = simulation_factory()
    sim.timestep = 2**64 - 100
    sim.create_state_from_snapshot(lattice_snapshot_factory())

    sim.run(200)
    assert sim.timestep == 100

def test_initial_timestep_range(simulation_factory):
    """Test that the initial timestep cannot be set out of range."""
    sim = simulation_factory()

    with pytest.raises(ValueError):
        sim.timestep = -1

    with pytest.raises(ValueError):
        sim.timestep = 2**64

    sim.timestep = 2**64 - 1
<commit_msg>Add range test for run()<commit_after>import hoomd
import numpy as np
import pytest
from copy import deepcopy
try:
    import gsd.hoomd
    skip_gsd = False
except ImportError:
    skip_gsd = True

skip_gsd = pytest.mark.skipif(skip_gsd,
                              reason="gsd Python package was not found.")

def make_gsd_snapshot(hoomd_snapshot):
    s = gsd.hoomd.Snapshot()
    for attr in dir(hoomd_snapshot):
        if attr[0] != '_' and attr not in ['exists', 'replicate']:
            for prop in dir(getattr(hoomd_snapshot, attr)):
                if prop[0] != '_':
                    # s.attr.prop = hoomd_snapshot.attr.prop
                    setattr(getattr(s, attr), prop,
                            getattr(getattr(hoomd_snapshot, attr), prop))
    return s

def set_types(s, inds, particle_types, particle_type):
    if s.exists:
        for i in inds:
            s.particles.typeid[i] = particle_types.index(particle_type)

def update_positions(snap):
    if snap.exists:
        noise = 0.01
        rs = np.random.RandomState(0)
        mean = [0] * 3
        var = noise * noise
        cov = np.diag([var, var, var])
        shape = snap.particles.position.shape
        snap.particles.position[:] += rs.multivariate_normal(mean,
                                                             cov,
                                                             size=shape[:-1])
    return snap

def assert_equivalent_snapshots(gsd_snap, hoomd_snap):
    for attr in dir(hoomd_snap):
        if attr[0] == '_' or attr in ['exists', 'replicate']:
            continue
        for prop in dir(getattr(hoomd_snap, attr)):
            if prop[0] == '_':
                continue
            elif prop == 'types':
                if hoomd_snap.exists:
                    assert getattr(getattr(gsd_snap, attr), prop) == \
                        getattr(getattr(hoomd_snap, attr), prop)
            else:
                if hoomd_snap.exists:
                    np.testing.assert_allclose(
                        getattr(getattr(gsd_snap, attr), prop),
                        getattr(getattr(hoomd_snap, attr), prop))

def random_inds(n):
    return np.random.choice(np.arange(n),
                            size=int(n * np.random.rand()),
                            replace=False)

def test_initialization(simulation_factory):
    with pytest.raises(TypeError):
        sim = hoomd.Simulation()

    sim = simulation_factory()  # noqa

def test_device_property(device):
    sim = hoomd.Simulation(device)
    assert sim.device is device

    with pytest.raises(ValueError):
        sim.device = device

def test_allows_compute_pressure(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory()
    assert sim.always_compute_pressure is False
    with pytest.raises(RuntimeError):
        sim.always_compute_pressure = True
    sim.create_state_from_snapshot(lattice_snapshot_factory())
    sim.always_compute_pressure = True
    assert sim.always_compute_pressure is True

def test_run(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory()
    with pytest.raises(RuntimeError):
        sim.run(1)  # Before setting state

    sim = simulation_factory(lattice_snapshot_factory())
    sim.run(1)

    assert sim.operations._scheduled

def test_tps(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory()
    assert sim.tps is None

    sim = simulation_factory(lattice_snapshot_factory())
    sim.run(100)

    assert sim.tps > 0

def test_timestep(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory()
    assert sim.timestep is None

    initial_steps = 10
    sim.timestep = initial_steps
    assert sim.timestep == initial_steps
    sim.create_state_from_snapshot(lattice_snapshot_factory())
    assert sim.timestep == initial_steps

    with pytest.raises(RuntimeError):
        sim.timestep = 20

def test_run_with_timestep(simulation_factory, lattice_snapshot_factory):
    sim = simulation_factory(lattice_snapshot_factory())

    steps = 0
    n_step_list = [1, 10, 100]
    for n_steps in n_step_list:
        steps += n_steps
        sim.run(n_steps)
        assert sim.timestep == steps

    assert sim.timestep == sum(n_step_list)

_state_args = [((10, ['A']), 10), ((5, ['A', 'B']), 20),
               ((8, ['A', 'B', 'C']), 4)]

@pytest.fixture(scope="function", params=_state_args)
def state_args(request):
    return deepcopy(request.param)

@skip_gsd
def test_state_from_gsd(simulation_factory, lattice_snapshot_factory,
                        state_args, tmp_path):
    snap_params, nsteps = state_args

    d = tmp_path / "sub"
    d.mkdir()
    filename = d / "temporary_test_file.gsd"
    with gsd.hoomd.open(name=filename, mode='wb+') as file:
        sim = simulation_factory(
            lattice_snapshot_factory(n=snap_params[0],
                                     particle_types=snap_params[1]))
        snap = sim.state.snapshot
        snapshot_dict = {}
        snapshot_dict[0] = snap
        file.append(make_gsd_snapshot(snap))
        box = sim.state.box
        for step in range(1, nsteps):
            particle_type = np.random.choice(snap_params[1])
            snap = update_positions(sim.state.snapshot)
            set_types(snap, random_inds(snap_params[0]), snap_params[1],
                      particle_type)
            file.append(make_gsd_snapshot(snap))
            snapshot_dict[step] = snap

    for step, snap in snapshot_dict.items():
        sim = simulation_factory()
        sim.create_state_from_gsd(filename, frame=step)
        assert box == sim.state.box
        assert_equivalent_snapshots(snap, sim.state.snapshot)

def test_writer_order(simulation_factory, two_particle_snapshot_factory):
    """Ensure that writers run at the end of the loop step."""

    class StepRecorder(hoomd.custom.Action):

        def __init__(self):
            self.steps = []

        def act(self, timestep):
            self.steps.append(timestep)

    record = StepRecorder()
    periodic = hoomd.trigger.Periodic(period=100, phase=0)
    writer = hoomd.write.CustomWriter(action=record, trigger=periodic)

    sim = simulation_factory(two_particle_snapshot_factory())
    sim.operations.writers.append(writer)

    sim.run(500)
    assert record.steps == [100, 200, 300, 400, 500]
    sim.run(500)
    assert record.steps == [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]

def test_writer_order_initial(simulation_factory,
                              two_particle_snapshot_factory):
    """Ensure that writers optionally run at the beginning of the loop."""

    class StepRecorder(hoomd.custom.Action):

        def __init__(self):
            self.steps = []

        def act(self, timestep):
            self.steps.append(timestep)

    record = StepRecorder()
    periodic = hoomd.trigger.Periodic(period=100, phase=0)
    writer = hoomd.write.CustomWriter(action=record, trigger=periodic)

    sim = simulation_factory(two_particle_snapshot_factory())
    sim.operations.writers.append(writer)

    sim.run(500, write_at_start=True)
    assert record.steps == [0, 100, 200, 300, 400, 500]
    sim.run(500, write_at_start=True)
    assert record.steps == [
        0,
        100,
        200,
        300,
        400,
        500,
        500,
        600,
        700,
        800,
        900,
        1000,
    ]

def test_large_timestep(simulation_factory, lattice_snapshot_factory):
    """Test that simluations suport large timestep values."""
    sim = simulation_factory()
    sim.timestep = 2**64 - 100
    sim.create_state_from_snapshot(lattice_snapshot_factory())

    sim.run(99)
    assert sim.timestep == 2**64 - 1

def test_timestep_wrap(simulation_factory, lattice_snapshot_factory):
    """Test that time steps wrap around."""
    sim = simulation_factory()
    sim.timestep = 2**64 - 100
    sim.create_state_from_snapshot(lattice_snapshot_factory())

    sim.run(200)
    assert sim.timestep == 100

def test_initial_timestep_range(simulation_factory):
    """Test that the initial timestep cannot be set out of range."""
    sim = simulation_factory()

    with pytest.raises(ValueError):
        sim.timestep = -1

    with pytest.raises(ValueError):
        sim.timestep = 2**64

    sim.timestep = 2**64 - 1

def test_run_limit(simulation_factory, lattice_snapshot_factory):
    """Test limits of Simulation.run()."""
    sim = simulation_factory(lattice_snapshot_factory())

    with pytest.raises(ValueError):
        sim.run(2**64 - 1)

    with pytest.raises(ValueError):
        sim.run(-1)
<|endoftext|>