Repository navigation
ref: Implement BobParser #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adedamola-sode
wants to merge
16
commits into
main
Choose a base branch
from
ref/autofill-bob-parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e6be2f2
ref: changed the pipeline of main.py to go to bob_parser first
adedamola-sode d2b6ecd
ref: using models instead of linked dictionaries for Widget and File …
adedamola-sode 8919bd5
ref: Replacing techui with bob_description
adedamola-sode 75dfa1a
ref: trying to establish contract between builder and parser
adedamola-sode 9ba06ca
ref: converted bob_descriptions into components
adedamola-sode 52e0a99
tests: fixed tests to work with the new bob parser stage
adedamola-sode 54a5f1c
tests:changed builder to use both index.bob and techui.yaml
adedamola-sode 7cc0de9
linting:optional techui.yaml, and new read_bob adjustments
adedamola-sode dcae2d1
ref: Changed from grouping to action button macros
adedamola-sode 51c920f
Rebased on main
adedamola-sode cf029b4
Fixed _read_bob in validator.py
adedamola-sode 8525d75
Merge branch 'main' into ref/autofill-bob-parser
adedamola-sode fcc23da
tests: added test_bob_parser
adedamola-sode 246ebb1
Made recommended changes, doc strings, widget.prfix.strip(), uneeded …
adedamola-sode b8d8438
Add regex checking for widget name
adedamola-sode 920dba3
Fix regex matching to name "dictionary"
OCopping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import logging | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
|
|
||
| from lxml.objectify import ObjectifiedElement | ||
|
|
||
| from techui_builder.models import BobFile, BobWidget, MacroMap | ||
| from techui_builder.utils import _INDEX_BOB_NAME_RE, WidgetType, read_bob | ||
|
|
||
| logger_ = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @dataclass | ||
| class BobParser: | ||
| """ | ||
| The BobParser class is responsible for parsing .bob files and extracting relevant | ||
| information such as widgets and macros. It provides methods to read the .bob file, | ||
| parse its contents, and return a structured representation of the data. | ||
| """ | ||
|
|
||
| bob_path: Path | ||
|
|
||
| def _parse_widgets(self, container: ObjectifiedElement) -> list[BobWidget]: | ||
| """ | ||
| Parses the widgets from the given container element | ||
| and returns a list of BobWidget instances. | ||
| """ | ||
| result = [] | ||
|
|
||
| for element in container.iterchildren("widget"): | ||
| if element.get("type", default="") == WidgetType.GROUP: | ||
| self._parse_widgets(element) | ||
| if element.get("type", default="") not in WidgetType: | ||
| continue | ||
| # Find the name and prefix of the widget | ||
| name_element = element.find("name") | ||
| name = ( | ||
| name_element.text | ||
| if (name_element is not None and name_element.text is not None) | ||
| else "" | ||
| ) | ||
| prefix_element = element.find("pv_name") | ||
| prefix = prefix_element.text if prefix_element is not None else "" | ||
|
|
||
| macros: MacroMap = {} | ||
| matches = _INDEX_BOB_NAME_RE.finditer(name) | ||
| macro_pairs = { | ||
| match.group("key") or "name": match.group("val") for match in matches | ||
| } | ||
|
|
||
| if macro_pairs is None: | ||
| print(f"No match for {name!r}") | ||
| continue | ||
|
|
||
| for key, value in macro_pairs.items(): | ||
| print(f"Match: {key, value}") | ||
|
|
||
| list_types = ("extras", "status") | ||
| if key not in list_types: | ||
| macros[key] = value | ||
| else: | ||
| value = value[1:-1] | ||
| macros[key] = [v.strip() for v in value.split(",") if v.strip()] | ||
|
|
||
| # Create a BobWidget instance for the widget | ||
| widget = BobWidget( | ||
| name=name or "", | ||
| widget_type=element.get("type", default=""), | ||
| macros=macros, | ||
| element=element, | ||
| children=None, | ||
| prefix=prefix or "", | ||
| ) | ||
|
|
||
| if widget.widget_type in WidgetType: | ||
| result.append(widget) | ||
|
|
||
| return result | ||
|
|
||
| def parse_bob( | ||
|
OCopping marked this conversation as resolved.
|
||
| self, | ||
| ) -> BobFile: | ||
| """ | ||
| Parses the .bob file specified by the bob_path | ||
| attribute and returns a BobFile instance | ||
| """ | ||
| tree, _, display_macros = read_bob(self.bob_path) | ||
| root = tree.getroot() | ||
|
|
||
| return BobFile( | ||
| path=self.bob_path, | ||
| macros=display_macros, | ||
| tree=tree, | ||
| widgets=self._parse_widgets(root), | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.