示例#1
0
def get_size(of_item: str = '.',
             unit: str = 'by',
             precision: int = 1) -> Tuple[float, str]:
    assert_exists(of_item)
    assert_valid_arg(unit, VALID_UNITS)

    item_full_path = get_full_path(of_item)
    item_trail = pc_path.trail(item_full_path)
    file_size_in_bytes = os_path.getsize(of_item)
    if is_dir(item_full_path):
        for directory, contents in traverse_contents(item_full_path):
            dir_path = pc_path.cat(item_trail, directory)
            file_size_in_bytes += os_path.getsize(of_item)
            for file in contents:
                try:
                    file_size_in_bytes += os_path.getsize(
                        pc_path.cat(dir_path, file))

                except FileNotFoundError:
                    pass

    if unit == 'auto':
        unit_factor = int(log(file_size_in_bytes) / log(1024))
        unit = UNIT_CONVERSION_MAP.get(unit_factor, 4)

    converted_size = round(
        float(file_size_in_bytes /
              (1024**UNIT_CONVERSION_MAP_REVERSED.get(unit, 'tb'))), precision)

    return converted_size, unit
示例#2
0
def find(item_name: str, in_dir: str = '.', max_depth: int = INF) -> str:
    item_name = item_name.lower()
    for directory, contents in traverse_contents(of_dir=in_dir,
                                                 max_depth=max_depth,
                                                 skip_empty=True):
        for item in contents:
            if item.lower() == item_name:
                return pc_path.cat(directory, item)
示例#3
0
def _change_perms(of_item: str, to_perm: Permission, for_party: Party,
                  recursively: bool) -> NoReturn:
    _change_item_perms(of_item, to_perm, for_party)

    if recursively and os_path.isdir(of_item):
        for directory, contents in os.walk(of_item):
            _change_item_perms(of_item=directory,
                               to_perm=to_perm,
                               for_party=for_party)
            for item in contents:
                _change_item_perms(of_item=pc_path.cat(directory, item),
                                   to_perm=to_perm,
                                   for_party=for_party)
示例#4
0
def find_all(items_with_name: str,
             in_dir: str = '.',
             max_depth: int = INF) -> List[str]:
    matches = []
    items_with_name = items_with_name.lower()
    for directory, contents in traverse_contents(of_dir=in_dir,
                                                 max_depth=max_depth,
                                                 skip_empty=True):
        for item in contents:
            if item.lower() == items_with_name:
                matches.append(pc_path.cat(directory, item))

    return matches
示例#5
0
def delete(item: str, *items: str, from_dir='.') -> NoReturn:
    for file in (item, *items):
        try:
            file = pc_path.cat(from_dir, file)
            if is_dir(file):
                shutil.rmtree(file, ignore_errors=True)
            else:
                Path(file).unlink()

            assert_not_exists(file)

        except FileExistsError:
            raise PermissionError()
示例#6
0
def _preprocess(item: str,
                destination: str,
                mode: str,
                make_hidden: bool = False) -> str:
    assert_valid_arg(mode, VALID_MODES)
    destination = str(Path(destination).resolve())
    item_base = pc_path.base(item)
    target_path = pc_path.cat(destination, item_base)

    if make_hidden:
        target_path = pc_path.hide(target_path)

    if already_exists(target_path):
        if mode == 'o':
            delete(item, from_dir=destination)
        elif mode == 'a':
            target_path = pc_path.increment_base(target_path)
            while already_exists(target_path):
                target_path = pc_path.increment_base(target_path)
        else:
            raise FileExistsError()

    return target_path
示例#7
0
def copy_contents(of_dir: str, to_dir: str, mode: str = 'x') -> List[str]:
    dir_contents = tuple(
        [pc_path.cat(of_dir, item) for item in get_contents(of_dir)])
    return copy_items(*dir_contents, to_dir=to_dir, mode=mode)