I'd say the most efficient way would be writing a new plugin, which extends MHAPI on load.
In a file called for example "9_myuserfunctions.py" placed under "plugins":
- Code: Select all
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
from core import G
# The reason for the folloring arcane snippet is that python
# refuses to import from modules with a name that starts
# with a number.
mhapimodule = __import__("1_mhapi")
try:
attrlist = mhapimodule.__all__
except AttributeError:
attrlist = dir (mhapimodule)
for attr in attrlist:
globals()[attr] = getattr (mhapimodule, attr)
# Extending Namespace is stricly speaking not necessary, but it's the structure
# that the rest of MHAPI follows. In __init__ we'll also make a convenience assignment
# so you can access the whole of MHAPI via "self.mhapi" in all the methods.
class MyUserFunctions(namespace.NameSpace):
def __init__(self,api):
self.mhapi = api
namespace.NameSpace.__init__(self)
def oneOfMyFunctions(self):
print("I'm alive!")
def load(app):
G.app.mhapi.myuserfunctions = MyUserFunctions(G.app.mhapi)
def unload(app):
pass
Now, after startup, you can call this like any other MHAPI module. For example in the shell tab:
- Code: Select all
from core import G
G.app.mhapi.myuserfunctions.oneOfMyFunctions()
... will respond by printing "I'm alive"