def add_langs(src:SceneLocalization, *langs:str) -> SceneLocalization: cstl = SceneLocalization() # if len(set(langs)) != len(langs): raise ValueError('add_langs: Duplicate language names') # cstl.languages = [] added_langs = list(langs) for src_lang in src.languages: cstl.languages.append(SceneLanguage(src_lang.name, len(cstl.languages))) if src_lang.name in added_langs: added_langs.remove(src_lang.name) for l in added_langs: cstl.languages.append(SceneLanguage(l, len(cstl.languages))) # # cstl.messages = [] for i,src_message in enumerate(src.messages): message_langs = [] for lang in cstl.languages: if lang.name not in added_langs: src_msg = src_message[lang.name] message_langs.append(SceneMessage(i, lang, src_msg.name, src_msg.message)) else: message_langs.append(SceneMessage(i, lang, '', '')) cstl.messages.append(SceneLocalizationMessage(i, cstl.languages, message_langs)) # # cstl.language_num = len(cstl.languages) cstl.message_num = len(cstl.messages) cstl.signature = SceneLocalization.SIGNATURE return cstl
def replace_messages(src:SceneLocalization, merge:SceneLocalization) -> SceneLocalization: cstl = SceneLocalization() # cstl.languages = [] replaced_langs = [] merged_langs = set([l.name for l in merge.languages]) for src_lang in src.languages: cstl.languages.append(SceneLanguage(src_lang.name, len(cstl.languages))) if src_lang.name in merged_langs: replaced_langs.append(src_lang.name) # # cstl.messages = [] for i,src_message in enumerate(src.messages): message_langs = [] for lang in cstl.languages: if lang.name in replaced_langs: m_msg = merge.messages[i][lang.name] if i < len(merge.messages): message_langs.append(SceneMessage(i, lang, m_msg.name, m_msg.message)) else: message_langs.append(SceneMessage(i, lang, '', '')) else: src_msg = src_message[lang.name] message_langs.append(SceneMessage(i, lang, src_msg.name, src_msg.message)) cstl.messages.append(SceneLocalizationMessage(i, cstl.languages, message_langs)) # # cstl.language_num = len(cstl.languages) cstl.message_num = len(cstl.messages) cstl.signature = SceneLocalization.SIGNATURE return cstl
def rename_langs(src:SceneLocalization, *langs:tuple) -> SceneLocalization: cstl = SceneLocalization() # if len(set(l[0] for l in langs)) != len(langs): raise ValueError('rename_langs: Duplicate input language names') if len(set(l[1] for l in langs)) != len(langs): raise ValueError('rename_langs: Duplicate output language names') # cstl.languages = [] lang_dict = dict(langs) lang_lookup = {} for src_lang in src.languages: if src_lang.name in lang_dict: new_name = lang_dict[src_lang.name] cstl.languages.append(SceneLanguage(new_name, len(cstl.languages))) lang_lookup[new_name] = src_lang.name else: cstl.languages.append(SceneLanguage(src_lang.name, len(cstl.languages))) # # cstl.messages = [] for i,src_message in enumerate(src.messages): message_langs = [] for lang in cstl.languages: src_msg = src_message[lang_lookup.get(lang.name, lang.name)] message_langs.append(SceneMessage(i, lang, src_msg.name, src_msg.message)) cstl.messages.append(SceneLocalizationMessage(i, cstl.languages, message_langs)) # # cstl.language_num = len(cstl.languages) cstl.message_num = len(cstl.messages) cstl.signature = SceneLocalization.SIGNATURE return cstl
def keep_langs(src:SceneLocalization, *langs:str) -> SceneLocalization: cstl = SceneLocalization() # if len(set(langs)) != len(langs): raise ValueError('keep_langs: Duplicate language names') # cstl.languages = [] for src_lang in src.languages: if src_lang.name in langs: cstl.languages.append(SceneLanguage(src_lang.name, len(cstl.languages))) # # cstl.messages = [] for i,src_message in enumerate(src.messages): message_langs = [] for lang in cstl.languages: src_msg = src_message[lang.name] message_langs.append(SceneMessage(i, lang, src_msg.name, src_msg.message)) cstl.messages.append(SceneLocalizationMessage(i, cstl.languages, message_langs)) # # cstl.language_num = len(cstl.languages) cstl.message_num = len(cstl.messages) cstl.signature = SceneLocalization.SIGNATURE return cstl
def compile(self, inifile:str) -> SceneLocalization: if isinstance(inifile, configparser.ConfigParser): config = inifile else: config = self.createparser() with open(inifile, 'rt', encoding='utf-8') as f: config.read_string(f.read()) sec_num = 0 max_id = -1 ini_messages = {} for sec in config.sections(): if sec.startswith(self.MESSAGE_PREFIX): if not sec[len(self.MESSAGE_PREFIX):].isdigit(): raise ValueError(f'Expected number after {self.MESSAGE_PREFIX!r} section prefix for section {sec!r}') msg_id = int(sec[len(self.MESSAGE_PREFIX):], 10) max_id = max(max_id, msg_id) sec_num += 1 ini_messages[msg_id] = config[sec] elif sec == self.CSTL_SECTION: pass else: raise ValueError(f'Unknown ini section: [{sec}]') # if self.CSTL_SECTION not in config: #.has_section(): raise ValueError(f'Missing ini section: [{self.CSTL_SECTION}]') if self.LANGS_PROPERTY not in config[self.CSTL_SECTION]: raise ValueError(f'Missing ini section / property: [{self.CSTL_SECTION}] / {self.LANGS_PROPERTY}') # langs = [l.strip() for l in config[self.CSTL_SECTION][self.LANGS_PROPERTY].split(',')] # if no_orig and ORIG_LANG in langs: # langs.remove(ORIG_LANG) # if sec_num != len(ini_messages) or max_id + 1 < len(ini_messages): raise ValueError('Duplicate or invalid message sections') num = max_id + 1 # cstl = SceneLocalization() cstl.languages = [SceneLanguage(l,i) for i,l in enumerate(langs)] # cstl.messages = [] # # for i in range(num): ini_message = ini_messages.get(i) message_langs = [] for lang in cstl.languages: line = ini_message.get(lang.name) if ini_message is not None else None if line is None: name, content = '', '' else: name, content = self.parse_line(line) message_langs.append(SceneMessage(i, lang, name, content)) cstl.messages.append(SceneLocalizationMessage(i, cstl.languages, message_langs)) cstl.language_num = len(langs) cstl.message_num = len(cstl.messages) cstl.signature = SceneLocalization.SIGNATURE return cstl