Skip to content

Commit 4693623

Browse files
committed
fixes #7
1 parent ec76a15 commit 4693623

3 files changed

Lines changed: 70 additions & 11 deletions

File tree

00_core.ipynb

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@
140140
"name": "stdout",
141141
"output_type": "stream",
142142
"text": [
143-
"00_core.ipynb\r\n",
144-
"CHANGELOG.md\r\n",
145-
"index.ipynb\r\n",
143+
"_docs\r\n",
144+
"_proc\r\n",
145+
"_quarto.yml\r\n",
146146
"\n"
147147
]
148148
}
@@ -236,9 +236,9 @@
236236
"name": "stdout",
237237
"output_type": "stream",
238238
"text": [
239-
"00_core.ipynb\r\n",
240-
"CHANGELOG.md\r\n",
241-
"index.ipynb\n"
239+
"_docs\r\n",
240+
"_proc\r\n",
241+
"_quarto.yml\n"
242242
]
243243
}
244244
],
@@ -271,6 +271,21 @@
271271
"# print(sh('whoami'))"
272272
]
273273
},
274+
{
275+
"cell_type": "code",
276+
"execution_count": null,
277+
"id": "a64bf8ae",
278+
"metadata": {},
279+
"outputs": [],
280+
"source": [
281+
"#| export\n",
282+
"@patch\n",
283+
"def sync_cwd(self:ShellInterpreter):\n",
284+
" \"Sync session's cwd to match pshnb's cwd\"\n",
285+
" _cwd = self._ex('pwd').strip()\n",
286+
" if _cwd != os.getcwd(): os.chdir(_cwd)"
287+
]
288+
},
274289
{
275290
"cell_type": "markdown",
276291
"id": "af3ce49d",
@@ -350,6 +365,7 @@
350365
" @argument('-X', '--no-expand', action='store_true', help='Disable variable expansion')\n",
351366
" @argument('-s', '--sudo', action='store_true', help='Enable sudo')\n",
352367
" @argument('-S', '--no-sudo', action='store_true', help='Disable sudo')\n",
368+
" @argument('-c', '--cwd', action='store_true', help=\"Sync session's cwd to match pshnb's cwd\")\n",
353369
" @argument('-t', '--timeout', type=int, help='Set timeout in seconds')\n",
354370
" @argument('command', nargs='*', help='The command to run')\n",
355371
" @no_var_expand\n",
@@ -359,6 +375,7 @@
359375
" if cell: cell = shell_replace(cell, self.shell)\n",
360376
" if line: line = shell_replace(line, self.shell)\n",
361377
" args = self.bash.parser.parse_args(line.split())\n",
378+
" if not self.o: self.reset()\n",
362379
" if not args.command and not cell:\n",
363380
" if args.expand: return self._xpand(True)\n",
364381
" if args.no_expand: return self._xpand(False)\n",
@@ -368,11 +385,11 @@
368385
" if args.reset: return self.reset(args.reset if isinstance(args.reset, str) else None)\n",
369386
" if args.help: return self.help()\n",
370387
" if args.obj: return self\n",
388+
" if args.cwd: return self.o.sync_cwd()\n",
371389
" if args.command: cell = ' '.join(args.command)\n",
372390
" if not cell and line: cell=line\n",
373391
" disp = True\n",
374392
" if cell.endswith(';'): disp,cell = False,cell[:-1]\n",
375-
" if not self.o: self.reset()\n",
376393
" try: res = self.o(cell) or None\n",
377394
" except Exception as e:\n",
378395
" self.o = None\n",
@@ -441,9 +458,10 @@
441458
"name": "stdout",
442459
"output_type": "stream",
443460
"text": [
444-
"00_core.ipynb\tLICENSE\t\tpshnb.egg-info\tsettings.ini\r\n",
445-
"CHANGELOG.md\tMANIFEST.in\tpyproject.toml\tsetup.py\r\n",
446-
"index.ipynb\tpshnb\t\tREADME.md\tstyles.css\n"
461+
"_docs\t\tCHANGELOG.bak\tLICENSE\t\tpshnb.egg-info\tsetup.py\r\n",
462+
"_proc\t\tCHANGELOG.md\tMANIFEST.in\tpyproject.toml\tsidebar.yml\r\n",
463+
"_quarto.yml\tdist\t\tnbdev.yml\tREADME.md\tstyles.css\r\n",
464+
"00_core.ipynb\tindex.ipynb\tpshnb\t\tsettings.ini\n"
447465
]
448466
}
449467
],
@@ -542,6 +560,37 @@
542560
"%bash ls | head -3"
543561
]
544562
},
563+
{
564+
"cell_type": "code",
565+
"execution_count": null,
566+
"id": "0f71b893",
567+
"metadata": {},
568+
"outputs": [],
569+
"source": [
570+
"%bash -c"
571+
]
572+
},
573+
{
574+
"cell_type": "code",
575+
"execution_count": null,
576+
"id": "ad940b0c",
577+
"metadata": {},
578+
"outputs": [
579+
{
580+
"data": {
581+
"text/plain": [
582+
"'/Users/jhoward/aai-ws'"
583+
]
584+
},
585+
"execution_count": null,
586+
"metadata": {},
587+
"output_type": "execute_result"
588+
}
589+
],
590+
"source": [
591+
"os.getcwd()"
592+
]
593+
},
545594
{
546595
"cell_type": "code",
547596
"execution_count": null,

pshnb/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'pshnb.core.ShellInterpreter.__call__': ('core.html#shellinterpreter.__call__', 'pshnb/core.py'),
1818
'pshnb.core.ShellInterpreter.__init__': ('core.html#shellinterpreter.__init__', 'pshnb/core.py'),
1919
'pshnb.core.ShellInterpreter._ex': ('core.html#shellinterpreter._ex', 'pshnb/core.py'),
20+
'pshnb.core.ShellInterpreter.sync_cwd': ('core.html#shellinterpreter.sync_cwd', 'pshnb/core.py'),
2021
'pshnb.core.ShellInterpreter.wait_echo': ('core.html#shellinterpreter.wait_echo', 'pshnb/core.py'),
2122
'pshnb.core.create_ipython_config': ('core.html#create_ipython_config', 'pshnb/core.py'),
2223
'pshnb.core.create_magic': ('core.html#create_magic', 'pshnb/core.py'),

pshnb/core.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ def __call__(self, cmd, timeout=None):
5959
output = self._ex(cmd.rstrip(), timeout=timeout)
6060
return output.replace(cmd + '\r\n', '', 1).rstrip()
6161

62+
# %% ../00_core.ipynb
63+
@patch
64+
def sync_cwd(self:ShellInterpreter):
65+
"Sync session's cwd to match pshnb's cwd"
66+
_cwd = self._ex('pwd').strip()
67+
if _cwd != os.getcwd(): os.chdir(_cwd)
68+
6269
# %% ../00_core.ipynb
6370
def shell_replace(s, shell=None):
6471
"Replace `@{var}` refs in `s` with their variable values, if they exist"
@@ -89,6 +96,7 @@ def _timeout(self, timeout=2):
8996
@argument('-X', '--no-expand', action='store_true', help='Disable variable expansion')
9097
@argument('-s', '--sudo', action='store_true', help='Enable sudo')
9198
@argument('-S', '--no-sudo', action='store_true', help='Disable sudo')
99+
@argument('-c', '--cwd', action='store_true', help="Sync session's cwd to match pshnb's cwd")
92100
@argument('-t', '--timeout', type=int, help='Set timeout in seconds')
93101
@argument('command', nargs='*', help='The command to run')
94102
@no_var_expand
@@ -98,6 +106,7 @@ def bash(self, line, cell=None):
98106
if cell: cell = shell_replace(cell, self.shell)
99107
if line: line = shell_replace(line, self.shell)
100108
args = self.bash.parser.parse_args(line.split())
109+
if not self.o: self.reset()
101110
if not args.command and not cell:
102111
if args.expand: return self._xpand(True)
103112
if args.no_expand: return self._xpand(False)
@@ -107,11 +116,11 @@ def bash(self, line, cell=None):
107116
if args.reset: return self.reset(args.reset if isinstance(args.reset, str) else None)
108117
if args.help: return self.help()
109118
if args.obj: return self
119+
if args.cwd: return self.o.sync_cwd()
110120
if args.command: cell = ' '.join(args.command)
111121
if not cell and line: cell=line
112122
disp = True
113123
if cell.endswith(';'): disp,cell = False,cell[:-1]
114-
if not self.o: self.reset()
115124
try: res = self.o(cell) or None
116125
except Exception as e:
117126
self.o = None

0 commit comments

Comments
 (0)