Exemplo n.º 1
0
 def _resolve_conflict(self, offended, offender, conflicted_name):
     if offended.is_container and offender.is_container:
         should_merge = self.on_should_merge(offender, offended)
         if should_merge:
             # There's a circular reference problem
             from .fs_utils import smart_move
             smart_move(offender, offended)
             offender.delete()
             return True
     return get_conflicted_name(self, conflicted_name)
Exemplo n.º 2
0
 def _resolve_conflict(self, offended, offender, conflicted_name):
     if offended.is_container and offender.is_container:
         should_merge = self.on_should_merge(offender, offended)
         if should_merge:
             # There's a circular reference problem
             from .fs_utils import smart_move
             smart_move(offender, offended)
             offender.delete()
             return True
     return get_conflicted_name(self, conflicted_name)
Exemplo n.º 3
0
    def save_to_xml(self, outfile):
        """Save results to ``outfile`` in XML.

        :param outfile: file object or path.
        """
        self.apply_filter(None)
        root = ET.Element('results')
        for g in self.groups:
            group_elem = ET.SubElement(root, 'group')
            dupe2index = {}
            for index, d in enumerate(g):
                dupe2index[d] = index
                try:
                    words = engine.unpack_fields(d.words)
                except AttributeError:
                    words = ()
                file_elem = ET.SubElement(group_elem, 'file')
                try:
                    file_elem.set('path', str(d.path))
                    file_elem.set('words', ','.join(words))
                except ValueError: # If there's an invalid character, just skip the file
                    file_elem.set('path', '')
                file_elem.set('is_ref', ('y' if d.is_ref else 'n'))
                file_elem.set('marked', ('y' if self.is_marked(d) else 'n'))
            for match in g.matches:
                match_elem = ET.SubElement(group_elem, 'match')
                match_elem.set('first', str(dupe2index[match.first]))
                match_elem.set('second', str(dupe2index[match.second]))
                match_elem.set('percentage', str(int(match.percentage)))
        tree = ET.ElementTree(root)

        def do_write(outfile):
            with FileOrPath(outfile, 'wb') as fp:
                tree.write(fp, encoding='utf-8')

        try:
            do_write(outfile)
        except IOError as e:
            # If our IOError is because dest is already a directory, we want to handle that. 21 is
            # the code we get on OS X and Linux, 13 is what we get on Windows.
            if e.errno in {21, 13}:
                p = str(outfile)
                dirname, basename = op.split(p)
                otherfiles = os.listdir(dirname)
                newname = get_conflicted_name(otherfiles, basename)
                do_write(op.join(dirname, newname))
            else:
                raise
        self.is_modified = False
Exemplo n.º 4
0
def smart_move(items, dest, allow_merge=True):
    """move items into dest by taking care of name conflicts.
    
    It is assumed that every item has an assigned parent.
    Don't include root items in items.
    """
    items = [item for item in items if item.parent not in items]
    for item in [item for item in items if item not in dest]:
        try:
            item.move(dest)
        except fs.AlreadyExistsError:
            merged = False
            if allow_merge:
                dest_item = dest[item.name]
                if dest_item.is_container and item.is_container:
                    smart_move(item, dest_item, True)
                    merged = True
            if not merged:
                item_name = conflict.get_unconflicted_name(item.name)
                item.move(dest, conflict.get_conflicted_name(dest, item_name))
Exemplo n.º 5
0
 def new_folder(self, parent):
     new_name = get_conflicted_name(parent, 'New Folder')
     new_folder = fs.manual.AutoMerge(parent, new_name)
     return new_folder.name
Exemplo n.º 6
0
 def do_move(song):
     dest_path = 'conflicts' + song.path[1:-1]
     dest = self.ignore_box.add_path(dest_path)
     song.move(dest, conflict.get_conflicted_name(dest,song.name))
Exemplo n.º 7
0
 def new_folder(self, parent):
     new_name = get_conflicted_name(parent, 'New Folder')
     new_folder = fs.manual.AutoMerge(parent, new_name)
     return new_folder.name