-
Notifications
You must be signed in to change notification settings - Fork 4
/
mspaint.py
executable file
·197 lines (189 loc) · 5.86 KB
/
mspaint.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3
"""
Cross-platform launcher to replace and/or wrap 'mspaint'
Opens the image file specified in argv[1] if given;
otherwise, open the first `.bmp` file found in the
current working directory or a subdirectory.
Installation:
=============
Windows
-------
Simply run your desired script from the directory
containing this file, or otherwise copy `mspaint.py`
to a directory in your environment PATH variable.
You may also add the directory containing this
file (`mspaint.py`) to your PATH variable in
"Modify Environment Variables."
Ensure `.PY` is in your `PATHEXT` environment
variable and that Python >= 3.8 is installed.
Unix/Mac/Linux/Android
----------------------
### One-Step Run
Replace `./Hello_World_Italic.py` with the script
you want to run and execute in terminal:
```
PATH=$PWD:$PATH python3 ./Hello_World_Italic.py
```
### Installation
Simply copy this script into your
`~/.local/bin/` directory (or preferred `bin`
directory such as `/usr/local/bin/`) and ensure
the new copy has read+execute permissions,
e.g. in terminal:
```
cp mspaint.py ~/.local/bin/mspaint && \
chmod 0755 ~/.local/bin/mspaint
```
"""
from subprocess import CalledProcessError, check_output
from pathlib import Path
from sys import argv, exit, modules, platform, stderr
from urllib.parse import ParseResult, urlunparse
# The path tp the file we want to open
def mtime(p: Path) -> float:
return p.stat().st_mtime
def determine_file(*argv) -> Path:
glob: Optional[str] = None
file: Optional[Path] = None
if argv[1:]:
if Path(argv[1]).exists():
return Path(argv[1]).resolve().absolute()
glob = argv[1]
else:
glob = "*.bmp"
for f in sorted(
Path.cwd().rglob(glob),
key=mtime,
reverse=True,
):
return f
print(f"No file specified and no {glob} " f"found in {Path.cwd()!s}", file=stderr)
exit(1)
def is_in_pytest():
return any(
k in modules for k in (
"doctest",
"py.test",
"unittest",
)
)
file: Path = determine_file(argv)
print(file)
is_windows: bool = platform == "win32"
is_android: bool
try:
is_android = (
check_output(
["toolbox", "getprop", "ro.product.system.manufacturer"], encoding="UTF-8"
).strip()
== "Android"
)
except (CalledProcessError, FileNotFoundException):
is_android = False
if is_windows:
print(
check_output(
["cmd.exe", "/C", "start", "", "mspaint.exe", str(file)],
shell=False,
)
)
else:
if is_android:
# Prefixes that indicate file is on internal SD card
sd_prefixes = (
("data", "media"),
("storage", "emulated"),
("mnt", "shell", "emulated"),
)
# Adjust path to a globally-readable form if Android
# and path is on internal SD card
file = (
Path(
*(
"/",
"sdcard",
)
+ next(
file.parts[1 + len(sl) :]
for sl in sd_prefixes
if file.parts[1 : 1 + len(sl)] == sl
)[1:]
)
if any(file.parts[1 : 1 + len(sl)] == sl for sl in sd_prefixes)
else file
)
if not file.exists():
print(
"File does not exist: "
f"{str(file)!r} "
f"from {str(Path.cwd())!r}",
file=stderr
)
if not is_in_pytest():
file.chmod(0o777)
url: str = urlunparse(
ParseResult(
scheme="file",
netloc="",
path=file.as_posix(),
params="",
query="",
fragment="",
)
)
print(
check_output(
[
"am",
"broadcast",
"--user",
"0",
"-a",
"android.intent.action.MEDIA_SCANNER_SCAN_FILE",
"-c",
"android.intent.category.DEFAULT",
"-t",
"image/*",
"--grant-read-uri-permission",
"--grant-write-uri-permission",
"--grant-persistable-uri-permission",
"--grant-prefix-uri-permission",
"-d",
url,
]
)
)
print(
check_output(
[
"am",
"start",
"--user",
"0",
"-a",
"android.intent.action.VIEW",
"-c",
"android.intent.category.DEFAULT",
"-t",
"image/*",
"--grant-read-uri-permission",
"--grant-write-uri-permission",
"--grant-persistable-uri-permission",
"--grant-prefix-uri-permission",
"-d",
url,
]
)
)
else:
# Linux, Mac
if not is_in_pytest():
print(
check_output(
[
"open" if platform == "darwin" else "xdg-open",
file.absolute().as_posix(),
],
shell=False,
)
)