-
Notifications
You must be signed in to change notification settings - Fork 6
/
steck.py
177 lines (144 loc) · 4.22 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
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
import pathlib
import subprocess
import sys
from typing import List, Tuple
import appdirs
import click
import requests
import toml
from magic import Magic
from termcolor import colored
mime_map = {
"text/plain": "text",
"text/x-python": "python",
}
configuration_path = (
pathlib.Path(appdirs.user_config_dir("steck")) / "steck.toml"
)
configuration = {
"base": "https://bpaste.net/",
"confirm": True,
"magic": True,
"ignore": True,
}
if configuration_path.exists():
with open(configuration_path) as f:
configuration.update(toml.load(f))
def ignored(
*passed_paths: str,
) -> List[str]:
try:
process = subprocess.run(
["git", "check-ignore"] + list(passed_paths),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf8",
)
except (subprocess.CalledProcessError, FileNotFoundError):
return list(passed_paths)
else:
return list(set(passed_paths) - set(process.stdout.splitlines()))
def aggregate(
*passed_paths: str,
) -> 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=configuration["confirm"],
show_default=True,
help="Enable or disable confirmation.",
)
@click.option(
"--magic/--no-magic",
default=configuration["magic"],
show_default=True,
help="Enable or disable guessing file types.",
)
@click.option(
"--ignore/--no-ignore",
default=configuration["ignore"],
show_default=True,
help="Enable or disable .gitignore checking.",
)
@click.argument("paths", nargs=-1)
def paste(confirm: bool, magic: bool, ignore: bool, paths: Tuple[str]) -> None:
"""Paste some files matching a pattern."""
if not paths:
print(colored("No paths found, did you forget to pass some?", "red"))
return
if paths == ("-",):
print(colored("Using stdin for input", "yellow"))
data = {
"expiry": "1day",
"files": [
{"name": "stdin", "content": sys.stdin.read(), "lexer": "text"}
],
}
if confirm:
print(
colored(
"You are about to paste stdin. Do you want to continue?",
"yellow",
)
)
else:
print(colored("Pasting stdin.", "yellow"))
else:
if ignore:
paths = ignored(*paths) # type: ignore
files = aggregate(*paths)
if not len(files):
print(colored("No files found in given paths?", "red"))
return
if confirm:
print(
colored(
f"You are about to paste the following {len(files)} files. Do you want to continue?",
"yellow",
)
)
else:
print(
colored(f"Pasting the following {len(files)} files.", "yellow")
)
for file in files:
print(f" - {file}")
if confirm:
print()
if input(colored("Continue? [y/N] ", "yellow")).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(
f"{configuration['base']}api/v1/paste", json=data
).json()
print()
print(colored("Completed paste.", "green"))
print("View link: ", response["link"])
print("Removal link:", response["removal"])
if __name__ == "__main__":
raise SystemExit(main())