-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChrisOS.py
More file actions
530 lines (505 loc) · 19 KB
/
ChrisOS.py
File metadata and controls
530 lines (505 loc) · 19 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import json
import platform
import os
import re
import random as rand
from datetime import datetime
import random
def date():
now = datetime.now()
return now.strftime("%Y-%m-%d")
time = 1
TLF = 10000
lines_taken = 0
vers = 'ChrisOS™ 3 beta'
files = {}
dirlist = ['Tic-Tac-Toe.pre',]
filelist = []
templist = []
history = []
var = None
def usage():
system_type = platform.system()
if system_type == 'Windows':
output = os.popen("wmic cpu get loadpercentage").read()
match = re.search(r'\d+', output)
if match:
return f"{match.group()}%"
else:
return "Error parsing CPU usage(maybe try again)"
elif system_type == 'Linux':
output = os.popen("top -bn1 | grep 'Cpu(s)'").read()
match = re.search(r'(\d+\.\d+)\s*id', output)
if match:
idle = float(match.group(1))
usage_percent = 100 - idle
return f"{int(usage_percent)}%"
else:
return "Error parsing CPU usage(maybe try again)"
else:
return f'Cannot search {system_type} for CPU stats'
class TTT:
def __init__(self):
self.board = [[' ' for _ in range(3)] for _ in range(3)]
self.player_won = False
self.bot_won = False
def print_board(self):
for row in self.board:
print(row)
def win_check(self, token):
b = self.board
return (
any(all(cell == token for cell in row) for row in b) or
any(all(b[r][c] == token for r in range(3)) for c in range(3)) or
all(b[i][i] == token for i in range(3)) or
all(b[i][2 - i] == token for i in range(3))
)
def is_draw(self):
return all(cell in ['X', 'O'] for row in self.board for cell in row)
def bot_turn(self):
while True:
row, col = random.randint(0, 2), random.randint(0, 2)
if self.board[row][col] == ' ':
self.board[row][col] = 'O'
self.bot_won = self.win_check('O')
break
def player_turn(self):
while True:
self.print_board()
try:
row = int(input("Row (1-3): ")) - 1
col = int(input("Column (1-3): ")) - 1
if not (0 <= row < 3 and 0 <= col < 3):
print("Invalid input. Try again.")
continue
if self.board[row][col] != ' ':
print("Occupied. Try again.")
continue
self.board[row][col] = 'X'
self.player_won = self.win_check('X')
break
except ValueError:
print("Invalid input. Try again.")
def start(self):
print("You're X, the bot is O. Rows go top to bottom, columns left to right.")
while not self.player_won and not self.bot_won:
self.player_turn()
if self.player_won:
print("You won!")
break
if self.is_draw():
print("It's a draw!")
break
self.bot_turn()
if self.bot_won:
print("The bot won!")
break
if self.is_draw():
print("It's a draw!")
break
self.print_board()
def new(nam):
global lines_taken
print("make a line with 'F{stopwr}' then press enter to stop")
templines = 0
templist = []
while True:
ln = input('ln> ')
if ln == 'F{stopwr}':
break
templist.append(ln)
templines += 1
print()
if lines_taken + templines > TLF:
excess = (lines_taken + templines) - TLF
print(f'Error: adding {templines} lines exceeds limit by {excess} lines; file cannot be stored.')
return
nm = nam
if f'{nm}' in filelist:
print(f'the name \'{nm}\' is already taken')
return ''
if not templist:
templist = ['']
return (templist, templines, nm)
def save_to_disk(check, skip):
global var
if skip == False or var == True:
try:
with open("chrisos2_mem.json", "w") as f:
json.dump({
"files": files,
"filelist": filelist,
"dirlist": dirlist,
"lines_taken": lines_taken,
"time": time,
}, f)
if check:
return True
except:
print('memory storer not found! ChrisOS: data: unsaved')
if check:
return False
else:
if var == False:
return
def load_from_disk(end):
global files, filelist, dirlist, lines_taken, time
with open("chrisos2_mem.json", "r") as f:
data = json.load(f)
files = data.get("files", {})
filelist = data.get("filelist", [])
dirlist = data.get("dirlist", ["Tic-Tac-Toe.pre"])
if 'Tic-Tac-Toe.pre' not in dirlist:
dirlist.insert(0, 'Tic-Tac-Toe.pre')
lines_taken = data.get("lines_taken", 0)
time = data.get("time", 1)
if end == 'y':
print('ChrisOS: data: loaded')
def rename():
global filelist, dirlist, files, command
try:
old = None
new = None
for fname in filelist:
if command.startswith(f'rename {fname} as '):
old = fname
break
if old is None:
print('file not found')
return
new = command[len(f'rename {old} as '):].strip()
if new.endswith((".im", ".txt")) == False:
print("need '.txt' or '.im' in replacement name; renaming cannot be done")
return
for i in range(len(filelist)):
if filelist[i].strip() == old.strip():
filelist[i] = new
for i in range(len(dirlist)):
if dirlist[i].strip() == old.strip():
dirlist[i] = new
if old in files:
files[new] = files[old]
del files[old]
if f'{old}.lines' in files:
files[f'{new}.lines'] = files[f'{old}.lines']
del files[f'{old}.lines']
save_to_disk(False, True)
print(f"renamed '{old}' to '{new}'")
except:
print(f'could not rename {old}')
def check():
if '(' in command or ')' in command:
print(f'Command \'{command}\' not found! Enter \'help\' to see the commands. or try removing parentheses')
return
else:
if command == '':
return
else:
print(f'Command \'{command}\' not found! Enter \'help\' to see the commands.')
return
try:
load_from_disk('n')
var = True
except:
files, filelist, dirlist, lines_taken, time = {}, [], ['Tic-Tac-Toe.pre'], 0, 1
var = save_to_disk(True, False)
if var:
load_from_disk('y')
oh = None
time += 1
save_to_disk(False, True)
history = []
command = ''
while True:
if var == False:
print()
print('reminder: no memory storer; changes like managing files can be made,\nbut next time you boot up the computer,\neverything will have reset,\ntry connecting an, HDD, SSD, or some portable storage device like SD or USB drive to the computer\nbut you will need to close the computer and turn it on again to save\nChrisOS: data: unsaved')
print()
if command != '':
history.append(command)
if oh == None: command = input('>> ').strip()
else: command = oh
if command == '+':
try:
num1 = float(input('num1: '))
num2 = float(input('num2: '))
print(f'{num1 + num2}')
except:
print('invalid input')
elif command == '*':
try:
num1 = float(input('num1: '))
num2 = float(input('num2: '))
print(f'{num1 * num2}')
except:
print('invalid input')
elif command == '-':
try:
num1 = float(input('num1: '))
num2 = float(input('num2: '))
print(f'{num1 - num2}')
except:
print('invalid input')
elif command == '/':
try:
num1 = float(input('num1: '))
num2 = float(input('num2: '))
if num2 == 0:
print('Can\'t do division by 0s!')
continue
print(f'{num1 / num2}')
except:
print('invalid input')
elif command == '^':
try:
num1 = float(input('num1: '))
num2 = float(input('num2: '))
print(f'{num1 ** num2}')
except:
print('invalid input')
elif command == 'randint':
try:
num1 = int(input('range param1: '))
num2 = int(input('range param2: '))
print(f'{rand.randint(num1, num2)}')
except:
print('invalid input')
elif command == 'randfloat':
try:
num1 = float(input('range param1: '))
num2 = float(input('range param2: '))
print(f'{rand.uniform(num1, num2)}')
except:
print('invalid input')
elif command == 'randstr':
try:
chars = int(input('characters: '))
complete = ''
for i in range(chars):
complete += chr(random.randint(0, 127))
print(complete)
except:
print('invalid input!')
elif command.startswith('new '):
if (command[4:].endswith(".im")) or command[4:].endswith(".txt"):
name = new(command[4:].lstrip())
if name != '':
templist, templines, nm = name
lines_taken += templines
files[f'{nm}'] = templist.copy()
files[f'{nm}.lines'] = templines
dirlist.append(f'{nm}')
filelist.append(f'{nm}')
save_to_disk(False, True)
print(f'created {command[4:]}')
else:
print("file type not found!")
elif command == 'dir' or command == 'ls':
print(dirlist)
elif command == 'redo':
try:
for i in range(len(history)):
print(f'{i+1}:{history[i]}')
print()
num = int(input('command: '))
oh = history[num-1]
continue
except:
print('invalid input!')
elif command.startswith('read '):
nme = command[5:].strip()
if nme.endswith(".txt"):
content = files.get(nme)
if content is not None:
if isinstance(content, list):
print('\n'.join(content))
else:
print(content)
else:
print('File not found!')
elif nme.endswith('im'):
content = files.get(nme)
if content is None:
print('File not found!')
continue
ncontent = []
for i in range (len(content)):
ncontent.append('')
for i in range(len(content)):
for j in range(len(content[i])):
th = content[i][j]
place = ncontent[i]
if th == 'R': place += "🟥"
elif th == 'O': place += '🟧'
elif th == 'Y': place += '🟨'
elif th == 'G': place += '🟩'
elif th == 'D': place += '🟦'
elif th == 'P': place += '🟪'
elif th == 'b': place += '🟫'
elif th == "B": place += '⬛'
elif th == "W": place += '⬜'
elif th == ' ': place += ' '
else:
print('problem reading image!')
ncontent.clear()
continue
ncontent[i] = place
if isinstance(content, list):
print('\n'.join(ncontent))
else:
print(ncontent)
else:
print('file type not found!')
elif command == 'version':
print(vers)
elif command == 'files':
print(filelist)
elif command == 'cl':
system_type = platform.system()
if system_type == 'Windows':
r = os.system('powershell "cls"')
else:
r = os.system('clear')
elif command.startswith('delete '):
nme = command[7:]
if nme in files:
confirm = input(f'Are you sure you want to delete \'{nme}\'? (y/n)>> ').lower()
if confirm == 'y':
lines_taken -= files[f"{nme}.lines"]
del files[nme]
files.pop(f"{nme}.lines", None)
for i in range(len(filelist)):
if filelist[i] == nme:
del filelist[i]
break
for i in range(len(dirlist)):
if dirlist[i] == nme:
del dirlist[i]
break
save_to_disk(False, True)
print(f'deleted {nme}')
elif confirm == 'n':
print('Deletion canceled.')
else:
print('invalid answer')
else:
print('File not found!')
elif command == 'help':
print('+ : addition\n- : subtraction\n* : multiplication\n/ : division\n^ : power\nrandint: generates a random integer in a range\nrandfloat: generates a random float in a range\nrandstr: generates a random string form an amount of characters\nnew (file name) : makes a new file(you can use the edit command to modify it)\nread (name file you want to read): reads a file\nversion : OS version\ndelete (name of thing you want to delete) : deletes a file\nfiles : view file names\ndir or ls : see the directory\nexit : shuts down the computer\nhelp : shows commands and their actions\ndate : shows the date\ndata : shows the total amount of lines in files\nboots : shows how many times the system has been booted\nformats : shows formats that are used in the sytem\nhistory : shows command history(on the main prompt not like in applications) of the session\ncodes : gives you color codes for image editor\nredo: you pick to run a command from your history\ncl : clears the screen\nCPUse : shows CPU usage percentage\nPrun (name of what ever pre you want to run) : runs applications with \'.pre\' ending\npres : displays .pre programs\nrename (name of file you want to rename) as (name you want to rename it to) : renames files\n(file name) : says if it\'s a file or not\nedit : edits a file\ncopy (file name) : copies a file\nChrisOS: data : shows state of memory')
elif command == 'codes':
print('R : red\nO : orange\nY : yellow\nG : green\nD : blue\nP : purple\nb : brown\nB : black\nW : white')
print("hint: when you are making a new image file, if you want one row to have red color, red color, then black, type RRB, also you can put spaces in it")
elif command == 'date':
print(date())
elif command == 'data':
print(f'{lines_taken}/{TLF} lines taken')
elif command == 'boots':
print(f'system has been booted {time - 1} times')
elif command == 'formats':
print('.txt : text file\n.pre : preinstalled application\n.im : image file')
elif command == 'pres':
print('Tic-Tac-Toe.pre')
elif command == 'CPUse':
var = usage()
print(var)
elif command == 'history':
print(history)
elif command == 'Prun Tic-Tac-Toe.pre':
print()
game = TTT()
game.start()
print()
elif command.startswith('copy '):
if command[5:] in filelist:
new = f'copy of {command[5:].strip()}'
old = command[5:].strip()
dirlist.append(new)
filelist.append(new)
files[new] = files[old]
files[f'{new}.lines'] = files[f'{old}.lines']
print(f'copied {old}')
else:
print('file to copy not found')
elif command.startswith('rename '):
rename()
elif command == 'ChrisOS: data':
if var == True:
print('loaded')
else:
print('unsaved')
elif command == 'exit':
save_to_disk(False, True)
break
elif command.startswith('edit '):
if (command[5:].endswith(".txt") or command[5:].endswith(".im")):
try:
file = files[command[5:]]
filename = command[5:]
while True:
print('add, to add line, edit to edit line, delete to delete line, exit to exit program:')
op = input()
if op == 'add':
ln = input('ln> ')
file.append(ln)
elif op == 'edit':
for i in range(len(file)):
print(f'{i+1}: {file[i]}')
try:
line = int(input('line num: ')) - 1
print(f'replace {file[line]} with:')
nl = input('ln> ')
file[line] = nl
except:
print('line does not exist!')
elif op == 'delete':
for i in range(len(file)):
print(f'{i+1}: {file[i]}')
try:
line = int(input('line num: ')) - 1
del file[line]
except:
print('line does not exist!')
elif op == 'exit':
print(f'{filename} edited')
lines_taken -= files[f'{filename}.lines']
files[f'{filename}.lines'] = len(file)
lines_taken += files[f'{filename}.lines']
files[filename] = file
save_to_disk(False, True)
break
else:
print('invalid input!')
except:
print('name not found!')
save_to_disk(False, True)
else:
print('file type not found!')
elif command.startswith('rename') == False:
if command.endswith('.txt'):
if command in dirlist:
print(f'{command} is a text file')
continue
else:
print(f'{command} is not a file')
continue
elif command.endswith('.im'):
if command in dirlist:
print(f'{command} is an image file')
continue
else:
print(f'{command} is not a file')
continue
elif command.endswith('.pre'):
if command in dirlist:
print(f'{command} is a preinstalled application')
continue
else:
print(f'{command} is not a file')
continue
else:
check()
continue
else:
check()
oh = None
exit()