Skip to content

Commit

Permalink
refactor(shader): _write_template_instance_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed Apr 25, 2024
1 parent 8083ff0 commit f515af1
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/hyprshade/shader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Callable
from dataclasses import KW_ONLY, asdict, dataclass
from functools import cached_property
from typing import Any, ClassVar, Final, TypeAlias, TypeVar
from typing import Any, ClassVar, Final, TextIO, TypeAlias, TypeVar

from more_itertools import flatten

Expand Down Expand Up @@ -149,11 +149,11 @@ def variables(self) -> ShaderVariables | None:
def _render_template(self, path: str) -> str:
with open(path) as f:
content = mustache.render(f, self.variables)
metadata = TemplateInstanceMetadata(source=path).encode()
metadata = TemplateInstanceMetadata(source=path)
out_path = Shader._template_instance_path_from_source_path(path)
os.makedirs(os.path.dirname(out_path), exist_ok=True)
with open(out_path, "w") as f:
f.write(f"{Shader.TEMPLATE_METADATA_PREFIX} {metadata}\n")
Shader._write_template_instance_metadata(f, metadata)
f.write("// This file was generated by Hyprshade.\n")
f.write("// Do not edit it directly.\n")
f.write("\n")
Expand All @@ -165,6 +165,15 @@ def _template_instance_path_from_source_path(path: str) -> str:
file_name, _ = os.path.splitext(os.path.basename(path))
return os.path.join(user_state_dir("hyprshade"), file_name)

@staticmethod
def _write_template_instance_metadata(
f: TextIO, /, metadata: TemplateInstanceMetadata
) -> int:
lines = [
f"{Shader.TEMPLATE_METADATA_PREFIX} {metadata.encode()}",
]
return f.write("\n".join(lines) + "\n")

@staticmethod
def _extract_template_instance_metadata(path: str) -> TemplateInstanceMetadata:
with open(path) as f:
Expand Down

0 comments on commit f515af1

Please sign in to comment.