From f8163dc3e865c2a36bd43881d5e727fb197e0c46 Mon Sep 17 00:00:00 2001 From: Ben Bullock Date: Sun, 4 Aug 2024 14:29:44 +0900 Subject: [PATCH] Improve the cleanup script --- clean.py | 19 ++++++++- utils.py | 124 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 85 insertions(+), 58 deletions(-) diff --git a/clean.py b/clean.py index dd26e2b04..8d892df9c 100755 --- a/clean.py +++ b/clean.py @@ -5,11 +5,28 @@ import os, glob -badFiles = glob.glob('kanjivg.xml') +# Print debugging information if set to a true value. + +verbose = True + +# These files are generated by updatepublic.sh but are not part of the +# repository. + +badFiles = glob.glob('kanjivg*.xml*') +badFiles.extend(glob.glob('kanjivg*.zip')) for file in badFiles: if os.path.exists(file): + if verbose: + print("Removing %s" % (file) ) os.remove(file) + +# Emacs and possibly other editors create files with a ~ at the end +# for backup purposes. + emacsBackups = glob.glob('kanji/*~') emacsBackups.extend(glob.glob('*~')) +emacsBackups.extend(glob.glob('.git/*~')) for file in emacsBackups: + if verbose: + print("Removing %s" % (file) ) os.remove(file) diff --git a/utils.py b/utils.py index 2e30e3514..8b7b9aaf2 100644 --- a/utils.py +++ b/utils.py @@ -1,75 +1,85 @@ -import sys, os +import sys, os, warnings PYTHON_VERSION_MAJOR = sys.version_info[0] if PYTHON_VERSION_MAJOR < 3: - # In python 2, io.open does not support encoding parameter - from codecs import open + # In python 2, io.open does not support encoding parameter + from codecs import open else: - from io import open - # In python 3, strings are used so unicode() is a pass-through - def unicode(s): - return s + from io import open + # In python 3, strings are used so unicode() is a pass-through + def unicode(s): + return s def canonicalId(id): - if isinstance(id, str): - idLen = len(id) - if idLen == 1: - id = ord(id) - elif idLen >= 2 and idLen <= 5: - id = int(id, 16) - else: - raise ValueError("Character id must be a 1-character string with the character itself, or 2-5 hex digit unicode codepoint.") - if not isinstance(id, int): - raise ValueError("canonicalId: id must be int or str") - if id > 0xf and id <= 0xfffff: - return "%05x" % (id) - raise ValueError("Character id out of range") + if isinstance(id, str): + idLen = len(id) + if idLen == 1: + id = ord(id) + elif idLen >= 2 and idLen <= 5: + id = int(id, 16) + else: + raise ValueError("Character id must be a 1-character string with the character itself, or 2-5 hex digit unicode codepoint.") + if not isinstance(id, int): + raise ValueError("canonicalId: id must be int or str") + if id > 0xf and id <= 0xfffff: + return "%05x" % (id) + raise ValueError("Character id out of range") class SvgFileInfo: - def __init__(self, file, dir): - self.path = os.path.join(dir, file) - if file[-4:].lower() != ".svg": - raise Exception("File should have .svg extension. (%s)" % (str(self.path))) - parts = (file[:-4]).split('-') - if len(parts) == 2: - self.variant = parts[1] - elif len(parts) != 1: - raise Exception("File should have at most 2 parts separated by a dash. (%s)" % (str(file))) - self.id = parts[0] - if self.id != canonicalId(self.id): - raise Exception("File name not in canonical format (%s)" % (str(self.path))) + def __init__(self, file, dir): + self.path = os.path.join(dir, file) + self.OK = True + if file[-4:].lower() != ".svg": + warnings.warn("File should have .svg extension. (%s)" % (str(self.path))) + self.OK = False + return + parts = (file[:-4]).split('-') + if len(parts) == 2: + self.variant = parts[1] + elif len(parts) != 1: + warnings.warn("File should have at most 2 parts separated by a dash. (%s)" % (str(file))) + self.OK = False + return + self.id = parts[0] + if self.id != canonicalId(self.id): + warnings.warn("File name not in canonical format (%s)" % (str(self.path))) + self.OK = False + return - def __repr__(self): - return repr(vars(self)) - def read(self, SVGHandler=None): - if SVGHandler is None: - from kanjivg import SVGHandler - handler = SVGHandler() - parseXmlFile(self.path, handler) - parsed = list(handler.kanjis.values()) - if len(parsed) != 1: - raise Exception("File does not contain 1 kanji entry. (%s)" % (self.path)) - return parsed[0] + def __repr__(self): + return repr(vars(self)) + + def read(self, SVGHandler=None): + if SVGHandler is None: + from kanjivg import SVGHandler + handler = SVGHandler() + parseXmlFile(self.path, handler) + parsed = list(handler.kanjis.values()) + if len(parsed) != 1: + raise Exception("File does not contain 1 kanji entry. (%s)" % (self.path)) + return parsed[0] def parseXmlFile(path, handler): - from xml.sax import parse - parse(path, handler) + from xml.sax import parse + parse(path, handler) def listSvgFiles(dir): - return [ - SvgFileInfo(f, dir) - for f in os.listdir(dir) - ] + list = [] + for f in os.listdir(dir): + sfi = SvgFileInfo(f, dir) + if sfi.OK: + list.append(sfi) + return list def readXmlFile(path, KanjisHandler=None): - if KanjisHandler is None: - from kanjivg import KanjisHandler - handler = KanjisHandler() - parseXmlFile(path, handler) - parsed = list(handler.kanjis.values()) - if len(parsed) == 0: - raise Exception("File does not contain any kanji entries. (%s)" % (path)) - return handler.kanjis + if KanjisHandler is None: + from kanjivg import KanjisHandler + handler = KanjisHandler() + parseXmlFile(path, handler) + parsed = list(handler.kanjis.values()) + if len(parsed) == 0: + raise Exception("File does not contain any kanji entries. (%s)" % (path)) + return handler.kanjis