NOX的使用之学习篇【二】
Developing in NOX:
Building a Python only component
Pure python components are much simpler to construct than C++ components. As an example, see src/nox/apps/examples/pyloop.py. A Python component must have the following construction:
from nox.lib.core import * class foo(Component): def __init__(self, ctxt): Component.__init__(self, ctxt) def install(self): # register for event here pass def getInterface(self): return str(foo) def getFactory(): class Factory: def instance(self, ctxt): return foo(ctxt) return Factory()
You may optionally add a configure method which is called in the same order as for C++ (before install on startup). The following steps should be all that is needed to build a bare-bones python component for NOX.
Add your .py file to src/nox/coreapps/examples/ Copy code from src/nox/coreapps/examples/pyloop.py (you need to mirror everything except for the code under the install method) Add your Python file(s) to NOX_RUNTIMEFILES in src/nox/apps/examples/Makefile.am Update src/nox/apps/examples/meta.json to include your new app. Make sure that “python” is a dependency (copying is the best approach). After you're done, recompile following the same 3 steps (the build/ directory is already there now)
Pointers:
- The core python API is in nox/lib/core.py and nox/lib/util.py.
- To get a handle to another component, use the Component.resolve(..) method on the class or interface to which you want a handle. For example:
from nox.app.examples.pyswitch import pyswitch self.resolve(pyswitch)
版权声明:本文为博主原创文章,未经博主允许不得转载。