This is a simple script to convert an old PyRadio theme to the new format!
Usage:
python pyradio_convert_theme.py [FILE | THEME NAME]
Note: Please make sure you are using the same pallette as the one used when the original theme was created
# -*- coding: utf-8 -*- | |
import curses | |
import sys | |
from os import environ, getenv, remove | |
import glob | |
from os.path import join, basename, getmtime, getsize, exists | |
def hex_to_rgb(h): | |
n = h.lstrip('#') | |
return tuple(int(n[i:i+2], 16) for i in (0, 2, 4)) | |
def rgb_to_hex(rgb): | |
return '#%02x%02x%02x' % rgb | |
def curses_rgb_to_hex(rgb): | |
x = [0, 0, 0] | |
for i, r in enumerate(rgb): | |
x[i] = int(r * 255 / 1000) | |
return rgb_to_hex(tuple(x)) | |
def restore_colors(save_colors): | |
for i in save_colors.keys(): | |
try: | |
curses.init_color(i, save_colors[i][0], save_colors[i][1], save_colors[i][2]) | |
except: | |
pass | |
if __name__ == '__main__': | |
# ls /usr/share/terminfo/x | |
term = getenv('TERM') | |
if term is None: | |
environ['TERM'] = 'xterm-256color' | |
print('TERM not set...\n Using xterm-256color') | |
if term.startswith('xterm') or term.startswith('screen') or term.startswith('tmux'): | |
print('TERM is ' + term) | |
environ['TERM'] = 'xterm-256color' | |
print(' Using xterm-256color') | |
if len(sys.argv) > 1: | |
theme_file = sys.argv[1] | |
if not exists(theme_file): | |
themes = join(getenv('HOME', '~'), '.config', 'pyradio', 'themes', theme_file + '.pyradio-theme') | |
if exists(themes): | |
theme_file = themes | |
else: | |
print('File not found: ' + theme_file) | |
sys.exit(1) | |
else: | |
print('Usage: python pyradio_convert_theme.py [FILE | THEME NAME]') | |
sys.exit(1) | |
print(' Input file: ' + theme_file) | |
curses.initscr() | |
curses.curs_set(0) | |
curses.start_color() | |
curses.use_default_colors() | |
lines = [] | |
try: | |
with open(theme_file, 'r') as thmfile: | |
lines = [line.strip() for line in thmfile if line.strip() and not line.startswith('#')] | |
except: | |
pass | |
out = {} | |
for l in lines: | |
sp = l.split('=') | |
sp[0] = sp[0].strip() | |
sp[1] = sp[1].strip() | |
s = sp[1].split(',') | |
s[0] = s[0].strip() | |
s[1] = s[1].strip() | |
sp = sp[:-1] | |
sp.extend(s) | |
out[sp[0]] = [ | |
curses_rgb_to_hex(curses.color_content(int(sp[1]))), | |
curses_rgb_to_hex(curses.color_content(int(sp[2]))), | |
] | |
curses.endwin() | |
# for n in out.keys(): | |
# print('{0}: {1}'.format(n, out[n])) | |
out_file = theme_file.replace('.pyradio-theme', '_converted.pyradio-theme') | |
print('Output file: ' + out_file) | |
if 'Extra Func' not in out.keys(): | |
out['Extra Func'] = [out['PyRadio URL'][0], ] | |
print( ' Notice: "Extra Func" color not found in theme.\n I have set it to "PyRadio URL" color.\n Please edit the theme and adjust as desired.\n') | |
msg = ''' | |
# Main foreground and background | |
Stations {0} {1} | |
# Playing station text color | |
# (background color will come from Stations) | |
Active Station {2} | |
# Status bar foreground and background | |
Status Bar {3} {4} | |
# Normal cursor foreground and background | |
Normal Cursor {5} {6} | |
# Cursor foreground and background | |
# when cursor on playing station | |
Active Cursor {7} {8} | |
# Cursor foreground and background | |
# This is the Line Editor cursor | |
Edit Cursor {9} {10} | |
# Text color for extra function indication | |
# and jump numbers within the status bar | |
# (background color will come from Stations) | |
Extra Func {11} | |
# Text color for URL | |
# (background color will come from Stations) | |
PyRadio URL {12} | |
# Message window borser foreground | |
# (background color will come from Stations) | |
Messages Border {13} | |
''' | |
with open(out_file, 'w') as o: | |
o.write(msg.format( | |
out['Stations'][0], | |
out['Stations'][1], | |
out['Active Station'][0], | |
out['Status Bar'][0], | |
out['Status Bar'][1], | |
out['Normal Cursor'][0], | |
out['Normal Cursor'][0], | |
out['Active Cursor'][0], | |
out['Active Cursor'][1], | |
out['Edit Cursor'][0], | |
out['Edit Cursor'][1], | |
out['Extra Func'][0], | |
out['PyRadio URL'][0], | |
out['Messages Border'][0] | |
)) |