aboutsummaryrefslogtreecommitdiff
path: root/thcolor/_ref.py
diff options
context:
space:
mode:
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