Init
im going to bed -=-
This commit is contained in:
24
lib/invoke/vendor/lexicon/__init__.py
vendored
Normal file
24
lib/invoke/vendor/lexicon/__init__.py
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
from ._version import __version_info__, __version__ # noqa
|
||||
from .attribute_dict import AttributeDict
|
||||
from .alias_dict import AliasDict
|
||||
|
||||
|
||||
class Lexicon(AttributeDict, AliasDict):
|
||||
def __init__(self, *args, **kwargs):
|
||||
# Need to avoid combining AliasDict's initial attribute write on
|
||||
# self.aliases, with AttributeDict's __setattr__. Doing so results in
|
||||
# an infinite loop. Instead, just skip straight to dict() for both
|
||||
# explicitly (i.e. we override AliasDict.__init__ instead of extending
|
||||
# it.)
|
||||
# NOTE: could tickle AttributeDict.__init__ instead, in case it ever
|
||||
# grows one.
|
||||
dict.__init__(self, *args, **kwargs)
|
||||
dict.__setattr__(self, "aliases", {})
|
||||
|
||||
def __getattr__(self, key):
|
||||
# Intercept deepcopy/etc driven access to self.aliases when not
|
||||
# actually set. (Only a problem for us, due to abovementioned combo of
|
||||
# Alias and Attribute Dicts, so not solvable in a parent alone.)
|
||||
if key == "aliases" and key not in self.__dict__:
|
||||
self.__dict__[key] = {}
|
||||
return super(Lexicon, self).__getattr__(key)
|
||||
BIN
lib/invoke/vendor/lexicon/__pycache__/__init__.cpython-314.pyc
vendored
Normal file
BIN
lib/invoke/vendor/lexicon/__pycache__/__init__.cpython-314.pyc
vendored
Normal file
Binary file not shown.
BIN
lib/invoke/vendor/lexicon/__pycache__/_version.cpython-314.pyc
vendored
Normal file
BIN
lib/invoke/vendor/lexicon/__pycache__/_version.cpython-314.pyc
vendored
Normal file
Binary file not shown.
BIN
lib/invoke/vendor/lexicon/__pycache__/alias_dict.cpython-314.pyc
vendored
Normal file
BIN
lib/invoke/vendor/lexicon/__pycache__/alias_dict.cpython-314.pyc
vendored
Normal file
Binary file not shown.
BIN
lib/invoke/vendor/lexicon/__pycache__/attribute_dict.cpython-314.pyc
vendored
Normal file
BIN
lib/invoke/vendor/lexicon/__pycache__/attribute_dict.cpython-314.pyc
vendored
Normal file
Binary file not shown.
2
lib/invoke/vendor/lexicon/_version.py
vendored
Normal file
2
lib/invoke/vendor/lexicon/_version.py
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
__version_info__ = (2, 0, 1)
|
||||
__version__ = ".".join(map(str, __version_info__))
|
||||
95
lib/invoke/vendor/lexicon/alias_dict.py
vendored
Normal file
95
lib/invoke/vendor/lexicon/alias_dict.py
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
class AliasDict(dict):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(AliasDict, self).__init__(*args, **kwargs)
|
||||
self.aliases = {}
|
||||
|
||||
def alias(self, from_, to):
|
||||
self.aliases[from_] = to
|
||||
|
||||
def unalias(self, from_):
|
||||
del self.aliases[from_]
|
||||
|
||||
def aliases_of(self, name):
|
||||
"""
|
||||
Returns other names for given real key or alias ``name``.
|
||||
|
||||
If given a real key, returns its aliases.
|
||||
|
||||
If given an alias, returns the real key it points to, plus any other
|
||||
aliases of that real key. (The given alias itself is not included in
|
||||
the return value.)
|
||||
"""
|
||||
names = []
|
||||
key = name
|
||||
# self.aliases keys are aliases, not realkeys. Easy test to see if we
|
||||
# should flip around to the POV of a realkey when given an alias.
|
||||
if name in self.aliases:
|
||||
key = self.aliases[name]
|
||||
# Ensure the real key shows up in output.
|
||||
names.append(key)
|
||||
# 'key' is now a realkey, whose aliases are all keys whose value is
|
||||
# itself. Filter out the original name given.
|
||||
names.extend(
|
||||
[k for k, v in self.aliases.items() if v == key and k != name]
|
||||
)
|
||||
return names
|
||||
|
||||
def _handle(self, key, value, single, multi, unaliased):
|
||||
# Attribute existence test required to not blow up when deepcopy'd
|
||||
if key in getattr(self, "aliases", {}):
|
||||
target = self.aliases[key]
|
||||
# Single-string targets
|
||||
if isinstance(target, str):
|
||||
return single(self, target, value)
|
||||
# Multi-string targets
|
||||
else:
|
||||
if multi:
|
||||
return multi(self, target, value)
|
||||
else:
|
||||
for subkey in target:
|
||||
single(self, subkey, value)
|
||||
else:
|
||||
return unaliased(self, key, value)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
def single(d, target, value):
|
||||
d[target] = value
|
||||
|
||||
def unaliased(d, key, value):
|
||||
super(AliasDict, d).__setitem__(key, value)
|
||||
|
||||
return self._handle(key, value, single, None, unaliased)
|
||||
|
||||
def __getitem__(self, key):
|
||||
def single(d, target, value):
|
||||
return d[target]
|
||||
|
||||
def unaliased(d, key, value):
|
||||
return super(AliasDict, d).__getitem__(key)
|
||||
|
||||
def multi(d, target, value):
|
||||
msg = "Multi-target aliases have no well-defined value and can't be read." # noqa
|
||||
raise ValueError(msg)
|
||||
|
||||
return self._handle(key, None, single, multi, unaliased)
|
||||
|
||||
def __contains__(self, key):
|
||||
def single(d, target, value):
|
||||
return target in d
|
||||
|
||||
def multi(d, target, value):
|
||||
return all(subkey in self for subkey in self.aliases[key])
|
||||
|
||||
def unaliased(d, key, value):
|
||||
return super(AliasDict, d).__contains__(key)
|
||||
|
||||
return self._handle(key, None, single, multi, unaliased)
|
||||
|
||||
def __delitem__(self, key):
|
||||
def single(d, target, value):
|
||||
del d[target]
|
||||
|
||||
def unaliased(d, key, value):
|
||||
return super(AliasDict, d).__delitem__(key)
|
||||
|
||||
return self._handle(key, None, single, None, unaliased)
|
||||
16
lib/invoke/vendor/lexicon/attribute_dict.py
vendored
Normal file
16
lib/invoke/vendor/lexicon/attribute_dict.py
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
class AttributeDict(dict):
|
||||
def __getattr__(self, key):
|
||||
try:
|
||||
return self[key]
|
||||
except KeyError:
|
||||
# to conform with __getattr__ spec
|
||||
raise AttributeError(key)
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
self[key] = value
|
||||
|
||||
def __delattr__(self, key):
|
||||
del self[key]
|
||||
|
||||
def __dir__(self):
|
||||
return dir(type(self)) + list(self.keys())
|
||||
Reference in New Issue
Block a user