From b70a69b42282c2f04c3b213b8281d37de03e5e5c Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 22 Sep 2019 23:00:25 -0400 Subject: [PATCH] bakefile --- Bakefile | 17 +++++++++++++++-- bake/scripts/__init__.py | 2 ++ bake/scripts/indent.py | 12 ++++++------ bake/scripts/red.py | 22 +++++++++++++++++++--- bake/scripts/redless.py | 16 ++++++++++++++++ bake/scripts/step.py | 33 +++++++++++++++++++++++++++++++++ setup.py | 7 +++++-- 7 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 bake/scripts/redless.py create mode 100644 bake/scripts/step.py diff --git a/Bakefile b/Bakefile index ebc41da..efac824 100644 --- a/Bakefile +++ b/Bakefile @@ -58,11 +58,24 @@ ci: ci/setup ci/setup: @skip:key=Pipfile.lock pipenv install --dev --deploy --system -random/python: +random/ip: #!/usr/bin/env python import requests - print(requests) + r = requests.get('https://httpbin.org/ip') + print(r.json()['origin'].split(',')[0]) +random/clis: + bake:red 'Testing sub–commands.' + bake:step 'sub-task' + echo 'I should *not* be red.' | bake:red | bake:indent | bake:redless + echo 'I *should* be red.' | bake:red --always | bake:indent + echo "$(echo test $(bake:red test) test | bake:indent)" + echo + +random/kr: + sparkescakesparkles="✨ 🍰 ✨" | pbcopy + echo "$sparkescakesparkles" | pbcopy + echo 'KR Copied!' | bake:red --fg cyan lazy_brew() { set -e diff --git a/bake/scripts/__init__.py b/bake/scripts/__init__.py index c543ad7..4ba9f04 100644 --- a/bake/scripts/__init__.py +++ b/bake/scripts/__init__.py @@ -1 +1,3 @@ from . import red +from . import indent +from . import step diff --git a/bake/scripts/indent.py b/bake/scripts/indent.py index 44a9a48..fe63f66 100644 --- a/bake/scripts/indent.py +++ b/bake/scripts/indent.py @@ -4,17 +4,17 @@ import click @click.command(context_settings=dict(help_option_names=["-h", "--help"])) -@click.option( - "--only-stdout", is_flag=True, type=click.BOOL, default=False, help="Use stdout." -) @click.option( "--read-stderr", is_flag=True, type=click.BOOL, default=True, help="Read stderr." ) @click.option("--char", nargs=1, type=click.STRING, default="|", help="Prefix char.") -def indent(*, char, read_stderr, only_stdout): +def entrypoint(*, char, read_stderr): pipe = sys.stdin if not read_stderr else sys.stderr for line in pipe: - click.echo(f" {char} ", err=(not only_stdout), nl=False) - click.echo(line, nl=False) + if line: + print(f" {char} ", end="") + print(line.rstrip()) + else: + print(f" {char} ", end="") diff --git a/bake/scripts/red.py b/bake/scripts/red.py index 54d9d47..ecce34b 100644 --- a/bake/scripts/red.py +++ b/bake/scripts/red.py @@ -1,11 +1,19 @@ import sys import click +import colorama @click.command(context_settings=dict(help_option_names=["-h", "--help"])) @click.argument("s", type=click.STRING, default=False, required=False) @click.option("--err", is_flag=True, type=click.BOOL, default=False, help="Use stderr.") +@click.option( + "--always", + is_flag=True, + type=click.BOOL, + default=False, + help="Always speak technicolor.", +) @click.option( "--fg", nargs=1, type=click.STRING, default="red", help="Foreground color to use." ) @@ -17,11 +25,19 @@ import click help="Background color to use (rare).", ) @click.option("--bold", is_flag=True, type=click.BOOL, default=False, help="Be bold.") -def red(s, *, fg, bg, bold, err): +def entrypoint(s, *, fg, bg, bold, err, always): + if always: + # Don't strip colors. + colorama.init(strip=False) + if s is False: s = sys.stdin.read() + s = s.rstrip() + try: - click.echo(click.style(s, fg=fg, bg=bg), err=err, nl=False) + s = click.style(s, fg=fg, bg=bg) except TypeError: - click.echo(click.style(s), err=err, nl=False) + pass + + print(s) diff --git a/bake/scripts/redless.py b/bake/scripts/redless.py new file mode 100644 index 0000000..582438f --- /dev/null +++ b/bake/scripts/redless.py @@ -0,0 +1,16 @@ +import sys + +import click + + +@click.command(context_settings=dict(help_option_names=["-h", "--help"])) +@click.argument("s", type=click.STRING, default=False, required=False) +@click.option("--err", is_flag=True, type=click.BOOL, default=False, help="Use stderr.") +def entrypoint(s, *, err): + + if s is False: + s = sys.stdin.read() + + s = s.rstrip() + + click.echo(s) diff --git a/bake/scripts/step.py b/bake/scripts/step.py new file mode 100644 index 0000000..9c61f04 --- /dev/null +++ b/bake/scripts/step.py @@ -0,0 +1,33 @@ +# + Executing random/entrypoints: + +import sys + +import colorama +import click + +# Don't strip colors. +colorama.init(strip=False) + + +@click.command(context_settings=dict(help_option_names=["-h", "--help"])) +@click.argument("s", type=click.STRING, default=False, required=False) +@click.option( + "--read-stderr", is_flag=True, type=click.BOOL, default=True, help="Read stderr." +) +@click.option("--char", nargs=1, type=click.STRING, default="+", help="Prefix char.") +@click.option( + "--color", nargs=1, type=click.STRING, default="yellow", help="Color to use." +) +def entrypoint(s, *, char, read_stderr, color): + + pipe = sys.stdin if not read_stderr else sys.stderr + if s is False: + s = pipe.read() + + for line in s.strip().split("\n"): + try: + title = str(click.style(line, fg=color)) + except TypeError: + title = line + + print(f" + {title}: ") diff --git a/setup.py b/setup.py index 6cbadc2..7de01af 100644 --- a/setup.py +++ b/setup.py @@ -106,8 +106,10 @@ setup( entry_points={ "console_scripts": [ "bake=bake.cli:entrypoint", - "bake:red=bake.scripts.red:red", - "bake:indent=bake.scripts.indent:indent", + "bake:red=bake.scripts.red:entrypoint", + "bake:redless=bake.scripts.redless:entrypoint", + "bake:indent=bake.scripts.indent:entrypoint", + "bake:step=bake.scripts.step:entrypoint", ] }, install_requires=REQUIRED, @@ -121,6 +123,7 @@ setup( "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", ],