-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshortcuts.py
More file actions
87 lines (72 loc) · 2.44 KB
/
Copy pathshortcuts.py
File metadata and controls
87 lines (72 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""%there magic Python code shortcuts."""
# pylint: disable=invalid-name
from base64 import b64decode
from io import BytesIO, StringIO
import click
from herethere.there.commands import there_code_shortcut, there_group
from IPython.display import display
from PIL import Image as PILImage
KV_COMMAND_TEMPLATE = r"""
from window_here import load_kv_string
load_kv_string(r'''{code} ''', clear_style={clear_style})
"""
SCREENSHOT_COMMAND_TEMPLATE = """
import sys
from window_here import encoded_screenshot
sys.stderr.write(encoded_screenshot())
"""
PIN_COMMAND_TEMPLATE = """
from android_here import pin_shortcut
pin_shortcut(script="{script}", label="{label}")
"""
@there_code_shortcut
@click.option(
"-c", "--clear-style", is_flag=True, help="Unload previously applied rules"
)
def kv(code: str, clear_style: bool) -> str:
"""Insert given rules into the Kivy Language Builder."""
code = "# %%there ... \n" + code.replace("'''", '"""')
return KV_COMMAND_TEMPLATE.format(code=code, clear_style=clear_style)
@there_group.command()
@click.pass_context
@click.option(
"-w",
"--width",
type=int,
default=None,
help="Width in pixels to which to constrain a displayed image",
)
@click.option(
"-o",
"--output",
type=click.File(mode="wb"),
default=None,
help="Path to a local file to save screenshot as PNG image",
)
def screenshot(ctx, width, output):
"""Display the actual image of the Kivy window."""
out = StringIO()
err = StringIO()
ctx.obj.code = SCREENSHOT_COMMAND_TEMPLATE
ctx.obj.stdout = out
ctx.obj.stderr = err
ctx.obj.runcode()
data = b64decode(err.getvalue())
img = PILImage.open(BytesIO(data)).convert("RGB")
if width:
height = int(width * img.size[1] // img.size[0])
resampling = getattr(PILImage, "Resampling", PILImage)
resample_filter = getattr(resampling, "LANCZOS", PILImage.LANCZOS)
img = img.resize((width, height), resample_filter)
if output:
img.save(output)
output.close()
display(img)
@there_code_shortcut
@click.option("-l", "--label", type=str, default="", help="Label for shortcut")
@click.argument("script", nargs=1)
def pin(code: str, script: str, label: str) -> str: # pylint: disable=unused-argument
"""Create pinned shortcut to run a Python script from Android home screen."""
if not label:
label = script.split("/")[-1]
return PIN_COMMAND_TEMPLATE.format(script=script, label=label)