pshnb IPython magic

Provides psh persistent bash magics in Jupyter and IPython
__file__ = './00_core.ipynb'
env = dict(os.environ, TERM='dumb', PS1='$', PS2='$')
eshell = os.environ['SHELL']
sh = pexpect.spawn(eshell, encoding='utf-8', env=env)
env = dict(os.environ, TERM='dumb', PS1='', PS2='')
eshell = os.environ['SHELL']
sh = pexpect.spawn(eshell, encoding='utf-8', env=env)
sh.sendline('stty -echo')
sh.readline()
echo = os.urandom(8).hex()
echo_re = re.compile(fr'^{echo}\s*$', flags=re.MULTILINE)
sh.sendline(f'export PS1=""')
sh.sendline('set +o vi +o emacs')
sh.sendline('echo '+echo)
sh.expect(echo_re, timeout=2)
0
sh.sendline('ls | head -3')
sh.sendline('echo '+echo)
sh.expect(echo_re, timeout=2)
print(sh.before)
00_core.ipynb
CHANGELOG.bak
CHANGELOG.md

source

ShellInterpreter

 ShellInterpreter (debug=False, timeout=2, shell_path=None, sudo=False,
                   dumb=False)

Initialize self. See help(type(self)) for accurate signature.

sh = ShellInterpreter()
print(sh('ls | head -3'))
00_core.ipynb
CHANGELOG.bak
CHANGELOG.md
sh = ShellInterpreter(sudo=True)

sh('cd')
print(sh('pwd'))
sh('cd ..')
print(sh('pwd'))
print(sh('whoami'))
/var/root
/var
root

source

shell_replace

 shell_replace (s, shell=None)

Replace @{var} refs in s with their variable values, if they exist

b = 1

a = '''asdf
$@{b} @{aa}
fdsa'''

print(shell_replace(a))
asdf
$1 @{aa}
fdsa

source

PshMagic

 PshMagic (shell, sudo=False, timeout=2, expand=True, o=None)

Initialize self. See help(type(self)) for accurate signature.


source

create_magic

 create_magic (shell=None)
# Only required if you don't load the extension
create_magic()
%psh pwd
/Users/jhoward/Documents/GitHub/pshnb
%psh cd ..
%psh pwd
/Users/jhoward/Documents/GitHub
%%psh
cat > tmp << EOF
hi
there
EOF
%psh cat tmp
hi
there
%psh rm tmp
%psh ls | head -3
ContextKit
FastHTML-Gallery
aimagic
n = 2
%psh echo @{n}
2
%psh ls | head -@{n}
ContextKit
FastHTML-Gallery
%%psh
echo starting
(sleep 1; echo finished) &
starting
[1] 99418
%psh
finished

[1]+  Done                    ( sleep 1; echo finished )
%psh -h
::

  %psh [-h] [-r] [-o] [-x] [-X] [-s] [-S] [-t TIMEOUT] [command ...]

Run line or cell in persistent shell

positional arguments:
  command               The command to run

options:
  -h, --help            Show this help
  -r, --reset           Reset the shell interpreter
  -o, --obj             Return this magic object
  -x, --expand          Enable variable expansion
  -X, --no-expand       Disable variable expansion
  -s, --sudo            Enable sudo
  -S, --no-sudo         Disable sudo
  -t TIMEOUT, --timeout TIMEOUT
                        Set timeout in seconds
%psh pwd
/Users/jhoward/Documents/GitHub
%psh -r
%psh pwd
/Users/jhoward/Documents/GitHub/pshnb
%psh -s
%psh whoami
root
%psh -S
%psh whoami
jhoward
%psh -t 1
try: get_ipython().run_line_magic('psh', 'sleep 2')
except TIMEOUT: print("timed out")
timed out

source

load_ipython_extension

 load_ipython_extension (ipython)

Required function for creating magic


source

create_ipython_config

 create_ipython_config ()

Called by pshnb_install to install magic