views:

247

answers:

8

hi, i am creating ( researching possibility of ) a highly customizable python client and would like to allow users to actually edit the code in another language to customize the running of program. ( analogous to browser which itself coded in c/c++ and run another language html/js ). so my question is , is there any programming language implemented in pure python which i can see as a reference ( or use directly ? ) -- i need simple language ( simple statements and ifs can do )

edit: sorry if i did not make myself clear but what i want is "a language to customize the running of program" , even though pypi seems a great option, what i am looking for is more simple which i can study and extend myself if need arise. my google searches pointing towards xml based langagues. ( BMEL , XForms etc ).

+2  A: 

Ren'Py is a modification to Python syntax built on top of Python itself, using the language tools in the stdlib.

Ignacio Vazquez-Abrams
this seems cool option. txs.
iamgopal
Ren'Py isn't a modification so much as it's a separate language which allows embedded Python in the source, and happens to be written in Python as well. Crawling through the parser.py and ast.py files in the Ren'Py source shows just how it works.
Chris Charabaruk
A: 

Possibly Common Lisp (or any other Lisp) will be the best choice for that task. Because Lisp make it possible to easily extend host language with powerful macroses and construct DSL (domain specific language).

I can't imagine asking users to learn common lisp to configure my application. It would be like giving them Emacs, only a bit less painful.
Carson Myers
Who said that your users will need to learn Common Lisp? I mean that Common Lisp can be as host language on top of which can be built custom DSL. Using that DSL customers can customize your app.
+4  A: 

The question isn't completely clear on scope, but I have a hunch that PyPy, embedding other full languages, and similar solutions might be overkill. It sounds like iamgopal may really be interested in something more like Interpreter Pattern or Little Language.

If the language you want to support is really small (see the Interpreter Pattern link), then hand-coding this yourself in Python won't be too hard. You can write a simple parser (Google around; here's one example), then walk the AST and evaluate user expressions.

However, if you expect this to be used for a long time or by many people, it may be worth throwing a real language at the problem. (I'd recommend Python itself if your users are already familiar with basic Python syntax).

Will Robinson
+1  A: 

Why not Python itself? With some care you can use eval to run user code.

One of the good thing about interpreted scripting languages is that you don't need another extra scripting language!

fortran
A: 

If all you need is simple if statements and expressions, I'm sure it wouldn't be an awful task to parse each line. Something like

if some flag
    activate some feature
    deactivate some feature
elif some other flag
    activate some feature
    activate some feature
else
    logout

Just write a class which, while parsing takes the first word, checks if it's "if, elif, else," etc, and if so, check a flag and set a flag saying you either are or are not executing until the next conditional. If it's not a conditional, call a function based on the first keyword that would modify the program state in some way.

The class could store some local execution state (are we in an if statement? If so are we executing this branch?) and have another class containing some global application state (flags that are checkable by if statements, etc).

This is probably the wrong thing to do in your situation (it's very prone to bugs, it's dangerous if you don't treat the data in the scripts correctly), but it's at least a start if you do decide to interpret your own mini-language.

Seriously though, if you try this, be very, very, srs careful. Don't give the scripts any functionality that they don't definitely need, because you are almost certainly opening security holes by doing something like this.

Don't say I didn't warn you.

Carson Myers
+3  A: 

For your user's sake, don't use an XML based language - XML is an awful basis for a programming language and your users will hate you for it.

Here is a suggestion. Use a strict subset of Python for your language. Use the compiler module to convert their code into an abstract syntax tree and walk the tree to to validate that the code conforms to your subset before converting the AST into python bytecode.

N.B. I just checked the docs and see that the compiler package is deprecated in 2.6 and removed in Python 3.x. Does anyone know why that is?

Dave Kirby
My guess is that its features are duplicated in parser and the other language packages. http://docs.python.org/library/language.html
Chris Charabaruk
+1  A: 

PLY (Python Lex-Yacc) is something of your interest.

TheMachineCharmer
+1  A: 

Numerous template languages such as Cheetah, Django templates, Genshi, Mako, Mighty might serve as an example.

J.F. Sebastian