summary refs log blame commit diff stats
path: root/doc/pydoc/ranger.ext.openstruct.html
blob: 798c7352a56c1586f35fd5ce68bec37ea7bb0b2c (plain) (tree)




















































                                                                                                                                                                                                                                                 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module ranger.ext.openstruct</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.<a href="ranger.ext.html"><font color="#ffffff">ext</font></a>.openstruct</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/ranger/ext/openstruct.py">/home/hut/work/ranger/ranger/ext/openstruct.py</a></font></td></tr></table>
    <p></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ee77aa">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
    
<tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><dl>
<dt><font face="helvetica, arial"><a href="builtins.html#object">builtins.object</a>
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="ranger.ext.openstruct.html#OpenStruct">OpenStruct</a>
</font></dt></dl>
</dd>
</dl>
 <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#000000" face="helvetica, arial"><a name="OpenStruct">class <strong>OpenStruct</strong></a>(<a href="builtins.html#object">builtins.object</a>)</font></td></tr>
    
<tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%">Methods defined here:<br>
<dl><dt><a name="OpenStruct-__contains__"><strong>__contains__</strong></a>(self, key)</dt></dl>

<dl><dt><a name="OpenStruct-__getitem__"><strong>__getitem__</strong></a>(self, key)</dt></dl>

<dl><dt><a name="OpenStruct-__init__"><strong>__init__</strong></a>(self, _OpenStruct__dictionary<font color="#909090">=None</font>, **_OpenStruct__keywords)</dt></dl>

<dl><dt><a name="OpenStruct-__setitem__"><strong>__setitem__</strong></a>(self, key, value)</dt></dl>

<hr>
Data descriptors defined here:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd>
</dl>
</td></tr></table></td></tr></table>
</body></html>
] = firstarg return self.add_binding(*args[:-1], **keywords) def decorator_function(func): keywords = {FUNC:func} self.add(*args, **keywords) return func return decorator_function def add_binding(self, *keys, **actions): assert keys bind = Binding(keys, actions) for key in keys: self.set(translate_keys(key), bind) def __getitem__(self, key): return self.traverse(translate_keys(key)) class Binding(object): """The keybinding object""" def __init__(self, keys, actions): assert hasattr(keys, '__iter__') assert isinstance(actions, dict) self.actions = actions try: self.function = self.actions[FUNC] except KeyError: self.function = None self.has_direction = False else: argnames = getargspec(self.function)[0] try: self.has_direction = actions['with_direction'] except KeyError: self.has_direction = DIRECTION in argnames try: self.direction = self.actions[DIRARG] except KeyError: self.direction = None try: alias = self.actions[ALIASARG] except KeyError: self.alias = None else: self.alias = tuple(translate_keys(alias)) class KeyBuffer(object): """The evaluator and storage for pressed keys""" def __init__(self, keymap, direction_keys): self.assign(keymap, direction_keys) def assign(self, keymap, direction_keys): self.keymap = keymap self.direction_keys = direction_keys def add(self, key): if self.failure: return None assert isinstance(key, int) assert key >= 0 # evaluate quantifiers if self.eval_quantifier and self._do_eval_quantifier(key): return # evaluate the command if self.eval_command and self._do_eval_command(key): return # evaluate (the first number of) the direction-quantifier if self.eval_quantifier and self._do_eval_quantifier(key): return # evaluate direction keys {j,k,gg,pagedown,...} if not self.eval_command: self._do_eval_direction(key) def _do_eval_direction(self, key): # swap quant and direction_quant in bindings like '<dir>' if self.quant is not None and self.command is None \ and self.direction_quant is None: self.direction_quant = self.quant self.quant = None try: assert isinstance(self.dir_tree_pointer, dict) self.dir_tree_pointer = self.dir_tree_pointer[key] except KeyError: self.failure = True else: self._direction_try_to_finish() def _direction_try_to_finish(self, rec=MAX_ALIAS_RECURSION): if rec <= 0: self.failure = True return None match = self.dir_tree_pointer assert isinstance(match, (Binding, dict, KeyMap)) if isinstance(match, KeyMap): self.dir_tree_pointer = self.dir_tree_pointer._tree match = self.dir_tree_pointer if isinstance(self.dir_tree_pointer, Binding): if 'alias' in match.actions: self.dir_tree_pointer = self.direction_keys.traverse( match.alias) self._direction_try_to_finish(rec - 1) else: direction = match.actions['dir'] * self.direction_quant self.directions.append(direction) self.direction_quant = None self.eval_command = True self._try_to_finish() def _do_eval_quantifier(self, key): if self.eval_command: tree = self.tree_pointer else: tree = self.dir_tree_pointer if is_ascii_digit(key) and ANYKEY not in tree: attr = self.eval_command and 'quant' or 'direction_quant' if getattr(self, attr) is None: setattr(self, attr, 0) setattr(self, attr, getattr(self, attr) * 10 + key - 48) else: self.eval_quantifier = False return None return True def _do_eval_command(self, key): assert isinstance(self.tree_pointer, dict), self.tree_pointer try: self.tree_pointer = self.tree_pointer[key] except TypeError: print(self.tree_pointer) self.failure = True return None except KeyError: if DIRKEY in self.tree_pointer: self.eval_command = False self.eval_quantifier = True self.tree_pointer = self.tree_pointer[DIRKEY] assert isinstance(self.tree_pointer, (Binding, dict)) self.dir_tree_pointer = self.direction_keys._tree elif ANYKEY in self.tree_pointer: self.matches.append(key) self.tree_pointer = self.tree_pointer[ANYKEY] assert isinstance(self.tree_pointer, (Binding, dict)) self._try_to_finish() else: self.failure = True return None else: if isinstance(self.tree_pointer, dict): try: self.command = self.tree_pointer[PASSIVE_ACTION] except (KeyError, TypeError): self.command = None self._try_to_finish() def _try_to_finish(self, rec=MAX_ALIAS_RECURSION): if rec <= 0: self.failure = True return None assert isinstance(self.tree_pointer, (Binding, dict, KeyMap)) if isinstance(self.tree_pointer, KeyMap): self.tree_pointer = self.tree_pointer._tree if isinstance(self.tree_pointer, Binding): if 'alias' in self.tree_pointer.actions: self.tree_pointer = self.keymap.traverse( translate_keys(self.tree_pointer.actions['alias'])) self._try_to_finish(rec - 1) else: self.command = self.tree_pointer self.done = True def clear(self): self.failure = False self.done = False self.quant = None self.matches = [] self.command = None self.direction_quant = None self.directions = [] self.all_keys = [] self.tree_pointer = self.keymap._tree self.dir_tree_pointer = self.direction_keys._tree self.eval_quantifier = True self.eval_command = True def __str__(self): """returns a concatenation of all characters""" return "".join(to_string(c) for c in self.all_keys) def simulate_press(self, string): for char in translate_keys(string): self.add(char) if self.done: return self.command if self.failure: break return self.command key_map = { 'dir': DIRKEY, 'any': ANYKEY, 'psv': PASSIVE_ACTION, 'cr': ord("\n"), 'enter': ord("\n"), 'space': ord(" "), 'space': ord(" "), 'down': curses.KEY_DOWN, 'up': curses.KEY_UP, 'left': curses.KEY_LEFT, 'right': curses.KEY_RIGHT, 'mouse': curses.KEY_MOUSE, 'resize': curses.KEY_RESIZE, 'pagedown': curses.KEY_NPAGE, 'pageup': curses.KEY_PPAGE, 'home': curses.KEY_HOME, 'end': curses.KEY_END, 'tab': ord('\t'), } for char in ascii_lowercase: key_map['c-' + char] = ord(char) - 96 def translate_keys(obj): """ Translate a keybinding to a sequence of integers Example: lol<CR> => (108, 111, 108, 10) """ assert isinstance(obj, (tuple, int, str)) if isinstance(obj, tuple): for char in obj: yield char elif isinstance(obj, int): yield obj elif isinstance(obj, str): in_brackets = False bracket_content = None for char in obj: if in_brackets: if char == '>': in_brackets = False string = ''.join(bracket_content).lower() try: yield key_map[string] except KeyError: yield ord('<') for c in bracket_content: yield ord(c) yield ord('>') else: bracket_content.append(char) else: if char == '<': in_brackets = True bracket_content = [] else: yield ord(char) if in_brackets: yield ord('<') for c in bracket_content: yield ord(c)