Skip to content

Commit 9ce895d

Browse files
committed
Administrivia:
* add 1st pytest - command completion * add Makefile "clean" target * blacken some files * Don't do default pymodules
1 parent 9889d20 commit 9ce895d

File tree

7 files changed

+107
-110
lines changed

7 files changed

+107
-110
lines changed

Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ check-rst:
3636

3737
#: Remove derived files
3838
clean:
39-
rm mathics/*/*.so; \
40-
for dir in mathics/doc ; do \
41-
($(MAKE) -C "$$dir" clean); \
42-
done;
39+
@find . -name "*.pyc" -type f -delete
4340

4441
#: Remove ChangeLog
4542
rmChangeLog:

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ To install from git shources so that you run from the git source tree:
3232
$ make develop
3333

3434

35-
Why not IPython via Jupyter
36-
---------------------------
35+
Why not IPython via Jupyter?
36+
----------------------------
3737

3838
There will always be a need for simple terminal-like
3939
interaction. Although there is IPython support via Jupyter all of this

mathicsscript/__main__.py

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
from pygments import highlight
1616
from pygments.lexers import MathematicaLexer
17+
1718
mma_lexer = MathematicaLexer()
1819

1920
from mathicsscript.version import __version__
2021

22+
2123
def format_output(obj, expr, format=None):
2224
if format is None:
2325
format = obj.format
@@ -28,7 +30,7 @@ def format_output(obj, expr, format=None):
2830
from mathics.core.expression import Expression, BoxError
2931

3032
expr_type = expr.get_head_name()
31-
if expr_type == "System`MathMLForm":
33+
if expr_type == "System`MathMLForm":
3234
format = "xml"
3335
leaves = expr.get_leaves()
3436
if len(leaves) == 1:
@@ -39,21 +41,17 @@ def format_output(obj, expr, format=None):
3941
if len(leaves) == 1:
4042
expr = leaves[0]
4143
elif expr_type == "System`Graphics":
42-
result = Expression(
43-
'StandardForm', expr).format(obj, 'System`MathMLForm')
44+
result = Expression("StandardForm", expr).format(obj, "System`MathMLForm")
4445
ml_str = result.leaves[0].leaves[0]
4546
# FIXME: not quite right. Need to parse out strings
4647
display_svg(str(ml_str))
4748

48-
49-
if format == 'text':
50-
result = expr.format(obj, 'System`OutputForm')
51-
elif format == 'xml':
52-
result = Expression(
53-
'StandardForm', expr).format(obj, 'System`MathMLForm')
54-
elif format == 'tex':
55-
result = Expression('StandardForm', expr).format(
56-
obj, 'System`TeXForm')
49+
if format == "text":
50+
result = expr.format(obj, "System`OutputForm")
51+
elif format == "xml":
52+
result = Expression("StandardForm", expr).format(obj, "System`MathMLForm")
53+
elif format == "tex":
54+
result = Expression("StandardForm", expr).format(obj, "System`TeXForm")
5755
else:
5856
raise ValueError
5957

@@ -63,10 +61,12 @@ def format_output(obj, expr, format=None):
6361
boxes = None
6462
if not hasattr(obj, "seen_box_error"):
6563
obj.seen_box_error = True
66-
obj.message('General', 'notboxes',
67-
Expression('FullForm', result).evaluate(obj))
64+
obj.message(
65+
"General", "notboxes", Expression("FullForm", result).evaluate(obj)
66+
)
6867
return boxes
6968

69+
7070
Evaluation.format_output = format_output
7171

7272

@@ -96,14 +96,14 @@ def out(self, out):
9696
"--persist",
9797
default=False,
9898
required=False,
99-
is_flag = True,
99+
is_flag=True,
100100
help="go to interactive shell after evaluating FILE or -e",
101101
)
102102
@click.option(
103103
"--quiet",
104104
"-q",
105105
default=False,
106-
is_flag = True,
106+
is_flag=True,
107107
required=False,
108108
help="don't print message at startup",
109109
)
@@ -148,25 +148,16 @@ def out(self, out):
148148
"--style",
149149
metavar="PYGMENTS-STYLE",
150150
type=str,
151-
help=(
152-
"Set pygments style. Use 'None' if you do not want any pygments styling."
153-
),
151+
help=("Set pygments style. Use 'None' if you do not want any pygments styling."),
154152
required=False,
155153
)
156154
@click.option(
157155
"--pygments-tokens/--no-pygments-tokens",
158156
default=False,
159-
help=(
160-
"Show pygments tokenization of output."
161-
),
162-
required=False,
163-
)
164-
@click.argument(
165-
"file",
166-
nargs=1,
167-
type=click.Path(readable=True),
157+
help=("Show pygments tokenization of output."),
168158
required=False,
169159
)
160+
@click.argument("file", nargs=1, type=click.Path(readable=True), required=False)
170161
def main(
171162
full_form,
172163
persist,
@@ -192,20 +183,11 @@ def main(
192183
if pyextensions:
193184
for ext in pyextensions:
194185
extension_modules.append(ext)
195-
else:
196-
from mathics.settings import default_pymathics_modules
197186

198-
extension_modules = default_pymathics_modules
199-
200-
definitions = Definitions(add_builtin=True, extension_modules=extension_modules)
187+
definitions = Definitions(add_builtin=True)
201188
definitions.set_line_no(0)
202189

203-
shell = TerminalShell(
204-
definitions,
205-
style,
206-
readline,
207-
completion,
208-
)
190+
shell = TerminalShell(definitions, style, readline, completion)
209191

210192
if initfile:
211193
feeder = FileLineFeeder(initfile)
@@ -215,7 +197,7 @@ def main(
215197
shell.definitions,
216198
output=TerminalOutput(shell),
217199
catch_interrupt=False,
218-
format = "text",
200+
format="text",
219201
)
220202
query = evaluation.parse_feeder(feeder)
221203
if query is None:
@@ -229,8 +211,9 @@ def main(
229211
if execute:
230212
for expr in execute:
231213
print(shell.get_in_prompt() + expr)
232-
evaluation = Evaluation(shell.definitions,
233-
output=TerminalOutput(shell), format = "text")
214+
evaluation = Evaluation(
215+
shell.definitions, output=TerminalOutput(shell), format="text"
216+
)
234217
result = evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT)
235218
shell.print_result(result, debug_pygments=pygments_tokens)
236219

@@ -245,7 +228,7 @@ def main(
245228
shell.definitions,
246229
output=TerminalOutput(shell),
247230
catch_interrupt=False,
248-
format = "text",
231+
format="text",
249232
)
250233
query = evaluation.parse_feeder(feeder)
251234
if query is None:
@@ -272,24 +255,23 @@ def main(
272255

273256
TeXForm = Symbol("System`TeXForm")
274257

275-
276258
while True:
277259
try:
278260
if shell.using_readline:
279261
import readline as GNU_readline
262+
280263
last_pos = GNU_readline.get_current_history_length()
281264
evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell))
282265
query, source_code = evaluation.parse_feeder_returning_code(shell)
283266
if shell.using_readline:
284267
current_pos = GNU_readline.get_current_history_length()
285-
for pos in range(last_pos, current_pos-1):
268+
for pos in range(last_pos, current_pos - 1):
286269
GNU_readline.remove_history_item(pos)
287270
GNU_readline.add_history(source_code.rstrip())
288271

289272
if query is None:
290273
continue
291274

292-
293275
if hasattr(query, "head") and query.head == TeXForm:
294276
output_style = "//TeXForm"
295277
else:

mathicsscript/termshell.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from pygments import highlight
1515
from mathicsscript.mmalexer import MathematicaLexer
16+
1617
mma_lexer = MathematicaLexer()
1718

1819
from pygments.formatters.terminal import TERMINAL_COLORS
@@ -47,7 +48,6 @@
4748
set_completer_delims,
4849
set_history_length,
4950
write_history_file,
50-
# parse_and_bind,
5151
)
5252

5353
try:
@@ -57,15 +57,13 @@
5757

5858
HISTFILE = osp.expanduser("~/.mathicsscript_hist")
5959

60-
RL_COMPLETER_DELIMS_WITH_BRACE = (
61-
" \t\n_~!@#%^&*()-=+{]}|;:'\",<>/?"
62-
)
63-
RL_COMPLETER_DELIMS = (
64-
" \t\n_~!@#%^&*()-=+[{]}\\|;:'\",<>/?"
65-
)
60+
RL_COMPLETER_DELIMS_WITH_BRACE = " \t\n_~!@#%^&*()-=+{]}|;:'\",<>/?"
61+
RL_COMPLETER_DELIMS = " \t\n_~!@#%^&*()-=+[{]}\\|;:'\",<>/?"
6662

6763

6864
from mathics.core.parser import LineFeeder, FileLineFeeder
65+
66+
6967
class TerminalShell(LineFeeder):
7068
def __init__(
7169
self, definitions, style: str, want_readline: bool, want_completion: bool
@@ -74,7 +72,7 @@ def __init__(
7472
self.input_encoding = locale.getpreferredencoding()
7573
self.lineno = 0
7674
self.terminal_formatter = None
77-
self.history_length = definitions.get_config_value('$HistoryLength', HISTSIZE)
75+
self.history_length = definitions.get_config_value("$HistoryLength", HISTSIZE)
7876

7977
# Try importing readline to enable arrow keys support etc.
8078
self.using_readline = False
@@ -90,7 +88,6 @@ def __init__(
9088

9189
self.named_character_names = set(named_characters.keys())
9290

93-
9491
# Make _ a delimiter, but not $ or `
9592
# set_completer_delims(RL_COMPLETER_DELIMS)
9693
set_completer_delims(RL_COMPLETER_DELIMS_WITH_BRACE)
@@ -118,7 +115,7 @@ def __init__(
118115
colorama_init()
119116
if style == "None":
120117
self.terminal_formatter = None
121-
self.incolors = self.outcolors = ["", "", "", ""]
118+
self.incolors = self.outcolors = ["", "", "", ""]
122119
else:
123120
# self.incolors = ["\033[34m", "\033[1m", "\033[22m", "\033[39m"]
124121
self.incolors = ["\033[32m", "\033[1m", "\033[22m", "\033[39m"]
@@ -132,7 +129,9 @@ def __init__(
132129
try:
133130
self.terminal_formatter = Terminal256Formatter(style=style)
134131
except ClassNotFound:
135-
print("Pygments style name '%s' not found; No pygments style set" % style)
132+
print(
133+
"Pygments style name '%s' not found; No pygments style set" % style
134+
)
136135

137136
self.definitions = definitions
138137

@@ -148,15 +147,17 @@ def get_in_prompt(self):
148147

149148
def get_out_prompt(self, output_style=""):
150149
line_number = self.get_last_line_number()
151-
return "{2}Out[{3}{0}{4}]{1}= {5}".format(line_number, output_style, *self.outcolors)
150+
return "{2}Out[{3}{0}{4}]{1}= {5}".format(
151+
line_number, output_style, *self.outcolors
152+
)
152153

153154
def to_output(self, text):
154155
line_number = self.get_last_line_number()
155156
newline = "\n" + " " * len("Out[{0}]= ".format(line_number))
156157
return newline.join(text.splitlines())
157158

158159
def out_callback(self, out):
159-
print(self.to_output(str(out)))\
160+
print(self.to_output(str(out)))
160161

161162
def read_line(self, prompt):
162163
if self.using_readline:
@@ -168,6 +169,7 @@ def print_result(self, result, output_style="", debug_pygments=False):
168169
out_str = str(result.result)
169170
if self.terminal_formatter: # pygmentize
170171
from pygments import lex
172+
171173
if debug_pygments:
172174
print(list(lex(out_str, mma_lexer)))
173175
out_str = highlight(out_str, mma_lexer, self.terminal_formatter)
@@ -185,7 +187,9 @@ def complete_symbol_name(self, text, state):
185187
try:
186188
match = re.match(r"^(.*\\\[)([A-Z][a-z]*)$", text)
187189
if match:
188-
return self._complete_named_characters(match.group(1), match.group(2), state)
190+
return self._complete_named_characters(
191+
match.group(1), match.group(2), state
192+
)
189193
return self._complete_symbol_name(text, state)
190194

191195
except Exception as e:
@@ -196,10 +200,13 @@ def complete_symbol_name(self, text, state):
196200
raise
197201

198202
def _complete_named_characters(self, prefix, text, state):
199-
"""prefix is the text after \[. Return a list of named character names.
200-
"""
203+
"""prefix is the text after \[. Return a list of named character names."""
201204
if state == 0:
202-
self.completion_candidates = [prefix + name + "]" for name in self.named_character_names if name.startswith(text)]
205+
self.completion_candidates = [
206+
prefix + name + "]"
207+
for name in self.named_character_names
208+
if name.startswith(text)
209+
]
203210
# self.completion_candidates = self.get_completion_symbol_candidates(prefix, text)
204211
try:
205212
return self.completion_candidates[state]
@@ -221,8 +228,8 @@ def get_completion_candidates(self, text):
221228

222229
brace_pos = text.rfind("[")
223230
if brace_pos >= 0:
224-
suffix = text[brace_pos+1:]
225-
prefix = text[:brace_pos+1]
231+
suffix = text[brace_pos + 1 :]
232+
prefix = text[: brace_pos + 1]
226233
else:
227234
prefix = ""
228235
suffix = text

setup.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,22 @@ def read(*rnames):
3838
print("mathicsscript does not support Python %d.%d" % sys.version_info[:2])
3939
sys.exit(-1)
4040

41-
# stores VERSION in the current namespace
41+
42+
def get_srcdir():
43+
filename = osp.normcase(osp.dirname(osp.abspath(__file__)))
44+
return osp.realpath(filename)
45+
46+
47+
def read(*rnames):
48+
return open(osp.join(get_srcdir(), *rnames)).read()
49+
50+
51+
# stores __version__ in the current namespace
4252
exec(compile(read("mathicsscript/version.py"), "mathicsscript/version.py", "exec"))
4353

54+
long_description = read("README.rst") + "\n"
55+
exec(read("mathicsscript/version.py"))
56+
4457
is_PyPy = platform.python_implementation() == "PyPy"
4558

4659
setup(
@@ -51,8 +64,16 @@ def read(*rnames):
5164
packages=find_packages(),
5265
include_package_data=True,
5366
package_data={"": ["inputrc"]},
54-
install_requires=["mathics >= 1.0", "click", "colorama", "pygments", "term-background >= 1.0.1"],
55-
entry_points={"console_scripts": ["mathicsscript = mathicsscript.__main__:main",],},
67+
install_requires=[
68+
"mathics >= 1.0",
69+
"click",
70+
"colorama",
71+
"pygments",
72+
"term-background >= 1.0.1",
73+
],
74+
entry_points={"console_scripts": ["mathicsscript = mathicsscript.__main__:main"]},
75+
long_description=long_description,
76+
long_description_content_type="text/x-rst",
5677
# don't pack Mathics in egg because of media files, etc.
5778
zip_safe=False,
5879
# metadata for upload to PyPI

0 commit comments

Comments
 (0)