コード例 #1
0
    def run_file(self, file: str) -> Union[tuple, str]:
        """Send code form file to the api and return the response."""
        args = helpers.get_args()
        stdin = helpers.get_stdin()

        try:
            with open(file, "r", encoding="utf-8") as f:
                code = f.read()

            if not any(file.endswith("." + ext) for ext in self.extensions):
                CONSOLE.print("File Extension language is not supported!",
                              style="bold red")
                helpers.close()

        except FileNotFoundError:
            CONSOLE.print("Path is invalid; File not found", style="bold red")
            helpers.close()

        language = self.extensions[file[file.rfind(".") + 1:]]

        payload = PistonQuery(
            language=language,
            code=code,
            args=args,
            stdin=stdin,
        )

        data = services.query_piston(CONSOLE, payload)

        if len(data["output"]) == 0:
            return "Your code ran without output.", language

        return data["output"].split("\n"), language
コード例 #2
0
    def ask_input(self, theme: str) -> Union[tuple, str]:
        """
        Make a multiline prompt for code input and send the code to the api.

        The compiled output from the api is returned.
        """
        language = helpers.get_lang()
        args = helpers.get_args()
        stdin = helpers.get_stdin()

        CONSOLE.print(
            "[green]Enter your code, (press esc + enter to run)[/green]")
        style = helpers.set_style(theme)

        CONSOLE.print()
        code = prompt(
            "",
            lexer=PygmentsLexer(lexers_dict[language]),
            include_default_pygments_style=False,
            style=style,
            multiline=True,
        )
        payload = PistonQuery(language=language,
                              args=args,
                              stdin=stdin,
                              code=code)

        data = services.query_piston(CONSOLE, payload)

        if len(data["output"]) == 0:
            return "Your code ran without output.", language

        return data["output"].split("\n"), language
コード例 #3
0
ファイル: shell.py プロジェクト: Piston-CLI/piston-cli
    def prompt(self) -> PistonQuery:
        """Prompt the user for code input."""
        code = self.prompt_session.prompt(style=self.style, )

        args = helpers.get_args(self.console)
        stdin = helpers.get_stdin(self.console)

        return PistonQuery(
            language=self.language,
            code=code,
            args=args,
            stdin=stdin,
        )
コード例 #4
0
    def ask_input(self) -> Optional[Union[tuple, str]]:
        """
        Make a multiline prompt for code input and send the code to the api.

        The compiled output from the api is returned.
        """
        language = helpers.get_lang()
        args = helpers.get_args()
        stdin = helpers.get_stdin()
        code = self.get_code()

        if not code:
            return

        payload = PistonQuery(language=language, args=args, stdin=stdin, code=code)
        data = services.query_piston(CONSOLE, payload)

        if len(data["output"]) == 0:
            return "Your code ran without output.", language

        return data["output"].split("\n"), language
コード例 #5
0
def run_link(ctx: click.Context, link: str,
             language: str) -> Optional[Union[list, str]]:
    """
    Make a multiline prompt for code input and send the code to the api.

    The compiled output from the api is returned.
    """
    console = ctx.obj["console"]
    args = helpers.get_args(console)
    stdin = helpers.get_stdin(console)
    code = get_code(console, link)

    if not code:
        return

    payload = PistonQuery(language=language, args=args, stdin=stdin, code=code)
    data = services.query_piston(ctx, console, payload)

    if len(data["output"]) == 0:
        return "Your code ran without output."

    return data["output"].split("\n")
コード例 #6
0
def run_file(ctx: click.Context,
             file: str,
             args: list[str] = None) -> Union[list, str]:
    """Send code form file to the api and return the response."""
    console = ctx.obj["console"]
    if not args:
        args = helpers.get_args(console)
    stdin = helpers.get_stdin(console)

    try:
        with open(file, "r", encoding="utf-8") as f:
            code = f.read()

        if not any(file.endswith("." + ext) for ext in lang_extensions):
            console.print("File Extension language is not supported!",
                          style="bold red")
            ctx.exit()

    except FileNotFoundError:
        console.print("Path is invalid; File not found", style="bold red")
        ctx.exit()

    language = lang_extensions[file[file.rfind(".") + 1:]]

    payload = PistonQuery(
        language=language,
        code=code,
        args=args,
        stdin=stdin,
    )

    data = services.query_piston(ctx, console, payload)

    if len(data["output"]) == 0:
        return "Your code ran without output."

    return data["output"].split("\n")