Skip to content

Commit

Permalink
[refacto] DRY for module/extensions method
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyiop committed Jun 5, 2022
1 parent 645124d commit dcb4d32
Showing 1 changed file with 17 additions and 138 deletions.
155 changes: 17 additions & 138 deletions kmk/kmk_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,138 +333,17 @@ def _init_matrix(self):

return self

def before_matrix_scan(self):
for module in self.modules:
try:
module.before_matrix_scan(self)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run before matrix scan function in module: ',
err,
module,
)

for ext in self.extensions:
try:
ext.before_matrix_scan(self.sandbox)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run before matrix scan function in extension: ',
err,
ext,
)

def after_matrix_scan(self):
for module in self.modules:
try:
module.after_matrix_scan(self)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run after matrix scan function in module: ',
err,
module,
)

for ext in self.extensions:
try:
ext.after_matrix_scan(self.sandbox)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run after matrix scan function in extension: ',
err,
ext,
)

def before_hid_send(self):
for module in self.modules:
try:
module.before_hid_send(self)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run before hid send function in module: ',
err,
module,
)

for ext in self.extensions:
try:
ext.before_hid_send(self.sandbox)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run before hid send function in extension: ',
err,
ext,
)

def after_hid_send(self):
for module in self.modules:
try:
module.after_hid_send(self)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run after hid send function in module: ', err, module
)

for ext in self.extensions:
try:
ext.after_hid_send(self.sandbox)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run after hid send function in extension: ', err, ext
)

def powersave_enable(self):
for module in self.modules:
try:
module.on_powersave_enable(self)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run on powersave enable function in module: ',
err,
module,
)

for ext in self.extensions:
try:
ext.on_powersave_enable(self.sandbox)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run on powersave enable function in extension: ',
err,
ext,
)

def powersave_disable(self):
for module in self.modules:
try:
module.on_powersave_disable(self)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run on powersave disable function in module: ',
err,
module,
)
for ext in self.extensions:
try:
ext.on_powersave_disable(self.sandbox)
except Exception as err:
if self.debug_enabled:
print(
'Failed to run on powersave disable function in extension: ',
err,
ext,
)
def _run_module_extension_method(self, fn_name):
for component, param in [(self.modules, self), (self.extensions, self.sandbox)]:
fn = getattr(component, fn_name, None)
if fn:
try:
fn(param)
except Exception as err:
if self.debug_enabled:
print(
f'Failed to run {fn_name} scan function in {component}: {err}'
)

def go(self, hid_type=HIDModes.USB, secondary_hid_type=None, **kwargs):
self._init(hid_type=hid_type, secondary_hid_type=secondary_hid_type, **kwargs)
Expand Down Expand Up @@ -501,7 +380,7 @@ def _main_loop(self):
self.state_changed = False
self.sandbox.active_layers = self.active_layers.copy()

self.before_matrix_scan()
self._run_module_extension_method('before_matrix_scan')

for matrix in self.matrix:
update = matrix.scan_for_changes()
Expand All @@ -510,7 +389,7 @@ def _main_loop(self):
break
self.sandbox.secondary_matrix_update = self.secondary_matrix_update

self.after_matrix_scan()
self._run_module_extension_method('after_matrix_scan')

if self.secondary_matrix_update:
self.matrix_update_queue.append(self.secondary_matrix_update)
Expand All @@ -524,7 +403,7 @@ def _main_loop(self):
if self.matrix_update_queue:
self._handle_matrix_report(self.matrix_update_queue.pop(0))

self.before_hid_send()
self._run_module_extension_method('before_hid_send')

if self.hid_pending:
self._send_hid()
Expand All @@ -538,13 +417,13 @@ def _main_loop(self):
if self.hid_pending:
self._send_hid()

self.after_hid_send()
self._run_module_extension_method('after_hid_send')

if self._trigger_powersave_enable:
self.powersave_enable()
self._run_module_extension_method('on_powersave_enable')

if self._trigger_powersave_disable:
self.powersave_disable()
self._run_module_extension_method('on_powersave_disable')

if self.state_changed:
self._print_debug_cycle()

0 comments on commit dcb4d32

Please sign in to comment.