Esempio n. 1
0
    def test_already_loaded(self):
        import spam
        imports.loaded_modules = ['spam']

        imports.when_imported('spam', self._hook)

        self.assertTrue(self.executed)
Esempio n. 2
0
    def test_already_loaded(self):
        import spam
        imports.loaded_modules = ['spam']

        imports.when_imported('spam', self._hook)

        self.assertTrue(self.executed)
Esempio n. 3
0
    def test_already_imported(self):
        import spam

        self.assertFalse(self.executed)

        imports.when_imported('spam', self._hook)

        self._check_module(spam)
        self.assertTrue(self.executed)
Esempio n. 4
0
    def test_already_imported(self):
        import spam

        self.assertFalse(self.executed)

        imports.when_imported('spam', self._hook)

        self._check_module(spam)
        self.assertTrue(self.executed)
Esempio n. 5
0
    def test_failed_hook(self):
        def h(mod):
            raise RuntimeError

        imports.when_imported('spam', h)

        try:
            import spam  # noqa
        except Exception, e:
            pass
Esempio n. 6
0
    def test_failed_hook(self):
        def h(mod):
            raise RuntimeError

        imports.when_imported('spam', h)

        try:
            import spam
        except Exception, e:
            pass
Esempio n. 7
0
    def test_import(self):
        imports.loaded_modules = []

        imports.when_imported('spam', self._hook)

        self.assertFalse(self.executed)

        import spam

        self.assertTrue(self.executed)
        self.assertEquals(imports.loaded_modules, ['spam'])
Esempio n. 8
0
    def test_import(self):
        imports.loaded_modules = []

        imports.when_imported('spam', self._hook)

        import logging
        logging.debug(sys.meta_path)

        self.assertFalse(self.executed)

        import spam

        self.assertTrue(self.executed)
        self.assertEquals(imports.loaded_modules, ['spam'])
Esempio n. 9
0
    def test_failed_hook(self):
        def h(mod):
            raise RuntimeError

        imports.when_imported('spam', h)

        try:
            import spam  # noqa
        except Exception as e:
            self.assertEqual(e.__class__, RuntimeError)
        else:
            self.fail('expected exception')

        self.assertFalse('spam' in self.finder.loaded_modules)
Esempio n. 10
0
    def test_import(self):
        imports.loaded_modules = []

        imports.when_imported('spam', self._hook)

        import logging
        logging.debug(sys.meta_path)

        self.assertFalse(self.executed)

        import spam

        self.assertTrue(self.executed)
        self.assertEquals(imports.loaded_modules, ['spam'])
Esempio n. 11
0
    def test_failed_hook(self):
        def h(mod):
            raise RuntimeError

        imports.when_imported('spam', h)

        try:
            import spam  # noqa
        except Exception:
            pass
        else:
            self.fail('expected exception')

        self.assertFalse('spam' in self.finder.loaded_modules)

        self.assertEqual(e.__class__, RuntimeError)
Esempio n. 12
0
def register_adapter(mod, func):
    """
    Registers a callable to be executed when a module is imported. If the
    module already exists then the callable will be executed immediately.
    You can register the same module multiple times, the callables will be
    executed in the order they were registered. The root module must exist
    (i.e. be importable) otherwise an `ImportError` will be thrown.

    @param mod: The fully qualified module string, as used in the imports
        statement. E.g. 'foo.bar.baz'. The string must map to a module
        otherwise the callable will not fire.
    @param func: The function to call when C{mod} is imported. This function
        must take one arg, the newly imported C{module} object.
    @type func: callable
    """
    if not hasattr(func, '__call__'):
        raise TypeError('func must be callable')

    imports.when_imported(mod, func)
Esempio n. 13
0
def register_adapter(mod, func):
    """
    Registers a callable to be executed when a module is imported. If the
    module already exists then the callable will be executed immediately.
    You can register the same module multiple times, the callables will be
    executed in the order they were registered. The root module must exist
    (i.e. be importable) otherwise an `ImportError` will be thrown.

    @param mod: The fully qualified module string, as used in the imports
        statement. E.g. 'foo.bar.baz'. The string must map to a module
        otherwise the callable will not fire.
    @param func: The function to call when C{mod} is imported. This function
        must take one arg, the newly imported C{module} object.
    @type func: callable
    """
    if not hasattr(func, '__call__'):
        raise TypeError('func must be callable')

    imports.when_imported(mod, func)
Esempio n. 14
0
    def test_already_imported(self):
        import spam

        imports.when_imported('spam', self._hook)

        self.assertTrue(self.executed)
Esempio n. 15
0
    def test_register(self):
        imports.when_imported('spam', self._hook)

        self.assertTrue('spam' in imports.post_load_hooks)
        self.assertEquals(imports.post_load_hooks['spam'], [self._hook])
Esempio n. 16
0
    try:
        referenced_object = gae_objects.getClassKey(kls, s)
    except KeyError:
        referenced_object = object
        gae_objects.addClassKey(kls, s, object)

    self.writeNonGAEObject(referenced_object, *args, **kwargs)


def install_gae_reference_model_hook(mod):
    """
    Called when L{pyamf.amf0} or L{pyamf.amf3} are imported. Attaches the
    L{writeGAEObject} method to the C{Encoder} class in that module.

    @param mod: The module imported.
    @since: 0.4.1
    """
    if not hasattr(mod.Encoder, 'writeNonGAEObject'):
        mod.Encoder.writeNonGAEObject = mod.Encoder.writeObject
        mod.Encoder.writeObject = writeGAEObject

# initialise the module here: hook into pyamf

pyamf.add_type(db.Query, util.to_list)
pyamf.register_alias_type(DataStoreClassAlias, db.Model, db.Expando)

# hook the L{writeGAEObject} method to the Encoder class on import
imports.when_imported('pyamf.amf0', install_gae_reference_model_hook)
imports.when_imported('pyamf.amf3', install_gae_reference_model_hook)
Esempio n. 17
0
    def test_already_imported(self):
        import spam

        imports.when_imported('spam', self._hook)

        self.assertTrue(self.executed)
Esempio n. 18
0
    def test_register(self):
        imports.when_imported('spam', self._hook)

        self.assertTrue('spam' in imports.post_load_hooks)
        self.assertEquals(imports.post_load_hooks['spam'], [self._hook])
Esempio n. 19
0
    try:
        referenced_object = django_objects.getClassKey(kls, s)
    except KeyError:
        referenced_object = obj
        django_objects.addClassKey(kls, s, obj)

    self.writeNonDjangoObject(referenced_object, *args, **kwargs)


def install_django_reference_model_hook(mod):
    """
    Called when L{pyamf.amf0} or L{pyamf.amf3} are imported. Attaches the
    L{writeDjangoObject} method to the C{Encoder} class in that module.

    @param mod: The module imported.
    @since: 0.4.1
    """
    if not hasattr(mod.Encoder, "writeNonDjangoObject"):
        mod.Encoder.writeNonDjangoObject = mod.Encoder.writeObject
        mod.Encoder.writeObject = writeDjangoObject


# initialise the module here: hook into pyamf

pyamf.register_alias_type(DjangoClassAlias, Model)

# hook the L{writeDjangobject} method to the Encoder class on import
imports.when_imported("pyamf.amf0", install_django_reference_model_hook)
imports.when_imported("pyamf.amf3", install_django_reference_model_hook)
Esempio n. 20
0
    try:
        referenced_object = django_objects.getClassKey(kls, s)
    except KeyError:
        referenced_object = obj
        django_objects.addClassKey(kls, s, obj)

    self.writeNonDjangoObject(referenced_object, *args, **kwargs)


def install_django_reference_model_hook(mod):
    """
    Called when :module:`pyamf.amf0` or :module:`pyamf.amf3` are imported. Attaches the
    :func:`writeDjangoObject` method to the `Encoder` class in that module.

    :param mod: The module imported.
    :since: 0.4.1
    """
    if not hasattr(mod.Encoder, 'writeNonDjangoObject'):
        mod.Encoder.writeNonDjangoObject = mod.Encoder.writeObject
        mod.Encoder.writeObject = writeDjangoObject


# initialise the module here: hook into pyamf

pyamf.register_alias_type(DjangoClassAlias, Model)

# hook the L{writeDjangobject} method to the Encoder class on import
imports.when_imported('pyamf.amf0', install_django_reference_model_hook)
imports.when_imported('pyamf.amf3', install_django_reference_model_hook)