Skip to content

Commit

Permalink
feat: Allow additional include paths to be specified in the parse con…
Browse files Browse the repository at this point in the history
…fig (#105)
  • Loading branch information
stijnveenman authored Jul 10, 2024
1 parent 4e6ccc8 commit dfd32fd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,11 @@ The default defines a `KEYMAP_DRAWER` symbol which can be used for checks with p
_Type:_ `string`

_Default:_ `"#define KEYMAP_DRAWER"`

#### `zmk_additional_includes`

A list of paths to add as search paths to the preprocessor for `#include` directives. This can be needed if you use Zephyr modules such as [`zmk-helpers`](https://github.com/urob/zmk-helpers/tree/v2) since they require augmenting the search path.

_Type:_ `list[str]`

_Default:_ `[]`
3 changes: 3 additions & 0 deletions keymap_drawer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ class ModifierFnMap(BaseModel):
# prepend this to ZMK keymaps before processing to customize parsing output
zmk_preamble: str = "#define KEYMAP_DRAWER"

# additional zmk include paths to be added to the preprocessor
zmk_additional_includes: list[str] = []


class Config(BaseSettings, env_prefix="KEYMAP_"):
"""All configuration settings used for this module."""
Expand Down
19 changes: 15 additions & 4 deletions keymap_drawer/parse/dts.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ 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, preamble: str | None = None):
def __init__( # pylint: disable=too-many-arguments
self,
in_str: str,
file_name: str | None = None,
preprocess: bool = True,
preamble: str | None = None,
additional_includes: list[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.
Expand All @@ -110,10 +117,11 @@ def __init__(self, in_str: str, file_name: str | None = None, preprocess: bool =
"""
self.raw_buffer = in_str
self.file_name = file_name
self.additional_includes = additional_includes
if preamble:
self.raw_buffer = preamble + "\n" + self.raw_buffer

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

# make sure node labels and names are glued together and comments are removed,
# then parse with nested curly braces
Expand Down Expand Up @@ -152,15 +160,18 @@ def assign_compatibles(node: DTNode) -> None:
self.chosen.content += " " + node.content

@staticmethod
def _preprocess(in_str: str, file_name: str | None = None) -> str:
def _preprocess(in_str: str, file_name: str | None = None, additional_includes: list[str] | None = None) -> str:
def include_handler(*args): # type: ignore
raise OutputDirective(Action.IgnoreAndPassThrough)

preprocessor = Preprocessor()
preprocessor.line_directive = None
preprocessor.on_include_not_found = include_handler
preprocessor.assume_encoding = "utf-8"
for path in additional_includes or []:
preprocessor.add_path(path)
preprocessor.parse(in_str, source=file_name)

with StringIO() as f_out:
preprocessor.write(f_out)
prepped = f_out.getvalue()
Expand All @@ -184,7 +195,7 @@ def preprocess_extra_data(self, data: str) -> str:
twice.
"""
in_str = self.raw_buffer + f"\n{self._custom_data_header}\n{data}"
out = self._preprocess(in_str, self.file_name)
out = self._preprocess(in_str, self.file_name, self.additional_includes)
data_pos = out.rfind(f"\n{self._custom_data_header}\n")
assert data_pos >= 0, (
f"Preprocessing extra data failed, please make sure '{self._custom_data_header}' "
Expand Down
8 changes: 7 additions & 1 deletion keymap_drawer/parse/zmk.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,13 @@ 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, preamble=self.cfg.zmk_preamble)
dts = DeviceTree(
in_str,
file_name,
self.cfg.preprocess,
preamble=self.cfg.zmk_preamble,
additional_includes=self.cfg.zmk_additional_includes,
)

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

0 comments on commit dfd32fd

Please sign in to comment.