def organize(self):
        fields = DocumentFields()
        fields.setField("title", "document about internet")
        fields.setField("year", "2014")
        fields.setField("authors", "John Bruf, Tommy Bamo")
        fields.setField("keywords", "Computer science, Photography")
        fields.setField("journal", "Journal of Computer Science")

        dir = Directory("document_about_internet")
        dir.removeDir()

        d = DocumentFolder()
        d.setDocFields(fields)

        d.create()

        assert(not d.hasVideoFolder())
        assert(not d.hasImageFolder())
        assert(not d.hasNotesFolder())
        assert(not d.hasDescFolder())

        d.organize()

        assert(d.hasVideoFolder())
        assert(d.hasImageFolder())
        assert(d.hasNotesFolder())
        assert(d.hasDescFolder())

        notesPath = d.getNotesFilePath()
        print notesPath
        assert(File(notesPath).exists())

        d.delete()
Exemplo n.º 2
0
 def __init__(self):
     Test.__init__(self, "Directory")
     d = Directory("dir")
     d.removeDir()
     self.addTestFunction(self.create)
     self.addTestFunction(self.remove)
     self.addTestFunction(self.createTree)
     self.addTestFunction(self.content)
     self.addTestFunction(self.containsFolder)
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
 def create(self, path="."):
     if (not self._fields.hasTitle()):
         raise RuntimeError("Error : attempt to create a document folder without title")
         return
     
     name = self.computeFolderName()
     self._path = MyPath(path + "/" + name)
     self._path = self._path.findNotUsedPath()
     folder = Directory(self._path)
     folder.createDir()
Exemplo n.º 5
0
 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()
Exemplo n.º 6
0
 def test_remove(self):
     d = Directory("dir")
     d.createDir()
     
     d.removeFile()
     self.assertTrue(d.exists())
 
     d.removeDir()
     self.assertFalse(d.exists())
Exemplo n.º 7
0
 def remove(self):
     d = Directory("dir")
     d.removeFile()
     assert(d.exists())
 
     d.removeDir()
     assert(not d.exists())
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
 def create(self):
     d = Directory("dir")
     assert(not d.exists())
 
     d.createDir()
     assert(d.exists())
     assert(not d.isRegularFile())
     assert(d.isDirectory())
Exemplo n.º 10
0
 def test_create(self):
     d = Directory("dir")
     self.assertFalse(d.exists())
 
     d.createDir()
     self.assertTrue(d.exists())
     self.assertFalse(d.isRegularFile())
     self.assertTrue(d.isDirectory())
Exemplo n.º 11
0
 def __init__(self, path="", fields=None):
     
     if(isinstance(path, MyPath)):
         self._path = path
     else:
         self._path = MyPath(path)
         
     if(fields is None):
         fields = DocumentFields()
         
     self._fields = fields
     self._directory = Directory(self._path)
Exemplo n.º 12
0
    def create(self):
        a = DocumentFields()
        a.setField("title", "document")

        d = DocumentFolder()
        d.setDocFields(a)

        for title in self.titles:
            a.setField("title", title)
            d.create()
            assert(d.getDir().exists())
            d.delete()
            assert(not d.getDir().exists())

        dir = Directory("documents")
        dir.createDir()

        for title in self.titles:
            a.setField("title", title)
            d.create(dir.getPath().getAbsolutePath())
            assert(d.getDir().exists())
            d.delete()
            assert(not d.getDir().exists())

        dir.removeDir()
Exemplo n.º 13
0
    def test_create(self):
        a = DocumentFields()
        a.setField("title", "document")

        d = DocumentFolder()
        d.setDocFields(a)

        # Creation in local folder
        for title in self.titles:
            a.setField("title", title)
            d.create()
            self.assertTrue(d.getDir().exists())
            d.delete()
            self.assertFalse(d.getDir().exists())

        dir = Directory("documents")
        dir.createDir()

        # Creaton in another folder
        for title in self.titles:
            a.setField("title", title)
            d.create(dir.getPath().getAbsolutePath())
            self.assertTrue(d.getDir().exists())
            d.delete()
            self.assertFalse(d.getDir().exists())

        dir.removeDir()
Exemplo n.º 14
0
 def test_containsExtension(self):
     d = Directory("dir")
     f1 = File("dir/text.txt")
     f2 = File("dir/image.png")
     
     d.createDir()
     
     f1.createFile()
     f2.createFile()
     
     self.assertTrue(d.containsFileExtension(".txt"), "Directory contains .txt")
     self.assertTrue(d.containsFileExtension(".png"), "Directory contains .png")
     self.assertFalse(d.containsFileExtension(".pdf"), "Directory does not contain .pdf")
     
     d.removeDir()
Exemplo n.º 15
0
    def containsFolder(self):
        d = Directory("dir")
        d1 = Directory("dir/a/b")
        d2 = Directory("dir/c")
        d3 = Directory("dir/a")

        d1.createTree()
        d2.createTree()

        assert(d.containsFolder("a"))
        assert(d.containsFolder("c"))
        assert(not d.containsFolder("b"))
        assert(d3.containsFolder("b"))
        assert(not d3.containsFolder("a"))

        d.removeDir()
Exemplo n.º 16
0
 def content(self):
     d = Directory("dir")
     d1 = Directory("dir/a/b")
     d2 = Directory("dir/c")
     d3 = Directory("dir/d/e")
     f1 = File("dir/g.txt")
     f2 = File("dir/e.txt")
     
     d1.createTree()
     d2.createTree()
     d3.createTree()
     f1.createFile()
     f2.createFile()
     
     l = d.getFilesList()
     assert (l == {"dir/e.txt", "dir/g.txt"})
     
     l = d.getDirsList()
     assert (l == {"dir/a", "dir/c", "dir/d"})
     
     l = d.getContentList()
     assert (l == {"dir/e.txt", "dir/g.txt", "dir/a", "dir/c", "dir/d"})
     
     d.removeDir()
Exemplo n.º 17
0
 def createTree(self):
     d = Directory("dir/fg/a")
     assert(not d.exists())
 
     d.createDir()
     assert(not d.exists())
 
     d.createTree()
     assert(d.exists())
 
     d.removeFile()
     assert(d.exists())
 
     d.removeDir()
     assert(not d.exists())
 
     d = Directory("dir")
     assert(d.exists())
 
     d.removeDir()
     assert(not d.exists())
Exemplo n.º 18
0
    def test_containsFolder(self):
        d = Directory("dir")
        d1 = Directory("dir/a/b")
        d2 = Directory("dir/c")
        d3 = Directory("dir/a")

        d1.createTree()
        d2.createTree()

        self.assertTrue(d.containsFolder("a"))
        self.assertTrue(d.containsFolder("c"))
        self.assertFalse(d.containsFolder("b"))
        self.assertTrue(d3.containsFolder("b"))
        self.assertFalse(d3.containsFolder("a"))

        d.removeDir()
Exemplo n.º 19
0
 def test_createTree(self):
     d = Directory("dir/fg/a")
     self.assertFalse(d.exists())
 
     with self.assertRaises(FileNotFoundError):
         d.createDir()
         
     self.assertFalse(d.exists())
 
     d.createTree()
     self.assertTrue(d.exists())
 
     d.removeFile()
     self.assertTrue(d.exists())
 
     d.removeDir()
     self.assertFalse(d.exists())
 
     d = Directory("dir")
     self.assertTrue(d.exists())
 
     d.removeDir()
     self.assertFalse(d.exists())
Exemplo n.º 20
0
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", "at", "and", "in", "of", "on", "the"]
    
    def __init__(self, path="", fields=None):
        
        if(isinstance(path, MyPath)):
            self._path = path
        else:
            self._path = MyPath(path)
            
        if(fields is None):
            fields = DocumentFields()
            
        self._fields = fields
        self._directory = Directory(self._path)

    def getDirectory(self):
        return self._directory
        
    def getPath(self):
        return self._path
        
    def setPath(self, path):
        if (path.__class__ == MyPath):
            self._path = path
        else:
            self._path = MyPath(path)

    def getDocFields(self, fields):
        return self._fields

    def setDocFields(self, fields):
        self._fields = fields
        
    path = property(getPath, setPath)
        
    def _checkDirectoryExists(self):
        if not self._directory.exists():
            raise IOError("Document directory does not exist anymore")

    # The name is computed from the title of the document, obtained in the fields.
    def computeFolderName(self):
        name = ""
        if (self._fields.hasTitle()):
            title = self._fields.getField("title")
            title = ''.join(ch for ch in title if ch.isalnum() or ch.isspace())
            words = title.split()
            for i, w in enumerate(words):
                if (w not in self.wordsRemoved): 
                    name += w
                    if (i < len(words)-1):
                        name += "_"
            name = name.lower()
            
        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 (not self._fields.hasTitle()):
            raise RuntimeError("Error : attempt to create a document folder without title")
            return
        
        name = self.computeFolderName()
        self._path = MyPath(path + "/" + name)
        self._path = self._path.findNotUsedPath()
        folder = Directory(self._path)
        folder.createDir()
        
    def containsDocumentFiles(self):
        self._checkDirectoryExists()

    def createInternFolder(self, name):
        self._checkDirectoryExists()
        
        directory = Directory(self._path.getAbsolutePath() + "/" + name)
        directory.createDir()
        
    def delete(self):
        Directory(self._path).removeDir()
        
    def readInfos(self):
        self._checkDirectoryExists()
        
        infofile = Infofile()
        success = infofile.readContent(self.infofilePath())
        
        files = self._directory.getContentList()
        
        if(success):
            print("title : ", infofile.title)
            print("Files : ")
            for f in files:  
                print(MyPath(f).filename)
        
    def organize(self, filesToMoveIn=None):
        self._checkDirectoryExists()

        notesPath = self._path.getAbsolutePath()
        latexNotes = LatexNotes(self._fields)
        latexNotes.createAllContent()
        latexNotes.writeContent(notesPath)
        
        title = self._fields.getField("title")
        infofile = Infofile()
        infofile.title = title
        infofile.writeContent(self.infofilePath())

        if(filesToMoveIn is not None):
            for file in filesToMoveIn:
                f = file
                if(type(file) != File):
                    f = File(file)
                f.moveFile(self._path.getAbsolutePath())

    def getNotesFilePath(self):
        path = self._path.getAbsolutePath() + "/" + LatexNotes.FILENAME
        return path
    
    def infofilePath(self):
        return MyPath(self._path.path + "/" + "info")

    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):
        self._checkDirectoryExists()
        return Directory(self._path).containsFolder(self.VIDEOS_DIR_NAME)

    def hasImageFolder(self):
        self._checkDirectoryExists()
        return Directory(self._path).containsFolder(self.IMAGES_DIR_NAME)

    def hasNotesFolder(self):
        self._checkDirectoryExists()
        return Directory(self._path).containsFolder(self.NOTES_DIR_NAME)

    def hasDescFolder(self):
        self._checkDirectoryExists()
        return Directory(self._path).containsFolder(self.DESCRIPTION_DIR_NAME)
Exemplo n.º 21
0
 def createInternFolder(self, name):
     self._checkDirectoryExists()
     
     directory = Directory(self._path.getAbsolutePath() + "/" + name)
     directory.createDir()