Created
May 2, 2013 22:48
-
-
Save cas--/5506061 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# script to switch from metadate to attributes | |
# | |
#- <property name="label" translatable="yes"><b>From Infohash</b></property> | |
#- <property name="use_markup">True</property> | |
#+ <property name="label" translatable="yes">From Infohash</property> | |
#+ <attributes> | |
#+ <attribute name="weight" value="bold"/> | |
#+ </attributes> | |
# | |
# <property name="label" translatable="yes"><b><i><big>Downloads</big></i></b></property> | |
# <property name="use_markup">True</property> | |
# <property name="label" translatable="yes">Downloads</property> | |
# <attributes> | |
# <attribute name="style" value="italic"/> | |
# <attribute name="weight" value="bold"/> | |
# <attribute name="scale" value="1.2"/> | |
# </attributes> | |
import re | |
from glob import glob | |
RE_LABEL = re.compile(r'translatable=\"yes\"><b>(?!&+)(?P<text>.*?)</b><') | |
sub_txt = [ | |
"<property name=\"label\" translatable=\"yes\">%s</property>\n", | |
"<attributes>\n", | |
" <attribute name=\"weight\" value=\"bold\"/>\n", | |
"</attributes>\n" | |
] | |
RE_LABEL_ITALIC = re.compile(r'translatable=\"yes\"><i>(?!&+)(?P<text>.*?)</i><') | |
sub_txt_italic = [ | |
"<property name=\"label\" translatable=\"yes\">%s</property>\n", | |
"<attributes>\n", | |
" <attribute name=\"style\" value=\"italic\"/>\n", | |
"</attributes>\n" | |
] | |
RE_LABEL_HEADER = re.compile(r'translatable=\"yes\"><b><i><big>(?P<text>.*?)</big></i></b><') | |
sub_txt_header = [ | |
"<property name=\"label\" translatable=\"yes\">%s</property>\n", | |
"<attributes>\n", | |
" <attribute name=\"style\" value=\"italic\"/>\n", | |
" <attribute name=\"weight\" value=\"bold\"/>\n", | |
" <attribute name=\"scale\" value=\"1.2\"/>\n", | |
"</attributes>\n" | |
] | |
glade_dir = glob("/home/ubuntu/deluge.git_master/deluge/ui/gtkui/glade/*.ui") | |
glade_dir += glob("/home/ubuntu/deluge.git_master/deluge/plugins/*/*/*/*/*/*.ui") | |
print "processing %d files" % len(glade_dir) | |
#ui_file = "/home/ubuntu/deluge.git_master/deluge/ui/gtkui/glade/main_window.tabs.ui" | |
for ui_file in glade_dir: | |
next_line = False | |
new_file = [] | |
with open(ui_file, "r") as f: | |
for line in f: | |
label_match = re.search(RE_LABEL, line) | |
label_match_italic = re.search(RE_LABEL_ITALIC, line) | |
label_match_header = re.search(RE_LABEL_HEADER, line) | |
if label_match: | |
txt = label_match.group(1) | |
count_whtsp = len(line) - len(line.lstrip(' ')) | |
lead_whtsp = ' '.join("" for i in range(count_whtsp + 1)) | |
new_file.append(lead_whtsp + sub_txt[0] % txt) | |
new_file.append(lead_whtsp + sub_txt[1]) | |
new_file.append(lead_whtsp + sub_txt[2]) | |
new_file.append(lead_whtsp + sub_txt[3]) | |
next_line = True | |
elif label_match_italic: | |
txt = label_match_italic.group(1) | |
count_whtsp = len(line) - len(line.lstrip(' ')) | |
lead_whtsp = ' '.join("" for i in range(count_whtsp + 1)) | |
new_file.append(lead_whtsp + sub_txt_italic[0] % txt) | |
new_file.append(lead_whtsp + sub_txt_italic[1]) | |
new_file.append(lead_whtsp + sub_txt_italic[2]) | |
new_file.append(lead_whtsp + sub_txt_italic[3]) | |
next_line = True | |
elif label_match_header: | |
txt = label_match_header.group(1) | |
count_whtsp = len(line) - len(line.lstrip(' ')) | |
lead_whtsp = ' '.join("" for i in range(count_whtsp + 1)) | |
new_file.append(lead_whtsp + sub_txt_header[0] % txt) | |
new_file.append(lead_whtsp + sub_txt_header[1]) | |
new_file.append(lead_whtsp + sub_txt_header[2]) | |
new_file.append(lead_whtsp + sub_txt_header[3]) | |
new_file.append(lead_whtsp + sub_txt_header[4]) | |
new_file.append(lead_whtsp + sub_txt_header[5]) | |
next_line = True | |
elif next_line and line.strip() == "<property name=\"use_markup\">True</property>": | |
#skip | |
next_line = False | |
else: | |
new_file.append(line) | |
next_line = False | |
with open(ui_file, "w") as f: | |
for item in new_file: | |
f.write("%s" % item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment