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

126 lines, 68 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 display 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 print('')
72 return 0
73
74 # Note: this is a heuristic. Typos will print bad URLs, but let's keep
75 # it simple.
76 lower = topic_id.lower()
77 if lower.startswith('oils-err'):
78 print('')
79 print(' %s/%s/doc/error-catalog.html#%s' %
80 (prefix, self.version_str, lower))
81 print('')
82 return 0
83
84 found = util.PrintEmbeddedHelp(self.loader, topic_id, self.f)
85 if not found:
86 # Notes:
87 # 1. bash suggests:
88 # man -k zzz
89 # info zzz
90 # help help
91 # We should do something smarter.
92
93 # 2. This also happens on 'build/dev.sh minimal', which isn't quite
94 # accurate. We don't have an exact list of help topics!
95
96 # 3. This is mostly an interactive command. Is it obnoxious to
97 # quote the line of code?
98 self.errfmt.Print_('no help topics match %r' % topic_id, blame_loc)
99 return 1
100
101 return 0
102
103 def Run(self, cmd_val):
104 # type: (cmd_value.Argv) -> int
105
106 attrs, arg_r = flag_util.ParseCmdVal('help', cmd_val)
107 #arg = arg_types.help(attrs.attrs)
108
109 topic_id, blame_loc = arg_r.Peek2()
110 if topic_id is None:
111 found = self._ShowTopic('help', blame_loc) == 0
112 assert found
113
114 # e.g. ysh-chapters
115 found = self._ShowTopic('%s-chapters' % self.lang, blame_loc) == 0
116 assert found
117
118 print('All docs: https://www.oilshell.org/release/%s/doc/' %
119 self.version_str)
120 print('')
121
122 return 0
123 else:
124 arg_r.Next()
125
126 return self._ShowTopic(topic_id, blame_loc)