From 1fecf8935330acd417370c32eea4f17f133d4776 Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 6 Apr 2010 22:26:38 +0200 Subject: ext.direction: simplification + docs --- ranger/ext/direction.py | 150 ++++++++++++++++-------------------------------- 1 file changed, 51 insertions(+), 99 deletions(-) diff --git a/ranger/ext/direction.py b/ranger/ext/direction.py index 30eb87ce..1cdaa97b 100644 --- a/ranger/ext/direction.py +++ b/ranger/ext/direction.py @@ -13,109 +13,61 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -class NoDefault(object): - pass - -class Direction(object): - """An object with a down and right method""" - def __init__(self, right=None, down=None, absolute=False, - percent=False, pages=False, **keywords): - self.has_explicit_direction = False - - if 'up' in keywords: - self.down = -keywords['up'] - else: - self.down = down - - if 'left' in keywords: - self.right = -keywords['left'] - else: - self.right = right - - if 'relative' in keywords: - self.absolute = not relative - else: - self.absolute = absolute - - if 'default' in keywords: - self.default = keywords['default'] - else: - self.default = NoDefault - - self.original_down = self.down - self.original_right = self.right - - self.percent = percent - self.pages = pages - - @property +""" +Directions provide convenience methods for movement operations. + +Direction objects are handled just like dicts but provide +methods like up() and down() which give you the correct value +for the vertical direction, even if only the "up" or "down" key +has been defined. + +Example application: +d = Direction(down=5) +print(d.up()) # prints -5 +print(bool(d.horizontal())) # False, since no horizontal direction is defined +""" + +class Direction(dict): + __doc__ = __doc__ # for nicer pydoc + + def __init__(self, **keywords): + dict.__init__(self, keywords) + + def copy(self): + return Direction(**self) + + def _get_bool(self, first, second, fallback=None): + try: return self[first] + except: + try: return not self[second] + except: return fallback + + def _get_direction(self, first, second, fallback=0): + try: return self[first] + except: + try: return -self[second] + except: return fallback + def up(self): - if self.down is None: - return None - return -self.down + return -Direction.down(self) - @property - def left(self): - if self.right is None: - return None - return -self.right + def down(self): + return Direction._get_direction(self, 'down', 'up') - @property - def relative(self): - return not self.absolute + def right(self): + return Direction._get_direction(self, 'right', 'left') - def down_or_default(self, default): - if self.has_been_modified: - return self.down - return default + def absolute(self): + return Direction._get_bool(self, 'absolute', 'relative') - def steps_down(self, page_length=10): - if self.pages: - return self.down * page_length - else: - return self.down + def left(self): + return -Direction.right(self) - def steps_right(self, page_length=10): - if self.pages: - return self.right * page_length - else: - return self.right + def relative(self): + return not Direction.absolute(self) - def copy(self): - new = type(self)() - new.__dict__.update(self.__dict__) - return new - - def __mul__(self, other): - copy = self.copy() - if self.absolute: - if self.down is not None: - copy.down = other - if self.right is not None: - copy.right = other - else: - if self.down is not None: - copy.down *= other - if self.right is not None: - copy.right *= other - copy.original_down = self.original_down - copy.original_right = self.original_right - return copy - __rmul__ = __mul__ - - def __str__(self): - s = ['') - return ''.join(s) + def vertical(self): + return set(self) & set(['up', 'down']) + + def horizontal(self): + return set(self) & set(['left', 'right']) -- cgit 1.4.1-2-gfad0