Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a Python virtual filesystem using shelve

I have set up a Python script that simulates an OS. It has a command prompt and a virtual file system. I am using the shelve module to simulate the file system, being multi-dimensional in order to support a hierarchy of directories. However, I am having trouble implementing a 'cd' command. I don't know how to get in and out of directories, even though I have a small set of directories created when you first launch the program. Here is my code:

import shelve

fs = shelve.open('filesystem.fs')
directory = 'root'
raw_dir = None
est_dir = None

def install(fs):
    fs['System'] = {}
    fs['Users'] = {}
    username = raw_input('What do you want your username to be? ')
    fs['Users'][username] = {}

try:
    test = fs['runbefore']
    del test
except:
    fs['runbefore'] = None
    install(fs)

def ls(args):
    print 'Contents of directory', directory + ':'
    if raw_dir:
        for i in fs[raw_dir[0]][raw_dir[1]][raw_dir[2]][raw_dir[3]]:
            print i
    else:
        for i in fs:
            print i

def cd(args):
    if len(args.split()) > 1:
        if args.split()[1] == '..':
            if raw_dir[3]:
                raw_dir[3] = 0
            elif raw_dir[2]:
                raw_dir[2] = 0
            elif raw_dir[1]:
                raw_dir[1] = 0
            else:
                print "cd : cannot go above root"

COMMANDS = {'ls' : ls}

while True:
    raw = raw_input('> ')
    cmd = raw.split()[0]
    if cmd in COMMANDS:
        COMMANDS[cmd](raw)

#Use break instead of exit, so you will get to this point.
raw_input('Press the Enter key to shutdown...')

I'm not getting an error, I just have no idea how to do it and no idea of what to search for besides 'python shelve file system', and that doesn't get anything useful.

like image 764
elijaheac Avatar asked Jan 11 '12 03:01

elijaheac


1 Answers

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!

like image 152
David Robinson Avatar answered Jan 03 '23 14:01

David Robinson