示例#1
0
def spec_parse(parser: ArgumentParser) -> SPEC:
    if (fzf_argv := environ.get(ARGV_ENV)) is not None:
        cmd = _parse_args().cmd
        argv = fzf_argv.split(EOF) if fzf_argv else ()

        if cmd.startswith(PREVIEW_HEAD):
            mode = Mode.preview
            tmp = Path(removeprefix(cmd, PREVIEW_HEAD))
        elif cmd.startswith(EXECUTE_HEAD):
            mode = Mode.execute
            tmp = Path(removeprefix(cmd, EXECUTE_HEAD))
        else:
            assert False, cmd

        args = parser.parse_args(argv)
        lines = tmp.read_text().rstrip("\0").split("\0")
        return mode, lines, args
示例#2
0
 def cont() -> Iterator[str]:
     stripped = removesuffix(removeprefix(pattern[1:-1], "^"), "$").strip()
     it = iter(stripped)
     for c in it:
         if c == "\\":
             nc = next(it, "")
             if nc in {"/", "\\"}:
                 yield nc
         else:
             yield c
示例#3
0
文件: __main__.py 项目: ms-jpq/py-dev
def _p_uri(uri: str, branch: str) -> str:
    github_prefix = "[email protected]:"

    if urlsplit(uri).scheme in {"http", "https"}:
        return uri
    elif uri.startswith(github_prefix):
        location = removesuffix(removeprefix(uri, github_prefix), ".git")
        q_location, q_branch = quote(location), quote(branch)
        return f"https://github.com/{q_location}/tree/{q_branch}"
    else:
        raise ValueError(f"Cannot parse {uri} into https://...")
示例#4
0
文件: git.py 项目: ms-jpq/chadtree
    def cont() -> Iterator[Tuple[str, PurePath]]:
        it = iter(stdout)
        sub_module = ROOT
        acc: MutableSequence[str] = []

        for char in it:
            if char == linesep:
                line = "".join(acc)
                acc.clear()

                if not line.startswith(_GIT_SUBMODULE_MARKER):
                    raise ValueError(stdout)
                else:
                    quoted = removeprefix(line, prefix=_GIT_SUBMODULE_MARKER)
                    if not (quoted.startswith("'") and quoted.endswith("'")):
                        raise ValueError(stdout)
                    else:
                        sub_module = PurePath(
                            removesuffix(removeprefix(quoted, prefix="'"), suffix="'")
                        )
                        yield _SUBMODULE_MARKER, sub_module

            elif char == "\0":
                line = "".join(acc)
                acc.clear()

                if not sub_module:
                    raise ValueError(stdout)
                else:
                    prefix, file = line[:2], line[3:]
                    yield prefix, sub_module / file

                    if "R" in prefix:
                        next(it, None)
            else:
                acc.append(char)
示例#5
0
def _preprocess(context: Context, doc: Doc) -> Doc:
    sep = "```"
    if doc.syntax == "markdown":
        esc_text = unescape(doc.text)
        split = esc_text.splitlines()

        if (split and split[0].startswith(sep) and split[-1].startswith(sep)
                and not sum(line.startswith(sep) for line in split[1:-1])):
            text = linesep.join(split[1:-1])
            ft = removeprefix(split[0], prefix=sep).strip()
            syntax = ft if ft.isalnum() else context.filetype
            return Doc(text=text, syntax=syntax)
        else:
            return Doc(text=esc_text, syntax=doc.syntax)
    else:
        return doc