-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtlxgen
More file actions
executable file
·94 lines (85 loc) · 3.85 KB
/
Copy pathrtlxgen
File metadata and controls
executable file
·94 lines (85 loc) · 3.85 KB
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
#!/usr/bin/python
import argparse
import os
import os.path
import subprocess
from cssjanus import cssjanus
RTL_SUFFIX = "-rtlx"
EXTENSIONS = set(["css", "jpg", "jpeg", "png", "gif"])
CSS_HEADER = '/* AUTOGENERATED FILE - DO NOT EDIT */\n\n'
class FileInfo():
def __init__(self):
self.base = None
self.ext = None
self.ltr_exists = False
self.rtl_exists = False
def rtl_filename(self):
return self.base + RTL_SUFFIX + self.ext
def ltr_filename(self):
return self.base + self.ext
# from Python Cookbook by Alex Martelli, David Ascher - http://my.safaribooksonline.com/book/programming/python/0596001673/files/pythoncook-chp-4-sect-16
def splitall(path):
allparts = []
while 1:
parts = os.path.split(path)
if parts[0] == path: # sentinel for absolute paths
allparts.insert(0, parts[0])
break
elif parts[1] == path: # sentinel for relative paths
allparts.insert(0, parts[1])
break
else:
path = parts[0]
allparts.insert(0, parts[1])
return allparts
def main():
files_dict = {}
for root, dirs, files in os.walk(args.path):
if not 'files' in splitall(root): # ignore 'files' directories
for name in files:
path = os.path.join(root, name)
base, extension = os.path.splitext(path)
if extension[1:].lower() in EXTENSIONS:
is_rtl = base.endswith(RTL_SUFFIX)
base_no_suffix = base[:-len(RTL_SUFFIX)] if is_rtl else base
key = (base_no_suffix, extension)
if not key in files_dict:
fi = files_dict[key] = FileInfo()
fi.base = base_no_suffix
fi.ext = extension
else:
fi = files_dict[key]
if is_rtl:
fi.rtl_exists = True
else:
fi.ltr_exists = True
for key, fi in files_dict.iteritems():
if args.clean:
if fi.rtl_exists:
if args.verbose: print "Removing RTL file '" + fi.rtl_filename() + "'"
os.remove(fi.rtl_filename())
else:
if fi.rtl_exists and not fi.ltr_exists:
if args.verbose: print "Removing orphaned RTL file '" + fi.rtl_filename() + "'"
os.remove(fi.rtl_filename())
else:
if (not fi.rtl_exists) or (os.path.getmtime(fi.rtl_filename()) < os.path.getmtime(fi.ltr_filename())):
if args.verbose: print "Converting '" + fi.ltr_filename() + "' to '" + fi.rtl_filename() + "'"
if fi.ext.lower() == ".css":
source = open(fi.ltr_filename(), 'r')
target = open(fi.rtl_filename(), 'w')
fixed_lines = cssjanus.ChangeLeftToRightToLeft(source.readlines())
target.write(CSS_HEADER + ''.join(fixed_lines))
target.close()
source.close()
elif fi.ext.lower() == ".gif":
subprocess.call(["gm", "convert", "-coalesce", fi.ltr_filename(), fi.rtl_filename()])
subprocess.call(["gm", "mogrify", "-flop", fi.rtl_filename()])
else:
subprocess.call(["gm", "convert", "-flop", fi.ltr_filename(), fi.rtl_filename()])
parser = argparse.ArgumentParser(description="Produce RTL stylesheets and images for anything under PATH (recursively).")
parser.add_argument("path", metavar="PATH")
parser.add_argument("--clean", help="delete all files with a '-rtlx' suffix.", action="store_true")
parser.add_argument("-v", "--verbose", help="print more info.", action="store_true")
args = parser.parse_args()
main()