diff --git a/00_core.ipynb b/00_core.ipynb index 0e0469f..be25784 100644 --- a/00_core.ipynb +++ b/00_core.ipynb @@ -29,16 +29,22 @@ "#| export\n", "from fastcore.utils import *\n", "import pexpect, re, os, shutil\n", - "from pexpect import TIMEOUT\n", "from pathlib import Path\n", - "from getpass import getpass\n", - "from IPython.core.magic import register_cell_magic, no_var_expand\n", - "from IPython.display import display, Javascript\n", + "from IPython.core.magic import no_var_expand\n", "from IPython.paths import get_ipython_dir\n", - "from IPython.core.interactiveshell import InteractiveShell\n", "from IPython.core.magic_arguments import magic_arguments, argument" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d229ee7f", + "metadata": {}, + "outputs": [], + "source": [ + "from pexpect import TIMEOUT" + ] + }, { "cell_type": "code", "execution_count": null, @@ -65,7 +71,7 @@ "outputs": [], "source": [ "env = dict(os.environ, TERM='dumb', PS1='$', PS2='$')\n", - "eshell = os.environ['SHELL']\n", + "eshell = '/bin/bash'\n", "sh = pexpect.spawn(eshell, encoding='utf-8', env=env)" ] }, @@ -377,7 +383,17 @@ " except Exception as e:\n", " self.o = None\n", " raise e from None\n", - " if disp and res: print(res)" + " if disp and res: print(res)\n", + "\n", + " @magic_arguments()\n", + " @argument('-a', '--append', action='store_true', help='Append contents of the cell to an existing file. The file will be created if it does not exist.')\n", + " @argument('filename', help='File to write')\n", + " @no_var_expand\n", + " def writefile(self, line, cell):\n", + " \"Write cell contents to a file in the current shell directory\"\n", + " args = self.writefile.parser.parse_args(line.split())\n", + " fp = Path(self.o('pwd').strip()) / args.filename \n", + " with open(fp, 'a' if args.append else 'w') as f: f.write(cell)" ] }, { @@ -399,17 +415,17 @@ "def create_magic(shell=None):\n", " if not shell: shell = get_ipython()\n", " magic = PshMagic(shell)\n", - " shell.register_magic_function(magic.bash, magic_name='bash', magic_kind='line_cell')" + " shell.register_magic_function(magic.bash, magic_name='bash', magic_kind='line_cell')\n", + " shell.register_magic_function(magic.writefile, magic_name='writefile', magic_kind='cell')\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "858b3925", + "id": "ebb1f44e", "metadata": {}, "outputs": [], "source": [ - "# Only required if you don't load the extension\n", "create_magic()" ] }, @@ -423,7 +439,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "/Users/jhoward/aai-ws/pshnb\n" + "/Users/rensdimmendaal/git/repos/pshnb\n" ] } ], @@ -431,6 +447,18 @@ "%bash pwd" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "e175cbc4", + "metadata": {}, + "outputs": [], + "source": [ + "%%bash\n", + "mkdir -p /tmp/pshnb-demo\n", + "cd /tmp/pshnb-demo" + ] + }, { "cell_type": "code", "execution_count": null, @@ -441,56 +469,105 @@ "name": "stdout", "output_type": "stream", "text": [ - "00_core.ipynb\tLICENSE\t\tpshnb.egg-info\tsettings.ini\r\n", - "CHANGELOG.md\tMANIFEST.in\tpyproject.toml\tsetup.py\r\n", - "index.ipynb\tpshnb\t\tREADME.md\tstyles.css\n" + "/tmp/pshnb-demo\n" ] } ], "source": [ - "%bash ls -h" + "%bash pwd" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "52112f28", + "metadata": {}, + "outputs": [], + "source": [ + "%%bash\n", + "cat > tmp << EOF\n", + "hi\n", + "there\n", + "EOF" + ] + }, + { + "cell_type": "markdown", + "id": "a6464647", + "metadata": {}, + "source": [ + "We can also use the writefile magic to write in the current working directory:" ] }, { "cell_type": "code", "execution_count": null, - "id": "4c0f8e60", + "id": "5185387f", "metadata": {}, "outputs": [], "source": [ - "%bash cd .." + "%%writefile file1.txt\n", + "hello" ] }, { "cell_type": "code", "execution_count": null, - "id": "9b4392f0", + "id": "ae7d956b", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "/Users/jhoward/aai-ws\n" - ] - } - ], + "outputs": [], "source": [ - "%bash pwd" + "%%writefile file2.txt\n", + "good afternoon" ] }, { "cell_type": "code", "execution_count": null, - "id": "52112f28", + "id": "22989860", "metadata": {}, "outputs": [], "source": [ - "%%bash\n", - "cat > tmp << EOF\n", - "hi\n", - "there\n", - "EOF" + "%%writefile file3.txt\n", + "goodbye" + ] + }, + { + "cell_type": "markdown", + "id": "be0ff6d0", + "metadata": {}, + "source": [ + "we can also append to files" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3492464b", + "metadata": {}, + "outputs": [], + "source": [ + "%%writefile -a file3.txt\n", + "and thanks for all the fish" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e8baf39", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "goodbye\r\n", + "and thanks for all the fish\n" + ] + } + ], + "source": [ + "%bash cat file3.txt" ] }, { @@ -503,13 +580,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "hi\r\n", - "there\n" + "file1.txt\tfile2.txt\tfile3.txt\ttmp\n" ] } ], "source": [ - "%bash cat tmp" + "%bash ls" ] }, { @@ -532,9 +608,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "_nbs\r\n", - "_proc\r\n", - "addnew.py\n" + "file1.txt\r\n", + "file2.txt\r\n", + "file3.txt\n" ] } ], @@ -580,8 +656,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "_nbs\r\n", - "_proc\n" + "file1.txt\r\n", + "file2.txt\n" ] } ], @@ -600,7 +676,7 @@ "output_type": "stream", "text": [ "starting\r\n", - "[1] 18411\n" + "[1] 82032\n" ] } ], @@ -615,17 +691,7 @@ "execution_count": null, "id": "58be93c1", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "finished\r\n", - "\r\n", - "[1]+ Done ( sleep 1; echo finished )\n" - ] - } - ], + "outputs": [], "source": [ "%bash" ] @@ -678,7 +744,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "/Users/jhoward/aai-ws\n" + "/tmp/pshnb-demo\n" ] } ], @@ -714,7 +780,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "/Users/jhoward/aai-ws/pshnb\n" + "/Users/rensdimmendaal/git/repos/pshnb\n" ] } ], @@ -732,7 +798,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "/opt/homebrew/bin/bash\n" + "/bin/zsh\n" ] } ], @@ -749,31 +815,22 @@ ] }, { - "cell_type": "code", - "execution_count": null, - "id": "8673aa0e", + "cell_type": "markdown", + "id": "1ac6e642", "metadata": {}, - "outputs": [], "source": [ - "%bash -s" + "NB: requires passwordless sudo to use this:" ] }, { "cell_type": "code", "execution_count": null, - "id": "7125b817", + "id": "8673aa0e", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "root\n" - ] - } - ], + "outputs": [], "source": [ - "%bash whoami" + "# %bash -s\n", + "# %bash whoami" ] }, { @@ -789,26 +846,17 @@ "execution_count": null, "id": "1169a0ab", "metadata": {}, - "outputs": [], - "source": [ - "%bash -S" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a2193f7e", - "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "jhoward\n" + "rensdimmendaal\n" ] } ], "source": [ + "%bash -S\n", "%bash whoami" ] }, @@ -849,6 +897,14 @@ "except TIMEOUT: print(\"timed out\")" ] }, + { + "cell_type": "markdown", + "id": "7ccace01", + "metadata": {}, + "source": [ + "## Configuration" + ] + }, { "cell_type": "code", "execution_count": null, @@ -900,14 +956,6 @@ "from nbdev.doclinks import nbdev_export\n", "nbdev_export()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "451d1ea6", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/pshnb/_modidx.py b/pshnb/_modidx.py index bc77bc9..952b5ed 100644 --- a/pshnb/_modidx.py +++ b/pshnb/_modidx.py @@ -13,6 +13,7 @@ 'pshnb.core.PshMagic.bash': ('core.html#pshmagic.bash', 'pshnb/core.py'), 'pshnb.core.PshMagic.help': ('core.html#pshmagic.help', 'pshnb/core.py'), 'pshnb.core.PshMagic.reset': ('core.html#pshmagic.reset', 'pshnb/core.py'), + 'pshnb.core.PshMagic.writefile': ('core.html#pshmagic.writefile', 'pshnb/core.py'), 'pshnb.core.ShellInterpreter': ('core.html#shellinterpreter', 'pshnb/core.py'), 'pshnb.core.ShellInterpreter.__call__': ('core.html#shellinterpreter.__call__', 'pshnb/core.py'), 'pshnb.core.ShellInterpreter.__init__': ('core.html#shellinterpreter.__init__', 'pshnb/core.py'), diff --git a/pshnb/core.py b/pshnb/core.py index f7141ef..b7c47ed 100644 --- a/pshnb/core.py +++ b/pshnb/core.py @@ -8,13 +8,9 @@ # %% ../00_core.ipynb from fastcore.utils import * import pexpect, re, os, shutil -from pexpect import TIMEOUT from pathlib import Path -from getpass import getpass -from IPython.core.magic import register_cell_magic, no_var_expand -from IPython.display import display, Javascript +from IPython.core.magic import no_var_expand from IPython.paths import get_ipython_dir -from IPython.core.interactiveshell import InteractiveShell from IPython.core.magic_arguments import magic_arguments, argument # %% ../00_core.ipynb @@ -118,11 +114,23 @@ def bash(self, line, cell=None): raise e from None if disp and res: print(res) + @magic_arguments() + @argument('-a', '--append', action='store_true', help='Append contents of the cell to an existing file. The file will be created if it does not exist.') + @argument('filename', help='File to write') + @no_var_expand + def writefile(self, line, cell): + "Write cell contents to a file in the current shell directory" + args = self.writefile.parser.parse_args(line.split()) + fp = Path(self.o('pwd').strip()) / args.filename + with open(fp, 'a' if args.append else 'w') as f: f.write(cell) + # %% ../00_core.ipynb def create_magic(shell=None): if not shell: shell = get_ipython() magic = PshMagic(shell) shell.register_magic_function(magic.bash, magic_name='bash', magic_kind='line_cell') + shell.register_magic_function(magic.writefile, magic_name='writefile', magic_kind='cell') + # %% ../00_core.ipynb def load_ipython_extension(ipython):