13 from listprinter import SimpleListPrinter
16 def __init__(self, fontpath):
17 self.fontpath = fontpath
18 self.__listbuffer = None
22 if self.__listbuffer is None:
24 return self.__listbuffer
27 if self.__count is None:
31 def __get_buffer(self):
32 out = StringIO.StringIO()
33 all_kanji.main(self.fontpath, out)
39 buff = self.__get_buffer()
40 pat = re.compile(r'\s([0-9]+)\s+char\(s\)$')
43 self.__listbuffer = buff[0:m.start(0)]
44 self.__count = int(m.group(1))
47 def __init__(self, fontpath, archive):
48 self.fontpath = fontpath
49 self.archive = archive
50 self.__listbuffer = None
54 if self.__listbuffer is None:
56 return self.__listbuffer
59 if self.__count is None:
63 def __get_buffer(self):
64 out = StringIO.StringIO()
65 p = SimpleListPrinter(delimiter=0x3001, out=out)
66 new_glyphs.print_new_glyph(self.archive, self.fontpath, p)
72 buff = self.__get_buffer()
73 pat = re.compile(r'\s([0-9]+)\s+char\(s\)$')
76 self.__listbuffer = buff[0:m.start(0)]
77 self.__count = int(m.group(1))
79 def print_readme(template_dir, out_dir, language, all_list):
80 template_name = 'README-%s-template.txt' % language
81 template_file = os.path.join(template_dir, template_name)
83 out_file = os.path.join(out_dir, 'README_%s.txt' % language)
84 d = {'kanji_count': __l(int(all_list.count())),
85 'kanji_list': all_list.listbuffer(),
86 'year': time.localtime()[0],
88 __print_document(template_file, out_file, d)
90 def print_releasenote(template_dir, out_dir, language, all_list, new_list):
91 template_name = 'NOTE-%s-template.txt' % language
92 template_file = os.path.join(template_dir, template_name)
94 out_file = os.path.join(out_dir, 'RELEASE-NOTE-%s.txt' % language)
95 d = {'kanji_count': __l(int(all_list.count())),
96 'new_kanji_count': __l(int(new_list.count())),
97 'kanji_list': all_list.listbuffer(),
98 'new_kanji_list': new_list.listbuffer(),
100 __print_document(template_file, out_file, d)
102 def __print_document(template_file, out_file, data):
103 if not os.path.exists(template_file): return
105 out = open(out_file, 'w')
106 template_lines = open(template_file, 'rU').readlines()
107 for line in template_lines:
108 out.write(line % data)
112 return locale.format('%d', num, True)
114 def prepare_release_dir(font_dir_path, out_path, name, weight):
115 for ext in ('ttf', 'otf'):
116 src = os.path.join(font_dir_path, '%s-%s.%s' % (name, weight, ext))
117 if os.path.exists(src):
119 dest = os.path.join(out_path, name)
121 dest = os.path.join(out_path, '%s-%s' % (name, ext))
122 if not os.path.exists(dest): os.makedirs(dest)
123 shutil.copy(src, dest)
126 def __init__(self, path):
127 if not os.path.exists(path):
128 raise 'No such file exists: {}'.format(path)
129 if not os.path.isfile(path):
130 raise 'The specified path is not a file'
132 self.conf = yaml.load(open(path).read())
133 self.rootdir = os.path.abspath(self.conf['rootdir'])
135 def get_workdir(self, style):
136 return self.__get_configurated_dir_path('workdir', style)
138 def get_archivedir(self, style):
139 return self.__get_configurated_dir_path('archivedir', style)
141 def bind_style(self, style):
143 self.workdir = self.get_workdir(style)
144 self.archivedir = self.get_archivedir(style)
146 def __get_configurated_dir_path(self, dirtype, style):
147 d = self.conf[dirtype]
148 rootpath = d['root'] or ''
150 return os.path.join(self.rootdir, rootpath, stylepath)
152 def get_fontpaths(self, style, weights=None, extensions=None):
153 subconf = self.conf['fonts'][style]
154 rootpath = subconf['root']
155 if weights != None and weights.__class__ != list:
157 weights = weights or subconf['weight'].keys()
158 if extensions != None and extensions.__class__ != list:
159 extensions = [extensions]
160 extensions = extensions or subconf['extension']
161 paths = [os.path.join(self.rootdir, rootpath, \
162 '%s.%s' % (subconf['weight'][key], ext)) \
163 for key in weights if key != 'root' \
164 for ext in extensions]
168 def __init__(self, path):
169 if not os.path.exists(path):
170 raise Exception('No such file exists: {}'.format(path))
171 if not os.path.isfile(path):
172 raise Exception('The specified path is not a file: {}'.format(path))
173 self.__setup_items(path)
175 def __setup_items(self, path):
176 self.dir = os.path.dirname(path)
177 self.basename = os.path.basename(path)
178 familyname, style, weight = self.basename.split('.')[0].split('-')
179 self.name = '%s-%s' % (familyname, style)
180 self.familyname = familyname
184 if __name__ == '__main__':
185 scriptdir = os.path.dirname(sys.argv[0])
186 conf = ReleaseConfig(os.path.join(scriptdir, 'release.yml'))
188 fontpath = sys.argv[1]
189 font = FontName(fontpath)
190 conf.bind_style(font.style)
192 if not os.path.exists(conf.archivedir) or \
193 not os.path.isdir(conf.archivedir):
194 raise Exception('The specified archive directory does not exits')
195 if not os.path.exists(conf.workdir): os.makedirs(conf.workdir)
197 archlist = os.listdir(conf.archivedir)
198 pat = re.compile(r'^%s\-%s\-[0-9]{8}\.tar\.gz$' % \
199 (font.familyname, font.style))
200 archfiles = [nm for nm in archlist if pat.match(nm)]
202 archfile = archfiles[-1]
204 all_list = AllKanjiList(fontpath)
205 new_list = NewKanjiList(fontpath, os.path.join(conf.archivedir, archfile))
207 prepare_release_dir(font.dir, conf.workdir, font.name, font.weight)
209 templatedir = os.path.join(scriptdir, 'templates', font.style)
210 for lang in ['ja', 'en']:
211 for ext in ['ttf', 'otf']:
213 dest = os.path.join(conf.workdir, font.name)
215 dest = os.path.join(conf.workdir, '%s-%s' % (font.name, ext))
216 if os.path.exists(dest):
217 print_readme(templatedir, dest, lang, all_list)
219 for lang in ['ja', 'en', 'de']:
220 print_releasenote(templatedir, conf.workdir, lang, all_list, new_list)