def fromFileName(klass, fileName): # stars seems to have switched to utf-8 at some time. more or less a guess # that it used to be iso-8859-1 before. data = None fp = codecs.open(fileName, encoding='utf-8') try: data = fp.read() except UnicodeDecodeError: pass else: #NOTE: remove BOM(s) if present. seen unicode BOM_BE trailing game headers # even within HHs (no idea why), so for now we remove all BOMs unconditionally data = data.replace(unicode(codecs.BOM_UTF8, 'utf-8'), '') #NOTE: found that some files start with \x10 (TourneySumaries) if data.startswith('\x10'): data = data[1:] finally: fp.close() if data is None: fp = codecs.open(fileName, encoding='iso-8859-1') try: data = fp.read() finally: fp.close() lines = HcConfig.linesFromString(data) return klass(lines, fileName=fileName)
def fromString(klass, string): lines = HcConfig.linesFromString(string) return klass(lines, fileName='')