forked from supakeen/steck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
steck.py
98 lines (71 loc) · 2.04 KB
/
steck.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
import pathlib
from typing import Tuple, List
import click
import requests
from magic import Magic
mime_map = {
"text/plain": "text",
"text/x-python": "python",
}
def aggregate(
*passed_paths: str, recursive: bool = False
) -> List[pathlib.Path]:
"""Get all the paths passed as arguments and turn them into
pathlib.Paths."""
stack = []
for passed_path in passed_paths:
path = pathlib.Path(passed_path)
if path.is_file():
stack.append(path)
return stack
def guess(path: pathlib.Path) -> str:
"""Guess a lexer type based on a path."""
return mime_map.get(Magic(mime=True).from_file(str(path)), "text")
@click.group()
def main() -> None:
"""Steck, a pastebin client for pinnwand."""
return
@main.command()
@click.option(
"--confirm/--no-confirm",
default=True,
help="Enable or disable confirmation.",
)
@click.option(
"--magic/--no-magic",
default=True,
help="Enable or disable guessing file types.",
)
@click.argument("paths", nargs=-1)
def paste(confirm: bool, magic: bool, paths: Tuple[str]) -> None:
"""Paste some files matching a pattern."""
if not paths:
print("No paths found, did you forget to pass some?")
return
files = aggregate(*paths)
if confirm:
print(
f"You are about to paste the following {len(files)} files. Do you want to continue?"
)
for file in files:
print(f" - {file}")
if input("Continue? [y/N] ").lower() != "y":
return None
data = {
"expiry": "1day",
"files": [
{
"name": file.name,
"content": file.read_text(),
"lexer": guess(file) if magic else "text",
}
for file in files
],
}
response = requests.post(
"https://bpaste.net/api/v1/paste", json=data
).json()
print("View Paste", response["link"])
print("Remove Paste", response["removal"])
if __name__ == "__main__":
raise SystemExit(main())