Example #1
0
def retrieve_nbt(filename):
    """
    Attempt to read an NBT blob from the file with the given filename.

    If the requested file does not exist, then the returned tag will be empty
    and will be saved to that file when write_file() is called on the tag.

    This function can and will make a good effort to create intermediate
    directories as needed.

    XXX should handle corner cases
    XXX should mmap() when possible
    XXX should use Twisted's VFS

    :param str filename: NBT file to load

    :returns: `NBTFile`
    """

    try:
        tag = NBTFile(filename)
    except IOError:
        # The hard way, huh? Wise guy...
        tag = NBTFile()
        tag.filename = filename

        try:
            # Make the directory holding this file.
            os.makedirs(os.path.normpath(os.path.split(filename)[0]))
        except OSError, e:
            if e.errno != EEXIST:
                raise