-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.py
107 lines (75 loc) · 2.38 KB
/
util.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
import json
import os
import random
import re
import time
from handlers.byte_handler import ByteHandler
from handlers.list_handler import ListHandler
from handlers.set_handler import SetHandler
write_handlers = [
ByteHandler(),
ListHandler(),
SetHandler()
]
def generate_random_filepath():
return "/tmp/" + str(time.time()) + "-" + str(random.randrange(0, 99999, 1))
def write_clipboard_to_file():
filepath = generate_random_filepath()
os.system("pbpaste > " + filepath)
return filepath
def read_from_clipboard():
filepath = write_clipboard_to_file()
f = open(filepath, "r")
content = f.read()
f.close()
return content
def trim_lines(content):
content = content.split("\n")
content = [line.strip() for line in content if line.strip()]
return "\n".join(content)
def sort_lines(content):
content = content.split("\n")
content = [line.strip() for line in content if line.strip()]
content.sort()
return "\n".join(content)
def deduplicate_lines(content):
content = content.split("\n")
content = set(content)
content = list(content)
return "\n".join(content)
def remove_duplicate_lines(content):
return sort_lines(deduplicate_lines(trim_lines(content)))
def write_to_clipboard(content):
for handler in write_handlers:
if handler.should_handle(content):
content = handler.handle(content)
break
filepath = generate_random_filepath()
f = open(filepath, "w")
f.write(content)
f.close()
os.system("cat " + filepath + " | pbcopy")
def convert_to_kebab_case(string):
string = string.lower().strip()
string = re.sub(r'\s+', '-', string)
return string
def is_debug():
return os.environ['DEBUG'] == 'true'
def debug(msg):
if is_debug():
print(msg)
def silent(func):
def handler(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
debug(e)
return 'false'
return handler
def json_prettify(content):
if type(content) is not str:
content = json.dumps(content)
parsed = json.loads(content)
return json.dumps(parsed, indent=4, sort_keys=True)
eval = lambda f: silent(write_to_clipboard(f(content=read_from_clipboard())))
eval_lines = lambda f: silent(write_to_clipboard('\n'.join([f()(line) for line in read_from_clipboard().split('\n')])))