def ExtractCharactersFromDirectory(svg_paths): result = set() for dirpath, _, filenames in util.WalkFileContainers(svg_paths): for filename in filenames: if os.path.splitext(filename)[1] != '.svg': # Do nothing for files other than svg. continue result.update( ExtractCharactersFromSvgFile(os.path.join(dirpath, filename))) return list(result)
def ConvertFiles(svg_paths, output_dir): """Converts SVG files into MechaMozc specific *pic* files. Args: svg_paths: Comma separated paths to a directory/zip which has svg files (recursively). output_dir: Path of the destination directory. """ logging.debug('Start SVG conversion. From:%s, To:%s', svg_paths, output_dir) # Ensure that the output directory exists. if not os.path.exists(output_dir): os.makedirs(output_dir) converter = MozcDrawableConverter() number_of_conversion = 0 for dirpath, _, filenames in util.WalkFileContainers(svg_paths): for filename in filenames: basename, ext = os.path.splitext(filename) if ext != '.svg': # Do nothing for files other than svg. continue # Filename hack to generate stateful .pic files. if basename.endswith('_release'): # 'XXX_release.svg' files will be processed with corresponding # '_center.svg' files. So just skip them. continue logging.debug('Converting %s...', filename) if basename.endswith('_center'): # Process '_center.svg' file with '_release.svg' file to make # stateful drawable. center_svg_file = os.path.join(dirpath, filename) release_svg_file = os.path.join(dirpath, basename[:-7] + '_release.svg') pic_file = os.path.join(output_dir, basename + '.pic') pic_data = converter.ConvertStateListDrawable([ ([STATE_PRESSED], center_svg_file), ([], release_svg_file) ]) else: # Normal .svg file. svg_file = os.path.join(dirpath, filename) pic_file = os.path.join(output_dir, basename + '.pic') pic_data = converter.ConvertPictureDrawable(svg_file) with open(pic_file, 'wb') as stream: stream.write(pic_data) number_of_conversion += 1 logging.debug('%d files are converted.', number_of_conversion)