Example #1
0
def move(
    query_args: QueryArgs,
    dest_path: str,
    kill_tmux: bool,
    transaction: Transaction,
    db: DataBase,
):
    dest_path_is_dir = any(
        [dest_path == ".", f"{dest_path}/%" in db,
         dest_path.endswith("/")])

    for src_pattern in query_args.patterns:
        dest_to_src = defaultdict(list)
        src_entries = db.get(**query_args._replace(
            patterns=[src_pattern])._asdict())

        for entry in src_entries:

            # parent, grandparent, great-grandparent, etc.
            parents = [str(entry.path)
                       ] + [str(p) + "/" for p in entry.path.parents]
            matches = [
                p for p in reversed(parents) if like(str(p),
                                                     str(src_pattern) + "%")
            ]

            head = next(iter(matches))  # a/b/% -> a/b
            tail = PurePath(*[PurePath(m).name
                              for m in matches])  # a/b/% -> c/d

            if dest_path_is_dir:
                dest = PurePath(dest_path, tail)
            else:
                dest = str(entry.path).replace(head.rstrip("/"), dest_path, 1)
            dest_to_src[dest] += [entry.path]

        for dest, srcs in dest_to_src.items():
            for i, src in enumerate(srcs):
                if len(srcs) > 1:
                    dest = PurePath(dest, str(i))
                else:
                    dest = PurePath(dest)
                transaction.move(src=src, dest=dest, kill_tmux=kill_tmux)
                if dest in db:
                    transaction.remove(dest)
Example #2
0
def move(query_args: QueryArgs, dest_path: str, kill_tmux: bool,
         transaction: Transaction, db: DataBase):
    dest_path_is_dir = any([
        dest_path == PurePath('.'), f'{dest_path}/%' in db,
        str(dest_path).endswith('/')
    ])
    if dest_path_is_dir:
        dest_path = add_slash(dest_path)

    for src_pattern in query_args.patterns:
        dest_to_src = defaultdict(list)
        src_entries = db.get(**query_args._replace(
            patterns=[src_pattern])._asdict())
        for entry in src_entries:
            parents = [entry.path] + [str(p) + '/' for p in entry.path.parents]
            matching = None
            for p in parents:
                if like(str(p), str(src_pattern) + '%'):
                    matching = PurePath(p)
            if matching is None:
                raise RuntimeError(
                    f'Somehow, {entry.path} does not match with {src_pattern}.'
                )

            part_to_replace = add_root(matching)

            if dest_path_is_dir:
                part_to_replace = add_root(add_slash(src_pattern.parent))
            path = add_root(entry.path)

            dest = path.replace(str(part_to_replace), str(dest_path))
            dest_to_src[dest] += [entry.path]

        for dest, srcs in dest_to_src.items():
            for i, src in enumerate(srcs):
                if len(srcs) > 1:
                    dest = PurePath(dest, str(i))
                else:
                    dest = PurePath(dest)
                transaction.move(src=src, dest=dest, kill_tmux=kill_tmux)
                if dest in db:
                    transaction.remove(dest)