def _get_encoding_from_first_or_second_line(text, filename): for line in text.split(b"\n")[:2]: m = re.search(br"coding[:=]\s*([-\w.]+)", line) if m is not None: e = m.group(1).decode("ascii") try: return codecs.lookup(e).name except LookupError: warning(f"unknown encoding in {filename}: {e}")
def _load_sound_if_needed(self, filename, root, dest, sound_is_not_needed): """load the sound to the dictionary If a text exists for the sound number, the sound won't be loaded.""" if filename[-4:] == ".ogg": key = filename[:-4] if sound_is_not_needed(key): warning("didn't load %s (text exists)", filename) return if key not in dest: full_path = os.path.join(root, filename) dest[key] = full_path # load it later
def _get_language(self): """guess and return the best language for this situation""" cfg = open("cfg/language.txt").read().strip() if cfg: lang = cfg else: try: lang = locale.getdefaultlocale()[0] except ValueError: lang = "en" warning("Couldn't get the system language. " "To use another language, edit 'cfg/language.txt' " "and write 'pl' for example.") return best_language_match(lang, self._available_languages())
def encoding(text, filename="test/tts.txt"): if not filename.endswith("/tts.txt"): return "utf-8" e = _get_encoding_from_first_or_second_line(text, filename) if text.startswith(b"\xef\xbb\xbf"): # UTF-8 with BOM signature if e and e.lower() not in ["utf8", "utf-8", "utf_8"]: warning( f"{filename} starts with an UTF-8 BOM signature but specifies a {e} encoding! using utf-8-sig" ) return "utf-8-sig" # the signature will be skipped if e is None: try: import chardet e = chardet.detect(text)["encoding"] except ImportError: e = locale.getpreferredencoding() warning(f"no encoding specified for {filename}, using {e}") return e
def translate_sound_number(self, sound_number): """Return the text or sound corresponding to the sound number. If the number is greater than NB_ENCODE_SHIFT, then its really a number. """ key = "%s" % sound_number if self.has_text(key): return self.get_text(key) if re.match("^[0-9]+$", key) is not None and int(key) >= NB_ENCODE_SHIFT: return "%s" % (int(key) - NB_ENCODE_SHIFT) if self.has_sound(key): return self.get_sound(key) if re.match("^[0-9]+$", key) is not None: warning("this sound may be missing: %s", sound_number) try: return str(key) except: warning("Unicode error in %s", repr(key)) return str(key, errors="ignore")
def load_texts( self, root: Optional[Union[str, zipfile.ZipFile]] = None) -> Dict[str, str]: result = {} for txt in self._get_text_files(TXT_FILE, localize=True, root=root): lines = txt.split("\n") for line in lines: try: line = line.strip() if line: key, value = line.split(None, 1) if value: result[key] = value else: warning("in '%s', line ignored: %s", TXT_FILE, line) except: warning("in '%s', syntax error: %s", TXT_FILE, line) return result
def get_sound(self, name, warn=True): """return the sound corresponding to the given name""" key = "%s" % name for layer in self.layers: if key in layer.sounds: s = layer.sounds[key] if isinstance(s, basestring): # full path of the sound # load the sound now try: layer.sounds[key] = pygame.mixer.Sound(s) return layer.sounds[key] except: warning("couldn't load %s" % s) del layer.sounds[key] continue # try next layer else: # sound return s if warn: warning("this sound may be missing: %s", name) return None
def load_texts(self, root=None): """load and return a dictionary of texts Args: root (str): the path of the root Returns: dict: texts """ result = {} for txt in self._get_text_files(TXT_FILE, localize=True, root=root): lines = txt.split("\n") encoding_name = encoding.encoding(txt) for line in lines: try: line = line.strip() if line: key, value = line.split(None, 1) if value: try: value = unicode(value, encoding_name) except ValueError: value = unicode(value, encoding_name, "replace") warning("in '%s', encoding error: %s", TXT_FILE, line) result[key] = value else: warning("in '%s', line ignored: %s", TXT_FILE, line) except: warning("in '%s', syntax error: %s", TXT_FILE, line) return result
def get_sound(self, name, warn=True): """return the sound corresponding to the given name""" key = "%s" % name for layer in reversed(self.layers): if key in layer.sounds: s = layer.sounds[key] if isinstance(s, str): # full path of the sound # load the sound now try: layer.sounds[key] = pygame.mixer.Sound(s) return layer.sounds[key] except: warning("couldn't load %s" % s) del layer.sounds[key] continue # try next layer elif isinstance(s, tuple): zip_archive, name = s layer.sounds[key] = pygame.mixer.Sound(file=zip_archive.open(name)) return layer.sounds[key] else: # sound return s if warn: warning("this sound may be missing: %s", name) return None
if lang.lower() == shortened_code.lower(): return available_language # default value return "en" _cfg = open("cfg/language.txt").read().strip() if _cfg: preferred_language = _cfg else: try: preferred_language = locale.getdefaultlocale()[0] except ValueError: preferred_language = "en" warning("Couldn't get the system language. " "To use another language, edit 'cfg/language.txt' " "and write 'pl' for example.") class ResourceLoader(object): """Load resources. Depends on language, active packages, loading order of the mods. Ideally, it should only care about folders and files. """ def __init__(self, mods, soundpacks, all_packages_paths, base_path="res"): self._paths = [] self.base_path = base_path self.update_mods_list(mods, soundpacks, all_packages_paths) self.language = self._get_language() def _available_languages(self):
return available_language # default value return "en" with open("cfg/language.txt") as t: _cfg = t.read().strip() if _cfg: preferred_language: Optional[str] = _cfg else: try: preferred_language = locale.getdefaultlocale()[0] except ValueError: preferred_language = "en" warning("Couldn't get the system language. " "To use another language, edit 'cfg/language.txt' " "and write 'pl' for example.") class ResourceLoader: """Load resources. Depends on language, active packages, loading order of the mods. Ideally, it should only care about folders and files. """ def __init__(self, mods, soundpacks, all_packages_paths, base_path="res"): self._paths = [] self.base_path = base_path self.update_mods_list(mods, soundpacks, all_packages_paths) self.language = self._get_language() def _available_languages(self):
def _load_sound(self, key, file_ref): # if a text exists with the same name, the sound won't be loaded if key in self.txt: warning("didn't load %s.ogg (text exists)", key) elif key not in self.sounds: self.sounds[key] = file_ref