@@ -3444,17 +3444,46 @@ def __init__(
3444
3444
self ._list [i ][- 2 ] = header_index
3445
3445
for n in self ._list :
3446
3446
logger .error (f'{ n } ' )
3447
+ '''
3448
+
3449
+ # do not read keys.json to self._keys_to_classes
3450
+ # use self._precompute_context_map to populate it dynamically
3451
+ # keys.json will not be part of the package
3452
+
3447
3453
keys_file = path.join(path.dirname(__file__), 'keyboard', 'keys.json')
3448
3454
logger.error(f'{keys_file = }')
3449
3455
with open(keys_file, 'r', encoding='utf-8') as f:
3450
3456
self._keys_to_classes = json.load(f)
3457
+
3458
+ '''
3451
3459
keys_file = path .join (path .dirname (__file__ ), 'keyboard' , 'classes.json' )
3452
- logger .error (f'{ keys_file = } ' )
3460
+ # logger.error(f'{keys_file = }')
3453
3461
with open (keys_file , 'r' , encoding = 'utf-8' ) as f :
3454
3462
self ._classes = json .load (f )
3463
+ # logger.error(f'{self._classes = }')
3464
+ self ._keys_to_classes = self ._precompute_context_map (self ._classes )
3465
+ # logger.error(f'{self._keys_to_classes = }')
3455
3466
self ._needs_update = False
3456
3467
3457
3468
3469
+ def _precompute_context_map (self , results ):
3470
+ """
3471
+ Precompute a map of keys to the classes or contexts they belong to.
3472
+
3473
+ Args:
3474
+ results (dict): The `results` dictionary mapping class names to their keys.
3475
+
3476
+ Returns:
3477
+ dict: A map of keys to their associated contexts.
3478
+ """
3479
+ context_map = {}
3480
+ for class_name , keys in results .items ():
3481
+ for key in keys :
3482
+ if key not in context_map :
3483
+ context_map [key ] = []
3484
+ context_map [key ].append (class_name )
3485
+ return context_map
3486
+
3458
3487
def item (self , an_item_id = None ):
3459
3488
logger .debug (f'{ an_item_id = } ' )
3460
3489
if an_item_id is None :
0 commit comments