-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrename_fonts.py
More file actions
executable file
·95 lines (87 loc) · 3.24 KB
/
rename_fonts.py
File metadata and controls
executable file
·95 lines (87 loc) · 3.24 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
95
#!/usr/bin/env python
import os
import sys
from subprocess import Popen,PIPE,STDOUT
import optparse
usage = 'usage: %prog [options]'
parser = optparse.OptionParser(
usage=usage)
parser.add_option('-p', '--path', dest='path',
default='.',
help='path to font dir',
metavar='PATH')
parser.add_option('-x', dest='execute',
action='store_true',
default=False,
help='execute',
)
(opt, args) = parser.parse_args()
def uniquify(path,sep='_'):
path=os.path.normpath(path)
num=0
newpath=path
idx=path.find('.')
if idx>0:
part1=path[:idx]
part2=path[idx:]
else:
part1=path
part2=''
while os.path.isdir(newpath) or os.path.isfile(newpath):
newpath=part1+sep+str(num)+part2
num+=1
return newpath
def standardize_name(astr):
astr=astr.replace('\\','')
astr=astr.replace(' ','_')
return astr
cmd='fc-cat -v'
proc=Popen(cmd, shell=True, stdout=PIPE, stderr=open('/dev/null'))
output=proc.communicate()[0].split('\n')
filenames=[]
newnames=[]
for line in output:
# A line looks like this:
# "Phetsarath_OT.ttf" 0 "Phetsarath OT:familylang=en:style=Regular:stylelang=en:fullname=Phetsarath OT:fullnamelang=en:slant=0:weight=80:width=100:foundry=unknown:index=0:outline=True:scalable=True:charset= |>^1!|>^1!P0oWQ |>^1!|>^1!|>^1!!!!%# !!71$!#>r1F3y?4!!K?& !!!)$ )KG/~ !!!.% 9WIlj !!!Q3 {y]9#8?/cS5fQdN !!#0GM>T@D#y#fx !!#eW !!#3H !!!!& !!#AL !!!W5 :lang=aa|ast|ay|bi|br|ch|co|da|de|en|es|et|eu|fi|fj|fo|fr|fur|fy|gd|gl|gv|ho|ia|id|ie|io|is|it|lb|lo|mg|nb|nds|nl|nn|no|nr|nso|oc|om|pt|rm|sma|smj|so|sq|ss|st|sv|sw|tn|ts|vo|vot|wa|xh|yap|zu:fontversion=65536:capability=otlayout\\:lao :fontformat=TrueType:decorative=False"
if line.startswith('"'):
row=line.split('"')
filename,num,info_str=row[1],row[2],row[3]
_,ext=os.path.splitext(filename)
info_list=info_str.split(':')
common_name=info_list[0]
if common_name=='.dir':
continue
full_name=None
style=None
filenames.append(filename)
for info in info_list:
if info.startswith('style='):
style=info.replace('style=','')
if info.startswith('fullname='):
full_name=info.replace('fullname=','')
full_name=full_name.split(',')[0]
if full_name:
if full_name.startswith(common_name):
newname=standardize_name('%s%s'%(full_name,ext))
else:
newname=standardize_name('%s_%s%s'%(common_name,full_name,ext))
elif style:
newname=standardize_name('%s_%s%s'%(common_name,style,ext))
else:
newname=standardize_name('%s%s'%(common_name,ext))
newnames.append(newname)
namedict=dict(zip(filenames,newnames))
os.chdir(opt.path)
filelist=os.listdir(opt.path)
errors=[]
for afile in filelist:
try:
print(afile,namedict[afile])
if opt.execute:
os.rename(afile,uniquify(namedict[afile]))
except KeyError:
errors.append(afile)
if errors:
print('These files are left unchanged:')
for error in errors:
print(error)