Exemplo n.º 1
0
 def doAction(self):
     selection = self.parent().selection
     insertPending = False
     self.level().stack.beginMacro(self.tr('delete elements'))
     files = list(selection.files())
     if selection.singleWrapper() and selection.hasContainers():
         container = selection.wrappers()[0]
         container.loadContents(recursive=True)
         if container.parent and container.parent.isContainer() and container.hasContents():
             ans = dialogs.question(self.tr('Replace by children?'),
                              self.tr('You have selected to remove an intermediate container. '
                              'Do you want to append its children to its parent?'))
             if ans:
                 insertPos = container.position
                 insertParent = container.parent.element
                 insertIndex = insertParent.contents.positions.index(insertPos)
                 insertChildren = [wrapper.element for wrapper in container.contents]
                 insertPending = True
     self.level().deleteElements(selection.elements())
     if insertPending:
         self.level().insertContentsAuto(insertParent, insertIndex, insertChildren)
     self.level().stack.endMacro()
     if self.allowDisk and len(files) > 0:
         from maestro.gui.dialogs import DeleteDialog
         dialog = DeleteDialog(files, self.parent())
         if dialog.exec_() == QtWidgets.QDialog.Accepted:
             self.level().deleteElements(files, fromDisk=True)
Exemplo n.º 2
0
 def _handleDeleteButton(self):
     """Ask the user again and delete the current profile."""
     if self.profile is not None and dialogs.question(
             self.tr("Delete profile"),
             self.tr("Should the profile '{}' really be deleted?").format(self.profile.name),
             parent=self):
         self.category.deleteProfile(self.profile)
Exemplo n.º 3
0
 def canClose(self):
     if self.editor.model().containsUncommitedData():
         return dialogs.question(
             self.tr("Unsaved changes"),
             self.tr(
                 "The editor contains uncommited changes. Really close?"))
     else:
         return True
Exemplo n.º 4
0
 def _handleDeleteButton(self):
     """Ask the user again and delete the current profile."""
     if self.profile is not None and dialogs.question(
             self.tr("Delete profile"),
             self.tr("Should the profile '{}' really be deleted?").format(
                 self.profile.name),
             parent=self):
         self.category.deleteProfile(self.profile)
Exemplo n.º 5
0
 def askSaveIfModified(self):
     """If the temporary profile is modified compared to the base profile it was copied from, ask the
     user to store it."""
     if self.baseProfile and self.profile != self.baseProfile:
         if dialogs.question(
                 self.tr('Save profile changes?'),
                 self.tr('You have modified the profile "{}". Do you want to store the changes?')
                         .format(self.baseProfile.name)):
             self.category.changeProfile(self.baseProfile, self.profile)
Exemplo n.º 6
0
 def askSaveIfModified(self):
     """If the temporary profile is modified compared to the base profile it was copied from, ask the
     user to store it."""
     if self.baseProfile and self.profile != self.baseProfile:
         if dialogs.question(
                 self.tr('Save profile changes?'),
                 self.
                 tr('You have modified the profile "{}". Do you want to store the changes?'
                    ).format(self.baseProfile.name)):
             self.category.changeProfile(self.baseProfile, self.profile)
Exemplo n.º 7
0
 def _triggered(self):
     if len(self.parent().selectedItems()) == 1:
         flag = self.parent().selectedItems()[0]
         number = self.parent().model.getElementCount(flag)
         if number > 0:
             question = self.tr(
                 "Do you really want to delete the flag '{}'? "
                 "It will be deleted from %n element(s).", None,
                 number).format(flag.name)
             if not dialogs.question(self.tr("Delete flag?"), question,
                                     self.parent()):
                 return
         flags.deleteFlagType(flag)
Exemplo n.º 8
0
def export(profile):
    if profile.criterion is not None:
        engine = search.SearchEngine()
        request = engine.searchAndBlock(db.prefix+"elements", profile.criterion)
    else:
        raise NotImplementedError()
    print("Found {} elements for export".format(len(request.result)))
    if len(request.result) == 0:
        dialogs.warning(translate("wtf", "No elements found"),
                        translate("wtf", "The given filter criterion does not match any elements."))
        return False
    
    exported = set()
    if profile.structure == STRUCTURE_FLAT or profile.delete:
        exportedPaths = set()
    toExport = levels.real.collect(request.result)
    while len(toExport) > 0:
        element = toExport.pop()
        if element.id in exported:
            continue
        exported.add(element.id)
        if element.isContainer():
            toExport.extend(levels.real.collect(element.contents))
            continue
        if element.url.scheme != 'file':
            print("I can only export regular files. Skipping", str(element.url))
            continue
        
        if profile.structure == STRUCTURE_FLAT:
            exportPath = os.path.basename(element.url.path)
            if exportPath in exportedPaths:
                exportPath, ext = os.path.splitext(exportPath)
                i = 1
                while exportPath+"-" + str(i) + ext in exportedPaths:
                    i += 1
                exportPath += "-" + str(i) + ext 
            assert exportPath not in exportedPaths
            exportedPaths.add(exportPath)
        else:
            exportPath = element.url.path
            if profile.delete:
                exportedPaths.add(exportPath)
        
        src = os.path.join(config.options.main.collection, element.url.path)
        dest = os.path.join(profile.path, exportPath)
        os.makedirs(os.path.dirname(dest), exist_ok=True)
        shutil.copyfile(src, dest)
    
    if profile.delete:
        toDelete = []
        for dirPath, dirNames, fileNames in os.walk(profile.path):
            dirPath = os.path.relpath(dirPath, profile.path)
            if dirPath == '.':
                dirPath = ''
            for filePath in fileNames:
                filePath = os.path.join(dirPath, filePath)
                if filePath not in exportedPaths:
                    toDelete.append(os.path.join(profile.path, filePath))

        if len(toDelete) > 0 and \
                dialogs.question(translate("wtf", "Delete files?"),
                                 translate("wtf",
                                           "The target folder contains %n file(s) that have not been"
                                           " exported. Should they be deleted?",
                                           '', QtCore.QCoreApplication.CodecForTr, len(toDelete))):
            for filePath in toDelete:
                os.remove(filePath)
                # Delete empty directories
                dirPath = os.path.dirname(filePath)
                while len(os.listdir(dirPath)) == 0:
                    assert len(dirPath) > len(profile.path) # profile.path should never be empty
                    os.rmdir(dirPath)
                    dirPath = os.path.dirname(dirPath)
            
    return True
Exemplo n.º 9
0
def export(profile):
    if profile.criterion is not None:
        engine = search.SearchEngine()
        request = engine.searchAndBlock(db.prefix + "elements",
                                        profile.criterion)
    else:
        raise NotImplementedError()
    print("Found {} elements for export".format(len(request.result)))
    if len(request.result) == 0:
        dialogs.warning(
            translate("wtf", "No elements found"),
            translate(
                "wtf",
                "The given filter criterion does not match any elements."))
        return False

    exported = set()
    if profile.structure == STRUCTURE_FLAT or profile.delete:
        exportedPaths = set()
    toExport = levels.real.collect(request.result)
    while len(toExport) > 0:
        element = toExport.pop()
        if element.id in exported:
            continue
        exported.add(element.id)
        if element.isContainer():
            toExport.extend(levels.real.collect(element.contents))
            continue
        if element.url.scheme != 'file':
            print("I can only export regular files. Skipping",
                  str(element.url))
            continue

        if profile.structure == STRUCTURE_FLAT:
            exportPath = os.path.basename(element.url.path)
            if exportPath in exportedPaths:
                exportPath, ext = os.path.splitext(exportPath)
                i = 1
                while exportPath + "-" + str(i) + ext in exportedPaths:
                    i += 1
                exportPath += "-" + str(i) + ext
            assert exportPath not in exportedPaths
            exportedPaths.add(exportPath)
        else:
            exportPath = element.url.path
            if profile.delete:
                exportedPaths.add(exportPath)

        src = os.path.join(config.options.main.collection, element.url.path)
        dest = os.path.join(profile.path, exportPath)
        os.makedirs(os.path.dirname(dest), exist_ok=True)
        shutil.copyfile(src, dest)

    if profile.delete:
        toDelete = []
        for dirPath, dirNames, fileNames in os.walk(profile.path):
            dirPath = os.path.relpath(dirPath, profile.path)
            if dirPath == '.':
                dirPath = ''
            for filePath in fileNames:
                filePath = os.path.join(dirPath, filePath)
                if filePath not in exportedPaths:
                    toDelete.append(os.path.join(profile.path, filePath))

        if len(toDelete) > 0 and \
                dialogs.question(translate("wtf", "Delete files?"),
                                 translate("wtf",
                                           "The target folder contains %n file(s) that have not been"
                                           " exported. Should they be deleted?",
                                           '', QtCore.QCoreApplication.CodecForTr, len(toDelete))):
            for filePath in toDelete:
                os.remove(filePath)
                # Delete empty directories
                dirPath = os.path.dirname(filePath)
                while len(os.listdir(dirPath)) == 0:
                    assert len(dirPath) > len(
                        profile.path)  # profile.path should never be empty
                    os.rmdir(dirPath)
                    dirPath = os.path.dirname(dirPath)

    return True
Exemplo n.º 10
0
 def canClose(self):
     if self.editor.model().containsUncommitedData():
         return dialogs.question(self.tr("Unsaved changes"),
                                 self.tr("The editor contains uncommited changes. Really close?"))
     else:
         return True