#hgvm-builder directory.py: Represent a bunch of Toil files with filenames

import os
import os.path
import logging
import urlparse
import shutil

Logger = logging.getLogger("directory")

class Directory(object):
    """
    Represents a collection of Toil file IDs with assigned file names (which can
    have directories in them). Allows a bunch of file IDs to be passed around as
    a group, dumped to disk, exported from the Toil master, and generally
    treated as a virtual filesystem.
    
    Note that changes are only visible to the current job, or child jobs to whom
    the modified Directory object is passed. Note that Toil may not allow you to
    retrieve a file you just wrote.
    
    File IDs may be Toil promises, but exporting and downloading will not work
    until they are filled in.
    
    """
    
    def __init__(self, values={}):
        """
        Make a new empty Directory with no files (by default), or the files
        given in the specified dictionary.
        
        """
        
        # This holds Toil IDs by file name
        # Make sure to copy it to aboid adding to our default value
        # Or weird behavior when the caller modifies their dict.
        self.ids = dict(values)
        
    def __repr__(self):
        """
        Report ourselves fully.
        """
        
        return "Directory(" + repr(self.ids) + ")"
        
    def add(self, file_name, file_id):
        """
        Save the file with the given ID in the directory under the given name.
        No leading slashes should be used. "." and ".." are not supported.
        
        Can be chained.
        """