-
Notifications
You must be signed in to change notification settings - Fork 138
Support @include directives in QSS templates #14101
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
kaamyashinde
wants to merge
2
commits into
implement-basis-for-new-styling
from
qss-include-directive
+224
−26
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,10 @@ | |
| from .theme_utils import ColorTheme, read_theming_resource | ||
|
|
||
| _TOKEN_PATTERN = re.compile(r"\{\{([^{}\s]+)\}\}") | ||
| _INCLUDE_PATTERN = re.compile( | ||
| r'^[ \t]*@include[ \t]+"([^"\n]+\.qss\.in)"[ \t]*$', re.MULTILINE | ||
| ) | ||
| _INCLUDE_LIKE_PATTERN = re.compile(r"^[ \t]*@include\b.*$", re.MULTILINE) | ||
|
|
||
| _BASE_TEMPLATE = "base" | ||
|
|
||
|
|
@@ -22,6 +26,40 @@ def read_qss_stylesheet_file(template_name: str) -> str: | |
| ) | ||
|
|
||
|
|
||
| def _validate_include_directives(template: str) -> None: | ||
| for match in _INCLUDE_LIKE_PATTERN.finditer(template): | ||
| line = match.group(0) | ||
| if not _INCLUDE_PATTERN.fullmatch(line): | ||
| raise QssProcessingError( | ||
| f"Malformed @include directive: {line.strip()}. " | ||
| 'Expected the form: @include "name.qss.in"' | ||
| ) | ||
|
Comment on lines
+30
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to separate the two pattern checks? What happens if we just use |
||
|
|
||
|
|
||
| def resolve_includes(template: str, *, _seen: frozenset[str] | None = None) -> str: | ||
| """Replace ``@include "file.qss.in"`` lines with the file's content. | ||
|
|
||
| A directive must occupy a whole line, may be indented, and must reference a | ||
| ``.qss.in`` file. Includes may nest; circular references are detected. | ||
| """ | ||
| if _seen is None: | ||
| _seen = frozenset() | ||
|
|
||
| _validate_include_directives(template) | ||
|
|
||
| def _replacer(match: re.Match[str]) -> str: | ||
| filename = match.group(1) | ||
| stem = filename.removesuffix(".qss.in") | ||
| if stem in _seen: | ||
| raise QssProcessingError( | ||
| f"Circular @include detected: {stem} is already being processed" | ||
| ) | ||
| content = read_qss_stylesheet_file(stem) | ||
| return resolve_includes(content, _seen=_seen | {stem}) | ||
|
|
||
| return _INCLUDE_PATTERN.sub(_replacer, template) | ||
|
|
||
|
|
||
| def substitute_tokens(template: str, tokens: dict[str, str]) -> str: | ||
| """Replace all {{token-name}} placeholders with values from the token dict.""" | ||
| for name, value in tokens.items(): | ||
|
|
@@ -37,5 +75,6 @@ def substitute_tokens(template: str, tokens: dict[str, str]) -> str: | |
| def process_qss(theme: ColorTheme) -> str: | ||
| """Load tokens for the given theme and produce a fully-resolved QSS string.""" | ||
| raw = read_qss_stylesheet_file(_BASE_TEMPLATE) | ||
| resolved = resolve_includes(raw) | ||
| tokens = load_tokens(theme) | ||
| return substitute_tokens(raw, tokens) | ||
| return substitute_tokens(resolved, tokens) | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the pros/cons of having multiple
.qss.in? Do we have enough components to that we need the added complexity of having between-qss communication?