OILS / builtin / misc_osh.py View on Github | oilshell.org

115 lines, 60 significant
1#!/usr/bin/env python2
2# Copyright 2016 Andy Chu. All rights reserved.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8"""Misc builtins."""
9from __future__ import print_function
10
11from _devbuild.gen.runtime_asdl import cmd_value
12from _devbuild.gen.syntax_asdl import loc_t
13from core import pyos
14from core import pyutil
15from core import util
16from core import vm
17from frontend import flag_util
18from mycpp import mylib
19from mycpp.mylib import log
20
21from typing import Dict, TYPE_CHECKING
22if TYPE_CHECKING:
23 from core.pyutil import _ResourceLoader
24 from core import ui
25
26_ = log
27
28
29class Times(vm._Builtin):
30
31 def __init__(self):
32 # type: () -> None
33 vm._Builtin.__init__(self)
34
35 def Run(self, cmd_val):
36 # type: (cmd_value.Argv) -> int
37 pyos.PrintTimes()
38 return 0
39
40
41# Needs a different _ResourceLoader to translate
42class Help(vm._Builtin):
43
44 def __init__(self, lang, loader, help_data, errfmt):
45 # type: (str, _ResourceLoader, Dict[str, str], ui.ErrorFormatter) -> None
46 self.lang = lang
47 self.loader = loader
48 self.help_data = help_data
49 self.errfmt = errfmt
50 self.version_str = pyutil.GetVersion(self.loader)
51 self.f = mylib.Stdout()
52
53 def _ShowTopic(self, topic_id, blame_loc):
54 # type: (str, loc_t) -> int
55
56 prefix = 'https://www.oilshell.org/release'
57
58 # For local preview
59 if 0:
60 prefix = 'file:///home/andy/git/oilshell/oil/_release'
61 self.version_str = 'VERSION'
62
63 chapter_name = self.help_data.get(topic_id)
64
65 # If we have a chapter name, it's not embedded in the binary. So just
66 # print the URL.
67 if chapter_name is not None:
68 util.PrintTopicHeader(topic_id, self.f)
69 print(' %s/%s/doc/ref/chap-%s.html#%s' %
70 (prefix, self.version_str, chapter_name, topic_id))
71 return 0
72
73 found = util.PrintEmbeddedHelp(self.loader, topic_id, self.f)
74 if not found:
75 # Notes:
76 # 1. bash suggests:
77 # man -k zzz
78 # info zzz
79 # help help
80 # We should do something smarter.
81
82 # 2. This also happens on 'build/dev.sh minimal', which isn't quite
83 # accurate. We don't have an exact list of help topics!
84
85 # 3. This is mostly an interactive command. Is it obnoxious to
86 # quote the line of code?
87 self.errfmt.Print_('no help topics match %r' % topic_id, blame_loc)
88 return 1
89
90 return 0
91
92 def Run(self, cmd_val):
93 # type: (cmd_value.Argv) -> int
94
95 attrs, arg_r = flag_util.ParseCmdVal('help', cmd_val)
96 #arg = arg_types.help(attrs.attrs)
97
98 topic_id, blame_loc = arg_r.Peek2()
99 if topic_id is None:
100 found = self._ShowTopic('help', blame_loc) == 0
101 assert found
102
103 # e.g. ysh-chapters
104 found = self._ShowTopic('%s-chapters' % self.lang, blame_loc) == 0
105 assert found
106
107 print('All docs: https://www.oilshell.org/release/%s/doc/' %
108 self.version_str)
109 print('')
110
111 return 0
112 else:
113 arg_r.Next()
114
115 return self._ShowTopic(topic_id, blame_loc)