forked from jediubuntu/virtual-filesystem-using-shelves-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualfilesystem.py
59 lines (46 loc) · 1.44 KB
/
virtualfilesystem.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
import shelve
fs = shelve.open('filesystem.fs', writeback=True)
current_dir = []
def install(fs):
# create root and others
username = raw_input('What do you want your username to be? ')
fs[""] = {"System": {}, "Users": {username: {}}}
def current_dictionary():
"""Return a dictionary representing the files in the current directory"""
d = fs[""]
for key in current_dir:
d = d[key]
return d
def ls(args):
print 'Contents of directory', "/" + "/".join(current_dir) + ':'
for i in current_dictionary():
print i
def cd(args):
if len(args) != 1:
print "Usage: cd <directory>"
return
if args[0] == "..":
if len(current_dir) == 0:
print "Cannot go above root"
else:
current_dir.pop()
elif args[0] not in current_dictionary():
print "Directory " + args[0] + " not found"
else:
current_dir.append(args[0])
def mkdir(args):
if len(args) != 1:
print "Usage: mkdir <directory>"
return
# create an empty directory there and sync back to shelve dictionary!
d = current_dictionary()[args[0]] = {}
fs.sync()
COMMANDS = {'ls' : ls, 'cd': cd, 'mkdir': mkdir}
install(fs)
while True:
raw = raw_input('> ')
cmd = raw.split()[0]
if cmd in COMMANDS:
COMMANDS[cmd](raw.split()[1:])
#Use break instead of exit, so you will get to this point.
raw_input('Press the Enter key to shutdown...')