Exemplo n.º 1
0
    def init_registry_with_bloks(self, bloks, function, **kwargs):
        """ call a function to filled the blok manager with new model and
        bloks to install

        :param bloks: list of blok's names
        :param function: function to call
        :param kwargs: kwargs for the function
        :rtype: registry instance
        """
        if bloks is None:
            bloks = []

        if isinstance(bloks, tuple):
            bloks = list(bloks)

        if 'anyblok-test' not in bloks:
            bloks.append('anyblok-test')

        from copy import deepcopy
        loaded_bloks = deepcopy(RegistryManager.loaded_bloks)
        if function is not None:
            EnvironmentManager.set('current_blok', 'anyblok-test')
            try:
                function(**kwargs)
            finally:
                EnvironmentManager.set('current_blok', None)

        try:
            self.registry = registry = self.__class__.getRegistry()
            if bloks:
                registry.upgrade(install=bloks)
        finally:
            RegistryManager.loaded_bloks = loaded_bloks

        return registry
Exemplo n.º 2
0
 def test_without_environment_for_set(self):
     # don't use define_environment_cls, because she must be verify
     EnvironmentManager.environment = None
     try:
         EnvironmentManager.set('db_name', 'test')
         self.fail('No watchdog for None environment')
     except EnvironmentException:
         pass
Exemplo n.º 3
0
    def _finish(self, final_state):
        assert self.transaction is not None
        del _SESSION_STATE[self.registry.session]
        registry = self.registry
        self.transaction = self.registry = None
        self.state = final_state
        if not self.keep_session:
            registry.session.close()
        else:
            registry.session.expire_all()

        EnvironmentManager.set('_precommit_hook', [])
Exemplo n.º 4
0
    def setUpClass(cls):
        super(TestMigration, cls).setUpClass()
        cls.init_configuration_manager()
        cls.createdb()
        BlokManager.load()

        register = Declarations.register
        Model = Declarations.Model

        cls.loaded_bloks = deepcopy(RegistryManager.loaded_bloks)
        EnvironmentManager.set('current_blok', 'anyblok-core')

        @register(Model)
        class Test:
            integer = Int(primary_key=True)
            other = Str()

        @register(Model)
        class TestUnique:
            integer = Int(primary_key=True)
            other = Str(unique=True)

        @register(Model)
        class TestFKTarget:
            integer = Int(primary_key=True)

        @register(Model)
        class TestFK:
            integer = Int(primary_key=True)
            other = Int(foreign_key=Model.TestFKTarget.use('integer'))

        @register(Model)
        class TestM2M1:
            idmodel1 = Int(primary_key=True)

        @register(Model)
        class TestM2M2:
            idmodel2 = Int(primary_key=True)
            rel_m2m = Many2Many(label="Rel", model=Model.TestM2M1,
                                join_table='reltable',
                                remote_columns='idmodel1',
                                m2m_remote_columns='idmodel1',
                                local_columns='idmodel2',
                                m2m_local_columns='idmodel2',
                                many2many='rel_m2m_inv')

        EnvironmentManager.set('current_blok', None)
Exemplo n.º 5
0
    def load(cls, entry_points=('bloks',)):
        """ Load all the bloks and import them

        :param entry_points: Use by ``iter_entry_points`` to get the blok
        :exception: BlokManagerException
        """
        if not entry_points:
            raise BlokManagerException("The entry_points mustn't be empty")

        cls.entry_points = entry_points

        if EnvironmentManager.get('current_blok'):
            while EnvironmentManager.get('current_blok'):
                sleep(0.1)

        EnvironmentManager.set('current_blok', 'start')

        bloks = []
        for entry_point in entry_points:
            count = 0
            for i in iter_entry_points(entry_point):
                count += 1
                blok = i.load()
                blok.required_by = []
                blok.optional_by = []
                blok.conditional_by = []
                blok.conflicting_by = []
                cls.set(i.name, blok)
                blok.name = i.name
                bloks.append((blok.priority, i.name))

            if not count:
                raise BlokManagerException(
                    "Invalid bloks group %r" % entry_point)

        # Empty the ordered blok to reload it depending on the priority
        cls.ordered_bloks = []
        bloks.sort()

        try:
            while bloks:
                blok = bloks.pop(0)[1]
                cls.get_need_blok(blok)

        finally:
            EnvironmentManager.set('current_blok', None)
Exemplo n.º 6
0
 def test_global_property(self):
     RegistryManager.declare_entry('Other')
     blok = 'newblok'
     RegistryManager.init_blok(blok)
     try:
         oldblok = EnvironmentManager.get('current_blok')
         EnvironmentManager.set('current_blok', blok)
         assert not RegistryManager.has_blok_property('myproperty')
         RegistryManager.add_or_replace_blok_property('myproperty', 2)
         assert RegistryManager.has_blok_property('myproperty')
         assert RegistryManager.get_blok_property('myproperty') == 2
         RegistryManager.add_or_replace_blok_property('myproperty', 3)
         assert RegistryManager.get_blok_property('myproperty') == 3
         RegistryManager.remove_blok_property('myproperty')
         assert not RegistryManager.has_blok_property('myproperty')
     finally:
         EnvironmentManager.set('current_blok', oldblok)
Exemplo n.º 7
0
    def load(cls, entry_points=('bloks', )):
        """ Load all the bloks and import them

        :param entry_points: Use by ``iter_entry_points`` to get the blok
        :exception: BlokManagerException
        """
        if not entry_points:
            raise BlokManagerException("The entry_points mustn't be empty")

        cls.entry_points = entry_points

        if EnvironmentManager.get('current_blok'):
            while EnvironmentManager.get('current_blok'):
                sleep(0.1)

        EnvironmentManager.set('current_blok', 'start')

        bloks = []
        for entry_point in entry_points:
            count = 0
            for i in iter_entry_points(entry_point):
                count += 1
                blok = i.load()
                blok.required_by = []
                blok.optional_by = []
                blok.conditional_by = []
                blok.conflicting_by = []
                cls.set(i.name, blok)
                blok.name = i.name
                bloks.append((blok.priority, i.name))

            if not count:
                raise BlokManagerException("Invalid bloks group %r" %
                                           entry_point)

        # Empty the ordered blok to reload it depending on the priority
        cls.ordered_bloks = []
        bloks.sort()

        try:
            while bloks:
                blok = bloks.pop(0)[1]
                cls.get_need_blok(blok)

        finally:
            EnvironmentManager.set('current_blok', None)
Exemplo n.º 8
0
    def reload(self):
        """ Reload all the imports for this module

        :exception: ImportManagerException
        """
        from anyblok.blok import BlokManager
        from anyblok.registry import RegistryManager
        from anyblok.environment import EnvironmentManager

        b = BlokManager.get(self.blok)
        if not hasattr(b, 'reload_declaration_module'):
            return

        try:
            EnvironmentManager.set('reload', True)
            RegistryManager.init_blok(self.blok)
            b.reload_declaration_module(reload_module)
        finally:
            EnvironmentManager.set('reload', False)
Exemplo n.º 9
0
    def reload(self):
        """ Reload all the imports for this module

        :exception: ImportManagerException
        """
        from anyblok.blok import BlokManager
        from anyblok.registry import RegistryManager
        from anyblok.environment import EnvironmentManager

        b = BlokManager.get(self.blok)
        if not hasattr(b, 'reload_declaration_module'):
            return

        try:
            EnvironmentManager.set('reload', True)
            RegistryManager.init_blok(self.blok)
            b.reload_declaration_module(reload_wraper)
        finally:
            EnvironmentManager.set('reload', False)
Exemplo n.º 10
0
 def test_global_property(self):
     RegistryManager.declare_entry('Other')
     blok = 'newblok'
     RegistryManager.init_blok(blok)
     try:
         oldblok = EnvironmentManager.get('current_blok')
         EnvironmentManager.set('current_blok', blok)
         self.assertEqual(RegistryManager.has_blok_property('myproperty'),
                          False)
         RegistryManager.add_or_replace_blok_property('myproperty', 2)
         self.assertEqual(
             RegistryManager.has_blok_property('myproperty'), True)
         self.assertEqual(
             RegistryManager.get_blok_property('myproperty'), 2)
         RegistryManager.add_or_replace_blok_property('myproperty', 3)
         self.assertEqual(
             RegistryManager.get_blok_property('myproperty'), 3)
         RegistryManager.remove_blok_property('myproperty')
         self.assertEqual(RegistryManager.has_blok_property('myproperty'),
                          False)
     finally:
         EnvironmentManager.set('current_blok', oldblok)
Exemplo n.º 11
0
 def test_global_property(self):
     RegistryManager.declare_entry('Other')
     blok = 'newblok'
     RegistryManager.init_blok(blok)
     try:
         oldblok = EnvironmentManager.get('current_blok')
         EnvironmentManager.set('current_blok', blok)
         self.assertEqual(RegistryManager.has_blok_property('myproperty'),
                          False)
         RegistryManager.add_or_replace_blok_property('myproperty', 2)
         self.assertEqual(RegistryManager.has_blok_property('myproperty'),
                          True)
         self.assertEqual(RegistryManager.get_blok_property('myproperty'),
                          2)
         RegistryManager.add_or_replace_blok_property('myproperty', 3)
         self.assertEqual(RegistryManager.get_blok_property('myproperty'),
                          3)
         RegistryManager.remove_blok_property('myproperty')
         self.assertEqual(RegistryManager.has_blok_property('myproperty'),
                          False)
     finally:
         EnvironmentManager.set('current_blok', oldblok)
Exemplo n.º 12
0
    def init_registry(self, function, **kwargs):
        """ call a function to filled the blok manager with new model

        :param function: function to call
        :param kwargs: kwargs for the function
        :rtype: registry instance
        """
        from copy import deepcopy
        loaded_bloks = deepcopy(RegistryManager.loaded_bloks)
        if function is not None:
            EnvironmentManager.set('current_blok', 'anyblok-core')
            try:
                function(**kwargs)
            finally:
                EnvironmentManager.set('current_blok', None)

        try:
            self.registry = registry = self.__class__.getRegistry()
        finally:
            RegistryManager.loaded_bloks = loaded_bloks

        return registry
Exemplo n.º 13
0
    def get_need_blok(cls, blok):
        if cls.has(blok):
            return True

        if blok not in cls.bloks:
            return False

        cls.get_need_blok_linked_bloks(blok)
        cls.ordered_bloks.append(blok)
        EnvironmentManager.set('current_blok', blok)

        if not ImportManager.has(blok):
            # Import only if not exist don't reload here
            mod = ImportManager.add(blok)
            mod.imports()
        else:
            mod = ImportManager.get(blok)
            mod.reload()

        if cls.bloks[blok].autoinstall:
            cls.auto_install.append(blok)

        return True
Exemplo n.º 14
0
    def get_need_blok(cls, blok):
        if cls.has(blok):
            return True

        if blok not in cls.bloks:
            return False

        cls.get_need_blok_linked_bloks(blok)
        cls.ordered_bloks.append(blok)
        EnvironmentManager.set('current_blok', blok)

        if not ImportManager.has(blok):
            # Import only if not exist don't reload here
            mod = ImportManager.add(blok)
            mod.imports()
        else:
            mod = ImportManager.get(blok)
            mod.reload()

        if cls.bloks[blok].autoinstall:
            cls.auto_install.append(blok)

        return True
Exemplo n.º 15
0
    def reload_registry(self, registry, function, **kwargs):
        """ call a function to filled the blok manager with new model and
        before reload the registry

        :param bloks: list of blok's names
        :param function: function to call
        :param kwargs: kwargs for the function
        :rtype: registry instance
        """
        from copy import deepcopy
        loaded_bloks = deepcopy(RegistryManager.loaded_bloks)
        if function is not None:
            EnvironmentManager.set('current_blok', 'anyblok-test')
            try:
                function(**kwargs)
            finally:
                EnvironmentManager.set('current_blok', None)

        try:
            registry.reload()
        finally:
            RegistryManager.loaded_bloks = loaded_bloks

        return registry
Exemplo n.º 16
0
    def reload_registry(self, registry, function, **kwargs):
        """ call a function to filled the blok manager with new model and
        before reload the registry

        :param bloks: list of blok's names
        :param function: function to call
        :param kwargs: kwargs for the function
        :rtype: registry instance
        """
        from copy import deepcopy
        loaded_bloks = deepcopy(RegistryManager.loaded_bloks)
        if function is not None:
            EnvironmentManager.set('current_blok', 'anyblok-test')
            try:
                function(**kwargs)
            finally:
                EnvironmentManager.set('current_blok', None)

        try:
            registry.reload()
        finally:
            RegistryManager.loaded_bloks = loaded_bloks

        return registry
Exemplo n.º 17
0
 def setUpClass(cls):
     super(TestCoreInterfaceCoreBase, cls).setUpClass()
     RegistryManager.init_blok('testCore' + cls._corename)
     EnvironmentManager.set('current_blok', 'testCore' + cls._corename)
Exemplo n.º 18
0
 def revert():
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testModel']
Exemplo n.º 19
0
 def tearDownClass(cls):
     super(TestRegistryEntry, cls).tearDownClass()
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testEntry']
     RegistryManager.undeclare_entry('Other')
Exemplo n.º 20
0
 def setUpClass(cls):
     super(TestRegistryCore, cls).setUpClass()
     RegistryManager.declare_core('test')
     RegistryManager.init_blok('testCore')
     EnvironmentManager.set('current_blok', 'testCore')
Exemplo n.º 21
0
 def tearDownClass(cls):
     super(TestRegistryCore, cls).tearDownClass()
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testCore']
     RegistryManager.undeclare_core('test')
Exemplo n.º 22
0
 def tearDownClass(cls):
     super(TestCoreInterfaceMixin, cls).tearDownClass()
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testMixin']
Exemplo n.º 23
0
 def setUpClass(cls):
     super(TestCoreInterfaceCoreBase, cls).setUpClass()
     RegistryManager.init_blok('testCore' + cls._corename)
     EnvironmentManager.set('current_blok', 'testCore' + cls._corename)
Exemplo n.º 24
0
 def test_set_and_get_variable(self):
     db_name = 'test db name'
     EnvironmentManager.set('db_name', db_name)
     self.assertEqual(EnvironmentManager.get('db_name'), db_name)
Exemplo n.º 25
0
 def setUpClass(cls):
     super(TestModel, cls).setUpClass()
     RegistryManager.init_blok('testModel')
     EnvironmentManager.set('current_blok', 'testModel')
Exemplo n.º 26
0
 def setUpClass(cls):
     super(TestModel, cls).setUpClass()
     RegistryManager.init_blok('testModel')
     EnvironmentManager.set('current_blok', 'testModel')
Exemplo n.º 27
0
 def tearDownClass(cls):
     super(TestRegistryCore, cls).tearDownClass()
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testCore']
     RegistryManager.undeclare_core('test')
Exemplo n.º 28
0
 def setUpClass(cls):
     super(TestRegistryCore, cls).setUpClass()
     RegistryManager.declare_core('test')
     RegistryManager.init_blok('testCore')
     EnvironmentManager.set('current_blok', 'testCore')
Exemplo n.º 29
0
 def tearDownClass(cls):
     super(TestModel, cls).tearDownClass()
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testModel']
Exemplo n.º 30
0
 def setUp(self):
     super(TestTask, self).setUp()
     self.registry = registry = self.init_registry(None)
     registry.upgrade(install=('test_dramatiq_2', ))
     EnvironmentManager.set('job_autocommit', False)
Exemplo n.º 31
0
 def __exit__(self, type, value, traceback):
     EnvironmentManager.set('context', self.previous_context)
Exemplo n.º 32
0
 def reset():
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testCore' + self._corename]
Exemplo n.º 33
0
 def revert():
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testCore']
     RegistryManager.undeclare_core('test')
Exemplo n.º 34
0
 def tearDownClass(cls):
     super(TestCoreInterfaceCoreBase, cls).tearDownClass()
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testCore' + cls._corename]
Exemplo n.º 35
0
 def setUpClass(cls):
     super(TestCoreInterfaceMixin, cls).setUpClass()
     RegistryManager.init_blok('testMixin')
     EnvironmentManager.set('current_blok', 'testMixin')
Exemplo n.º 36
0
    def setUpClass(cls):
        super(TestMigration, cls).setUpClass()
        cls.init_configuration_manager()
        cls.createdb()
        BlokManager.load()

        register = Declarations.register
        Model = Declarations.Model

        cls.loaded_bloks = deepcopy(RegistryManager.loaded_bloks)
        EnvironmentManager.set('current_blok', 'anyblok-core')

        @register(Model)
        class Test:
            integer = Int(primary_key=True)
            other = Str()

        @register(Model)
        class TestUnique:
            integer = Int(primary_key=True)
            other = Str(unique=True)

        @register(Model)
        class TestIndex:
            integer = Int(primary_key=True)
            other = Str(index=True)

        @register(Model)
        class TestCheck:
            integer = Int(primary_key=True)

            @classmethod
            def define_table_args(cls):
                table_args = super(TestCheck, cls).define_table_args()
                return table_args + (
                    CheckConstraint('integer > 0', name='test'),)

        @register(Model)
        class TestCheckLongConstraintName:
            integer = Int(primary_key=True)

            @classmethod
            def define_table_args(cls):
                table_args = super(TestCheckLongConstraintName,
                                   cls).define_table_args()
                return table_args + (
                    CheckConstraint('integer > 0', name=(
                        'long_long_long_long_long_long_long_long_long_long_'
                        'long_long_long_long_long_long_long_long_test')),)

        @register(Model)
        class TestFKTarget:
            integer = Int(primary_key=True)

        @register(Model)
        class TestFK:
            integer = Int(primary_key=True)
            other = Int(
                foreign_key=Model.TestFKTarget.use('integer'))

        @register(Model)
        class TestFK2:
            integer = Int(primary_key=True)
            other = Int(
                foreign_key=Model.TestFKTarget.use('integer').options(
                    ondelete='cascade'))

        @register(Model)
        class TestM2M1:
            idmodel1 = Int(primary_key=True)

        @register(Model)
        class TestM2M2:
            idmodel2 = Int(primary_key=True)
            rel_m2m = Many2Many(label="Rel", model=Model.TestM2M1,
                                join_table='reltable',
                                remote_columns='idmodel1',
                                m2m_remote_columns='idmodel1',
                                local_columns='idmodel2',
                                m2m_local_columns='idmodel2',
                                many2many='rel_m2m_inv')

        EnvironmentManager.set('current_blok', None)
Exemplo n.º 37
0
 def test_set_and_get_variable(self):
     db_name = 'test db name'
     EnvironmentManager.set('db_name', db_name)
     self.assertEqual(EnvironmentManager.get('db_name'), db_name)
Exemplo n.º 38
0
    def setUpClass(cls):
        super(TestMigration, cls).setUpClass()
        cls.init_configuration_manager()
        cls.createdb()
        BlokManager.load()

        register = Declarations.register
        Model = Declarations.Model

        cls.loaded_bloks = deepcopy(RegistryManager.loaded_bloks)
        EnvironmentManager.set('current_blok', 'anyblok-core')

        @register(Model)
        class Test:
            integer = Int(primary_key=True)
            other = Str()

        @register(Model)
        class TestUnique:
            integer = Int(primary_key=True)
            other = Str(unique=True)

        @register(Model)
        class TestIndex:
            integer = Int(primary_key=True)
            other = Str(index=True)

        @register(Model)
        class TestCheck:
            integer = Int(primary_key=True)

            @classmethod
            def define_table_args(cls):
                table_args = super(TestCheck, cls).define_table_args()
                return table_args + (CheckConstraint('integer > 0',
                                                     name='test'), )

        @register(Model)
        class TestCheckLongConstraintName:
            integer = Int(primary_key=True)

            @classmethod
            def define_table_args(cls):
                table_args = super(TestCheckLongConstraintName,
                                   cls).define_table_args()
                return table_args + (CheckConstraint(
                    'integer > 0',
                    name=('long_long_long_long_long_long_long_long_long_long_'
                          'long_long_long_long_long_long_long_long_test')), )

        @register(Model)
        class TestFKTarget:
            integer = Int(primary_key=True)

        @register(Model)
        class TestFK:
            integer = Int(primary_key=True)
            other = Int(foreign_key=Model.TestFKTarget.use('integer'))

        @register(Model)
        class TestFK2:
            integer = Int(primary_key=True)
            other = Int(foreign_key=Model.TestFKTarget.use('integer').options(
                ondelete='cascade'))

        @register(Model)
        class TestM2M1:
            idmodel1 = Int(primary_key=True)

        @register(Model)
        class TestM2M2:
            idmodel2 = Int(primary_key=True)
            rel_m2m = Many2Many(label="Rel",
                                model=Model.TestM2M1,
                                join_table='reltable',
                                remote_columns='idmodel1',
                                m2m_remote_columns='idmodel1',
                                local_columns='idmodel2',
                                m2m_local_columns='idmodel2',
                                many2many='rel_m2m_inv')

        EnvironmentManager.set('current_blok', None)
Exemplo n.º 39
0
 def __init__(self, *args, **kwargs):
     EnvironmentManager.set('current_blok', None)
     super(BlokManagerException, self).__init__(*args, **kwargs)
Exemplo n.º 40
0
 def setUpClass(cls):
     super(TestCoreInterfaceMixin, cls).setUpClass()
     RegistryManager.init_blok('testMixin')
     EnvironmentManager.set('current_blok', 'testMixin')
Exemplo n.º 41
0
 def setUpClass(cls):
     super(TestRegistryEntry, cls).setUpClass()
     RegistryManager.declare_entry('Other')
     RegistryManager.init_blok('testEntry')
     EnvironmentManager.set('current_blok', 'testEntry')
Exemplo n.º 42
0
 def tearDownClass(cls):
     super(TestCoreInterfaceMixin, cls).tearDownClass()
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testMixin']
Exemplo n.º 43
0
 def tearDownClass(cls):
     super(TestModel, cls).tearDownClass()
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testModel']
Exemplo n.º 44
0
 def tearDownClass(cls):
     super(TestCoreInterfaceCoreBase, cls).tearDownClass()
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testCore' + cls._corename]
Exemplo n.º 45
0
 def __init__(self, *args, **kwargs):
     EnvironmentManager.set('current_blok', None)
     super(BlokManagerException, self).__init__(*args, **kwargs)
Exemplo n.º 46
0
 def revert():
     EnvironmentManager.set('current_blok', None)
     del RegistryManager.loaded_bloks['testEntry']
     RegistryManager.undeclare_entry('Other')