def update_combo_encoding(self, text):
        check_and_save_path(self.config, 'df_exe_translation_file', text)

        # Update codepage combobox
        # TODO: Cache supported codepages' list
        codepages = get_codepages().keys()
        if self.fileentry_translation_file.path_is_valid():
            translation_file = self.fileentry_translation_file.text
            with open(translation_file, 'r', encoding='utf-8') as fn:
                pofile = po.PoReader(fn)
                self.translation_file_language = pofile.meta['Language']
                strings = [
                    cleanup_special_symbols(entry['msgstr'])
                    for entry in pofile
                ]
            codepages = filter_codepages(codepages, strings)
        self.combo_encoding.values = sorted(
            codepages, key=lambda x: int(x.strip(string.ascii_letters)))

        if self.translation_file_language not in self.config[
                'language_codepages']:
            if self.combo_encoding.values:
                self.combo_encoding.current(0)
            else:
                self.combo_encoding.text = 'cp437'
        else:
            self.combo_encoding.text = self.config['language_codepages'][
                self.translation_file_language]
示例#2
0
 def filter_files_by_language(directory, language):
     for filename in os.listdir(directory):
         if filename.endswith('.po'):
             with open(path.join(directory, filename),
                       encoding='utf-8') as file:
                 if po.PoReader(file).meta['Language'] == language:
                     yield filename
示例#3
0
    def get_languages(directory):
        languages = set()
        for filename in os.listdir(directory):
            if filename.endswith('.po'):
                with open(path.join(directory, filename), encoding='utf-8') as file:
                    languages.add(po.PoReader(file).meta['Language'])

        return sorted(languages)
 def load_dictionary(self, translation_file):
     with open(translation_file, 'r', encoding='utf-8') as fn:
         pofile = po.PoReader(fn)
         meta = pofile.meta
         dictionary = OrderedDict(
             cleanup_spaces(((entry['msgid'], cleanup_special_symbols(entry['msgstr'])) for entry in pofile),
                            self.exclusions.get(meta['Language'], self.exclusions))
         )
     return dictionary
    def bt_exclusions(self):
        translation_file = self.fileentry_translation_file.text
        language = None
        dictionary = None
        if translation_file and path.exists(translation_file):
            with open(translation_file, 'r', encoding='utf-8') as fn:
                pofile = po.PoReader(fn)
                meta = pofile.meta
                language = meta['Language']
                dictionary = {entry['msgid']: entry['msgstr'] for entry in pofile}

        dialog = DialogDontFixSpaces(self, self.config['fix_space_exclusions'], language, dictionary)
        self.config['fix_space_exclusions'] = dialog.exclusions or self.config['fix_space_exclusions']
        self.exclusions = self.config['fix_space_exclusions']
示例#6
0
    def update_combo_encoding(self, _=None):
        language = self.combo_language.text
        directory = self.fileentry_translation_files.text
        # TODO: Unify with PatchExecutableFrame.update_combo_encoding()
        if path.exists(directory):
            files = self.filter_files_by_language(directory, language)
            codepages = get_codepages().keys()
            for file in files:
                with open(path.join(directory, file), 'r', encoding='utf-8') as fn:
                    pofile = po.PoReader(fn)
                    strings = [cleanup_special_symbols(entry['msgstr']) for entry in pofile]
                codepages = filter_codepages(codepages, strings)
            self.combo_encoding.values = sorted(codepages,
                                                key=lambda x: int(x.strip(string.ascii_letters)))

            self.combo_encoding.current(0)
    def __init__(self, master, config: Config, debug=False):
        super().__init__(master)

        self.config = init_section(
            config,
            section_name='patch_executable',
            defaults=dict(
                fix_space_exclusions=dict(ru=['Histories of ']),
                language_codepages=dict(),
            ))
        config = self.config
        self.exclusions = config['fix_space_exclusions']

        self.dfrus_process = None

        self._dictionary = None

        tk.Label(self, text='DF executable file:').grid()

        self.fileentry_executable_file = FileEntry(
            self,
            dialogtype='askopenfilename',
            filetypes=[('Executable files', '*.exe')],
            default_path=config.get('df_executable', ''),
            on_change=lambda text: check_and_save_path(self.config,
                                                       'df_executable', text),
        )
        self.fileentry_executable_file.grid(column=1,
                                            row=0,
                                            columnspan=2,
                                            sticky='EW')

        tk.Label(self, text='DF executable translation file:').grid()

        self.fileentry_translation_file = FileEntry(
            self,
            dialogtype='askopenfilename',
            filetypes=[
                ("Hardcoded strings' translation", '*hardcoded*.po'),
                ('Translation files', '*.po'),
                # ('csv file', '*.csv'), # @TODO: Currently not supported
            ],
            default_path=config.get('df_exe_translation_file', ''),
            on_change=self.update_combo_encoding,
            change_color=True)
        self.fileentry_translation_file.grid(column=1,
                                             row=1,
                                             columnspan=2,
                                             sticky='EW')

        tk.Label(self, text='Encoding:').grid()

        self.combo_encoding = ComboboxCustom(self)
        self.combo_encoding.grid(column=1, row=2, sticky=tk.E + tk.W)

        codepages = get_codepages().keys()

        if not self.fileentry_translation_file.path_is_valid():
            self.translation_file_language = None
        else:
            translation_file = self.fileentry_translation_file.text
            with open(translation_file, 'r', encoding='utf-8') as fn:
                pofile = po.PoReader(fn)
                self.translation_file_language = pofile.meta['Language']
                strings = [
                    cleanup_special_symbols(entry['msgstr'])
                    for entry in pofile
                ]
            codepages = filter_codepages(codepages, strings)

        self.combo_encoding.values = natsorted(codepages)

        if 'last_encoding' in config:
            self.combo_encoding.text = config['last_encoding']
        elif self.combo_encoding.values:
            self.combo_encoding.current(0)

        def save_encoding_into_config(event):
            config['last_encoding'] = event.widget.text
            if self.translation_file_language:
                config['language_codepages'][
                    self.translation_file_language] = event.widget.text

        self.combo_encoding.bind('<<ComboboxSelected>>',
                                 func=save_encoding_into_config)

        # FIXME: chk_dont_patch_charmap does nothing
        self.chk_dont_patch_charmap = self.setup_checkbutton(
            text="Don't patch charmap table",
            config_key='dont_patch_charmap',
            default_state=False)

        self.chk_dont_patch_charmap.grid(column=1, sticky=tk.W)

        self.chk_add_leading_trailing_spaces = self.setup_checkbutton(
            text='Add necessary leading/trailing spaces',
            config_key='add_leading_trailing_spaces',
            default_state=True)

        self.chk_add_leading_trailing_spaces.grid(columnspan=2, sticky=tk.W)

        button_exclusions = ttk.Button(self,
                                       text='Exclusions...',
                                       command=self.bt_exclusions)
        button_exclusions.grid(row=4, column=2)

        self.debug_frame = None if not debug else DebugFrame(
            self, dictionary=self.dictionary)
        if self.debug_frame:
            self.debug_frame.grid(columnspan=3, sticky='NSWE')
            self.grid_rowconfigure(5, weight=1)

        self.chk_debug_output = self.setup_checkbutton(
            text='Enable debugging output',
            config_key='debug_output',
            default_state=False)

        self.chk_debug_output.grid(columnspan=2, sticky=tk.W)

        self.button_patch = TwoStateButton(self,
                                           text='Patch!',
                                           command=self.bt_patch,
                                           text2='Stop!',
                                           command2=self.bt_stop)
        self.button_patch.grid(row=6, column=2)

        self.log_field = CustomText(self, width=48, height=8, enabled=False)
        self.log_field.grid(columnspan=3, sticky='NSWE')
        self.grid_rowconfigure(self.log_field.grid_info()['row'], weight=1)

        self.grid_columnconfigure(1, weight=1)

        self.bind('<Destroy>', self.kill_processes)