Clear cache rather than reassign, correct ALPHA case check and creation

This commit is contained in:
John Morrison 2022-04-23 07:57:39 +01:00 committed by xs5871
parent bdc2bbb3cf
commit bb8dec907a
2 changed files with 24 additions and 33 deletions

View File

@ -76,6 +76,9 @@ class KeyAttrDict:
except Exception:
return default
def clear(self):
self.__cache.clear()
def __getitem__(self, key):
if DEBUG_OUTPUT:
print(f'__getitem__ {key}')
@ -84,6 +87,8 @@ class KeyAttrDict:
except Exception:
pass
key_upper = key.upper()
# Try all the other weird special cases to get them out of our way:
# This need to be done before or ALPHAS because NO will be parsed as alpha
# Internal, diagnostic, or auxiliary/enhanced keys
@ -105,8 +110,14 @@ class KeyAttrDict:
)
# Basic ASCII letters/numbers don't need anything fancy, so check those
# in the laziest way
elif key in ALL_ALPHAS:
make_key(code=4 + ALL_ALPHAS.index(key), names=(key,))
elif key_upper in ALL_ALPHAS:
make_key(
code=4 + ALL_ALPHAS.index(key_upper),
names=(
key_upper,
key.lower(),
),
)
elif key in ALL_NUMBERS or key in ALL_NUMBER_ALIASES:
try:
offset = ALL_NUMBERS.index(key)
@ -614,27 +625,6 @@ class ConsumerKey(Key):
pass
def register_key_names(key, names=tuple()): # NOQA
'''
Names are globally unique. If a later key is created with
the same name as an existing entry in `KC`, it will overwrite
the existing entry.
If a name entry is only a single letter, its entry in the KC
object will not be case-sensitive (meaning `names=('A',)` is
sufficient to create a key accessible by both `KC.A` and `KC.a`).
'''
for name in names:
KC[name] = key
if len(name) == 1:
KC[name.upper()] = key
KC[name.lower()] = key
return key
def make_key(code=None, names=tuple(), type=KEY_SIMPLE, **kwargs): # NOQA
'''
Create a new key, aliased by `names` in the KC lookup table.
@ -643,7 +633,11 @@ def make_key(code=None, names=tuple(), type=KEY_SIMPLE, **kwargs): # NOQA
internal key to be handled in a state callback rather than
sent directly to the OS. These codes will autoincrement.
See register_key_names() for details on the assignment.
Names are globally unique. If a later key is created with
the same name as an existing entry in `KC`, it will overwrite
the existing entry.
Names are case sensitive.
All **kwargs are passed to the Key constructor
'''
@ -670,7 +664,8 @@ def make_key(code=None, names=tuple(), type=KEY_SIMPLE, **kwargs): # NOQA
key = constructor(code=code, **kwargs)
register_key_names(key, names)
for name in names:
KC[name] = key
gc.collect()

View File

@ -99,8 +99,7 @@ class TestKmkKeys(unittest.TestCase):
class TestKeys_dot(unittest.TestCase):
def setUp(self):
global KC
KC = KeyAttrDict()
KC.clear()
def test_expected_code_uppercase(self):
assert 4 == KC.A.code
@ -150,8 +149,7 @@ class TestKeys_dot(unittest.TestCase):
class TestKeys_index(unittest.TestCase):
def setUp(self):
global KC
KC = KeyAttrDict()
KC.clear()
def test_expected_code_uppercase(self):
assert 4 == KC['A'].code
@ -196,8 +194,7 @@ class TestKeys_index(unittest.TestCase):
class TestKeys_get(unittest.TestCase):
def setUp(self):
global KC
KC = KeyAttrDict()
KC.clear()
def test_expected_code_uppercase(self):
assert 4 == KC.get('A').code
@ -248,8 +245,7 @@ class TestKeys_get(unittest.TestCase):
# order of request doesn't matter
class TestKeys_instances(unittest.TestCase):
def setUp(self):
global KC
KC = KeyAttrDict()
KC.clear()
def test_make_key_new_instance(self):
key1 = make_key(code=1)