Skip to content

Commit

Permalink
[refactor] Get rid of _devbuild/gen/help_index.py
Browse files Browse the repository at this point in the history
It's simpler to translate with a text file and the 'loader'.
  • Loading branch information
Andy Chu committed Jun 15, 2020
1 parent 9c4786b commit e0c81eb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 27 deletions.
7 changes: 1 addition & 6 deletions bin/oil.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ def _tlog(msg):
_ = id_kind
from _devbuild.gen.option_asdl import option_i
from _devbuild.gen.syntax_asdl import source
# Hack because we don't want libcmark.so dependency for build/dev.sh minimal
try:
from _devbuild.gen import help_index # generated file
except ImportError:
help_index = None

from core import alloc
from core import error
Expand Down Expand Up @@ -260,7 +255,7 @@ def AppBundleMain(argv):

if first_arg in ('-h', '--help'):
errfmt = None # not needed here
help_builtin = builtin_misc.Help(loader, help_index, errfmt)
help_builtin = builtin_misc.Help(loader, errfmt)
help_builtin.Run(main.MakeBuiltinArgv(['bundle-usage']))
sys.exit(0)

Expand Down
3 changes: 1 addition & 2 deletions build/doc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ readonly CODE_DIR=_devbuild/gen

# NOTE: Should eventually take .html instead of .md
help-index-cards() {
local py_out=$CODE_DIR/help_index.py
_make-help cards-for-index $TEXT_DIR $py_out < $HTML_DIR/doc/help-index.html
_make-help cards-for-index $TEXT_DIR < $HTML_DIR/doc/help-index.html
}

help-cards() {
Expand Down
7 changes: 1 addition & 6 deletions core/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
from _devbuild.gen.option_asdl import option_i, builtin_i
from _devbuild.gen.runtime_asdl import cmd_value
from _devbuild.gen.syntax_asdl import source
# Hack because we don't want libcmark.so dependency for build/dev.sh minimal
try:
from _devbuild.gen import help_index # generated file
except ImportError:
help_index = None

from asdl import runtime

Expand Down Expand Up @@ -216,7 +211,7 @@ def ShellMain(lang, argv0, arg_r, environ, login_shell, loader, line_input):
arena = alloc.Arena()
errfmt = ui.ErrorFormatter(arena)

help_builtin = builtin_misc.Help(loader, help_index, errfmt)
help_builtin = builtin_misc.Help(loader, errfmt)
if flag.help:
help_builtin.Run(MakeBuiltinArgv(['%s-usage' % lang]))
return 0
Expand Down
9 changes: 5 additions & 4 deletions doctools/make_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ def main(argv):
# Extract sections from help-index.html and make them into "cards".

out_dir = argv[2]
py_out = argv[3]

groups = []

Expand All @@ -348,10 +347,12 @@ def main(argv):

groups.append(group_id)

with open(py_out, 'w') as f:
f.write('GROUPS = %s\n' % pprint.pformat(groups))
groups_out = os.path.join(out_dir, 'groups.txt')
with open(groups_out, 'w') as f:
for g in groups:
print(g, file=f)
log('')
log('Wrote %s', py_out)
log('Wrote %s', groups_out)

elif action == 'cards':
# Split help into cards.
Expand Down
16 changes: 12 additions & 4 deletions osh/builtin_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,20 @@ def Run(self, cmd_val):
# Needs a different _ResourceLoader to translate
class Help(vm._Builtin):

def __init__(self, loader, help_index, errfmt):
# type: (_ResourceLoader, Any, ErrorFormatter) -> None
def __init__(self, loader, errfmt):
# type: (_ResourceLoader, ErrorFormatter) -> None
self.loader = loader
self.help_index = help_index
self.errfmt = errfmt

def _Groups(self):
# type: () -> List[str]
# TODO: cache this?
f = self.loader.open('_devbuild/help/groups.txt')
lines = f.readlines()
f.close()
groups = [line.rstrip() for line in lines]
return groups

def Run(self, cmd_val):
# type: (cmd_value__Argv) -> int

Expand All @@ -543,7 +551,7 @@ def Run(self, cmd_val):
groups = cmd_val.argv[2:]
if len(groups) == 0:
# Print the whole index
groups = self.help_index.GROUPS
groups = self._Groups()

for group in groups:
try:
Expand Down
6 changes: 1 addition & 5 deletions osh/builtin_misc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

import unittest

try:
from _devbuild.gen import help_index
except ImportError:
help_index = None
from core import pyutil
from frontend import flag_def # side effect: flags are defined!
_ = flag_def
Expand Down Expand Up @@ -46,7 +42,7 @@ def testPrintHelp(self):
# generated? Because I don't want to deal with a C toolchain for it.

loader = pyutil.GetResourceLoader()
builtin_misc.Help([], help_index, loader)
builtin_misc.Help([], loader)

if __name__ == '__main__':
unittest.main()

0 comments on commit e0c81eb

Please sign in to comment.