I provide some code to help you out below, but first, some overall advice that should help you with your design:
The reason you're having difficulty with changing directories is that you are representing the current directory variable the wrong way. Your current directory should be something like a list, from your top level directory to your current one. Once you have that, you just make a choice about how store files using shelve based on their directory (considering that all keys in Shelve must be strings).
It looks like you were planning on representing the filesystem as a series of nested dictionaries- a good choice. But note that if you change mutable objects in shelve
, you have to a) set writeback to True and b) call fs.sync() to set them.
You should be structuring your entire filesystem in a class rather than in a series of functions. It will help you keep your shared data organized. The below code doesn't follow that but it is worth thinking about.
So, I fixed up cd
and also wrote a rudimentary mkdir command for you. The critical thing for making them work is to, as I said above, have current_dir be a list that shows your current path, and also to have an easy way (the current_dictionary
function) to get from that list to the appropriate filesystem directory.
With that, here's the code to get you started:
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...')
And here's a demonstration:
What do you want your username to be? David
> ls
Contents of directory /:
System
Users
> cd Users
> ls
Contents of directory /Users:
David
> cd David
> ls
Contents of directory /Users/David:
> cd ..
> ls
Contents of directory /Users:
David
> cd ..
> mkdir Other
> ls
Contents of directory /:
System
Users
Other
> cd Other
> ls
Contents of directory /Other:
> mkdir WithinOther
> ls
Contents of directory /Other:
WithinOther
It's important to note that this is so far just a toy: there is still a ton left to do. Here are a few examples:
Right now there is only such a thing as directories- no regular files.
mkdir
doesn't check if a directory already exists, it would overwrite one with an empty directory.
You can't ls
with a specific directory as an argument, (like ls Users
), only your current directory.
Still, this should show you an example of a design for keeping track of your current directory. Good luck!