manualtools.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:skaff 作者: jhxie 项目源码 文件源码
def manpath_select(select: bool=True) -> Union[str, List[str]]:
    """
    Parses the output of the 'manpath' program and returns one of its non-empty
    results (non-empty directory) if 'select' is set to True; otherwise returns
    all the results un-altered.

    NOTE: A platform-dependent path separator will be appended to the result.
    """
    paths = None
    result_manpath = None

    with os.popen("manpath") as proc, TemporaryDirectory() as tmpdir:
        paths = proc.read().strip().split(os.pathsep)
        if select:
            for candidate in paths:
                # "Elect" the candidate directory with "rich" non-empty status
                if dircmp(candidate, tmpdir).left_only:
                    result_manpath = candidate
                    break

    if not paths:
        raise RuntimeError("Output of the 'manpath' program cannot be parsed")

    if select and not result_manpath:
        raise RuntimeError("All the directories in 'manpath' is empty")

    if select:
        if result_manpath.endswith(os.sep):
            return result_manpath
        else:
            return result_manpath + os.sep
    else:
        return [path + os.sep for path in paths if not path.endswith(os.sep)]
# -------------------------------- FUNCTIONS ----------------------------------
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号