Package translate :: Package storage :: Package placeables :: Module strelem :: Class StringElem |
[hide private]
[frames] | no frames]

# Class StringElem

source code

This class represents a sub-tree of a string parsed into a rich structure. It is also the base class of all placeables.

Instance Methods | [hide private]
__init__(self, sub=None, id=None, rid=None, xid=None, **kwargs) x.__init__(...) initializes x; see x.__class__.__doc__ for signature | source code
__add__(self, rhs) Emulate the `unicode` class. | source code
__contains__(self, item) Emulate the `unicode` class. | source code
__eq__(self, rhs) Returns: `True` if (and only if) all members as well as sub-trees are equal. | source code
__ge__(self, rhs) Emulate the `unicode` class. | source code
__getitem__(self, i) Emulate the `unicode` class. | source code
__getslice__(self, i, j) Emulate the `unicode` class. | source code
__gt__(self, rhs) Emulate the `unicode` class. | source code
__iter__(self) Create an iterator of this element's sub-elements. | source code
__le__(self, rhs) Emulate the `unicode` class. | source code
__len__(self) Emulate the `unicode` class. | source code
__lt__(self, rhs) Emulate the `unicode` class. | source code
__mul__(self, rhs) Emulate the `unicode` class. | source code
__ne__(self, rhs) | source code
__radd__(self, lhs) Emulate the `unicode` class. | source code
__rmul__(self, lhs) Emulate the `unicode` class. | source code
__repr__(self) repr(x) | source code
__str__(self) str(x) | source code
__unicode__(self) | source code
apply_to_strings(self, f) Apply `f` to all actual strings in the tree. | source code
copy(self) Returns a copy of the sub-tree. | source code
delete_elem(self, elem) | source code
delete_range(self, start_index, end_index) Delete the text in the range given by the string-indexes `start_index` and `end_index`. | source code
depth_first(self, filter=None) Returns a list of the nodes in the tree in depth-first order. | source code
encode(self, encoding=`'``ascii``'`) More `unicode` class emulation. | source code
elem_offset(self, elem) Find the offset of `elem` in the current tree. | source code
elem_at_offset(self, offset) Get the `StringElem` in the tree that contains the string rendered at the given offset. | source code
find(self, x) Find sub-string `x` in this string tree and return the position at which it starts. | source code
find_elems_with(self, x) Find all elements in the current sub-tree containing `x`. | source code
flatten(self, filter=None) Flatten the tree by returning a depth-first search over the tree's leaves. | source code
get_ancestor_where(self, child, criteria) | source code
get_index_data(self, index) Get info about the specified range in the tree. | source code
get_parent_elem(self, child) Searches the current sub-tree for and returns the parent of the `child` element. | source code
insert(self, offset, text) Insert the given text at the specified offset of this string-tree's string (Unicode) representation. | source code
insert_between(self, left, right, text) Insert the given text between the two parameter `StringElem`s. | source code
bool |
isleaf(self) Whether or not this instance is a leaf node in the `StringElem` tree. | source code
iter_depth_first(self, filter=None) Iterate through the nodes in the tree in dept-first order. | source code
map(self, f, filter=None) Apply `f` to all nodes for which `filter` returned `True` (optional). | source code
print_tree(self, indent=0, verbose=False) Print the tree from the current instance's point in an indented manner. | source code
prune(self) Remove unnecessary nodes to make the tree optimal. | source code
remove_type(self, ptype) Replace nodes with type `ptype` with base `StringElem`s, containing the same sub-elements. | source code
translate(self) Transform the sub-tree according to some class-specific needs. | source code
Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`

Class Methods | [hide private]
parse(cls, pstr) Parse an instance of this class from the start of the given string. |

Class Variables | [hide private]
| renderer = `None` An optional function that returns the Unicode representation of the string.
| sub = ``[``]`` The sub-elements that make up this this string.
| has_content = `True` Whether this string can have sub-elements.
| iseditable = `True` Whether this string should be changable by the user.
| isfragile = `False` Whether this element should be deleted in its entirety when partially deleted.
| istranslatable = `True` Whether this string is translatable into other languages.
| isvisible = `True` Whether this string should be visible to the user.

Properties | [hide private]
Inherited from `object`: `__class__`

Method Details | [hide private]

### __init__(self, sub=None, id=None, rid=None, xid=None, **kwargs) (Constructor) | source code
x.__init__(...) initializes x; see x.__class__.__doc__ for signature Overrides: object.__init__ (inherited documentation)

### __eq__(self, rhs) (Equality operator) | source code
Returns: `True` if (and only if) all members as well as sub-trees are equal. False otherwise.

### __repr__(self) (Representation operator) | source code
repr(x) Overrides: object.__repr__ (inherited documentation)

### __str__(self) (Informal representation operator) | source code
str(x) Overrides: object.__str__ (inherited documentation)

### apply_to_strings(self, f) | source code
Apply `f` to all actual strings in the tree. Parameters: `f` - Must take one (str or unicode) argument and return a string or unicode.

### copy(self) | source code
Returns a copy of the sub-tree. This should be overridden in sub-classes with more data. NOTE: `self.renderer` is not copied.

### delete_range(self, start_index, end_index) | source code
Delete the text in the range given by the string-indexes `start_index` and `end_index`. Partial nodes will only be removed if they are editable. Returns: A `StringElem` representing the removed sub-string, the parent node from which it was deleted as well as the offset at which it was deleted from. `None` is returned for the parent value if the root was deleted. If the parent and offset values are not `None`, `parent.insert(offset, deleted)` effectively undoes the delete.

### elem_offset(self, elem) | source code
Find the offset of `elem` in the current tree. This cannot be reliably used if `self.renderer` is used and even less so if the rendering function renders the string differently upon different calls. In Virtaal the `StringElemGUI.index()` method is used as replacement for this one. Returns: The string index where element `e` starts, or -1 if `e` was not found.

### get_index_data(self, index) | source code
Get info about the specified range in the tree. Returns: A dictionary with the following items: * elem: The element in which `index` resides. * index: Copy of the `index` parameter * offset: The offset of `index` into `'elem'`.

### isleaf(self) | source code
Whether or not this instance is a leaf node in the `StringElem` tree. A node is a leaf node if it is a `StringElem` (not a sub-class) and contains only sub-elements of type `str` or `unicode`. Returns: bool

### parse(cls, pstr) Class Method |
Parse an instance of this class from the start of the given string. This method should be implemented by any sub-class that wants to parseable by translate.storage.placeables.parse. Parameters: `pstr` (unicode) - The string to parse into an instance of this class. Returns: An instance of the current class, or `None` if the string not parseable by this class.

### remove_type(self, ptype) | source code
Replace nodes with type `ptype` with base `StringElem`s, containing the same sub-elements. This is only applicable to elements below the element tree root node.

### translate(self) | source code
Transform the sub-tree according to some class-specific needs. This method should be either overridden in implementing sub-classes or dynamically replaced by specific applications. Returns: The transformed Unicode string representing the sub-tree.

Class Variable Details | [hide private]

  ### iseditable Whether this string should be changable by the user. Not used at the moment. Value:
      True

  ### isfragile Whether this element should be deleted in its entirety when partially deleted. Only checked when `iseditable = False` Value:
      False

  ### isvisible Whether this string should be visible to the user. Not used at the moment. Value:
      True