def create(self, path="."):
     if (self.fields_ == None):
         print "Error : attempt to create a document folder without fields"
         return
     elif (not self.fields_.hasTitle()):
         print "Error : attempt to create a document folder without title"
         return
     
     name = self.computeFolderName()
     self.path_ = Path(path + "/" + name)
     folder = Directory(self.path_)
     folder.createDir()
 def createEmpty(self):
     p = Path()
     assert(p.getAbsolutePath() == "")
     assert(p.getAbsoluteBasename() == "")
     assert(p.getFileName() == "")
     assert(p.getFileBasename() == "")
     assert(p.getExtension() == "")
 def createMultipleExtensions(self):
     path = "a/b/c.tar.gz"
     p = Path(path)
     assert(p.getAbsolutePath() == "a/b/c.tar.gz")
     assert(p.getAbsoluteBasename() == "a/b/c.tar")
     assert(p.getFileName() == "c.tar.gz")
     assert(p.getFileBasename() == "c.tar")
     assert(p.getExtension() == ".gz")
 def createNotClean(self):
     path = "a/b/../cd//file.pdf"
     p = Path(path)
     assert(p.getAbsolutePath() == "a/cd/file.pdf")
     assert(p.getAbsoluteBasename() == "a/cd/file")
     assert(p.getFileName() == "file.pdf")
     assert(p.getFileBasename() == "file")
     assert(p.getExtension() == ".pdf")
 def createLocal(self):
     path = "file.pdf"
     p = Path(path)
     assert(p.getAbsolutePath() == "file.pdf")
     assert(p.getAbsoluteBasename() == "file")
     assert(p.getFileName() == "file.pdf")
     assert(p.getFileBasename() == "file")
     assert(p.getExtension() == ".pdf")
 def setPath(self, path):
     if (path.__class__ == Path):
         self.path_ = path
     else:
         self.path_ = Path(path)
 def __init__(self, path=""):
     if (path.__class__ == Path):
         self.path_ = path
     else:
         self.path_ = Path(path)
     self.fields_ = None
class DocumentFolder:
    
    VIDEOS_DIR_NAME = "videos"
    IMAGES_DIR_NAME = "images"
    NOTES_DIR_NAME = "notes"
    DESCRIPTION_DIR_NAME = "description"

    INTERN_FOLDERS = [VIDEOS_DIR_NAME, IMAGES_DIR_NAME, NOTES_DIR_NAME, DESCRIPTION_DIR_NAME]
    
    BIBLIODATA_FILE_NAME = "biblio_data"
    
    wordsRemoved = ["a", "an", "and", "of", "on", "the"]
    
    def __init__(self, path=""):
        if (path.__class__ == Path):
            self.path_ = path
        else:
            self.path_ = Path(path)
        self.fields_ = None

    def getDir(self):
        return Directory(self.path_)
        
    def setPath(self, path):
        if (path.__class__ == Path):
            self.path_ = path
        else:
            self.path_ = Path(path)

    def setDocFields(self, fields):
        self.fields_ = fields

    # The name is computed from the title of the document, obtained in the fields.
    def computeFolderName(self):
        def isAlphaNum(l) : return (l.isalnum() or l.isspace())
        name = ""
        if (self.fields_ != None and self.fields_.hasTitle()):
            title = self.fields_.getField("title")
            title = filter(isAlphaNum, title)
            words = title.split()
            for i, w in enumerate(words):
                if (w not in self.wordsRemoved): 
                    name += w
                    if (i < len(words)-1):
                        name += "_"
        return name

    # Creates a document folder in the directory referenced by the given path
    # See the method computeFolderName for more details.
    def create(self, path="."):
        if (self.fields_ == None):
            print "Error : attempt to create a document folder without fields"
            return
        elif (not self.fields_.hasTitle()):
            print "Error : attempt to create a document folder without title"
            return
        
        name = self.computeFolderName()
        self.path_ = Path(path + "/" + name)
        folder = Directory(self.path_)
        folder.createDir()

    def createInternFolder(self, name):
        docDir = Directory(self.path_)

        # TODO : exception
        if (not docDir.exists()):
            print "Unable to create internal folder ", name, " in : ", self.path_
            return

        dir = Directory(self.path_.getAbsolutePath() + "/" + name)
        dir.createDir()

    def delete(self):
        Directory(self.path_).removeDir()

    def organize(self):
        d = Directory(self.path_)
        if (not d.exists()):
            print "Error : attempt to organize a non existing folder : ", self.path_
            return

        for internFolderName in self.INTERN_FOLDERS:
            if (not d.containsFolder(internFolderName)):
                self.createInternFolder(internFolderName)

        notesPath = self.path_.getAbsolutePath() + "/" + self.NOTES_DIR_NAME
        latexNotes = LatexNotes(self.fields_)
        latexNotes.createAllContent()
        latexNotes.writeContent(notesPath)

        wikiNotes = WikiNotes(self.fields_)
        wikiNotes.createAllContent()
        wikiNotes.writeContent(notesPath)
        # notes = Notes(self.fields_)

    def getNotesFilePath(self):
        path = self.path_.getAbsolutePath() + "/" + self.NOTES_DIR_NAME + "/" + LatexNotes.FILENAME
        return path

    def createVideoFolder(self):
        self.createInternFolder(self.VIDEOS_DIR_NAME)

    def createImageFolder(self):
        self.createInternFolder(self.IMAGES_DIR_NAME)

    def createNotesFolder(self):
        self.createInternFolder(self.NOTES_DIR_NAME)

    def createDescFolder(self):
        self.createInternFolder(self.DESCRIPTION_DIR_NAME)
    
    def hasVideoFolder(self):
        return Directory(self.path_).containsFolder(self.VIDEOS_DIR_NAME)

    def hasImageFolder(self):
        return Directory(self.path_).containsFolder(self.IMAGES_DIR_NAME)

    def hasNotesFolder(self):
        return Directory(self.path_).containsFolder(self.NOTES_DIR_NAME)

    def hasDescFolder(self):
        return Directory(self.path_).containsFolder(self.DESCRIPTION_DIR_NAME)