def generate_filter_lists(filename): # 'GF-{script}-rest.nam' => {script}-rest basename = os.path.basename(filename).split('.', 1)[0].split('-', 2)[-1] filerListFileName = '{0}.txt'.format(basename) dirname = os.path.dirname(filename) nice_names_filename = os.path.join(dirname, 'filter lists', 'nice names', filerListFileName) prod_names_filename = os.path.join(dirname, 'filter lists', 'uni names', filerListFileName) _mkdir(os.path.dirname(nice_names_filename)) _mkdir(os.path.dirname(prod_names_filename)) with codecs.open(nice_names_filename, 'w', encoding='utf-8') as niceNamesFile, \ codecs.open(prod_names_filename, 'w', encoding='utf-8') as prodNamesFile: for name in _names_generator(filename): print(filter_lists.translate_name(name, production_name=False), file=niceNamesFile) print(filter_lists.translate_name(name, production_name=True), file=prodNamesFile)
def _reformat_namelist(f, out=None): entries = [] before = [] header = [] for line in f: line = line.rstrip() if (not entries and not before) and line.startswith('#'): header.append(line) continue entry = None if line.startswith('0x'): # uni chr codepoint = google_fonts.get_codepoint_from_line(line) entry = (codepoint, None, line) elif line.startswith(' '): # unencoded name name = filter_lists.translate_name(line.rsplit(' ', 1)[1]) entry = (None, name, line) if entry is not None: entry += (before, ) before = [] entries.append(entry) else: # these lines will stick before the next entry before.append(line) entries.sort(key=_sortkey_namelist_entries) _print = lambda *args: print(*args, file=out) map(_print, header) for codepoint, name, original, item_before in entries: map(_print, item_before) if codepoint is not None: _print(format_codepoint(codepoint)) elif name is not None: _print((' ' * 9 + name)) # output left over lines at the end of the file map(_print, before)