def __init__(self, parent, text): self._text = text self.encoding = None self._builder = Gtk.Builder() glade_file = Resources.find(path.join('data', 'gui', 'glade', 'encoding_dialog.glade')) self._builder.add_from_file(glade_file) self._dialog = self._builder.get_object('encoding_dialog') self._dialog.set_transient_for(parent) self._builder.connect_signals(self) self._text_content = self._builder.get_object('text_content') self._store_encoding = self._builder.get_object('store_encoding') encodings = sorted(TextFile.get_available_encodings_with_title(), key=itemgetter(0)) # Test which encoding can be used to decode the text-file and # add those encodings to the combo box. for encoding in encodings: try: self._text.decode(encoding[1]) # decode successfull --> add to list self._store_encoding.append(encoding) except UnicodeDecodeError: continue
def __init__(self, parent, title, enable_encoding_selection=False): self._enable_encoding_selection = enable_encoding_selection self.encoding = None self._builder = Gtk.Builder() glade_file = Resources.find(path.join('data', 'gui', 'glade', 'ext_file_chooser_dialog.glade')) self._builder.add_from_file(glade_file) self._dialog = self._builder.get_object('filechooserdialog') self._dialog.set_transient_for(parent) self._dialog.set_title(title) self._builder.connect_signals(self) self._box_encoding = self._builder.get_object('box_encoding') self._combo_encoding = self._builder.get_object('combo_encoding') self._store_encoding = self._builder.get_object('store_encoding') if not enable_encoding_selection: self._box_encoding.hide() self._combo_encoding.set_row_separator_func(self._is_separator, None) encodings = sorted(TextFile.get_available_encodings_with_title(), key=itemgetter(0)) self._store_encoding.append([_('Detect automatically'), None]) self._store_encoding.append(['-', None]) for encoding in encodings: self._store_encoding.append(encoding) self._combo_encoding.set_active(0)
def detect_textfile_encoding(parent, file_): """Try to automatically detect the character encoding of the specified file. If that fails show a dialog to force the user to select the correct character encoding. """ encoding = TextFile.detect_encoding(file_) if encoding is None: enc_dlg = EncodingDialog(parent, open(file_).read()) res = enc_dlg.run() enc_dlg.destroy_dialog() if res == Gtk.ResponseType.OK and enc_dlg.encoding is not None: encoding = enc_dlg.encoding else: return None return encoding
def _validate_encoding(self, value): self._validate_any_str(value) if value not in TextFile.get_available_encodings(): raise TypeError(_('Invalid encoding ({}) in Submod-script!').format( value))