-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreplace_functions.py
190 lines (144 loc) · 4.86 KB
/
replace_functions.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
from keyboard_listener import KeyboardListener, Combo, KeyWord
from pynput.keyboard import Key, Controller
import pyperclip
import random
import time
keyboard = Controller()
def get_clipboard_data():
data = pyperclip.paste()
return data
def copy_to_clipboard(content):
pyperclip.copy(content)
def clear_clipboard():
pyperclip.copy('')
def copy_text():
keyboard.press(Key.ctrl_l)
keyboard.press('c')
keyboard.release(Key.ctrl_l)
time.sleep(0.01)
def paste_text():
time.sleep(0.01)
keyboard.press(Key.ctrl_l)
keyboard.press('v')
keyboard.release(Key.ctrl_l)
def delete(string):
for char in string:
keyboard.press(Key.backspace)
keyboard.release(Key.backspace)
time.sleep(0.01)
def replace(old, new):
copy_to_clipboard(new)
delete(old)
paste_text()
def eval_replace(old, new):
try:
new = str(eval(new))
except:
new = new
copy_to_clipboard(new)
delete(old)
paste_text()
def exec_replace(keyword, function):
try:
exec(function)
delete(keyword)
keyword = keyword.replace('\n','')
print(f'{keyword} executed')
except:
copy_to_clipboard(function)
delete(keyword)
paste_text()
keyword = keyword.replace('\n','')
print(f'Cannot execute {keyword} - replacement made instead')
print(f'Error in function:\n\n{function}')
def split_string(string, ls='\n', l='\t', s=' '):
lines = string.split(ls)
for line_index, line in enumerate(lines):
lines[line_index] = line.split(l)
for line_index, line in enumerate(lines):
for sentence_index, sentence in enumerate(line):
line[sentence_index] = sentence.split(s)
return lines
def join_string(lines, ls='\n', l='\t', s=' '):
for line_index, line in enumerate(lines):
for sentence_index, sentence in enumerate(line):
line[sentence_index] = s.join(sentence)
for line_index, line in enumerate(lines):
lines[line_index] = l.join(line)
lines = ls.join(lines)
return lines
def make_upper(string):
lines = split_string(string)
for line_index, line in enumerate(lines):
for sentence_index, sentence in enumerate(line):
for word_index, word in enumerate(sentence):
sentence[word_index] = word.upper()
string = join_string(lines)
return string
def make_lower(string):
lines = split_string(string)
for line_index, line in enumerate(lines):
for sentence_index, sentence in enumerate(line):
for word_index, word in enumerate(sentence):
sentence[word_index] = word.lower()
string = join_string(lines)
return string
def cap_all(string):
lines = split_string(string)
for line_index, line in enumerate(lines):
for sentence_index, sentence in enumerate(line):
for word_index, word in enumerate(sentence):
sentence[word_index] = word.capitalize()
string = join_string(lines)
return string
def cap_first(string):
lines = split_string(string)
for line_index, line in enumerate(lines):
for sentence_index, sentence in enumerate(line):
for word_index, word in enumerate(sentence):
if word_index == 0:
sentence[word_index] = word.capitalize()
string = join_string(lines)
return string
def snake_case(string):
lines = split_string(string)
for line_index, line in enumerate(lines):
for sentence_index, sentence in enumerate(line):
for word_index, word in enumerate(sentence):
sentence[word_index] = word.lower()
string = join_string(lines, s='_')
return string
def undo_snake_case(string):
lines = split_string(string, s='_')
for line_index, line in enumerate(lines):
for sentence_index, sentence in enumerate(line):
for word_index, word in enumerate(sentence):
sentence[word_index] = word.capitalize()
string = join_string(lines, s=' ')
return string
def camel_case(string):
lines = split_string(string, s=' ')
for line_index, line in enumerate(lines):
for sentence_index, sentence in enumerate(line):
for word_index, word in enumerate(sentence):
sentence[word_index] = word.capitalize()
string = join_string(lines, s='')
return string
def modify(modification):
modifications = {
'upper': make_upper,
'lower': make_lower,
'cap_all': cap_all,
'snake_case': snake_case,
'undo_snake_case': undo_snake_case,
'camel_case': camel_case
}
copy_text()
data = get_clipboard_data()
data = modifications[modification](data)
print(data)
copy_to_clipboard(data)
paste_text()
print(f'{modification} done')
time.sleep(0.1)
clear_clipboard()