python custom command interpreter?

Fredrik Lundh fredrik at pythonware.com
Wed Aug 20 21:52:38 CEST 2008


joey boggs wrote:

> In the end I'd like to be able to run a custom interpreter and just feed 
> it one command and a directory. The end result in the kickstart 
> something like this:
> 
> %post --interpreter #!/usr/bin/myinterpreter
> DROP /tmp/directory
> DROP /tmp/directory2
> 
> How would I setup the interpreter to take the DROP command? I've been 
> reading and searching all day but I haven't found anything close to what 
> I'm doing. I realize that using custom commands in this case is overkill 
> but in the end is used to make the users life easier. If anyone can 
> point me to some documentation I would be more than grateful.

I'm not sure I understand what you're doing, but maybe you'll find the 
following somewhat useful:

 >>> import code
 >>> class MyInterpreter(code.InteractiveConsole):
...     def raw_input(self, prompt=None):
...         # override input function
...         while 1:
...             text = raw_input(prompt)
...             if text.startswith("DROP"):
...                 # deal with custom command
...                 print "CUSTOM DROP COMMAND", text
...             else:
...                 return text
...
 >>> i = MyInterpreter()
 >>> i.interact()
Python 2.5.2 [...]
Type "help", "copyright", "credits" or "license" for more information.
(MyInterpreter)
 >>> print "hello"
hello
 >>> DROP something
CUSTOM DROP COMMAND DROP something
 >>> x = 10
 >>> y = 20
 >>> x + y
30
 >>> DROP something else
CUSTOM DROP COMMAND DROP something else
 >>>

</F>




More information about the Python-list mailing list