-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell.py
More file actions
394 lines (347 loc) · 14.3 KB
/
shell.py
File metadata and controls
394 lines (347 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Nougaro : a python-interpreted high-level programming language
# Copyright (C) 2021-2026 Jean Dubois (https://github.com/jd-develop) <jd-dev@laposte.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Works with python 3.11 and 3.12
# IMPORTS
# nougaro modules imports
import src.nougaro as nougaro
from src.misc import print_in_red
from src.runtime.values.basevalues.value import Value
from src.runtime.values.basevalues.basevalues import List
from src.errors.errors import Error
from src.noug_version import VERSION, VERSION_ID, DATA_VERSION, LIB_VERSION
import src.conffiles
# built in python imports
import sys
import os
import platform
import pathlib
from datetime import datetime
if platform.system().lower() in ["linux", "darwin"] or \
"bsd" in platform.system().lower():
try:
import readline # browse command history
except ImportError:
readline = None
else:
readline = None
def check_arguments(args: list[str], noug_dir: str, version: str) -> \
tuple[str, str | None, bool, bool]:
"""Returns a file to exec, the line to exec, and dont_verbose"""
if len(args) == 0:
return "<stdin>", None, False, False
line_to_exec = None
dont_verbose = False
interactive = False
if args[0] in ["-v", "-V", "--version"]:
print(version)
sys.exit()
if args[0] in ["-h", "-H", "--help"]:
with open(f"{noug_dir}/src/cli_help.txt", "r", encoding="UTF-8") \
as help_file:
help_text = help_file.readlines()
for line in help_text[1:]:
print(line, end="")
sys.exit()
if args[0] in ["-c", "-d", "--command", "--cd", "--command-dont-verbose"]:
path = "<commandline>"
if args[0] in ["-d", "--cd", "--command-dont-verbose"]:
dont_verbose = True
try:
line_to_exec = args[1]
del args[1]
except IndexError:
print_in_red(f"[nougaro] expected argument with {args[0]}.")
sys.exit(1)
# note that bash, zsh and fiSH automatically delete quotes.
# TODO: test in windows cmd and powershell
assert isinstance(line_to_exec, str), \
"please report this bug on GitHub: https://github.com/jd-develop/" \
"nougaro/issues"
# del args[0] # this line is commented because when you execute a file, say using `nougaro file.noug`,
# # __args__(0) is set to `"file.noug"`, so when using `nougaro -c`, `-c` should be the default
# # argument in __args__(0)
else:
if args[0] in ["-i", "--interactive"]:
interactive = True
del args[0]
if not os.path.exists(args[0]):
# we check if the file exist, if not we quit with an error message
print_in_red(f"[nougaro] file '{args[0]}' does not exist.")
sys.exit(-1) # DO NOT USE exit() OR quit() PYTHON BUILTINS !!!
# (it breaks with Nuitka)
elif args[0] in ["<stdin>", "<stdout>", "<commandline>"]:
# these names can not be files : <stdin> is the shell input and
# <stdout> is his output
print_in_red(
f"[nougaro] file '{args[0]}' can not be used by Nougaro because this name is used internally.\n"
f"[nougaro] This is not an unexpected error, you do not need to open an issue on GitHub.\n"
f"[nougaro] Note that the Nougaro shell will open."
)
path = "<stdin>" # this opens the shell
else: # valid file :)
path = args[0]
return path, line_to_exec, dont_verbose, interactive
def execute_file(path: str, debug_on: bool, noug_dir: str, version: str,
args: list[str], interactive: bool):
work_dir = os.path.dirname(os.path.realpath(path))
endswith_slash = work_dir.endswith("/") or work_dir.endswith("\\")
if endswith_slash:
work_dir += "/"
if debug_on:
print(f"Nougaro working directory is {work_dir} ({type(work_dir)})")
with open(path, "r", encoding="UTF-8") as file:
file_content = str(file.read())
if file_content == "": # no need to run this empty file
error = None
else: # the file isn't empty, let's run it !
try:
_, error, _ = nougaro.run(path, file_content, noug_dir, version,
args=args, work_dir=work_dir)
except KeyboardInterrupt: # if CTRL+C, just exit the Nougaro shell
print_in_red("\nKeyboardInterrupt")
error = None
if not interactive:
sys.exit()
except EOFError:
print_in_red("\nEOF")
error = None
if not interactive:
sys.exit()
if error is not None: # there is an error, so we print it before exiting
print_in_red(error.as_string())
if not interactive:
sys.exit(1)
def print_result_and_error(
result: Value | None, error: Error | None, dont_verbose: bool,
exit_on_cd: bool = False, should_print_stuff: bool = True
):
if error is not None: # there is an error, we print it in red
print_in_red(error.as_string())
return
if result is None:
return
if exit_on_cd and dont_verbose:
return
if not isinstance(result, List):
print("WARNING: Looks like something went wrong. Don't panic, and just report this bug at:\n"
"https://jd-develop.github.io/nougaro/bugreport.html.\n"
"Error details: result from src.nougaro.run in shell is not a List.")
print(f"The actual result is {result}, of type {type(result)}, error is {error}.")
return
if not should_print_stuff:
return
if len(result.elements) == 1:
# there is one single result, let's print it without the "[]".
if result.elements[0].should_print: # the value should be printed
print(result.elements[0])
else:
# there are multiple results (multi-line statements (e.g.
# `print(a);var a+=1`))
# This code determines what the list contains
# * if the list contains only NoneValues that shouldn't be printed, we
# don't print it
# * in any other case, we do.
should_print = False
for e in result.elements:
if e.should_print:
should_print = True
break
if should_print: # if we should print, we print
print(result)
def print_greet_text(
debug_on: bool,
noug_dir: str,
work_dir: str,
print_context: bool,
print_time: bool
):
"""Prints the greeter text when interactive shell"""
if debug_on:
print(f"Welcome to Nougaro {VERSION} (id {VERSION_ID}) on {platform.system()}!")
else:
print(f"Welcome to Nougaro {VERSION} on {platform.system()}!")
print(f"Contribute: https://github.com/jd-develop/nougaro/")
print(f"Changelog: see {noug_dir}/CHANGELOG.md")
print()
print("This program comes with ABSOLUTELY NO WARRANTY; for details type")
print("`__disclaimer_of_warranty__'.")
print("This program is under the GNU General Public License, meaning that")
print("this is free software, and you are welcome to redistribute it")
print("under certain conditions; type `__gpl__()' for details (or")
print("`__gpl__(1)' to stay in terminal).")
print("You should also have received a copy of the GPL along with this")
print("program. If not, see <https://www.gnu.org/licences>.")
print()
print("Found a bug? Feel free to report it at")
print("https://jd-develop.github.io/nougaro/bugreport.html")
now = datetime.now()
if now.month == 12 and 24 <= now.day <= 26:
print("\nMerry Christmas!")
elif now.month == now.day == 1:
print(f"\nHappy new year {now.year}!")
if debug_on:
print()
print(f"Current working directory is {work_dir} (type: {type(work_dir)})")
print(f"Current config files directory is {src.conffiles.CONFIG_DIRECTORY} (type: {type(src.conffiles.CONFIG_DIRECTORY)})")
print(f"Current data version is {DATA_VERSION} (type: {type(DATA_VERSION)})")
print(f"Current lib version is {LIB_VERSION} (type: {type(LIB_VERSION)})")
print(f"Python version is {sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]} "
f"({list(sys.version_info)})")
print("DEBUG mode is ENABLED")
if print_context:
if not debug_on:
print()
print("PRINT CONTEXT debug option is ENABLED")
if print_time:
if not (debug_on or print_context):
print()
print("PRINT TIME debug option is ENABLED")
print() # blank line
def run_shell(
should_print_stuff: bool,
interactive: bool,
debug_on: bool,
noug_dir: str,
work_dir: str,
print_context: bool,
print_time: bool,
args: list[str],
dont_verbose: bool
):
"""Runs the shell"""
if should_print_stuff and not interactive:
print_greet_text(debug_on, noug_dir, work_dir, print_context, print_time)
previous_metas = None
while True: # the shell loop
try: # we ask for an input to be interpreted
if should_print_stuff:
text = input("nougaro> ")
else:
text = input()
except KeyboardInterrupt: # if CTRL+C, exit the shell
print_in_red("\nKeyboardInterrupt")
break # breaks the `while True` loop to the end of the file
except EOFError:
if should_print_stuff:
print_in_red("\nEOF")
break # breaks the `while True` loop to the end of the file
if str(text) == "": # nothing was entered: we don't do anything
result, error = None, None
continue
try:
result, error, previous_metas = nougaro.run(
'<stdin>', text, noug_dir, VERSION, args=args,
work_dir=work_dir, lexer_metas=previous_metas
)
except KeyboardInterrupt:
# if CTRL+C, just stop to run the line and ask for another input
print_in_red("\nKeyboardInterrupt")
continue # continue the `while True` loop
except EOFError:
print_in_red("\nEOF")
return # breaks the `while True` loop and stops the function
print_result_and_error(
result, error, dont_verbose, should_print_stuff=should_print_stuff
)
def main():
noug_dir = os.path.abspath(pathlib.Path(__file__).parent.absolute())
src.conffiles.create_config_files()
debug = src.conffiles.access_data("debug")
if debug is None:
debug = 0
debug_on = bool(int(debug))
print_context = src.conffiles.access_data("print_context")
if print_context is None:
print_context = 0
print_context = bool(int(print_context))
print_time = src.conffiles.access_data("print_time")
if print_time is None:
print_time = 0
print_time = bool(int(print_time))
HISTORY_FILE = src.conffiles.ROOT_CONFIG_DIRECTORY + ".noughistory"
if readline is not None:
try:
import atexit
if not os.path.exists(HISTORY_FILE):
if debug_on:
print(f"[history] creating {HISTORY_FILE}")
with open(HISTORY_FILE, "w+", encoding="UTF-8") as histf:
histf.write("")
readline.read_history_file(HISTORY_FILE)
atexit.register(readline.write_history_file, HISTORY_FILE)
except Exception as e:
if debug_on:
print(f"[history] Error: {e.__class__.__name__}: {e}")
args = sys.argv.copy()
# print(args)
# print(f"Arguments count: {len(sys.argv)}")
# for i, arg in enumerate(sys.argv):
# print(f"Argument {i:>6}: {arg}")
# Uncomment the last 3 lines to understand the following line.
# Tested on Windows and GNU/Linux. Tested after compiling with Nuitka on
# Windows and GNU/Linux.
del args[0]
path, line_to_exec, dont_verbose, interactive = check_arguments(
args, noug_dir, VERSION
)
has_to_run_a_file = path not in ["<stdin>", "<commandline>"]
if has_to_run_a_file:
execute_file(path, debug_on, noug_dir, VERSION, args, interactive)
if not interactive:
return
path = "<stdin>"
work_dir = os.getcwd()
endswith_slash = work_dir.endswith("/") or work_dir.endswith("\\")
if not endswith_slash:
work_dir += "/"
# We print stuff if this is an interactive shell.
# HOWEVER, if we are in a pipe, like `echo "$" | nougaro`, we don’t want our
# prompt to be printed
should_print_stuff = sys.stdin.isatty()
if path == "<stdin>": # we open the shell
run_shell(
should_print_stuff,
interactive,
debug_on,
noug_dir,
work_dir,
print_context,
print_time,
args,
dont_verbose
)
elif path == "<commandline>":
if line_to_exec == "":
sys.exit()
try: # we try to run it
result, error, _ = nougaro.run(
'<commandline>', line_to_exec, noug_dir, VERSION, args=args,
work_dir=work_dir
)
except KeyboardInterrupt:
# if CTRL+C, just stop to run the line and ask for another input
print_in_red("\nKeyboardInterrupt")
sys.exit()
except EOFError:
print_in_red("\nEOF")
sys.exit()
print_result_and_error(result, error, dont_verbose, True)
if error is not None:
sys.exit(1)
if __name__ == '__main__':
main()