本文整理汇总了Python中translate.storage.placeables.StringElem类的典型用法代码示例。如果您正苦于以下问题:Python StringElem类的具体用法?Python StringElem怎么用?Python StringElem使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StringElem类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse
def parse(tree, parse_funcs):
"""Parse placeables from the given string or sub-tree by using the
parsing functions provided.
The output of this function is **heavily** dependent on the order of the
parsing functions. This is because of the algorithm used.
An over-simplification of the algorithm: the leaves in the ``StringElem``
tree are expanded to the output of the first parsing function in
``parse_funcs``. The next level of recursion is then started on the new
set of leaves with the used parsing function removed from
``parse_funcs``.
:type tree: unicode|StringElem
:param tree: The string or string element sub-tree to parse.
:type parse_funcs: A list of parsing functions. It must take exactly
one argument (a ``unicode`` string to parse) and
return a list of ``StringElem``s which, together,
form the original string. If nothing could be
parsed, it should return ``None``.
"""
if isinstance(tree, unicode):
tree = StringElem(tree)
if not parse_funcs:
return tree
parse_func = parse_funcs[0]
for leaf in tree.flatten():
# FIXME: we might rather want to test for editability, but for now this
# works better
if not leaf.istranslatable:
continue
unileaf = unicode(leaf)
if not unileaf:
continue
subleaves = parse_func(unileaf)
if subleaves is not None:
if len(subleaves) == 1 and isinstance(subleaves[0], type(leaf)) and leaf == subleaves[0]:
pass
elif isinstance(leaf, unicode):
parent = tree.get_parent_elem(leaf)
if parent is not None:
if len(parent.sub) == 1:
parent.sub = subleaves
leaf = parent
else:
leafindex = parent.sub.index(leaf)
parent.sub[leafindex] = StringElem(subleaves)
leaf = parent.sub[leafindex]
else:
leaf.sub = subleaves
parse(leaf, parse_funcs[1:])
if isinstance(leaf, StringElem):
leaf.prune()
return tree
示例2: xml_to_strelem
def xml_to_strelem(dom_node, xml_space="preserve"):
if dom_node is None:
return StringElem()
if isinstance(dom_node, basestring):
dom_node = etree.fromstring(dom_node)
normalize_xml_space(dom_node, xml_space, remove_start=True)
result = StringElem()
if dom_node.text:
result.sub.append(StringElem(unicode(dom_node.text)))
for child_dom_node in dom_node:
result.sub.append(make_placeable(child_dom_node, xml_space))
if child_dom_node.tail:
result.sub.append(StringElem(unicode(child_dom_node.tail)))
result.prune()
return result
示例3: set_text
def set_text(self, text, update=False):
"""Set the text rendered in this text box.
Uses C{gtk.TextBuffer.set_text()}.
@type text: str|unicode|L{StringElem}
@param text: The text to render in this text box."""
if not isinstance(text, StringElem):
text = StringElem(text)
if self.elem is None:
self.elem = StringElem(u'')
if text is not self.elem:
# If text is self.elem, we are busy with a refresh and we should remember the selected element.
self.selected_elem = None
self.selected_elem_index = None
# We have to edit the existing .elem for the sake of the undo controller
if self.placeables_controller:
self.elem.sub = [elem_parse(text, self.placeables_controller.get_parsers_for_textbox(self))]
self.elem.prune()
else:
self.elem.sub = [text]
self.update_tree()
elif update:
self.update_tree()
self.emit("changed")
示例4: update_tree
def update_tree(self, text=None):
if not self.placeables_controller:
return
if not isinstance(text, StringElem):
return
if self.elem is None:
self.elem = StringElem(u'')
if text is not self.elem:
self.elem.sub = [text]
self.elem.prune()
self.add_default_gui_info(self.elem)
self.buffer.handler_block_by_func(self._on_delete_range)
self.buffer.handler_block_by_func(self._on_insert_text)
self.elem.gui_info.render()
self.show_suggestion()
self.buffer.handler_unblock_by_func(self._on_delete_range)
self.buffer.handler_unblock_by_func(self._on_insert_text)
tagtable = self.buffer.get_tag_table()
def remtag(tag, data):
tagtable.remove(tag)
# FIXME: The following line caused the program to segfault, so it's removed (for now).
#tagtable.foreach(remtag)
# At this point we have a tree of string elements with GUI info.
self.apply_gui_info(text)
示例5: _rewrite_prepend_append
def _rewrite_prepend_append(self, string, prepend, append=None):
if append is None:
append = prepend
if not isinstance(string, StringElem):
string = StringElem(string)
string.sub.insert(0, prepend)
if six.text_type(string).endswith(u'\n'):
# Try and remove the last character from the tree
try:
lastnode = string.flatten()[-1]
if isinstance(lastnode.sub[-1], six.text_type):
lastnode.sub[-1] = lastnode.sub[-1].rstrip(u'\n')
except IndexError:
pass
string.sub.append(append + u'\n')
else:
string.sub.append(append)
return string
示例6: test_prune
def test_prune(self):
elem = StringElem(u'foo')
child = StringElem(u'bar')
elem.sub.append(child)
elem.prune()
assert elem == StringElem(u'foobar')
示例7: TextBox
class TextBox(gtk.TextView):
"""
A C{gtk.TextView} extended to work with our nifty L{StringElem} parsed
strings.
"""
__gtype_name__ = 'TextBox'
__gsignals__ = {
'element-selected': (SIGNAL_RUN_FIRST, None, (object,)),
'key-pressed': (SIGNAL_RUN_LAST, bool, (object, str)),
'refreshed': (SIGNAL_RUN_FIRST, None, (object,)),
'text-deleted': (SIGNAL_RUN_LAST, bool, (object, object, int, int, object)),
'text-inserted': (SIGNAL_RUN_LAST, bool, (object, int, object)),
'changed': (SIGNAL_RUN_LAST, None, ()),
}
SPECIAL_KEYS = {
'alt-down': [(gtk.keysyms.Down, gtk.gdk.MOD1_MASK)],
'alt-left': [(gtk.keysyms.Left, gtk.gdk.MOD1_MASK)],
'alt-right': [(gtk.keysyms.Right, gtk.gdk.MOD1_MASK)],
'enter': [(gtk.keysyms.Return, 0), (gtk.keysyms.KP_Enter, 0)],
'ctrl-enter':[(gtk.keysyms.Return, gtk.gdk.CONTROL_MASK), (gtk.keysyms.KP_Enter, gtk.gdk.CONTROL_MASK)],
'ctrl-shift-enter':[(gtk.keysyms.Return, gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK), (gtk.keysyms.KP_Enter, gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK)],
'shift-tab': [(gtk.keysyms.ISO_Left_Tab, gtk.gdk.SHIFT_MASK), (gtk.keysyms.Tab, gtk.gdk.SHIFT_MASK)],
'ctrl-tab': [(gtk.keysyms.ISO_Left_Tab, gtk.gdk.CONTROL_MASK), (gtk.keysyms.Tab, gtk.gdk.CONTROL_MASK)],
'ctrl-shift-tab': [(gtk.keysyms.ISO_Left_Tab, gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK), (gtk.keysyms.Tab, gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK)],
}
"""A table of name-keybinding mappings. The name (key) is passed as the
second parameter to the 'key-pressed' event."""
unselectables = [StringElem]
"""A list of classes that should not be selectable with Alt+Left or Alt+Right."""
# INITIALIZERS #
def __init__(self, main_controller, text=None, selector_textbox=None, role=None):
"""Constructor.
@type main_controller: L{virtaal.controllers.main_controller}
@param main_controller: The main controller instance.
@type text: String
@param text: The initial text to set in the new text box. Optional.
@type selector_textbox: C{TextBox}
@param selector_textbox: The text box in which placeable selection
(@see{select_elem}) should happen. Optional."""
super(TextBox, self).__init__()
self.buffer = self.get_buffer()
self.elem = None
self.main_controller = main_controller
self.placeables_controller = main_controller.placeables_controller
self.refresh_actions = []
self.refresh_cursor_pos = -1
self.role = role
self.selector_textbox = selector_textbox or self
self.selector_textboxes = [selector_textbox or self]
self.selected_elem = None
self.selected_elem_index = None
self._suggestion = None
self.undo_controller = main_controller.undo_controller
self.__connect_default_handlers()
if self.placeables_controller is None or self.undo_controller is None:
# This should always happen, because the text boxes are created
# when the unit controller is created, which happens before the
# creation of the placeables- and undo controllers.
self.__controller_connect_id = main_controller.connect('controller-registered', self.__on_controller_register)
if text:
self.set_text(text)
def __connect_default_handlers(self):
self.connect('button-press-event', self._on_event_remove_suggestion)
self.connect('focus-out-event', self._on_event_remove_suggestion)
self.connect('key-press-event', self._on_key_pressed)
self.connect('move-cursor', self._on_event_remove_suggestion)
self.buffer.connect('insert-text', self._on_insert_text)
self.buffer.connect('delete-range', self._on_delete_range)
self.buffer.connect('begin-user-action', self._on_begin_user_action)
self.buffer.connect('end-user-action', self._on_end_user_action)
def _get_suggestion(self):
return self._suggestion
def _set_suggestion(self, value):
if value is None:
self.hide_suggestion()
self._suggestion = None
return
if not (isinstance(value, dict) and \
'text' in value and value['text'] and \
'offset' in value and value['offset'] >= 0):
raise ValueError('invalid suggestion dictionary: %s' % (value))
if self.suggestion_is_visible():
self.suggestion = None
self._suggestion = value
self.show_suggestion()
suggestion = property(_get_suggestion, _set_suggestion)
# OVERRIDDEN METHODS #
def get_stringelem(self):
if self.elem is None:
#.........这里部分代码省略.........