Skip to content

Commit 2a4ef55

Browse files
authored
Merge pull request #8 from Mathics3/add-!
Start ! to allow running of OS commands
2 parents fbfcb39 + 1dfda1c commit 2a4ef55

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

.github/workflows/ubuntu.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
run: |
2323
sudo apt-get update -qq && sudo apt-get install -qq liblapack-dev llvm-dev
2424
python -m pip install --upgrade pip
25+
python -m pip install -e git://github.com/mathics/Mathics#egg=Mathics3
2526
- name: Install mathicsscript
2627
run: |
2728
make

mathicsscript/__main__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import sys
66
import os
77
import re
8+
import subprocess
89
from pathlib import Path
910

1011
from mathicsscript.termshell import (
12+
ShellEscapeException,
1113
TerminalShell,
1214
wl_replace_dict_esc,
1315
wl_replace_pattern,
@@ -333,6 +335,7 @@ def main(
333335

334336
evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell))
335337
query, source_code = evaluation.parse_feeder_returning_code(shell)
338+
336339
if shell.using_readline and hasattr(GNU_readline, "remove_history_item"):
337340
current_pos = GNU_readline.get_current_history_length()
338341
for pos in range(last_pos, current_pos - 1):
@@ -355,6 +358,22 @@ def main(
355358
)
356359
if result is not None:
357360
shell.print_result(result, output_style)
361+
362+
except ShellEscapeException as e:
363+
source_code = e.line
364+
if len(source_code) and source_code[1] == "!":
365+
try:
366+
print(open(source_code[2:], "r").read())
367+
except:
368+
print(str(sys.exc_info()[1]))
369+
else:
370+
subprocess.run(source_code[1:], shell=True)
371+
372+
# Should we test exit code for adding to history?
373+
GNU_readline.add_history(source_code.rstrip())
374+
## FIXME add this... when in Mathics core updated
375+
## shell.defintions.increment_line(1)
376+
358377
except (KeyboardInterrupt):
359378
print("\nKeyboardInterrupt")
360379
except EOFError:

mathicsscript/termshell.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ def is_pygments_style(style):
115115
return True
116116

117117

118+
class ShellEscapeException(Exception):
119+
def __init__(self, line):
120+
self.line = line
121+
122+
118123
class TerminalShell(LineFeeder):
119124
def __init__(
120125
self,
@@ -268,8 +273,12 @@ def out_callback(self, out):
268273

269274
def read_line(self, prompt):
270275
if self.using_readline:
271-
return self.rl_read_line(prompt)
272-
return replace_unicode_to_wl(input(prompt))
276+
line = self.rl_read_line(prompt)
277+
else:
278+
line = input(prompt)
279+
if line.startswith("!") and self.lineno == 0:
280+
raise ShellEscapeException(line)
281+
return replace_unicode_to_wl(line)
273282

274283
def print_result(self, result, output_style=""):
275284
if result is None:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def read(*rnames):
6767
"": ["inputrc", "inputrc-no-unicode", "inputrc-unicode", "settings/settings.m"]
6868
},
6969
install_requires=[
70-
"Mathics3 >= 1.1.1",
70+
"Mathics3 >= 2.0.0dev",
7171
"click",
7272
"colorama",
7373
"columnize",

0 commit comments

Comments
 (0)