def remove(self): d = Directory("dir") d.removeFile() assert(d.exists()) d.removeDir() assert(not d.exists())
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())
def create(self): d = Directory("dir") assert(not d.exists()) d.createDir() assert(d.exists()) assert(not d.isRegularFile()) assert(d.isDirectory())
def test_remove(self): d = Directory("dir") d.createDir() d.removeFile() self.assertTrue(d.exists()) d.removeDir() self.assertFalse(d.exists())
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 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)
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())
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())
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)