|
From: Ype K. <yk...@xs...> - 2002-01-21 21:39:40
|
Klaus,
>Hi,
>
>I am writing a Home Automation server in Java and would like to add some
>scripting. There is no better choice than Python IMHO.
>
>Basically the server contains a number of objects, each of which represents a
>switch, relay, sensor, etc. I would then like for the user to be able to add
>a Python script to each of these objects.
>
>The scripts will be saved in a MYSQL database (possibly cached in the server
>for speed) and when an object changes state the associated script be executed
>by Jython in it's own thread, doing interactions with the home automation
>state event process ("Sensor fired: Activate Foghorn Object") in the server
>over a threadsafe synchronized event queue.
You can use a PythonInterpreter to do this. It's .java code is worthwhile
studying.
>I would appreciate any thoughts on using Jython for scripting in such
>embedded app (I am very new to Jython).
In case you are not new to python:
Many things are the same in jython and python.
The executed scripts might change the state of eg. sys.modules
when they import jython modules. You might need to unload these modules.
You will have to provide the namespaces for the executed scripts.
You'll need to catch exceptions from the scripts and report them somewhere.
Some things are different:
You can invoke java.lang.System.gc() and friends.
You can manipulate thread priorities.
You can use any available java library from jython.
>The best way to do it, thoughts? Are there examples out there?
There is a very basic embedding example in the jython docs.
This will allow you to call jython code from java.
Once you get there I'd recommend writing the rest in jython
first, and move to java if/when needed later.
(You might also prototype/write your whole server in jython,
it's fast enough for home automation...)
You can cache code (compiled or source) for execution in both
jython and java. When using exec() you'll have to find
the source code in your database.
To start a jython thread use the thread module from jython or
be inspired by PyThread.java from the jython sources.
>Can Jython validate the code without executing it to give the user a chance to
>correct superficial errors (e.g. syntax, semantics)?
Not really. You can import the script using the __import function, but
that might already do more than you would like. You might consider
providing a test mode with eg. output queues that just print/log their contents
instead of exercising the server and input queues feeding from a
constant jython list or java array.
>Will the Jython machine make available errors of the interpretation I can
>give to the user or put in a log (hmm, maybe it goes to the outputstream)?
Yes. You can catch them in jython or in java. See eg.
org/python/core/PyFinalizableObject.java at the __del__() method.
>What is the best way to make the script communicate with the underlying
>Java-app (I understand there might be several)?
In jython you can import java classes on the class path directly, meaning
that while running in your server all(!) public java classes
can be imported by your scripts.
The scripts can then call any public method on any object they
can get hold of. If security is of concern you'll have to use
either java's security features or write your own interpreter.
What would life be without choices?
Kind regards,
Ype
--
|