This repository has been archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apps.py
116 lines (99 loc) · 3.51 KB
/
apps.py
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
#!/usr/bin/env/ python3
# Apps module originally created by Sebastian-Byte, FabioPolancoE and FRostri
# Edited by Suaj
import os
import sys
import setup
import subprocess
from random import randint
from dotenv import load_dotenv
load_dotenv()
# Each command's description
all_commands = {"hello": "A simple command that prints 'Hello World!'",
"numguess": "An awesome Guess-The-Number Game",
"ls": "Lists all the files and directories inside the home folder",
"new": "Creates a new file, use with 'new [filename]', i.e 'new hi.txt', do never use whitespace.",
"show": "Prints the contents of a file",
"help": "Shows information about a available commands",
"exit": "Stops the execution of FakeOS",
"pyshell": "Opens an embedded python3 shell",
"pyrun": "Executes a python3 file, place the file inside the home folder and type 'pyrun [filename]', i.e. 'pyrun hello.py'",
"interactive": "Opens an interactive python3 shell, requires 'ipython3' installed",
"edit": "Edit or create files, type 'edit [filename]', i.e. 'edit note.txt', requires a text editor installed",
"install": "Activates setup script",
"clear": "Clears the console"}
# Definition of the commands
def hello():
print("Hello, World!")
def numguess():
number = randint(1, 10)
while True:
guess = int(input("I'm thinking of a number between 1 and 10, try\
to guess it...\n"))
if guess == number:
print("Wow, you did it! :D")
break
else:
print("Oops! that isn't the number I'm thinking of D:")
def ls():
home = os.listdir("./home")
if home:
for i in home:
print(i)
elif not home or len(home) == 0:
print("The home folder is empty.")
def new(filename):
if sys.platform.startswith("win32"):
os.system(f"type nul > ./home/{filename}")
else:
subprocess.call(["touch", f"./home/{filename}"])
def show(filename):
with open(f"./home/{filename}", "r") as f:
fcontent = f.readlines()
for i in fcontent:
print(i, end='') # Each line already has the \n char at the end
def help(method=False):
if method:
print(all_commands[method])
else:
print("All available commands:\n")
for i in all_commands.keys():
print(f"{i}: {all_commands[i]}\n")
def pyrun(filename):
if sys.platform.startswith("win32"):
os.system(f"python {filename}")
else:
subprocess.call(["python3", filename])
def pyshell():
print("")
if sys.platform.startswith("win32"):
os.system("python")
else:
subprocess.call("python3")
print("")
def interactive():
print("")
if sys.platform.startswith("win32"):
os.system('ipython')
else:
subprocess.call("ipython3")
print("")
def edit(filename):
try:
editor = os.getenv("EDITOR")
if sys.platform.startswith("win32"):
os.system(f"{editor} {filename}")
else:
subprocess.call([editor, filename])
except: # There should be an specific exception here
if sys.platform.startswith("win32"):
os.system(f"start notepad.exe {filename}")
else:
subprocess.call(["nano", f"./home/{filename}"])
def clear():
if sys.platform.startswith("win32"):
os.system("cls")
else:
subprocess.call(["tput reset"])
def install():
setup.start()