From 5a85c6cfb36682cf63eb9343e77f778c941f42bc Mon Sep 17 00:00:00 2001 From: xs5871 Date: Thu, 21 Jul 2022 15:50:30 +0000 Subject: [PATCH] move validators and metas to their respective modules --- kmk/key_validators.py | 28 +--------------------------- kmk/modules/holdtap.py | 17 ++++++++++++++++- kmk/modules/layers.py | 21 ++++++++++++++++++--- kmk/modules/modtap.py | 23 +++++++++++++++++++++-- kmk/modules/oneshot.py | 3 +-- kmk/modules/tapdance.py | 3 +-- kmk/types.py | 27 --------------------------- 7 files changed, 58 insertions(+), 64 deletions(-) diff --git a/kmk/key_validators.py b/kmk/key_validators.py index 6fcf46e..676f221 100644 --- a/kmk/key_validators.py +++ b/kmk/key_validators.py @@ -1,35 +1,9 @@ -from kmk.types import KeySeqSleepMeta, LayerKeyMeta, ModTapKeyMeta, UnicodeModeKeyMeta +from kmk.types import KeySeqSleepMeta, UnicodeModeKeyMeta def key_seq_sleep_validator(ms): return KeySeqSleepMeta(ms) -def layer_key_validator(layer, kc=None): - ''' - Validates the syntax (but not semantics) of a layer key call. We won't - have access to the keymap here, so we can't verify much of anything useful - here (like whether the target layer actually exists). The spirit of this - existing is mostly that Python will catch extraneous args/kwargs and error - out. - ''' - return LayerKeyMeta(layer, kc) - - -def mod_tap_validator( - kc, mods=None, prefer_hold=True, tap_interrupted=False, tap_time=None -): - ''' - Validates that mod tap keys are correctly used - ''' - return ModTapKeyMeta( - kc=kc, - mods=mods, - prefer_hold=prefer_hold, - tap_interrupted=tap_interrupted, - tap_time=tap_time, - ) - - def unicode_mode_key_validator(mode): return UnicodeModeKeyMeta(mode) diff --git a/kmk/modules/holdtap.py b/kmk/modules/holdtap.py index 5515d7d..05f6e26 100644 --- a/kmk/modules/holdtap.py +++ b/kmk/modules/holdtap.py @@ -2,7 +2,6 @@ from micropython import const from kmk.keys import make_argumented_key from kmk.modules import Module -from kmk.types import HoldTapKeyMeta class ActivationType: @@ -20,6 +19,22 @@ class HoldTapKeyState: self.activated = ActivationType.PRESSED +class HoldTapKeyMeta: + def __init__( + self, + tap, + hold, + prefer_hold=True, + tap_interrupted=False, + tap_time=None, + ): + self.tap = tap + self.hold = hold + self.prefer_hold = prefer_hold + self.tap_interrupted = tap_interrupted + self.tap_time = tap_time + + class HoldTap(Module): tap_time = 300 diff --git a/kmk/modules/layers.py b/kmk/modules/layers.py index 6f341e7..5bc4e94 100644 --- a/kmk/modules/layers.py +++ b/kmk/modules/layers.py @@ -1,13 +1,22 @@ '''One layer isn't enough. Adds keys to get to more of them''' -from kmk.key_validators import layer_key_validator from kmk.keys import KC, make_argumented_key -from kmk.modules.holdtap import ActivationType, HoldTap -from kmk.types import HoldTapKeyMeta +from kmk.modules.holdtap import ActivationType, HoldTap, HoldTapKeyMeta from kmk.utils import Debug debug = Debug(__name__) +def layer_key_validator(layer, kc=None): + ''' + Validates the syntax (but not semantics) of a layer key call. We won't + have access to the keymap here, so we can't verify much of anything useful + here (like whether the target layer actually exists). The spirit of this + existing is mostly that Python will catch extraneous args/kwargs and error + out. + ''' + return LayerKeyMeta(layer, kc) + + def layer_key_validator_lt(layer, kc, prefer_hold=False, **kwargs): return HoldTapKeyMeta(tap=kc, hold=KC.MO(layer), prefer_hold=prefer_hold, **kwargs) @@ -18,6 +27,12 @@ def layer_key_validator_tt(layer, prefer_hold=True, **kwargs): ) +class LayerKeyMeta: + def __init__(self, layer, kc=None): + self.layer = layer + self.kc = kc + + class Layers(HoldTap): '''Gives access to the keys used to enable the layer system''' diff --git a/kmk/modules/modtap.py b/kmk/modules/modtap.py index f450673..e591c3e 100644 --- a/kmk/modules/modtap.py +++ b/kmk/modules/modtap.py @@ -1,7 +1,26 @@ import kmk.handlers.stock as handlers -from kmk.key_validators import mod_tap_validator from kmk.keys import make_argumented_key -from kmk.modules.holdtap import HoldTap +from kmk.modules.holdtap import HoldTap, HoldTapKeyMeta + + +def mod_tap_validator( + kc, mods=None, prefer_hold=True, tap_interrupted=False, tap_time=None +): + ''' + Validates that mod tap keys are correctly used + ''' + return ModTapKeyMeta( + kc=kc, + mods=mods, + prefer_hold=prefer_hold, + tap_interrupted=tap_interrupted, + tap_time=tap_time, + ) + + +class ModTapKeyMeta(HoldTapKeyMeta): + def __init__(self, kc=None, mods=None, **kwargs): + super().__init__(tap=kc, hold=mods, **kwargs) class ModTap(HoldTap): diff --git a/kmk/modules/oneshot.py b/kmk/modules/oneshot.py index 5562f9c..a95a825 100644 --- a/kmk/modules/oneshot.py +++ b/kmk/modules/oneshot.py @@ -1,6 +1,5 @@ from kmk.keys import make_argumented_key -from kmk.modules.holdtap import ActivationType, HoldTap -from kmk.types import HoldTapKeyMeta +from kmk.modules.holdtap import ActivationType, HoldTap, HoldTapKeyMeta def oneshot_validator(kc, tap_time=None): diff --git a/kmk/modules/tapdance.py b/kmk/modules/tapdance.py index b54779e..8843fcd 100644 --- a/kmk/modules/tapdance.py +++ b/kmk/modules/tapdance.py @@ -1,6 +1,5 @@ from kmk.keys import KC, make_argumented_key -from kmk.modules.holdtap import ActivationType, HoldTap -from kmk.types import HoldTapKeyMeta +from kmk.modules.holdtap import ActivationType, HoldTap, HoldTapKeyMeta class TapDanceKeyMeta: diff --git a/kmk/types.py b/kmk/types.py index 050a5e7..4d05158 100644 --- a/kmk/types.py +++ b/kmk/types.py @@ -11,33 +11,6 @@ class AttrDict(dict): return self[key] -class HoldTapKeyMeta: - def __init__( - self, - tap, - hold, - prefer_hold=True, - tap_interrupted=False, - tap_time=None, - ): - self.tap = tap - self.hold = hold - self.prefer_hold = prefer_hold - self.tap_interrupted = tap_interrupted - self.tap_time = tap_time - - -class LayerKeyMeta: - def __init__(self, layer, kc=None): - self.layer = layer - self.kc = kc - - -class ModTapKeyMeta(HoldTapKeyMeta): - def __init__(self, kc=None, mods=None, **kwargs): - super().__init__(tap=kc, hold=mods, **kwargs) - - class KeySequenceMeta: def __init__(self, seq): self.seq = seq