-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_fileio.py
More file actions
326 lines (261 loc) · 11.1 KB
/
Copy pathadd_fileio.py
File metadata and controls
326 lines (261 loc) · 11.1 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
#!/usr/bin/env python3
import sys
# Read the entire file
with open('moo_interp/builtin_functions.py', 'r') as f:
lines = f.readlines()
# Find the insertion point (after file_tell, before # property functions)
insert_idx = None
for i, line in enumerate(lines):
if '# property functions' in line and i > 1100:
insert_idx = i
break
if insert_idx is None:
print("Could not find insertion point", file=sys.stderr)
sys.exit(1)
# The new functions to insert
new_functions = '''
def file_exists(self, path):
"""Check if a file exists.
file_exists(path) => 1 if exists, 0 otherwise
Requires wizard permissions.
"""
# Type check
if not isinstance(path, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_exists() requires a string argument")
# Convert to Python string
path_str = str(path) if isinstance(path, MOOString) else path
return 1 if os.path.exists(path_str) else 0
def file_read(self, handle: int, bytes_to_read: int):
"""Read bytes from an open file handle.
file_read(handle, bytes) => string data read
"""
try:
open_file = os.fdopen(handle)
data = open_file.read(bytes_to_read)
return MOOString(data)
except (OSError, ValueError) as e:
raise MOOException(MOOError.E_INVARG, f"file_read failed: {e}")
def file_write(self, handle: int, data):
"""Write data to an open file handle.
file_write(handle, data) => bytes written
"""
try:
open_file = os.fdopen(handle)
data_str = str(data) if isinstance(data, MOOString) else str(data)
bytes_written = open_file.write(data_str)
return bytes_written if bytes_written is not None else len(data_str)
except (OSError, ValueError) as e:
raise MOOException(MOOError.E_INVARG, f"file_write failed: {e}")
def file_eof(self, handle: int):
"""Check if file handle is at end of file.
file_eof(handle) => 1 if at EOF, 0 otherwise
"""
try:
open_file = os.fdopen(handle)
current_pos = open_file.tell()
open_file.seek(0, os.SEEK_END)
end_pos = open_file.tell()
open_file.seek(current_pos)
return 1 if current_pos >= end_pos else 0
except (OSError, ValueError) as e:
raise MOOException(MOOError.E_INVARG, f"file_eof failed: {e}")
def file_stat(self, path):
"""Get file statistics.
file_stat(path) => list [size, type, mode, owner, group, atime, mtime, ctime]
"""
if not isinstance(path, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_stat() requires a string argument")
path_str = str(path) if isinstance(path, MOOString) else path
try:
st = os.stat(path_str)
import stat as stat_module
# Determine file type
if stat_module.S_ISREG(st.st_mode):
file_type = "reg"
elif stat_module.S_ISDIR(st.st_mode):
file_type = "dir"
elif stat_module.S_ISCHR(st.st_mode):
file_type = "chr"
elif stat_module.S_ISBLK(st.st_mode):
file_type = "block"
elif stat_module.S_ISFIFO(st.st_mode):
file_type = "fifo"
elif stat_module.S_ISSOCK(st.st_mode):
file_type = "socket"
else:
file_type = "unknown"
mode_octal = oct(st.st_mode & 0o777)[2:]
return MOOList([
st.st_size,
MOOString(file_type),
MOOString(mode_octal),
MOOString(""),
MOOString(""),
int(st.st_atime),
int(st.st_mtime),
int(st.st_ctime)
])
except OSError as e:
raise MOOException(MOOError.E_INVARG, f"file_stat failed: {e}")
def file_type(self, path):
"""Get file type.
file_type(path) => string ("reg", "dir", "chr", etc)
"""
if not isinstance(path, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_type() requires a string argument")
path_str = str(path) if isinstance(path, MOOString) else path
try:
st = os.stat(path_str)
import stat as stat_module
if stat_module.S_ISREG(st.st_mode):
return MOOString("reg")
elif stat_module.S_ISDIR(st.st_mode):
return MOOString("dir")
elif stat_module.S_ISCHR(st.st_mode):
return MOOString("chr")
elif stat_module.S_ISBLK(st.st_mode):
return MOOString("block")
elif stat_module.S_ISFIFO(st.st_mode):
return MOOString("fifo")
elif stat_module.S_ISSOCK(st.st_mode):
return MOOString("socket")
else:
return MOOString("unknown")
except OSError as e:
raise MOOException(MOOError.E_INVARG, f"file_type failed: {e}")
def file_list(self, path, detailed: int = 0):
"""List directory contents.
file_list(path [, detailed]) => list of filenames or detailed info
"""
if not isinstance(path, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_list() requires a string argument")
path_str = str(path) if isinstance(path, MOOString) else path
try:
entries = os.listdir(path_str)
result = []
for entry in entries:
if entry in ['.', '..']:
continue
if detailed:
full_path = os.path.join(path_str, entry)
st = os.stat(full_path)
import stat as stat_module
if stat_module.S_ISREG(st.st_mode):
file_type = "reg"
elif stat_module.S_ISDIR(st.st_mode):
file_type = "dir"
elif stat_module.S_ISCHR(st.st_mode):
file_type = "chr"
elif stat_module.S_ISBLK(st.st_mode):
file_type = "block"
elif stat_module.S_ISFIFO(st.st_mode):
file_type = "fifo"
elif stat_module.S_ISSOCK(st.st_mode):
file_type = "socket"
else:
file_type = "unknown"
mode_octal = oct(st.st_mode & 0o777)[2:]
result.append(MOOList([
MOOString(entry),
MOOString(file_type),
MOOString(mode_octal),
st.st_size
]))
else:
result.append(MOOString(entry))
return MOOList(result)
except OSError as e:
raise MOOException(MOOError.E_INVARG, f"file_list failed: {e}")
def file_mkdir(self, path, mode: int = 0o777):
"""Create a directory.
file_mkdir(path [, mode]) => 0 on success
"""
if not isinstance(path, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_mkdir() requires a string argument")
path_str = str(path) if isinstance(path, MOOString) else path
try:
os.mkdir(path_str, mode)
return 0
except OSError as e:
raise MOOException(MOOError.E_INVARG, f"file_mkdir failed: {e}")
def file_rmdir(self, path):
"""Remove a directory.
file_rmdir(path) => 0 on success
"""
if not isinstance(path, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_rmdir() requires a string argument")
path_str = str(path) if isinstance(path, MOOString) else path
try:
os.rmdir(path_str)
return 0
except OSError as e:
raise MOOException(MOOError.E_INVARG, f"file_rmdir failed: {e}")
def file_remove(self, path):
"""Remove a file.
file_remove(path) => 0 on success
"""
if not isinstance(path, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_remove() requires a string argument")
path_str = str(path) if isinstance(path, MOOString) else path
try:
os.remove(path_str)
return 0
except OSError as e:
raise MOOException(MOOError.E_INVARG, f"file_remove failed: {e}")
def file_rename(self, old_path, new_path):
"""Rename a file or directory.
file_rename(old, new) => 0 on success
"""
if not isinstance(old_path, (str, MOOString)) or not isinstance(new_path, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_rename() requires string arguments")
old_str = str(old_path) if isinstance(old_path, MOOString) else old_path
new_str = str(new_path) if isinstance(new_path, MOOString) else new_path
try:
os.rename(old_str, new_str)
return 0
except OSError as e:
raise MOOException(MOOError.E_INVARG, f"file_rename failed: {e}")
def file_copy(self, src, dst):
"""Copy a file.
file_copy(src, dst) => 0 on success
"""
import shutil
if not isinstance(src, (str, MOOString)) or not isinstance(dst, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_copy() requires string arguments")
src_str = str(src) if isinstance(src, MOOString) else src
dst_str = str(dst) if isinstance(dst, MOOString) else dst
try:
shutil.copy2(src_str, dst_str)
return 0
except OSError as e:
raise MOOException(MOOError.E_INVARG, f"file_copy failed: {e}")
def file_chmod(self, path, mode):
"""Change file permissions.
file_chmod(path, mode) => 0 on success
"""
if not isinstance(path, (str, MOOString)):
raise MOOException(MOOError.E_TYPE, "file_chmod() requires path as string")
path_str = str(path) if isinstance(path, MOOString) else path
# Convert mode to integer
if isinstance(mode, (str, MOOString)):
mode_str = str(mode) if isinstance(mode, MOOString) else mode
try:
mode_int = int(mode_str, 8)
except ValueError:
raise MOOException(MOOError.E_INVARG, f"Invalid mode string: {mode_str}")
elif isinstance(mode, int):
mode_int = mode
else:
raise MOOException(MOOError.E_TYPE, "file_chmod() mode must be string or integer")
try:
os.chmod(path_str, mode_int)
return 0
except OSError as e:
raise MOOException(MOOError.E_INVARG, f"file_chmod failed: {e}")
'''
# Insert the new functions
new_lines = lines[:insert_idx] + [new_functions] + lines[insert_idx:]
# Write back
with open('moo_interp/builtin_functions.py', 'w') as f:
f.writelines(new_lines)
print(f"Inserted new FileIO builtins before line {insert_idx}")