-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcycle_themes.py
268 lines (235 loc) · 7.93 KB
/
cycle_themes.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
import os
import sys
from time import sleep
import shutil
import curses
import threading
from glob import glob
from argparse import ArgumentParser
try:
import queue
except:
import Queue as queue
win = None
def print_msg(msg):
win.move(0, 0)
win.insdelln(1)
win.addstr(msg, curses.color_pair(0))
win.refresh()
def make_target():
if sys.platform.startswith('win'):
target = os.path.join(os.getenv('appdata'), 'pyradio', 'themes')
else:
target = os.path.join(os.getenv('HOME', '~'), '.config', 'pyradio', 'themes')
if not os.path.exists(target):
os.makedirs(target)
if not os.path.exists(target):
print(1, 0, 'Error: Cannot create output folder: "{}"\n\n'.format(target))
sys.exit(1)
target = os.path.join(target, 'cycle_base16_themes.pyradio-theme')
''' reset the file '''
try:
with open(target, 'w') as f:
pass
except:
print(1, 0, 'Error: Cannot create output file: "{}"\n\n'.format(target))
sys.exit(1)
return target
def copy_a_theme(scr, in_file, out_file, a_dir):
o_file = os.path.join(os.path.dirname(out_file), os.path.basename(in_file).replace('.pyradio-theme', '-' + a_dir + '.pyradio-theme'))
try:
shutil.copy(in_file, o_file)
print_msg('Theme copyied: {}'.format(os.path.basename(o_file)).replace('.pyradio-theme', ''))
except:
print_msg('Theme copy failed: {}'.format(os.path.basename(o_file)).replace('.pyradio-theme', ''))
def copy_theme_group(scr, in_file, out_file, dirs):
theme_name = os.path.basename(in_file).replace('.pyradio-theme', '')
for n in dirs:
theme_name = theme_name.replace(n, '')
# print_msg('theme_name: {}'.format(theme_name))
in_list = []
out_list = []
error = False
for n in dirs:
in_list.append(os.path.join('themes', n, theme_name + '.pyradio-theme'))
out_list.append(os.path.join(os.path.dirname(out_file), theme_name + '-' + n + '.pyradio-theme'))
# print_msg('in: ' + in_list[-1])
# print_msg('out: ' + out_list[-1])
try:
shutil.copy(in_list[-1], out_list[-1])
except:
error = True
if error:
print_msg('Theme group copy failed: ' + theme_name)
else:
print_msg('Theme group copyied: ' + theme_name)
def do_copy_files(scr, out_file, stop, delay, th_start_count, que):
''' thread to copy files to out file '''
start_count = int(th_start_count / 4)
scr.clrtoeol()
dirs = (
'default',
'default-alt',
'variation',
'variation-alt'
)
files = glob(os.path.join('themes', 'default', '*.pyradio-theme'))
files.sort()
y, x = scr.getmaxyx()
msg = ' Available commands:'
scr.addstr(8, 0, msg.ljust(x), curses.A_REVERSE)
msg = ' Total number of themes: {0}, delay: {1} sec'.format(4 * len(files), delay())
scr.addstr(1, 0, msg.ljust(x), curses.A_REVERSE)
scr.addstr(0, 0, ' Cycle PyRadio Base16 Themes', curses.A_BOLD)
end_count = len(files)
scr.clrtoeol()
while True:
max_len = len(str(len(files)))
for i in range(start_count, end_count):
for n in range(0, 4):
num = str(4 * i + n + 1)
scr.addstr(n+3, 0, ' ' + num.rjust(max_len) + '. ' + os.path.basename(files[i]).replace('.pyradio-theme', '') + '-' + dirs[n], curses.color_pair(0))
scr.clrtoeol()
if stop():
return
scr.refresh()
''' delete all ticks '''
for n in range(0, 4):
scr.addstr(n + 3, 1, ' ', curses.color_pair(0))
for n in range(0, 4):
# scr.addstr(n + 3, 1, '>', curses.color_pair(0))
scr.addstr(n + 3, 1, '>', curses.A_BOLD)
if n > 0:
scr.addstr(n + 2, 1, ' ', curses.color_pair(0))
''' copy file '''
in_file = os.path.join('themes', dirs[n], os.path.basename(files[i]))
shutil.copy(in_file, out_file)
y, x = scr.getmaxyx()
msg = ' Available commands:'
scr.addstr(8, 0, msg.ljust(x), curses.A_REVERSE)
msg = ' Total number of themes: {0}, delay: {1} sec'.format(4 * len(files), delay())
scr.addstr(0, 0, ' Cycle PyRadio Base16 Themes', curses.A_BOLD)
scr.addstr(1, 0, msg.ljust(x), curses.A_REVERSE)
msg = ''' + / . Increase delay
- / , Decrease delay
c , C Copy theme by name / group
q Exit
'''
scr.addstr(9, 0, msg, curses.color_pair(0))
for l in range(9, 13):
for k in 3, 7:
scr.chgat(l, k, 1, curses.A_BOLD)
msg = ' Messages:'
scr.addstr(14, 0, msg.ljust(x), curses.A_REVERSE)
scr.refresh()
step = int(delay() / .2) + 1
for _ in range(0, step):
sleep(.2)
try:
command = que.get(block=False)
except queue.Empty:
command = None
if command is not None:
if command == ord('c'):
copy_a_theme(scr, in_file, out_file, dirs[n])
elif command == ord('C'):
copy_theme_group(scr, in_file, out_file, dirs)
if stop():
return
if stop():
return
start_count = 0
if stop():
return
parser = ArgumentParser(description='Cycle through PyRadio Base16 themes')
parser.add_argument('-s', '--start',
help='start with theme number')
parser.add_argument('-d', '--delay',
help='counter delay')
args = parser.parse_args()
''' start curses '''
scr = curses.initscr()
curses.start_color()
curses.use_default_colors()
try:
curses.curs_set(0)
except:
pass
scr.nodelay(0)
curses.noecho()
curses.cbreak()
start_count = 0
if args.start:
try:
start_count = int(args.start) - (int(args.start) % 4)
except:
pass
delay = 2
if args.delay:
try:
delay = float(args.delay)
except:
pass
out_file = make_target()
msg = '''Please execute PyRadio now and "watch" the "cycle_base16_themes" theme
To do that:
1. Execute PyRadio
2. Press "t" to open the "Themes Selection" window
3. Select the "cycle_base16_themes" entry
4. Press "c" to watch it
5. Press "ESCAPE" to close the "Themes Selection" window
When you are ready, please press any key to continue...
Parameters:
Start counter: {0}
Counter delay: {1} sec
'''
scr.addstr(0, 0, msg.format(start_count, delay), curses.color_pair(0))
scr.refresh()
scr.getch()
scr.clear()
scr.refresh()
y, x = scr.getmaxyx()
win = curses.newwin(y-15, x, 15, 0)
print_msg('Program started...')
que = queue.Queue()
stop_thread = False
copy_thread = threading.Thread(
target=do_copy_files,
args=(
scr,
out_file,
lambda: stop_thread,
lambda: delay,
start_count,
que
)
)
copy_thread.start()
while True:
char = scr.getch()
if char == curses.KEY_RESIZE:
ny, nx = scr.getmaxyx()
win.resize(ny-15, nx)
win.mvwin(15, 0)
# win.touchwin()
# win.redrawwin()
# win.refresh()
elif char == ord('q'):
stop_thread = True
break
elif char in (ord('+'), ord('.')):
delay = round(delay + .1, 1)
print_msg('Delay increased by .1 sec...')
elif char in (ord('-'), ord(',')):
if delay > .5:
delay = round(delay - .1, 1)
print_msg('Delay decreased by .1 sec...')
elif char in (ord('c'), ord('C')):
try:
que.put(char, block=False)
except queue.Full:
pass
print_msg('Waiting for threads to terminate...')
copy_thread.join()
curses.endwin()
os.remove(out_file)