Webmin and Perl inside Python

Today I’ve been talking to Cavassin about Webmin, and wondering if it would be possible to write Python modules for it. I was sure that I wasn’t being original in that idea, and Cavassin also mentioned that he had read something about it somewhere, so I’ve looked in google for possible references.

Of course, we were right. I have found the python-webmin project. OTOH, I’ve looked in the code, and that’s not really what I had mind. This module reimplements the Webmin API in Python. This means that one will have to maintain it forever, and check for updates in every Webmin release, and even then, it won’t be able to do more advanced stuff which requires deeper interaction with the Webmin system.

The correct solution for that would be to somehow wrap the Webmin API in a way that Python calls the real methods, thus requiring little or no maintenance. It’d be even better if this system was generic and not bound to Webmin in any way. While looking for the solution, I found Inline, which did exactly what I had in mind, but reversed (Python inside Perl), and PyInline, which unfortunately doesn’t seem to support Perl yet. Then, I finally found what I was looking for: pyperl.

The pyperl project embeds a Perl interpreter inside a Python module, allowing arbitrary Perl functionality to be used from Python. It’s really amazing to see it working. Have a look at these simple examples:

>>> import perl
>>> perl.eval('sub func { printf("Hello $_[0]!n"); }')
>>> ret = perl.call("func", "world")
Hello world!

>>> func = perl.get_ref("func")
>>> ret = func("world")
Hello world!

>>> perl.require("Digest::MD5")
>>> md5 = perl.callm("new", "Digest::MD5")
>>> md5.add("something").hexdigest()

Isn’t it fantastic? :-)

Another amazing thing is that pyperl also includes support for doing Python inside Perl. Of course, you can do Python inside Perl inside Python:

>>> n = 5
>>> perl.eval('Python::eval("n")*2')

Now, it’s just a matter of finding some time to create a basic wrapper on top of the Webmin Perl API, turning it into something more pythonic and also protecting Webmin modules developed in Python from changes in the pyperl API.


This entry was posted in Perl, Python, Snippet. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *