aboutsummaryrefslogtreecommitdiff
path: root/thcolor/_ref.py
diff options
context:
space:
mode:
authorThomas Touhey <thomas@touhey.fr>2019-05-21 15:33:48 +0200
committerThomas Touhey <thomas@touhey.fr>2019-05-21 15:33:48 +0200
commitb3edd31df8c96035d70c0216e444d6dd9951699f (patch)
tree1990a3b3d82b2f078777074512c6c099423a709f /thcolor/_ref.py
parent191aa991c32b9e0124bf8256c1ae7c130b74b248 (diff)
Added an alias directive.
Diffstat (limited to 'thcolor/_ref.py')
-rwxr-xr-xthcolor/_ref.py56
1 files changed, 48 insertions, 8 deletions
diff --git a/thcolor/_ref.py b/thcolor/_ref.py
index 1229634..8a39cbb 100755
--- a/thcolor/_ref.py
+++ b/thcolor/_ref.py
@@ -252,6 +252,23 @@ class Reference:
return self._value
# ---
+ # Function helper.
+ # ---
+
+ def alias(*names):
+ """ Decorator for aliasing with another name. Defines a lot, you
+ know. """
+
+ def _decorator(func):
+ if not hasattr(func, '_ref_aliases'):
+ func._ref_aliases = ()
+
+ func._ref_aliases += names
+ return func
+
+ return _decorator
+
+ # ---
# Function and named color getters.
# ---
@@ -275,18 +292,41 @@ class Reference:
def __getitem__(self, name):
fref = self._fref
+ found = False
- # Find the method.
-
- if name[0:1] == '_' or name in ('functions', 'named',
- 'default'):
- raise KeyError(repr(name))
+ # First, check if the function name is valid and if the
+ # method exists.
members = dict(_getmembers(fref, predicate = _ismethod))
+ validname = lambda n: type(n) == str and n[0:1] != '_' \
+ and n not in ('functions', 'named', 'default', 'alias')
- try:
- method = members[name]
- except (KeyError, AssertionError):
+ if validname(name):
+ try:
+ method = members[name]
+ found = True
+ except (KeyError, AssertionError):
+ pass
+
+ # Then, if we haven't found the method, check the aliases.
+
+ if not found:
+ for member in members.values():
+ try:
+ aliases = member._ref_aliases
+ except (AttributeError, AssertionError):
+ continue
+
+ if name not in aliases:
+ continue
+ method = member
+ found = True
+ break
+
+ # If we still haven't found the method, well… time to raise
+ # the not found exception.
+
+ if not found:
raise KeyError(repr(name))
# Make a function separated from the class, copy the