def addManyItemsRecursively(tool, dialogs, listOfPaths): ''' Creates and saves in repository a number of items linked with files defined by given listOfPaths. listOfPaths is a list of paths to files and directories. For every file an Item is created. Every directory is scanned recursively and for every file found an Item is created. Returns a tuple (itemsCreatedCount, filesSkippedCount, listOfSavedItemIds) or raises an exception. ''' items = [] for path in listOfPaths: path = os.path.normpath(path) if os.path.isfile(path): file = path item = db.Item(user_login=tool.user.login) item.title, _ = os.path.splitext(os.path.basename(file)) srcAbsPath = os.path.abspath(file) item.data_ref = db.DataRef( objType=db.DataRef.FILE, url=srcAbsPath) #DataRef.url can be changed in ItemsDialog item.data_ref.srcAbsPath = srcAbsPath item.data_ref.srcAbsPathToRoot = os.path.dirname(file) # item.data_ref.dstRelPath will be set by ItemsDialog items.append(item) elif os.path.isdir(path): dirPath = path for root, _dirs, files in os.walk(dirPath): if os.path.relpath(root, dirPath) == ".reggata": continue for file in files: item = db.Item(user_login=tool.user.login) item.title, _ = os.path.splitext( os.path.basename(file)) srcAbsPath = os.path.join(root, file) item.data_ref = db.DataRef( objType=db.DataRef.FILE, url=srcAbsPath ) #DataRef.url can be changed in ItemsDialog item.data_ref.srcAbsPath = srcAbsPath item.data_ref.srcAbsPathToRoot = os.path.join( dirPath, "..") # item.data_ref.dstRelPath will be set by ItemsDialog items.append(item) else: logger.info("Skipping path '{}'".format(path)) if not dialogs.execItemsDialog(items, tool.gui, tool.repo, ItemsDialog.CREATE_MODE, sameDstPath=False): raise err.CancelOperationError("User cancelled operation.") thread = CreateGroupOfItemsThread(tool.gui, tool.repo, items) dialogs.startThreadWithWaitDialog(thread, tool.gui, indeterminate=False) return (thread.createdCount, thread.skippedCount, thread.lastSavedItemIds)
def test_saveNewItemWithFileOutsideRepo(self): ''' User wants to add an external file into the repo. File is copied to the repo. ''' item = db.Item("user", "Item's title") self.srcAbsPath = os.path.abspath( os.path.join(self.repo.base_path, "..", "tmp", "file.txt")) self.dstRelPath = os.path.join("dir1", "dir2", "dir3", "newFile.txt") try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item, self.srcAbsPath, self.dstRelPath) self.savedItemId = uow.executeCommand(cmd) finally: uow.close() try: uow = self.repo.createUnitOfWork() savedItem = uow.executeCommand( cmds.GetExpungedItemCommand(self.savedItemId)) self.assertEqual(savedItem.title, item.title) self.assertIsNotNone(savedItem.data_ref) self.assertEqual(savedItem.data_ref.url_raw, hlp.to_db_format(self.dstRelPath)) self.assertTrue( os.path.exists( os.path.join(self.repo.base_path, savedItem.data_ref.url))) self.assertTrue(os.path.exists(self.srcAbsPath)) finally: uow.close()
def test_saveNewItemWithACopyOfAStoredFileInRepo(self): ''' User wants to add a copy of a stored file from the repo into the same repo but to the another location. The copy of the original file will be attached to the new Item object. ''' item = db.Item("user", "Item's title") self.srcAbsPath = os.path.abspath( os.path.join(self.repo.base_path, "lyrics", "led_zeppelin", "stairway_to_heaven.txt")) self.dstRelPath = os.path.join("dir1", "dir2", "dir3", "copy_of_stairway_to_heaven.txt") try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item, self.srcAbsPath, self.dstRelPath) self.savedItemId = uow.executeCommand(cmd) finally: uow.close() try: uow = self.repo.createUnitOfWork() savedItem = uow.executeCommand( cmds.GetExpungedItemCommand(self.savedItemId)) self.assertEqual(savedItem.title, item.title) self.assertIsNotNone(savedItem.data_ref) self.assertEqual(savedItem.data_ref.url_raw, hlp.to_db_format(self.dstRelPath)) self.assertTrue( os.path.exists( os.path.join(self.repo.base_path, savedItem.data_ref.url))) self.assertTrue(os.path.exists(self.srcAbsPath)) finally: uow.close()
def test_saveNewItemWithFileInsideRepo(self): ''' There is an untracked file inside the repo tree. User wants to add such file into the repo to make it a stored file. File is not copied, because it is alredy in the repo tree. It's essential that srcAbsPath and dstRelPath point to the same file. ''' item = db.Item("user", "Item's title") self.srcAbsPath = os.path.abspath( os.path.join(self.repo.base_path, "this", "is", "untracked", "file.txt")) self.dstRelPath = os.path.join("this", "is", "untracked", "file.txt") try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item, self.srcAbsPath, self.dstRelPath) self.savedItemId = uow.executeCommand(cmd) finally: uow.close() try: uow = self.repo.createUnitOfWork() savedItem = uow.executeCommand( cmds.GetExpungedItemCommand(self.savedItemId)) self.assertEqual(savedItem.title, item.title) self.assertIsNotNone(savedItem.data_ref) self.assertEqual(savedItem.data_ref.url_raw, hlp.to_db_format(self.dstRelPath)) self.assertTrue( os.path.exists( os.path.join(self.repo.base_path, savedItem.data_ref.url))) finally: uow.close()
def addManyItems(tool, dialogs, files): ''' Creates and saves in repository items linked with given list of files (filenames). Returns a tuple (itemsCreatedCount, filesSkippedCount, listOfSavedItemIds) or raises an exception. ''' if len(files) <= 1: raise ValueError("You should give more than one file.") items = [] for file in files: file = os.path.normpath(file) item = db.Item(user_login=tool.user.login) item.title, _ = os.path.splitext(os.path.basename(file)) item.data_ref = db.DataRef( objType=db.DataRef.FILE, url=file) #DataRef.url can be changed in ItemsDialog item.data_ref.srcAbsPath = file items.append(item) if not dialogs.execItemsDialog(items, tool.gui, tool.repo, ItemsDialog.CREATE_MODE, sameDstPath=True): raise err.CancelOperationError("User cancelled operation.") thread = CreateGroupOfItemsThread(tool.gui, tool.repo, items) dialogs.startThreadWithWaitDialog(thread, tool.gui, indeterminate=False) return (thread.createdCount, thread.skippedCount, thread.lastSavedItemIds)
def test_saveNewItemByEmptyUserLogin(self): userLogin = "" item = db.Item(userLogin, "Item's title") try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item) self.assertRaises(err.AccessError, uow.executeCommand, (cmd)) finally: uow.close()
def test_saveNewMinimalItem(self): # "Minimal item" here means that this item has no data references, tags or fields item = db.Item("user", "Minimal Item") try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item) self.savedItemId = uow.executeCommand(cmd) finally: uow.close() try: uow = self.repo.createUnitOfWork() savedItem = uow.executeCommand( cmds.GetExpungedItemCommand(self.savedItemId)) self.assertEqual(savedItem.title, item.title) finally: uow.close()
def addSingleItem(tool, dialogs, file=None): ''' Creates and saves in repo an Item linked with a given file (or without file). Returns id of saved Item, or raises an exception. ''' savedItemId = None item = db.Item(user_login=tool.user.login) if not hlp.is_none_or_empty(file): file = os.path.normpath(file) item.title, _ = os.path.splitext(os.path.basename(file)) item.data_ref = db.DataRef(objType=db.DataRef.FILE, url=file) if not dialogs.execItemDialog(item=item, gui=tool.gui, repo=tool.repo, dialogMode=ItemDialog.CREATE_MODE): raise err.CancelOperationError("User cancelled operation.") uow = tool.repo.createUnitOfWork() try: srcAbsPath = None dstRelPath = None if item.data_ref is not None: srcAbsPath = item.data_ref.srcAbsPath dstRelPath = item.data_ref.dstRelPath cmd = cmds.SaveNewItemCommand(item, srcAbsPath, dstRelPath) thread = BackgrThread(tool.gui, uow.executeCommand, cmd) dialogs.startThreadWithWaitDialog(thread, tool.gui, indeterminate=True) savedItemId = thread.result finally: uow.close() return savedItemId
def test_saveNewItemWithFields(self): userLogin = "******" item = db.Item(userLogin, "Item with fields") fieldOne = ("Year", 2012) fieldTwo = ("No items in test repo with such Field!", "Some value") item.setFieldValue(fieldOne[0], fieldOne[1], userLogin) item.setFieldValue(fieldTwo[0], fieldTwo[1], userLogin) try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item) savedItemId = uow.executeCommand(cmd) finally: uow.close() savedItem = self.getExistingItem(savedItemId) self.assertEqual(savedItem.title, item.title) self.assertTrue(savedItem.hasField(fieldOne[0], fieldOne[1])) self.assertTrue(savedItem.hasField(fieldTwo[0], fieldTwo[1])) self.assertEqual(len(savedItem.item_fields), 2)
def test_saveNewItemWithTags(self): userLogin = "******" item = db.Item(userLogin, "Item with tags") tagNameThatExistsInRepo = "Lyrics" tagNameNew = "No items in test repo with such Tag!" item.addTag(tagNameThatExistsInRepo, userLogin) item.addTag(tagNameNew, userLogin) try: uow = self.repo.createUnitOfWork() cmd = cmds.SaveNewItemCommand(item) savedItemId = uow.executeCommand(cmd) finally: uow.close() savedItem = self.getExistingItem(savedItemId) self.assertEqual(savedItem.title, item.title) self.assertTrue(savedItem.hasTag(tagNameThatExistsInRepo)) self.assertTrue(savedItem.hasTag(tagNameNew)) self.assertEqual(len(savedItem.item_tags), 2)