-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchange_encoding.py
More file actions
executable file
·105 lines (81 loc) · 3.19 KB
/
change_encoding.py
File metadata and controls
executable file
·105 lines (81 loc) · 3.19 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
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
#encoding=utf-8
from os import listdir, remove, rename
from os.path import join, exists
def overwrite_config_file(file_path, readonly):
old_code = None
new_code = None
is_found_new_code = False
output_filepath = file_path + ".tmp"
input_file = open(file_path, 'r')
output_file = open(output_filepath, 'w')
left_part_encoding = 'Encoding: '
left_part_encoding_length = len(left_part_encoding)
for x_line in input_file:
#print(x_line)
new_line = x_line
# match Encoding line.
if left_part_encoding == x_line[:left_part_encoding_length]:
if ' ' in new_line:
#print("match at file:", file_path)
new_line_array = new_line.split(' ')
old_code = new_line_array[1]
new_code = new_line_array[2]
if not new_code is None:
if len(new_code) > 0:
if new_code != "-1":
is_found_new_code = True
if is_found_new_code:
my_delimitor_symbol = u' '
if my_delimitor_symbol in x_line:
my_delimitor_index = x_line.find(my_delimitor_symbol)
if my_delimitor_index >=0:
#print("1:",my_delimitor_index)
my_delimitor_index = x_line.find(my_delimitor_symbol,my_delimitor_index+1)
if my_delimitor_index >=0:
#print("2:",my_delimitor_index)
new_line = left_part_encoding + new_code + x_line[my_delimitor_index:]
output_file.write(new_line)
input_file.close()
output_file.close()
if not readonly:
remove(file_path)
rename(output_filepath, file_path)
return is_found_new_code
def scan_files_from_folder(ff_folder, readonly):
files = listdir(ff_folder)
match_count = 0
is_match_in_file = False
filename_ext = ".glyph"
filename_ext_length = len(filename_ext)
for f in files:
if filename_ext == f[-1 * filename_ext_length:]:
# skip hidden files.
if f[:1] == ".":
continue
target_path = join(ff_folder,f)
#print("target_path:", target_path)
is_match_in_file = overwrite_config_file(target_path, readonly)
if is_match_in_file:
match_count += 1
#break
print("match file count:", match_count)
def change_encoding(source_ff, readonly):
scan_files_from_folder(source_ff, readonly)
if __name__ == '__main__':
#source_ff = 'Bakudai-Regular.sfdir'
#readonly = True # for test
readonly = False # online
import sys
argument_count = 2
if len(sys.argv)==argument_count:
source_ff = sys.argv[1]
if len(source_ff) > 0:
if not exists(source_ff):
if not ".sfdir" in source_ff:
source_ff += ".sfdir"
if exists(source_ff):
change_encoding(source_ff, readonly)
else:
print("Argument must be: %d" % (argument_count -1))
print("Ex:get_ttf_chars.py folder_name")