Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@aeidon.deco.revertable
def set_text(self, index, doc, value, register=-1):
"""Set the value of `doc`'s text."""
subtitle = self.subtitles[index]
orig_value = subtitle.get_text(doc)
if value == orig_value: return
subtitle.set_text(doc, value)
action = aeidon.RevertableAction(register=register)
action.docs = (doc,)
action.description = _("Editing text")
action.revert_function = self.set_text
action.revert_args = (index, doc, orig_value)
self.register_action(action)
signal = self.get_text_signal(doc)
self.emit(signal, (index,))
@aeidon.deco.revertable
@aeidon.deco.notify_frozen
def replace_texts(self, indices, doc, texts, register=-1):
"""Replace texts in `doc`'s `indices` with `texts`."""
orig_texts = [self.subtitles[i].get_text(doc) for i in indices]
for i, index in enumerate(indices):
self.subtitles[index].set_text(doc, texts[i])
action = aeidon.RevertableAction(register=register)
action.docs = (doc,)
action.description = _("Replacing texts")
action.revert_function = self.replace_texts
action.revert_args = (indices, doc, orig_texts)
self.register_action(action)
self.emit(self.get_text_signal(doc), indices)
@aeidon.deco.revertable
def adjust_durations(self, indices=None, speed=None, lengthen=False,
shorten=False, minimum=None, maximum=None, gap=None,
register=-1):
"""
Lengthen or shorten durations by changing end positions.
`indices` can be ``None`` to process all subtitles. `speed` is reading
speed in characters per second. `lengthen` is ``True`` to increase
durations to match reading speed. `shorten` is ``True`` to decrease
durations to match reading speed. `maximum` is the longest allowed
duration in seconds. `minimum` is the shortest allowed duration in
seconds. `gap` is seconds to be left between consecutive subtitles.
Using a gap of at least zero is always a good idea if overlapping
is not desired. Return changed indices.
"""
new_indices = []
@aeidon.deco.revertable
def italicize(self, indices, doc, register=-1):
"""Surround texts with italic markup."""
new_texts = []
markup = self.get_markup(doc)
re_italic_tag = markup.italic_tag
for index in indices:
text = self.subtitles[index].get_text(doc)
text = re_italic_tag.sub("", text)
text = markup.italicize(text)
new_texts.append(text)
self.replace_texts(indices, doc, new_texts, register=register)
self.set_action_description(register, _("Italicizing"))
@aeidon.deco.revertable
def set_translation_text(self, index, value, register=-1):
"""Set the value of translation document's text."""
return self.set_text(index,
aeidon.documents.TRAN,
value,
register=register)
@aeidon.deco.revertable
def capitalize(self, indices, doc, patterns, register=-1):
"""
Capitalize texts as defined by `patterns`.
`indices` can be ``None`` to process all subtitles. `patterns` should
be a sequence of instances of :class:`aeidon.Pattern`. Raise
:exc:`re.error` if a bad regular expression among `patterns`.
"""
new_indices = []
new_texts = []
parser = self.get_parser(doc)
patterns = [x for x in patterns if x.enabled]
indices = indices or self.get_all_indices()
for indices in aeidon.util.get_ranges(indices):
cap_next = False
for index in indices:
@aeidon.deco.revertable
def transform_positions(self, indices, p1, p2, register=-1):
"""
Change positions by a linear two-point correction.
`indices` can be ``None`` to process all subtitles.
`p1` and `p2` should be tuples of index, position.
"""
new_subtitles = []
indices = indices or self.get_all_indices()
coefficient, constant = self._get_transform(p1, p2)
for index in indices:
subtitle = self.subtitles[index].copy()
subtitle.scale_positions(coefficient)
subtitle.shift_positions(constant)
new_subtitles.append(subtitle)
self.replace_positions(indices, new_subtitles, register=register)
@aeidon.deco.revertable
def remove_dialogue_dashes(self, indices, doc, register=-1):
"""Remove dialogue dashes from all lines of texts."""
new_texts = []
parser = self.get_parser(doc)
for index in indices:
subtitle = self.subtitles[index]
parser.set_text(subtitle.get_text(doc))
parser.set_regex(r"^[\-\–\—]\s*")
parser.replacement = ""
parser.replace_all()
new_texts.append(parser.get_text())
self.replace_texts(indices, doc, new_texts, register=register)
self.set_action_description(register, _("Removing dialogue dashes"))
@aeidon.deco.revertable
def set_main_text(self, index, value, register=-1):
"""Set the value of main document's text."""
return self.set_text(index,
aeidon.documents.MAIN,
value,
register=register)
@aeidon.deco.revertable
def unitalicize(self, indices, doc, register=-1):
"""Remove any italic markup surrounding texts."""
new_texts = []
markup = self.get_markup(doc)
re_italic_tag = markup.italic_tag
for index in indices:
text = self.subtitles[index].get_text(doc)
text = re_italic_tag.sub("", text)
new_texts.append(text)
self.replace_texts(indices, doc, new_texts, register=register)
self.set_action_description(register, _("Unitalicizing"))