-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
298 lines (259 loc) · 12.4 KB
/
main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import re
import os
import sys
import csv
import yaml
import toml
import json
import textwrap
import pyperclip
from colorama import init, Fore, Style
init(autoreset=True)
def load_tiers(file_path):
tiers = {}
try:
with open(file_path, "r", encoding="utf-8") as f:
current_tier, tier_data = None, []
for line in map(str.strip, f):
if "$" in line:
if current_tier:
tiers[current_tier] = (tier_data, price)
current_tier, price = line.split()[0].lower(), re.search(r"\d[\d,]*(\.\d+)?\s*\$", line).group(0)
tier_data = []
elif line:
tier_data.append(line)
if current_tier:
tiers[current_tier] = (tier_data, price)
except FileNotFoundError:
print(f"{Fore.RED}File '{file_path}' not found!{Style.RESET_ALL}")
sys.exit(1)
return tiers
def cascade(tiers):
tier_keys = list(tiers.keys())
for i in range(1, len(tier_keys)):
higher_tier = tier_keys[i]
lower_tier_benefits = tiers[tier_keys[i - 1]][0]
tiers[higher_tier] = (
lower_tier_benefits + tiers[higher_tier][0],
tiers[higher_tier][1],
)
def cascade_reverse(tiers):
tier_keys = list(tiers.keys())
for i in range(len(tier_keys) - 1, -1, -1):
current_benefits = list(tiers[tier_keys[i]][0])
for j in range(i - 1, -1, -1):
current_benefits.extend(tiers[tier_keys[j]][0])
tiers[tier_keys[i]] = (current_benefits, tiers[tier_keys[i]][1])
def tier_info(tiers, selected_tier, copy_to_clipboard, output_file):
selected_tier = selected_tier.lower()
tier_data = tiers.get(selected_tier)
if not tier_data:
print(f"{Fore.RED}Tier '{selected_tier}' not found!{Style.RESET_ALL}")
return
output = "\n".join(tier_data[0])
if output_file:
with open(output_file, "w", encoding="utf-8") as f:
f.write(output)
print(f"{Fore.GREEN}Output written to '{output_file}'.{Style.RESET_ALL}")
else:
print(output)
if copy_to_clipboard:
pyperclip.copy(output)
print(f"{Fore.GREEN}Content copied to clipboard.{Style.RESET_ALL}")
def all_tiers(tiers, copy_to_clipboard, output_file):
output = "\n".join(
f"{tier.capitalize()} {price}\n" + "\n".join(benefits) + "\n"
for tier, (benefits, price) in tiers.items()
)
if output_file:
with open(output_file, "w", encoding="utf-8") as f:
f.write(output)
print(f"{Fore.GREEN}Output written to '{output_file}'.{Style.RESET_ALL}")
else:
print(output)
if copy_to_clipboard:
pyperclip.copy(output)
print(f"{Fore.GREEN}Content copied to clipboard.{Style.RESET_ALL}")
def export_tiers(tiers, format_type, output_file):
export_formats = {
"json": lambda: json.dump(tiers, open(output_file, "w", encoding="utf-8"), indent=4),
"csv": lambda: write_csv(tiers, output_file),
"markdown": lambda: write_markdown(tiers, output_file),
"html": lambda: write_html(tiers, output_file),
"xml": lambda: write_xml(tiers, output_file),
"yaml": lambda: yaml.dump(tiers, open(output_file, "w", encoding="utf-8")),
"toml": lambda: toml.dump(tiers, open(output_file, "w", encoding="utf-8")),
"latex": lambda: write_latex(tiers, output_file),
"rss": lambda: write_rss(tiers, output_file),
"asciidoc": lambda: write_asciidoc(tiers, output_file),
"odt": lambda: write_odt(tiers, output_file),
"turtle": lambda: write_turtle(tiers, output_file),
}
if format_type in export_formats:
export_formats[format_type]()
print(f"{Fore.GREEN}Exported to '{output_file}' in {format_type.upper()} format.{Style.RESET_ALL}")
else:
print(f"{Fore.RED}Unsupported export format: '{format_type}'.{Style.RESET_ALL}")
def write_csv(tiers, output_file):
with open(output_file, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["Tier", "Price", "Benefit"])
for tier, (benefits, price) in tiers.items():
writer.writerows([[tier.capitalize(), price, benefit] for benefit in benefits])
def write_markdown(tiers, output_file):
with open(output_file, "w", encoding="utf-8") as f:
for tier, (benefits, price) in tiers.items():
f.write(f"## {tier.capitalize()} {price}\n" + "".join(f"- {benefit}\n" for benefit in benefits) + "\n")
def write_html(tiers, output_file):
with open(output_file, "w", encoding="utf-8") as f:
f.write("<html><body>\n" + "".join(
f"<h2>{tier.capitalize()} {price}</h2><ul>" + "".join(f"<li>{benefit}</li>" for benefit in benefits) + "</ul>\n"
for tier, (benefits, price) in tiers.items()) + "</body></html>\n")
def write_xml(tiers, output_file):
with open(output_file, "w", encoding="utf-8") as f:
f.write("<tiers>\n" + "".join(
f' <tier name="{tier.capitalize()}" price="{price}">' + "".join(
f"<benefit>{benefit}</benefit>" for benefit in benefits) + "</tier>\n"
for tier, (benefits, price) in tiers.items()) + "</tiers>\n")
def write_latex(tiers, output_file):
with open(output_file, "w", encoding="utf-8") as f:
f.write("\\documentclass{article}\n\\begin{document}\n" + "".join(
f"\\section*{{{tier.capitalize()} {price}}}\n\\begin{{itemize}}\n" + "".join(
f"\\item {benefit}\n" for benefit in benefits) + "\\end{itemize}\n"
for tier, (benefits, price) in tiers.items()) + "\\end{document}\n")
def write_rss(tiers, output_file):
with open(output_file, "w", encoding="utf-8") as f:
f.write("<?xml version='1.0' encoding='UTF-8' ?>\n<rss version='2.0'><channel>\n"
"<title>Patreon Tiers</title>\n<link>http://example.com</link>\n"
"<description>Patreon Tiers Description</description>\n" + "".join(
f"<item><title>{tier.capitalize()} {price}</title><description>\n" + "".join(
f"{benefit}<br />\n" for benefit in benefits) + "</description></item>\n"
for tier, (benefits, price) in tiers.items()) + "</channel></rss>\n")
def write_asciidoc(tiers, output_file):
with open(output_file, "w", encoding="utf-8") as f:
f.write("".join(f"== {tier.capitalize()} {price}\n" + "".join(
f"- {benefit}\n" for benefit in benefits) + "\n" for tier, (benefits, price) in tiers.items()))
def write_odt(tiers, output_file):
from odf.opendocument import OpenDocumentText
from odf.text import P, H
doc = OpenDocumentText()
for tier, (benefits, price) in tiers.items():
doc.text.addElement(H(outlinelevel=1, text=f"{tier.capitalize()} {price}"))
for benefit in benefits:
doc.text.addElement(P(text=benefit))
doc.save(output_file)
def write_turtle(tiers, output_file):
with open(output_file, "w", encoding="utf-8") as f:
f.write("@prefix ex: <http://example.org/> .\n" + "".join(
f"ex:{tier.capitalize()} a ex:Tier ;\n ex:price \"{price}\" ;\n" + "".join(
f" ex:benefit \"{benefit}\" ;\n" for benefit in benefits) + " .\n\n"
for tier, (benefits, price) in tiers.items()))
def show_help():
help_text = (
f"{Fore.CYAN}Usage: python main.py <tier_name> [options]{Style.RESET_ALL}\n\n"
f"{Fore.YELLOW}Options:{Style.RESET_ALL}\n"
" -h, --help Show this help message and exit\n"
" -i, --input <file> Specify the input file (default: tiers.txt)\n"
" -t, --tiers List available tiers and prices\n"
" -b, --benefits List all unique benefits across tiers\n"
" -a, --all Output all tiers with benefits\n"
" -r, --reverse List benefits in reverse order (higher tier benefits appear first)\n"
" -c, --copy Copy output to clipboard\n"
" -o, --output <file> Specify a file to write output (default: output.txt)\n"
" -e, --export <fmt> Export all tiers to different format (json, csv, markdown, html, xml, yaml, toml, turtle, latex, rss, asciidoc, odt)\n"
)
terminal_width = 80
try:
terminal_width = os.get_terminal_size().columns
except OSError:
pass
for line in help_text.splitlines():
print("\n".join(textwrap.wrap(line, width=terminal_width)))
def main():
input_file = "tiers.txt"
output_file = None
args = sys.argv[1:]
if not args or any(arg in args for arg in ("--help", "-h")):
show_help()
return
if "-i" in args or "--input" in args:
try:
input_index = args.index("-i") if "-i" in args else args.index("--input")
input_file = args[input_index + 1]
except:
return print(f"{Fore.RED}Please provide an input file! (Like: ... -i tiers.txt){Style.RESET_ALL}")
tiers = load_tiers(input_file)
copy_to_clipboard = "-c" in args or "--copy" in args
output_file = "output.txt"
try:
args[args.index("-o") + 1] if ("-o" in args or "--output" in args) else "output.txt"
except:
return print(f"{Fore.RED}Please provide an output file! (Like: '... -o out.txt'){Style.RESET_ALL}")
cascade_reverse(tiers) if ("-r" in args or "--reverse" in args) else cascade(tiers)
if "-t" in args or "--tiers" in args:
table = ""
for tier, (benefits, price) in tiers.items():
table += f"{tier.capitalize():<15} {price:<10}\n"
print(f"{Fore.CYAN}{table}{Style.RESET_ALL}")
if copy_to_clipboard:
pyperclip.copy(table)
if ("-o" in args or "--output" in args):
with open(output_file, "w", encoding="utf-8") as f:
f.write(table)
return
if "-b" in args or "--benefits" in args:
benefits = "\n".join({benefit for tier in tiers.values() for benefit in tier[0]})
print(benefits)
if copy_to_clipboard:
pyperclip.copy(benefits)
if ("-o" in args or "--output" in args):
with open(output_file, "w", encoding="utf-8") as f:
f.write(benefits)
return
if "-a" in args or "--all" in args:
all_tiers(tiers, copy_to_clipboard, output_file)
return
if "-e" in args or "--export" in args:
try:
format_type = args[args.index("--export") + 1] if "--export" in args else args[args.index("-e") + 1]
export_tiers(tiers, format_type, output_file)
except:
print(f"{Fore.RED}Please provide an export type! (Like: '... -e csv'){Style.RESET_ALL}")
return
if "--logo" in args:
return print("""
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#%#
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@%%%%%%%%%%%%%%%%%%%%%%#*+*#%%%%%%%%%%%%%%%%%%%%%#%
@%%%%%%%%%%%%%%%%%%%#=:::::::=#%%%%%%%%%%%%%%%%%%%%
@%%%%%%%%%%%%%%%%%#=:::::::::::=#%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%#**+=::::::=*=::::::=+**##%%%%%%%%%%%%
@%%%%%%%%#+:::::::::::=#%%%#=:::::::::::=#%%%%%%%%%
@%%%%%%%+:::::::::::=#%%%%%%%*-:::::::::::=#%%%%%%%
@%%%%%%=:::::=+*###%%%%%%%%%%%%%##**+=:::::=#%%%%%%
%%%%%%#:::::#%%%%%%%%%%%%%%%%%%%%%%%%%*:::::*%%%%%%
%%%%%%+::::+%%%%%##%%%%%%%%#*****#%%%%%=::::+%%%%%%
%%%%%%+::::*%%%#-:::=%%%%+::::::::-#%%%+::::+%%%%%%
%%%%%%+::::*%%%#-:::=%%%%+::::::::-#%%%+::::+%%%%%%
@%%%%%+::::*%%%%%##%%%%%%%%#######%%%%%+::::+%%%%%%
%%%%%%+::::*%%%%%%%%%%%%%%%%%%%%%%%%%%%+::::+%%%%%%
%%%%%%+::::*%%%%#***%%%%%%#*+++++*%%%%%+::::+%%%%%%
%%%%%%+::::*%%%#-:::-%%%%+::::::::-#%%%+::::+%%%%%%
@%%%%%+::::*%%%%=:::+%%%%*::::::::=%%%%+::::+%%%%%%
@%%%%%+::::=%%%%%%#%%%%%%%%######%%%%%%=::::*%%%%%%
%%%%%%#:::::*%%%%%%%%%%%%%%%%%%%%%%%%%*::::-#%%%%%%
@%%%%%%+:::::-=+*******************+=-:::::+%%%%%%%
@%%%%%%%+:::::::::::::::::::::::::::::::::+%%%%%%%%
@%%%%%%%%%+:::::::::::::::::::::::::::::*%%%%%%%%%%
%%%%%%%%%%%%%%##*******************#%%%%%%%%%%%%%%%
@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@
""")
selected_tier = args[0]
if not ("-o" in args or "--output" in args):
output_file = None
tier_info(tiers, selected_tier, copy_to_clipboard, output_file)
if __name__ == "__main__":
main()