Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
setup builds a lot smaller now
Browse files Browse the repository at this point in the history
  • Loading branch information
rjdbcm committed Nov 8, 2021
1 parent 0461864 commit c4c7b46
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 248 deletions.
1 change: 1 addition & 0 deletions Aspidites/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def __init__(self, compile_args: CompilerArgs):
self.setup(compile_args)

self.file_stack.finalize()
os.remove(self.file_c)

def bytecode_compile(self) -> None:
fname_pyc = str(self.app_name) + ".pyc"
Expand Down
238 changes: 124 additions & 114 deletions Aspidites/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import os
import curses
import re
import ast
import warnings
from contextlib import suppress
import time
Expand Down Expand Up @@ -80,6 +81,124 @@ def __exit__(self, exception, value, tb):
single_arg_help = re.compile(r'(?:help[\(])(\w+)(?:[\)])')


class Help:
def __init__(self, names, stdout, ruler):
"""
>>> h = Help(dir(__class__), sys.stdout)
>>> h('help')
"""
self.names = names
self.stdout = stdout
self.ruler = ruler
self.cmds_doc = []
self.cmds_undoc = []

def __call__(self, arg):
if arg:
try:
func = getattr(self, 'help_' + arg)
except AttributeError:
try:
doc = getattr(self, 'do_' + arg).__doc__
if doc:
self.stdout.write("%s\n" % str(doc))
return
except AttributeError:
pass
self.stdout.write("%s\n" % str(self.nohelp % (arg,)))
return
func()
else:
self.help = {}
for name in names:
if name[:5] == 'help_':
self.help[name[5:]] = 1
names.sort()
# There can be duplicates if routines overridden
self.handle_names()

def handle_names(self):
prevname = ''
for name in self.names:
if name[:3] == 'do_':
if name == prevname:
continue
prevname = name
cmd = name[3:]
if cmd in self.help:
self.cmds_doc.append(cmd)
del self.help[cmd]
elif getattr(self, name).__doc__:
self.cmds_doc.append(cmd)
else:
self.cmds_undoc.append(cmd)

def print_topics(self, header, cmds, cmdlen, maxcol):
if cmds:
self.stdout.write("%s\n" % str(header))
if self.ruler:
self.stdout.write("╭%s╮\n" % str(self.ruler * len(header)))
self.columnize(cmds, maxcol - 1)
self.stdout.write("\n")

def columnize(self, list, displaywidth=80):
"""Display a list of strings as a compact set of columns.
Each column is only as wide as necessary.
Columns are separated by two spaces (one was not legible enough).
"""
if not list:
self.stdout.write("<empty>\n")
return

nonstrings = [i for i in range(len(list))
if not isinstance(list[i], str)]
if nonstrings:
raise TypeError("list[i] not a string for i in %s"
% ", ".join(map(str, nonstrings)))
size = len(list)
if size == 1:
self.stdout.write(' %s\n' % str(list[0]))
return
# Try every row count from 1 upwards
for nrows in range(1, len(list)):
ncols = (size + nrows - 1) // nrows
colwidths = []
totwidth = -2
for col in range(ncols):
colwidth = 0
for row in range(nrows):
i = row + nrows * col
if i >= size:
break
x = list[i]
colwidth = max(colwidth, len(x))
colwidths.append(colwidth)
totwidth += colwidth + 2
if totwidth > displaywidth:
break
if totwidth <= displaywidth:
break
else:
nrows = len(list)
ncols = 1
colwidths = [0]
for row in range(nrows):
texts = []
for col in range(ncols):
i = row + nrows * col
if i >= size:
x = ""
else:
x = list[i]
texts.append(x)
while texts and not texts[-1]:
del texts[-1]
for col in range(len(texts)):
texts[col] = texts[col].ljust(colwidths[col])
self.stdout.write(" %s\n" % str(" ".join(texts)))


class ReadEvalParse: # pragma: no cover
intro = "Welcome to the Woma Interactive Shell. Use the 'help()' or '?' command to see a list of commands.\nThis is experimental and mainly aims to help developers to sandbox " \
"Woma without compilation."
Expand Down Expand Up @@ -138,9 +257,7 @@ def eval_exec(self, x: Union[List, AnyStr]):
# noinspection PyBroadException
warnings.resetwarnings()
try:
out = eval(compile(x, filename='<inline code>', mode='eval'),
self.__locals__,
self.__locals__)
out = eval(compile(x, filename='<inline code>', mode='eval'), self.__locals__, self.__locals__)
except Exception:
out = exec(compile(x, filename='<inline code>', mode='exec'), self.__locals__, self.__locals__)
self.stdout.write('\n')
Expand Down Expand Up @@ -205,6 +322,10 @@ def loop(self) -> None:
except KeyboardInterrupt as e:
self.do_exit()

def do_help(self, arg=None):
h = Help(dir(self.__class__), self.stdout, self.ruler)
h(arg)

def do_exit(self, arg=None):
"""Exit the woma interactive interpreter."""
self.stdout.write("\nExiting...")
Expand All @@ -230,117 +351,6 @@ def do_flush(self):
"""forcibly flush stdout"""
self.stdout.flush()

def do_help(self, arg=None):
"""List available commands with "help" or detailed help with "help cmd"."""
if arg:
# XXX check arg syntax
try:
func = getattr(self, 'help_' + arg)
except AttributeError:
try:
doc = getattr(self, 'do_' + arg).__doc__
if doc:
self.stdout.write("%s\n" % str(doc))
return
except AttributeError:
pass
self.stdout.write("%s\n" % str(self.nohelp % (arg,)))
return
func()
else:
names = self.get_names()
cmds_doc = []
cmds_undoc = []
help = {}
for name in names:
if name[:5] == 'help_':
help[name[5:]] = 1
names.sort()
# There can be duplicates if routines overridden
prevname = ''
for name in names:
if name[:3] == 'do_':
if name == prevname:
continue
prevname = name
cmd = name[3:]
if cmd in help:
cmds_doc.append(cmd)
del help[cmd]
elif getattr(self, name).__doc__:
cmds_doc.append(cmd)
else:
cmds_undoc.append(cmd)
self.stdout.write("%s\n" % str(self.doc_leader))
self.print_topics(self.doc_header, cmds_doc, 15, 80)
self.print_topics(self.misc_header, list(help.keys()), 15, 80)
self.print_topics(self.undoc_header, cmds_undoc, 15, 80)

def print_topics(self, header, cmds, cmdlen, maxcol):
if cmds:
self.stdout.write("%s\n" % str(header))
if self.ruler:
self.stdout.write("╭%s╮\n" % str(self.ruler * len(header)))
self.columnize(cmds, maxcol - 1)
self.stdout.write("\n")

def columnize(self, list, displaywidth=80):
"""Display a list of strings as a compact set of columns.
Each column is only as wide as necessary.
Columns are separated by two spaces (one was not legible enough).
"""
if not list:
self.stdout.write("<empty>\n")
return

nonstrings = [i for i in range(len(list))
if not isinstance(list[i], str)]
if nonstrings:
raise TypeError("list[i] not a string for i in %s"
% ", ".join(map(str, nonstrings)))
size = len(list)
if size == 1:
self.stdout.write(' %s\n' % str(list[0]))
return
# Try every row count from 1 upwards
for nrows in range(1, len(list)):
ncols = (size + nrows - 1) // nrows
colwidths = []
totwidth = -2
for col in range(ncols):
colwidth = 0
for row in range(nrows):
i = row + nrows * col
if i >= size:
break
x = list[i]
colwidth = max(colwidth, len(x))
colwidths.append(colwidth)
totwidth += colwidth + 2
if totwidth > displaywidth:
break
if totwidth <= displaywidth:
break
else:
nrows = len(list)
ncols = 1
colwidths = [0]
for row in range(nrows):
texts = []
for col in range(ncols):
i = row + nrows * col
if i >= size:
x = ""
else:
x = list[i]
texts.append(x)
while texts and not texts[-1]:
del texts[-1]
for col in range(len(texts)):
texts[col] = texts[col].ljust(colwidths[col])
self.stdout.write(" %s\n" % str(" ".join(texts)))


if __name__ == "__main__":
rep = ReadEvalParse()
Expand Down

0 comments on commit c4c7b46

Please sign in to comment.