Reg Jython Python wrap for Java - jython

I have been assigned a task to convert a java standalone application to python web application.Recoding the entire module in python language would take a lot of time and effort.Hence I was adviced do a quick wrap up of python and get the code working (jython.org) (Jython is Python wrap for JAVA )..Could any one guide me how to get started as I am new to Python as well as Jython?

To get you started:
If you're familiar with Java, then you should be able to get to the Jython prompt no problem. Just execute it like any other .jar. If you didn't download the standalone jython.jar, be sure to include the Jython libraries in your classpath.
Say your Java application's package is named com.stackoverflow.q10715162, and is compiled as a .jar in C:\jars\your_app.jar.
Then, you can get access to its classes in Jython. At the Jython prompt:
>>> import sys
>>> sys.path
['', 'C:\\jython\\Lib', 'C:\\jython\\jython.jar\\Lib', '__classpath__',
'__pyclasspath__']
Here, sys.path is, among other things, a list of directories where your Jython distribution is looking for compiled modules. By adding your compiled Java application to the list, it will become accessible (more in-depth info is available for this at http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html):
>>> sys.path.append('C:\\jars\\your_app.jar')
>>> import com.stackoverflow.q10715162 as yourapp
*sys-package-mgr*: processing new jar, 'C:\\jars\\your_app.jar'
>>> dir(yourapp)
['Class1', 'Class2', 'Class3', ...]
By using dir(yourapp) you can see the classes you defined in your Java application. dir(yourapp.Class1) will list all methods, functions, etc. that are within the class.
You probably want to read through the first few pages at least of the Jython Book to familiarize yourself with the new syntax. I find it much simpler than Java's.
For making a Jython web app, I've heard cgi is by far the fastet way to get started with the least overhead:
#!/usr/bin/python
print("Content-Type: text/plain\n\n")
print("Hello, World!\n")
This tutorial seems helpful: http://www.cs.virginia.edu/~lab2q/lesson_1/. Although it is for Python, almost all of it should be applicable to Jython.
And of course, there are many other Python/Jython web service options if cgi doesn't suit you or your project. I've used web2py and really liked it.

Related

Read from stdin with monkeyrunner

How do I read from stdin with monkeyrunner? I tried the following code.
import sys
print("type something")
something = sys.stdin.readline()
print(something)
I also tried the following:
print("type something")
something = raw_input()
print(something)
In both cases the program prints "type something" but it does not respond after I type something. It seems i am making some silly mistake?
This seems to be a bug with Jython 2.5.3, the version that is included with MonkeyRunner. The issue says Mac OS X but I am able to reproduce on Ubuntu.
To fix it, you can download the Jython 2.5.4rc1 standalone Jar from the Jython website (download link) and copy it into the $SDK/tools/lib directory. Note that the Jython 2.7.0 standalone Jar will not work properly. You don't need to rename the new Jar, but you do need to (re)move the old jython-standalone-2.5.3.jar from the directory.
Another option (if you only need user input) is to use the input function in the MonkeyRunner class.

Jython does not resolve python imports from bundled Lib files/folders on Weblogic 10.3.5

I am new to Jython and Python, trying to build a prototype that makes use of Python code to be called from within Java. The code I am developing works in Jetty and in standalone mode (running java -jar from the command line), but not when deployed to weblogic.
How can I make weblogic(10.3.5) server/Jython recognize the Lib folder within jython-standalone-2.5.4-rc1.jar?
My Java code uses the JythonObjectFactory to invoke python modules as outlined in the Jython book:
http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html
The Python modules are using external libraries like csv, logging etc. that are not packaged with jython.jar, hence I am using jython-standalone jar.
The java code includes an interface that would define the class type of the first invoked py module from within java. The interface and the input and output (to python modules) type classes are in a package structure as com.abc.xpackage. and the py modules exist at the root of this package. A controller layer calls the objectfactory and in turn executes the python code thus:
JythonObjectFactory calFactory = new JythonObjectFactory(CalcType.class, "Calculate", "Calculate");
CalcType engine = (CalcType)calFactory.createObject();
output = engine.execute(input);
The entire code is bundled as a jar file which would become part of a web application deployed on weblogic. The code was compiled with maven (with jython dependencies included in the repository) and runs fine on the included Jetty runtime within eclipse.
When deployed on weblogic, however, I get a "ImportError: no module named csv" error.
To analyze what is happening, I tried printing the Jython system state path on weblogic and the standalone environment/Jetty. What I found is,
on Jetty, the system path consists of the following:
C:\.m2\repo\org\python\jython\jython-standalone-2.5.3-rc1.jar\Lib, ____classpath__, ____pyclasspath__
on Weblogic, printing the system path by default shows the following:
____classpath__, ____pyclasspath__
I tried forcing the inclusion of the missing path using the code as follows:
public JythonObjectFactory(PySystemState state, Class interfaceType, String moduleName, String className) {
String pathToAppend = new File(state.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getAbsolutePath()+"\\Lib";
state.path.insert(0, new PyString(pathToAppend));
state.path.append(new PyString(pathToAppend));
System.out.println("Jython sys path: "+state.path);
Please note, I prepended as well as appended the path in different trials. The sys path on weblogic now displays the following:
Jython sys path: ['C:\\wldomain\\wls135\\servers\\cgServer\\tmp\\app-1\\war\\WEB-INF\\lib\\jython-standalone-2.5.4-rc1.jar\\Lib', '__classpath__', '__pyclasspath__/', 'C:\\wldomain\\wls135\\servers\\cgServer\\tmp\\app-1\\war\\WEB-INF\\lib\\jython-standalone-2.5.4-rc1.jar\\Lib']
I am still getting ImportError despite this forcing of sys path. Please help why this works in a local environment, and not on weblogic, and if there is any configuration I am missing. Apologize for the rambling long post, I did not know how to explain the problem better. I will try and include any code/artifacts as needed.
Based on a comment(by Lassi) on the blog post below:
http://www.petervannes.nl/files/e1c3c56d15d25dcfd4adb5397a9ef71e-53.php
The jython issue was resolved after explicitly adding the Lib folder python.path to the weblogic startup script as a JAVA_OPTION.
In my case I added the exploded Lib folder to the domain server lib, but based on my test this works also from within the jython jar. Both the following JAVA_OPTIONS worked:
-Dpython.path=C:\wldomain\wls135\lib\Lib
-Dpython.path=C:\wldomain\wls135\lib\jython-standalone-2.5.4-rc1.jar\Lib
The programmatic way of sys.path.append worked for the local environment(jetty) but did not seem to work for weblogic.

Viability of running noflo.js in another javascript engine (not node.js platform)

We're evaluating noflo to be executed on an embedded linux box using an simple javascript engine, being an interpreter (no JIT). In our case, the Node.js engine (with embedded V8 engine) might be too resource intensive.
The immediate question is the how to run the noflo runtime in there. Checking out the GitHub repository (https://github.com/noflo/noflo) and using grunt, we have generated the noflo for the browser using grunt build:browser.
As simple example to actually try and run the generated browser/noflo.js file, I used the d8 shell (V8 engine shell) for an isolated Javascript engine outside the Node.js universe, and appended the following code to the noflo.js generated file:
var fbpData = "<some FBP language connections>";
var noflo = require('noflo');
noflo.graph.loadFbp(fbpData, function(graph) {
print("Graph loaded");
});
Then,
d8 noflo.js
on the Linux shell, which reports
rtm.js:9559: TypeError: undefined is not a function
noflo.graph.loadFbp(fbpData, function(graph) {
^
TypeError: undefined is not a function
at rtm.js:9559:13
Without knowing further, leads me to believe that the noflo.js is not self-contained with all core noflo runtime functionality.
What necessary steps are missing here, for me to get noflo library running in an isolated JS engine (V8 is just an example - it could be any engine the is ECMA V5 compliant)
All code examples on the noflo project web site are tailored for Node.js...
PS: I tried as an alternative to build a browser-based noflo from http://noflojs.org/download/, however this always returns "server error".
Best regards
Gunther Strube
The NoFlo-Gnome project contains a browser build of the noflo-runtime-base repository (https://github.com/noflo/noflo-runtime-base) which itself embeds NoFlo.
You might need to add some aliases because the browser build doesn't necessarily fit your engine : https://github.com/noflo/noflo-gnome/blob/master/src/noflo.js#L89
noflo-gnome runs NoFlo in GJS, which is based on Spidermonkey and GLib/GObject.
It has some minimal require() compatibility which allows pulling in NoFlo. There is a checked in build of noflo (+ noflo-runtime-base) in ./src/libs but I did not immediately find how this is created.
If you're considering using a browser build to speed up the startup time, you might also want to look at : https://github.com/djdeath/noflo-iot
At some point I tried to run NoFlo on a board with very slow I/O. It turned out that a single file compacted build of NoFlo (including all the needed components) was significantly faster.

Import another python script, using PythonInterpreter

I am trying to execute a python method from eclipse using jython. I managed to run it with following code:
PythonInterpreter.initialize(System.getProperties(),
System.getProperties(), new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("Mypython.py");
interpreter.eval("MyClassName().MyMethodName()")
My problem is when I import another python script, which exists even in the same directory with Mypython.py. For example, when I add:
from food import Pizza
to Mypython.py, it starts to complain that cannot import. ImportError..
I found some questions about importing python libaries like os, but in my case this is not an issue.
I tried to make the folder as a package, add init.py etc but it failed. I saw some people use PySystemState, but I think it is for jython modules not user python scripts. if this is the solution please give me a simple example.
Could you please help me with that problem.
sys.path is your module-import search path. You can import sys and then modify sys.path as required.

Why is sys.stdin.encoding under PyDev / Jython different from standalone Jython console?

I am trying to get python-gnupg to work in Jython under PyDev under Eclipse. There is a problem which seems to be caused by the sys.stdin.encoding, which is tested by python-gnupg at it's initialisation.
I found that the following script
import sys
print sys.stdin.encoding
outptus cp850 when I run it from Python standalone in a console and from Jython standalone in a console. When I run it in Python under PyDev, it outputs Cp1252, and when I run it in Jython under PyDev, I get None.
Looking at python-gnupg, both encodings cp850 and Cp1252 seem to be ok, but if it's None, it raises an exception:
File "C:\Python27\lib\site-packages\gnupg.py", line 487, in __init__
self._collect_output(p, result, stdin=p.stdin)
File "C:\Python27\lib\site-packages\gnupg.py", line 561, in _collect_output
stderr = codecs.getreader(self.encoding)(process.stderr)
File "C:\jython2.5.2\Lib\codecs.py", line 920, in getreader
return lookup(encoding).streamreader
at org.python.core.codecs.normalizestring(codecs.java:101)
at org.python.core.codecs.lookup(codecs.java:75)
at org.python.modules._codecs.lookup(_codecs.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.NullPointerException: java.lang.NullPointerException
Looks like there are differences between PyDev and standalone encodings, as well as no (?) encoding for PyDev/Jython? I'd really like to use the convenient PyDev development environment, but how can I get the sys.stdin.encoding to be something sensible when developing a Jython program?
Update 1
Maybe the problem is related to one of the following Jython bug reports:
http://bugs.jython.org/issue1839
http://bugs.jython.org/issue1807 (see msg6717)
But then again, these seem to be independent from PyDev/Eclipse..?
It's actually a Jython issue:
In PySystemState.java, when setting up the encodings, Jython will skip setting any encoding if it's not a tty console:
private void initEncoding() {
String encoding = registry.getProperty(PYTHON_CONSOLE_ENCODING);
if (encoding == null) {
return;
}
for (PyFile stdStream : new PyFile[] {(PyFile)this.stdin, (PyFile)this.stdout,
(PyFile)this.stderr}) {
if (stdStream.isatty()) {
stdStream.encoding = encoding;
}
}
}
So, a workaround could be creating a java module to set that (as encoding is a public field of PyFile, but does not have a setter from the Jython side -- or you could use java reflection to set it)...
Maybe you could ask the Jython guys why this is made in the first place (I think the encoding could be set even if it was not a tty device, but I'm not sure which implications could it have and why it's done that way).
EDIT to summarise the results of the many comments below, between the developers of Jython and PyDev as well as the author of the original question (Philip Jenvey, Fabio Zadrozny and Christian Gelinek, resp.):
During the progress, a couple of test scripts were discussed and developed:
A Python unit test written by Philip to test different encodings set by minimal custom created sitecusomize.py scripts. Fabio thinks that the reason for it not actually setting the encoding is because sys.stdin.encoding is inherited from the parent process instead configured by the unit test script.
A Java test program written by Christian which sets up custom I/O streams for creating a Python subprocess, which was found by Fabio to be more similar to how PyDev sets up the subprocess.
As this didn't work as expected by Fabio, he remembered that PyDev also uses the PYTHONIOENCODING environment variable.
As was pointed out by Philip, the current (2.5) version of Jython doesn't support the PYTHONIOENCODING environment variable, which is used by PyDev (in combination with sitecusomize.py) to set the encoding:
You can log a bug for it, although PYTHONIOENCODING was only added in CPython 2.6, so Jython probably wouldn't support it until 2.7 (Jython's skipping 2.6).
I believe you should be able to explicitly encoding on the launch properties.
Run configurations -> Python run -> Xxx -> Common tab -> Encoding.
PyDev inherits some settings from global Eclipse settings. On Linux the encoding is sane UTF-8 everywhere, but some cranky operating systems cause their own issues..
Also you can probably detect encoding in your .py main module and have PyDev specific hack to fix problems related to your configuration.