Skip to content

Commit

Permalink
feat: Add parse_config.zmk_preamble
Browse files Browse the repository at this point in the history
Defaults to the current behavior that `#define`s `KEYMAP_DRAWER`.

Fixes #103
  • Loading branch information
caksoylar committed Jun 25, 2024
1 parent fa7327b commit 687fb48
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
9 changes: 9 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,12 @@ E.g. `{"combo_esc": {"align": "top", "offset": 0.5}}` would add these two fields
_Type:_ `dict[str, dict]`

_Default:_ `{}`

#### `zmk_preamble`

A string to prepend to ZMK keymaps before parsing that can be used to influence the parsed content.
The default defines a `KEYMAP_DRAWER` symbol which can be used for checks with preprocessor directives.

_Type:_ `str`

_Default:_ `#define KEYMAP_DRAWER`
3 changes: 3 additions & 0 deletions keymap_drawer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ class ModifierFnMap(BaseModel):
# e.g. {"combo_esc": {"align": "top", "offset": 0.5}}
zmk_combos: dict[str, dict] = {}

# prepend this to ZMK keymaps before processing to customize parsing output
zmk_preamble: str = "#define KEYMAP_DRAWER"


class Config(BaseSettings, env_prefix="KEYMAP_"):
"""All configuration settings used for this module."""
Expand Down
10 changes: 4 additions & 6 deletions keymap_drawer/parse/dts.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,17 @@ class DeviceTree:
_compatible_re = re.compile(r'compatible = "(.*?)"')
_custom_data_header = "__keymap_drawer_data__"

def __init__(
self, in_str: str, file_name: str | None = None, preprocess: bool = True, add_define: str | None = None
):
def __init__(self, in_str: str, file_name: str | None = None, preprocess: bool = True, preamble: str | None = None):
"""
Given an input DTS string `in_str` and `file_name` it is read from, parse it into an internap
tree representation and track what "compatible" value each node has.
If `add_define` is set to a string, #define it for the preprocessor.
If `preamble` is set to a non-empty string, prepend it to the read buffer.
"""
self.raw_buffer = in_str
self.file_name = file_name
if add_define:
self.raw_buffer = f"#define {add_define}\n" + self.raw_buffer
if preamble:
self.raw_buffer = preamble + "\n" + self.raw_buffer

prepped = self._preprocess(self.raw_buffer, file_name) if preprocess else in_str

Expand Down
2 changes: 1 addition & 1 deletion keymap_drawer/parse/zmk.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _parse(self, in_str: str, file_name: str | None = None) -> tuple[dict, Keyma
"""
Parse a ZMK keymap with its content and path and return the layout spec and KeymapData to be dumped to YAML.
"""
dts = DeviceTree(in_str, file_name, self.cfg.preprocess, add_define="KEYMAP_DRAWER")
dts = DeviceTree(in_str, file_name, self.cfg.preprocess, preamble=self.cfg.zmk_preamble)

if self.cfg.preprocess and self.raw_binding_map:
self._update_raw_binding_map(dts)
Expand Down

0 comments on commit 687fb48

Please sign in to comment.