Skip to content

Commit

Permalink
fix + refactor: file read/writes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Sep 20, 2024
1 parent bc68c8d commit f02d765
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions dooit/utils/css_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,54 @@

dooit_cache_path = Path(user_cache_dir("dooit"))


def generate_random_id():
return uuid4().hex


class CssManager:
css_file: Path = dooit_cache_path / "dooit.tcss"
base_css: Path = Path(__file__).parent.parent.parent / "ui" / "styles.tcss"
stylesheets: Path = dooit_cache_path / "stylesheets"

css_file: Path = dooit_cache_path / "dooit.tcss"
theme_css_file: Path = dooit_cache_path / "theme.tcss"
base_css_file: Path = dooit_cache_path / "base.tcss"

def _create_files(self):
if not self.stylesheets.exists():
self.stylesheets.mkdir(
parents=True,
exist_ok=True,
)

if not self.theme_css_file.exists():
self.theme_css_file.touch()

if not self.base_css_file.exists():
self.base_css_file.touch()

def refresh_css(self):
self._create_files()

css = ""

# setup theme variables
with open(self.theme_css_file, "r") as f:
css = css + "\n" + f.read()

# setup base variables
with open(self.base_css_file, "r") as f:
css = css + "\n" + f.read()

# inject extra stylesheets
for sheet in self.stylesheets.iterdir():
with open(sheet, "r") as f:
css = css + "\n" + f.read()

self.write(css)

def set_theme(self, theme: DooitThemeBase):
theme_file = self.stylesheets / "theme.tcss"

with open(theme_file, "w") as f:
with open(self.theme_css_file, "w") as f:
f.write(theme.to_css())

self.refresh_css()
Expand All @@ -36,6 +64,7 @@ def inject_css(self, css: str) -> str:
with open(css_file, "w") as f:
f.write(css)

self.refresh_css()
return uuid

def uninject_css(self, _id: str) -> bool:
Expand Down

0 comments on commit f02d765

Please sign in to comment.