-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathfiles.py
More file actions
494 lines (433 loc) · 16 KB
/
files.py
File metadata and controls
494 lines (433 loc) · 16 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# -*- coding: utf-8 -*-
"""
File related evaluation functions.
"""
import os
from typing import Callable, Literal, Optional, Sequence
from mathics_scanner.errors import (
IncompleteSyntaxError,
InvalidSyntaxError,
SyntaxError,
)
from mathics_scanner.location import ContainerKind
import mathics
import mathics.core.parser
import mathics.core.streams as streams
from mathics.core.atoms import Integer, String
from mathics.core.builtin import MessageException
from mathics.core.convert.expression import to_expression, to_mathics_list
from mathics.core.convert.python import from_python
from mathics.core.evaluation import Evaluation
from mathics.core.expression import BaseElement, Expression
from mathics.core.parser import MathicsFileLineFeeder, MathicsMultiLineFeeder
from mathics.core.parser.util import (
dump_exprs_to_pcl_file,
parse_from_pcl_file,
parse_incrementally_by_line,
)
from mathics.core.streams import path_search, stream_manager
from mathics.core.symbols import Symbol, SymbolNull, SymbolTrue
from mathics.core.systemsymbols import (
SymbolEndOfFile,
SymbolExpression,
SymbolFailed,
SymbolHold,
SymbolHoldExpression,
SymbolPath,
SymbolReal,
SymbolWord,
)
from mathics.core.util import canonic_filename
from mathics.eval.files_io.read import (
READ_TYPES,
Mathics3Open,
close_stream,
read_from_stream,
read_get_separators,
)
# Python representation of $InputFileName. On Windows platforms, we
# canonicalize this to its POSIX equivalent name.
# FIXME: Remove this as a module-level variable and instead
# define it in a session definitions object.
# With this, multiple sessions will have separate
# $InputFilename
INPUT_VAR: str = ""
DEFAULT_TRACE_FN: Literal[None] = None
def print_line_number_and_text(line_number: int, text: str):
"""Prints a line number an text on that line with it.
This is used as the default trace function in Get[]
"""
if line_number == 0:
print(f"Reading file: {text}")
else:
print("%5d: %s" % (line_number, text.rstrip()))
GET_PRINT_FN: Callable = print_line_number_and_text
def get_file_time(file) -> float:
"""Return the last time that a file was accessed"""
try:
return os.stat(file).st_mtime
except OSError:
return 0
def set_input_var(input_string: str):
"""
Allow INPUT_VAR to get set, e.g. from main program.
"""
global INPUT_VAR
INPUT_VAR = canonic_filename(input_string)
def eval_Close(obj, evaluation: Evaluation):
"""
Closes a stream or socket `obj` which can be an 'InputStream' or
'OutputStream' object, or `SocketObject`. If there is only one
stream with a particular name, `obj` can be the string name, the
file path, of `obj`.
"""
n = name = None
if obj.has_form(("InputStream", "OutputStream"), 2):
[name, n] = obj.elements
stream = stream_manager.lookup_stream(n.value)
elif isinstance(obj, String):
stream, channel = stream_manager.get_stream_and_channel_by_name(obj.value)
if stream is None:
if channel == -1:
evaluation.message("General", "openx", obj)
return
close_stream(stream, channel)
return obj
else:
stream = None
if stream is None or stream.io is None or stream.io.closed:
evaluation.message("General", "openx", obj)
return
if n is not None:
close_stream(stream, n.value)
return name
def eval_DumpParse(
input_path: str,
output_path: str,
evaluation: Evaluation,
encoding: str,
trace_fn: Optional[Callable] = DEFAULT_TRACE_FN,
path_directories: Optional[Sequence[str]] = None,
) -> Symbol:
"""
Reads file `input_path`, parses each expression in the file
accumulating them in a list. This list is Python Pickled and
written to `output_path`. True is returned if everything succeeded.
"""
if path_directories is None:
path_directories = tuple(streams.PATH_VAR)
resolved_path, _ = path_search(input_path, path_directories)
if resolved_path is None:
resolved_path = input_path
definitions = evaluation.definitions
# Wrap actual evaluation to handle setting $Input
# and $InputFileName
# store input paths of calling context
global INPUT_VAR
outer_input_var = INPUT_VAR
outer_inputfile = definitions.get_inputfile()
# Set a new input path.
INPUT_VAR = resolved_path
definitions.set_inputfile(INPUT_VAR)
# Save old PATH_VAR in case it gets changed in running Get?
# This seems to be needed, but not 100% sure there isn't
# a better and more robust way.
old_streams_path_var = streams.PATH_VAR
streams.PATH_VAR = SymbolPath.evaluate(evaluation).to_python(string_quotes=False)
queries = []
if trace_fn is not None:
trace_fn(0, resolved_path + "\n")
try:
with Mathics3Open(resolved_path, "r", encoding=encoding) as f:
feeder = MathicsFileLineFeeder(f, trace_fn)
while not feeder.empty():
try:
# Note: we use mathics.core.parser.parse
# so that tracing/debugging can intercept parse()
query = mathics.core.parser.parse(definitions, feeder)
except SyntaxError:
return SymbolNull
finally:
feeder.send_messages(evaluation)
if query is None: # blank line / comment
continue
else:
queries.append(query)
# result = query.evaluate(evaluation)
except IOError:
evaluation.message("DumpParse", "noopen", input_path)
return SymbolFailed
except MessageException as e:
e.message(evaluation)
return SymbolFailed
finally:
# Whether we had an exception or not, restore the input path
# and the state of definitions prior to calling Get.
INPUT_VAR = outer_input_var
definitions.set_inputfile(outer_inputfile)
streams.PATH_VAR = old_streams_path_var
dump_exprs_to_pcl_file(queries, output_path)
return SymbolTrue
def eval_Get(
path: str,
evaluation: Evaluation,
encoding: str,
trace_fn: Optional[Callable] = DEFAULT_TRACE_FN,
path_directories: Optional[Sequence[str]] = None,
):
"""
Reads a file and evaluates each expression, returning only the last one.
"""
result = None
if path_directories is None:
path_directories = tuple(streams.PATH_VAR)
resolved_path, _ = path_search(path, path_directories)
if resolved_path is None:
resolved_path = path
definitions = evaluation.definitions
# Wrap actual evaluation to handle setting $Input
# and $InputFileName
# store input paths of calling context
global INPUT_VAR
outer_input_var = INPUT_VAR
outer_inputfile = definitions.get_inputfile()
# Set a new input path.
INPUT_VAR = resolved_path
definitions.set_inputfile(INPUT_VAR)
# Save old PATH_VAR in case it gets changed in running Get?
# This seems to be needed, but not 100% sure there isn't
# a better and more robust way.
old_streams_path_var = streams.PATH_VAR
streams.PATH_VAR = SymbolPath.evaluate(evaluation).to_python(string_quotes=False)
if trace_fn is not None:
trace_fn(0, resolved_path + "\n")
try:
with Mathics3Open(resolved_path, "r", encoding=encoding) as f:
feeder = MathicsFileLineFeeder(f, trace_fn)
while not feeder.empty():
try:
# Note: we use mathics.core.parser.parse
# so that tracing/debugging can intercept parse()
query = mathics.core.parser.parse(definitions, feeder)
except SyntaxError:
return SymbolNull
finally:
feeder.send_messages(evaluation)
if query is None: # blank line / comment
continue
result = query.evaluate(evaluation)
except IOError:
evaluation.message("Get", "noopen", path)
return SymbolFailed
except MessageException as e:
e.message(evaluation)
return SymbolFailed
finally:
# Whether we had an exception or not, restore the input path
# and the state of definitions prior to calling Get.
INPUT_VAR = outer_input_var
definitions.set_inputfile(outer_inputfile)
streams.PATH_VAR = old_streams_path_var
return result
def eval_Get_from_PCL(
path: str,
evaluation: Evaluation,
encoding: str,
trace_fn: Optional[Callable] = DEFAULT_TRACE_FN,
path_directories: Optional[Sequence[str]] = None,
):
"""
Reads a file and evaluates each expression, returning only the last one.
"""
result = None
if path_directories is None:
path_directories = tuple(streams.PATH_VAR)
resolved_path, _ = path_search(path, path_directories)
if resolved_path is None:
resolved_path = path
parse_list = parse_from_pcl_file(evaluation.definitions, resolved_path)
if not isinstance(parse_list, list):
return None
for query in parse_list:
result = query.evaluate(evaluation)
return result
def eval_Open(
name: String,
mode: str,
stream_type,
encoding: Optional[str],
evaluation: Evaluation,
):
path = name.value
tmp, is_temporary_file = path_search(path)
if tmp is None:
if mode in ["r", "rb"]:
evaluation.message("General", "noopen", name)
return SymbolFailed
else:
path = tmp
try:
opener = Mathics3Open(
path,
mode=mode,
name=name.value,
encoding=encoding,
is_temporary_file=is_temporary_file,
)
opener.__enter__(is_temporary_file=is_temporary_file)
n = opener.n
except IOError:
evaluation.message("General", "noopen", name)
return SymbolFailed
except MessageException as e:
e.message(evaluation)
return
return Expression(Symbol(stream_type), name, Integer(n))
def eval_Read(
name: str, n: int, types: tuple, stream, evaluation: Evaluation, options: dict
):
"""
Evaluation method for Read[] and ReadList[]. `name` will be either "Read" or
"ReadList" and is used in error messages
"""
types = to_mathics_list(*types)
for typ in types.elements:
if typ not in READ_TYPES:
evaluation.message(name, "readf", typ)
return SymbolFailed
separators = read_get_separators(options, evaluation)
if separators is None:
return
record_separators, token_words, word_separators = separators
# name = name.to_python()
result = []
read_word = read_from_stream(
stream, word_separators + record_separators, token_words, evaluation.message
)
read_record = read_from_stream(
stream, record_separators, token_words, evaluation.message
)
read_number = read_from_stream(
stream,
word_separators + record_separators,
token_words,
evaluation.message,
["+", "-", "."] + [str(i) for i in range(10)],
)
read_real = read_from_stream(
stream,
word_separators + record_separators,
token_words,
evaluation.message,
["+", "-", ".", "e", "E", "^", "*"] + [str(i) for i in range(10)],
)
for typ in types.elements:
try:
if typ is Symbol("Byte"):
tmp = stream.io.read(1)
if tmp == "":
raise EOFError
result.append(ord(tmp))
elif typ is Symbol("Character"):
tmp = stream.io.read(1)
if tmp == "":
raise EOFError
result.append(tmp)
elif typ in (SymbolExpression, SymbolHoldExpression):
tmp = next(read_record)
assert isinstance(tmp, str)
while True:
try:
feeder = MathicsMultiLineFeeder(tmp, [], ContainerKind.STREAM)
expr = parse_incrementally_by_line(
evaluation.definitions, feeder
)
break
except (IncompleteSyntaxError, InvalidSyntaxError):
try:
nextline = next(read_record)
assert isinstance(nextline, str)
tmp = tmp + "\n" + nextline
except EOFError:
expr = SymbolEndOfFile
break
except Exception as e:
print(e)
if expr is None:
result.append(None)
elif expr is SymbolEndOfFile:
evaluation.message(name, "readt", tmp, String(stream.name))
return SymbolFailed
elif isinstance(expr, BaseElement):
if typ is SymbolHoldExpression:
expr = Expression(SymbolHold, expr)
result.append(expr)
# else:
# TODO: Supposedly we can't get here
# what code should we put here?
elif typ is Symbol("Number"):
tmp = next(read_number)
try:
tmp = int(tmp)
except ValueError:
try:
tmp = float(tmp)
except ValueError:
evaluation.message(
name, "readn", to_expression("InputSteam", name, n)
)
return SymbolFailed
result.append(tmp)
elif typ is SymbolReal:
tmp = next(read_real)
tmp = tmp.replace("*^", "E")
try:
tmp = float(tmp)
except ValueError:
evaluation.message(
name, "readn", to_expression("InputSteam", name, n)
)
return SymbolFailed
result.append(tmp)
elif typ is Symbol("Record"):
result.append(next(read_record))
elif typ is Symbol("String"):
tmp = stream.io.readline()
if len(tmp) == 0:
raise EOFError
result.append(tmp.rstrip("\n"))
elif typ is SymbolWord:
# next() for word tokens can return one or two words:
# the next word in the list and a following TokenWord
# match. Therefore, test for this and do list-like
# appending here.
# THINK ABOUT: We might need to reconsider/refactor
# other cases to allow for multiple words as well. And
# for uniformity, we may want to redo the generators to
# always return *lists* instead instead of either a
# word or a list (which is always at most two words?)
words = next(read_word)
if not isinstance(words, list):
words = [words]
result += words
except EOFError:
return SymbolEndOfFile
except UnicodeDecodeError:
evaluation.message(name, "ucdec")
if isinstance(result, Symbol):
return result
if isinstance(result, list):
result_len = len(result)
if result_len == 0:
if SymbolHoldExpression in types:
return Expression(SymbolHold, SymbolNull)
elif result_len == 2 and SymbolWord in types:
return [from_python(part) for part in result]
elif result_len == 1:
result = result[0]
if SymbolHoldExpression in types:
if hasattr(result, "head") and result.head is SymbolHold:
return from_python(result)
else:
return Expression(SymbolHold, from_python(result))
return from_python(result)