W3C home > Mailing lists > Public > public-cwm-talk@w3.org > April to June 2013

New CWM extension

From: Atila <atilaromero@gmail.com>
Date: Fri, 17 May 2013 18:07:20 -0300
Message-ID: <51969C08.80902@gmail.com>
To: public-cwm-talk@w3.org
Hi, I have built a little cwm extension that creates built-ins on the fly.

When a URI like <python://mymodule#myfunction> is found in a rule,
it imports the module and uses the function as a built-in.


     :c :input ("All these worlds are yours except europa") .

     {?x :input (?y).
         ?z1b <python://#unicode.upper> (?y) .
         ?z2 <python://#unicode.rsplit> ( ?z1b " " 2 ) .
         ?x :output ?z2.

cwm_py.py --think --data teste.n3

      @prefix : <#> .

     :c     :input  (
         "All these worlds are yours except europa" );
          :output  (
         "EUROPA" ) .

The equivalent python code would be y.upper.rsplit(" ", 2)

Another example, a little more useful, would be to put a custom function 
in a file, like teste.py
and use that function as a built-in:


     import subprocess
     def callExt(arglist):


     @prefix teste: <python://teste#>.
     :d :input ().
     {?x :input ().
         ?z1 teste:callExt (("ls" "-l" "../")) .
         ?x :output ?z1.

cwm_py.py --think --data teste.n3

      @prefix : <#> .

     :d     :input ();
          :output """total 16
drwxrwxr-x  2 atila.alr atila.alr  4096 Mai  9 08:52 CVS
drwxrwxr-x 26 atila.alr atila.alr 12288 Mai 17 17:54 swap
""" .

To "install" the extension, it should suffice to put cwm_py.py in the 
cwm.py folder.
Then, to use it, call cwm_py.py like you would call cwm.
Internally, all the extension does is to monkeypatch an internal 
function (so it understand "python://" URI)
and then pass the ball to cwm.

Sure, the idea of call any function is not very 
production-security-friendly, but it may come handy in a controlled 

The code is at https://github.com/atilaromero/cwm_py/blob/master/cwm_py.py

If anyone like it, let me know.
Received on Friday, 17 May 2013 21:07:49 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 20:01:06 UTC