Skip to content

Commit 6032b01

Browse files
committed
upysh: Minimalistic file shell using native Python syntax.
E.g. cat("file") is equivalent of Unix shell's 'cat file'. With some tricks though makes pwd and ls work without parens.
1 parent f3683b1 commit 6032b01

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

upysh/upysh.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import sys
2+
import os
3+
4+
class LS:
5+
6+
def __repr__(self):
7+
l = os.listdir()
8+
l.sort()
9+
return "\n".join(l)
10+
11+
def __call__(self, path="."):
12+
l = os.listdir(path)
13+
l.sort()
14+
for f in l:
15+
print(f)
16+
17+
class PWD:
18+
19+
def __repr__(self):
20+
return os.getcwd()
21+
22+
def __call__(self):
23+
return self.__repr__()
24+
25+
pwd = PWD()
26+
ls = LS()
27+
28+
def cd(path):
29+
os.chdir(path)
30+
31+
def head(f, n=10):
32+
with open(f) as f:
33+
for i in range(n):
34+
l = f.readline()
35+
if not l: break
36+
sys.stdout.write(l)
37+
38+
def cat(f):
39+
head(f, 1 << 30)
40+
41+
def help():
42+
print("""
43+
This is 'upysh' help, for builtin Python help run:
44+
import builtins
45+
builtins.help()
46+
47+
upysh commands:
48+
pwd, cd("new_dir"), ls, ls(...), head(...), cat(...)
49+
""")

0 commit comments

Comments
 (0)