Page 1 of 1

Import variable from plugin a to plugin b

Posted: Wed Feb 05, 2014 6:16 am
by BrunoG
Hello,

So i'm trying to import a variable from plugin a to plugin b, but i'm not being able to understand where the error is because the plugin2 just says it can't be loaded.

Code

Syntax: Select all

#  cstrike/addons/source-python/plugins/plugin1/plugin1.py

variable = ["foo"]


Syntax: Select all

# cstrike/addons/source-python/plugins/plugin2/plugin2.py

from plugin1 import variable


Code: Select all

#  cstrike/cfg/autoexec.cfg

sp load plugin1

sp load plugin2


Is this even possible with SP? I've done something similar with ES and it works. It's probably a namespace issue, but i can't seem to figure it out.

Thanks!

Edit:

I think i "figured it out", i've created at __init__.py with the necessary variables and it allows me to import it correctly.

Posted: Wed Feb 05, 2014 6:38 am
by satoon101
Actually, just as the other recent thread about imports, you need to use the correct name. Notice that your plugin1.py is inside the plugin1 folder. Therefor, the import path for that module is plugin1.plugin1. So, if you want to get a variable from a different script, you could just use:

Syntax: Select all

from plugin1.plugin1 import variable

Also, you could use the SPPluginManager's get_plugin_instance method to retrieve plugin1's globals:

Syntax: Select all

from _core.command import SPPluginManager

test = SPPluginManager.get_plugin_instance('plugin1')
variable = test.globals['variable']

The SPPluginManager object will be moved (or at least imported) somewhere that isn't private fairly soon. There are still some changes planned for the PluginManager system. I will also look into using __getattr__ to retrieve values from <plugin>.globals automatically, so that it will more resemble the following in the future:

Syntax: Select all

variable = test.variable


Satoon

Posted: Wed Feb 05, 2014 6:44 am
by BrunoG
Using the SPPluginManager works.

Thanks for the quick reply!

Posted: Wed Dec 03, 2014 2:10 am
by 8guawong
satoon101 wrote:

Syntax: Select all

from plugin1.plugin1 import variable

this does not work


satoon101 wrote:

Syntax: Select all

from _core.command import SPPluginManager

test = SPPluginManager.get_plugin_instance('plugin1')
variable = test.globals['variable']


don't work either (probably b/c i have newer version)

test.py

Syntax: Select all

x = 30


test2.py

Syntax: Select all

from test.test import x
print(x)


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '../addons/source-python/packages/source-python/plugins/manager.py', line 72, in __missing__
    instance = self.instance(plugin_name, self.base_import)
  File '../addons/source-python/packages/source-python/plugins/instance.py', line 82, in __init__
    self._plugin = import_module(import_name)
  File '../addons/source-python/plugins/test2/test2.py', line 12, in <module>
    from test.test import x

ImportError: cannot import name 'x'


test3.py

Syntax: Select all

from _core.command import SPPluginManager
test = plugins.manager.get_plugin_instance('test')
x = test.globals['x']


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '../addons/source-python/packages/source-python/plugins/manager.py', line 72, in __missing__
    instance = self.instance(plugin_name, self.base_import)
  File '../addons/source-python/packages/source-python/plugins/instance.py', line 82, in __init__
    self._plugin = import_module(import_name)
  File '../addons/source-python/plugins/test3/test3.py', line 12, in <module>
    from _core.command import SPPluginManager

ImportError: No module named '_core'



tried

Syntax: Select all

import plugins.manager

test = plugins.manager.get_plugin_instance('test')
x = test.globals['x']

print(x)


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '../addons/source-python/packages/source-python/plugins/manager.py', line 72, in __missing__
    instance = self.instance(plugin_name, self.base_import)
  File '../addons/source-python/packages/source-python/plugins/instance.py', line 82, in __init__
    self._plugin = import_module(import_name)
  File '../addons/source-python/plugins/test4/test4.py', line 14, in <module>
    test = plugins.manager.get_plugin_instance('test')

AttributeError: 'module' object has no attribute 'get_plugin_instance'

Posted: Wed Dec 03, 2014 2:24 am
by L'In20Cible
_core.command.SPPluginManager has been moved.

Syntax: Select all

from core.manager import core_plugin_manager

test = core_plugin_manager.get_plugin_instance('test')
x = test.globals['x']
Works as soon as "test" was loaded with "sp load test" prior test2.

Syntax: Select all

from test.test import x

print(x)
Also works.

Posted: Wed Dec 03, 2014 2:57 am
by 8guawong
ok this is very weird

Code: Select all

sp load test
[SP] Loading plugin 'test'...
[SP] Successfully loaded plugin 'test'.
sp load test2
30
[SP] Loading plugin 'test2'...
[SP] Successfully loaded plugin 'test2'.
sp unload test
[SP] Unloading plugin 'test'...
[SP] Successfully unloaded plugin 'test'.
sp unload test2
[SP] Unloading plugin 'test2'...
[SP] Successfully unloaded plugin 'test2'.
sp load test2
30
[SP] Loading plugin 'test2'...
[SP] Successfully loaded plugin 'test2'.


30 still gets printed when test is unloaded

Posted: Wed Dec 03, 2014 3:51 am
by satoon101
That isn't weird at all. Try loading test2 prior to ever loading test and you will get the same result. The import still exists, therefor you can still import the value. This is why you should check if test is loaded prior to importing from it. To do this, you can use either of the following:

Syntax: Select all

from core.manager import core_plugin_manager

if not core_plugin_manager.is_loaded('test'):
raise ImportError('Plugin "test" is not loaded.')

from test.test import x


Syntax: Select all

from core.manager import core_plugin_manager

if core_plugin_manager.get_plugin_instance('test') is None:
raise ImportError('Plugin "test" is not loaded.')

from test.test import x