Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup iPython as a simplified command shell for the average user

Summary: Is it possible to setup iPython to look like a basic dos/bash command line terminal to the average user (cd, ls, mkdir, rm... etc) , and then expose a few extra proprietary commands?

In Depth: I work for a video game company where the users have never experienced a "good" CLI in a production pipeline. So in response i've been working for some time writing a CLI using cmd.py in python, recreating commonly used dos commands and then augmenting them with my own custom commands to do various things, ex: jump quickly across pre-defined production folders, simplified perforce management commands, find/import files with recursive ant-glob patterns into our authoring tool... etc etc...

In the middle of the process i stumbled upon ipython. I'm not very familiar with it but i already feel it's what i should be using, as it does a lot of what i want for free. It handle's ansi colors perfectly. All shell commands are available via "!", can use "|,>,<" and all built in shell functions for free, the benefits go on and on...

The one caveat is that my targeted audience is easily intimidated by CLI's... and so when they type "more textfile.txt" as an input, they expect the default dos behavior of printing the contents of the file, and not:

In [42]: more textfile.txt
  File "<ipython-input-42-e442b4ca857b>", line 1
    more file.txt
            ^
SyntaxError: invalid syntax

Of course, "!more textfile.txt" would do what they expect, but i don't want them to have to type "!" for essentially 95% of the commands they would use.

So i'm looking for a way to launch ipython in a completely simplified manner where in my ideal (fantasy perhaps) world:

  • All the shell commands (cd, ls/dir, mkdir, more, rm/del, etc...) would be accessible at the forefront without any "!"
  • All python commands (import, def, class, for, print, while, etc...) would be inaccessible, except from a predefined few proprietary commands to do various pipeline operations, maybe leave basic arithmetic commands accessible?

And as a bonus: a way to alternate between default ipython and the very restrained and simplified command shell replacement version i'm dreaming about... ex: typing "normal" would restore all of ipython's default behavior... and typing "cli" would go back to the simplified version.

Final note: What i'm looking for is likely a fool's errand and i should probably stick with launching python.exe in a command shell with my own clumsy CLI script and call it a day... But I figured that if what i'd like iPython to do here is remotely possible, Stackoverflow is where i'd get the answer quickly :)

like image 854
Fnord Avatar asked Oct 21 '22 18:10

Fnord


1 Answers

IPython use to have something close to what you are asking, it was removed in 0.11 version for lack of use/testing and interest. I suppose it is feaseable to bring it back, but I'm not sure of the amount of work it will involve knowing the the architecture have change quite a bit.

I'll underline some of the reason you might want/not want to go this way, as well as some alternative and technical point.

  1. you will never get a full real shell experience with IPython you will always come across things that wont work (getpass in 2 process architecture for example, or command with dash in them)
  2. preventing the full usage of Python will be really hard.
  3. defining your own commands with alias or magics is super simple. (already pointed out in
    comment but wil not always works

The way IPython itself is working is looking wether something exist is valid python, if so execute. Otherwise suppose it is a magic, if it makes sens, transfom it to python syntax and execute. Then fallback to aliases. I guess the logic can be reversed, but determining if smth is valid shell might be difficult.

You can also hook your own input trasformer SageMath is dooing it in some places to allow custom syntax.

I'm not sure the "alterning" part will be easy, but IPython is really flexible, input transformer is coroutine base so should be live modifiable, still it will probably require modif in IPython itself I think to expose the right API.

You might be interested in looking at writing a full IPython "kernel" (don't be afraid it's simpler that it looks). That is to say just the part that get user input and send back output, the rest comes for free. If you look at IJulia and run ipython console --profile=julia you are dropped in a julia shell. and you can do the same for ruby and Haskell ...etc. Other advantages are that you are not required to write the kernel in python, and that it will work of the shelf with the qtconsole, notebook emacs, vim... and other IPython frontends.

I as see that the question has been upvoted 3 times, I'll add that IPython is open source, and if people come, add the functionality back and help us maintaining it we can probably add it back in the core.

like image 101
Matt Avatar answered Oct 23 '22 09:10

Matt