Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_scripts(self):
"""Return a sequence of scripts for which patterns exist."""
codes = list(self._patterns.keys())
while "Zyyy" in codes:
codes.remove("Zyyy")
scripts = [x.split("-")[0] for x in codes]
return tuple(aeidon.util.get_unique(scripts))
def get_languages(self, script):
"""Return a sequence of languages for which patterns exist."""
codes = list(self._patterns.keys())
start = "{}-".format(script)
codes = [x for x in codes if x.startswith(start)]
languages = [x.split("-")[1] for x in codes]
return tuple(aeidon.util.get_unique(languages))
def read_replacements(self):
"""Read list of replacements from file."""
if not os.path.isfile(self.replacement_file): return
with aeidon.util.silent(IOError, OSError, tb=True):
lines = aeidon.util.readlines(self.replacement_file)
lines = aeidon.util.get_unique(lines)
lines = list(filter(lambda x: x.strip(), lines))
self.replacements = [tuple(x.strip().split("|", 1)) for x in lines]
def _get_encodings(self, first=None):
"""Return a sequence of encodings to try when opening files."""
encodings = [first]
if gaupol.conf.encoding.try_locale:
encoding = aeidon.encodings.get_locale_code()
encodings.append(encoding)
encodings += gaupol.conf.encoding.fallback
try_auto = gaupol.conf.encoding.try_auto
if try_auto and aeidon.util.chardet_available():
encodings.append("auto")
encodings = list(filter(None, encodings))
encodings = encodings or ["utf_8"]
return tuple(aeidon.util.get_unique(encodings))
def _add_replacement_to_history(self):
"""Add current replacement to the replacement combo box."""
replacement = self._replacement_entry.get_text()
store = self._replacement_combo.get_model()
if replacement == self.application.replacement: return
self.application.replacement = replacement
self.replacements.insert(0, replacement)
self.replacements = aeidon.util.get_unique(self.replacements)
del self.replacements[self.max_history:]
store.clear()
for replacement in self.replacements:
store.append((replacement,))
self._write_history("replacements")
self.application.update_gui()
def _add_pattern_to_history(self):
"""Add current pattern to the pattern combo box."""
pattern = self._pattern_entry.get_text()
store = self._pattern_combo.get_model()
if pattern == self.application.pattern: return
self.application.pattern = pattern
self.patterns.insert(0, pattern)
self.patterns = aeidon.util.get_unique(self.patterns)
del self.patterns[self.max_history:]
store.clear()
for pattern in self.patterns:
store.append((pattern,))
self._write_history("patterns")
self.application.update_gui()
def _on_apply(self, *args):
"""Apply accepted changes to projects."""
gaupol.util.set_cursor_busy(self)
edits = removals = 0
changes = self._confirmation_page.get_confirmed_changes()
changed_pages = aeidon.util.get_unique([x[0] for x in changes])
field = self._introduction_page.get_field()
doc = gaupol.util.text_field_to_document(field)
description = _("Correcting texts")
register = aeidon.registers.DO
for page in changed_pages:
indices = [x[1] for x in changes if x[0] is page]
texts = [x[3] for x in changes if x[0] is page]
if indices and texts:
page.project.replace_texts(indices, doc, texts)
page.project.set_action_description(register, description)
edits += len(indices)
indices = [x for i, x in enumerate(indices) if not texts[i]]
if indices and gaupol.conf.text_assistant.remove_blank:
page.project.remove_subtitles(indices)
page.project.group_actions(register, 2, description)
removals += len(indices)
def _populate_encoding_combo(self, custom=None):
"""Populate the encoding combo box, including custom encoding."""
encodings = list(gaupol.conf.encoding.visible)
locale = aeidon.encodings.get_locale_code()
encodings.insert(0, locale)
encodings.append(custom)
while None in encodings:
encodings.remove(None)
encodings = aeidon.util.get_unique(encodings)
encodings = encodings or ["utf_8"]
for i, encoding in enumerate(encodings):
name = aeidon.encodings.code_to_long_name(encoding)
encodings[i] = (encoding, name)
if locale is not None:
name = aeidon.encodings.get_locale_long_name()
encodings[0] = (locale, name)
a = 0 if locale is None else 1
encodings[a:] = sorted(encodings[a:], key=lambda x: x[1])
separator = gaupol.COMBO_SEPARATOR
if self._use_autodetection:
encodings.append((separator, separator))
encodings.append(("auto", _("Auto-detected")))
encodings.append((separator, separator))
encodings.append(("other", _("Other…")))
self._encoding_combo.get_model().clear()