From 892cb267edbcb878ca5ba387ff2f58140fe1d732 Mon Sep 17 00:00:00 2001 From: David Schultz Date: Thu, 22 Feb 2024 11:42:27 -0600 Subject: [PATCH] try using a class method instead --- src/sigal/gallery.py | 3 +- src/sigal/plugins/media_page.py | 2 +- src/sigal/writer.py | 53 +++++++++++++++++---------------- tests/test_encrypt.py | 1 + 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/sigal/gallery.py b/src/sigal/gallery.py index 0d0d925..58f927b 100644 --- a/src/sigal/gallery.py +++ b/src/sigal/gallery.py @@ -891,8 +891,9 @@ class Gallery: if self.settings["write_html"]: album_writer = AlbumPageWriter(self.settings, index_title=self.title) + album_writer.copy_theme_files() album_list_writer = AlbumListPageWriter( - self.settings, index_title=self.title, copy_files=False + self.settings, index_title=self.title ) with progressbar( self.albums.values(), diff --git a/src/sigal/plugins/media_page.py b/src/sigal/plugins/media_page.py index 3e2bcc1..5234bd3 100644 --- a/src/sigal/plugins/media_page.py +++ b/src/sigal/plugins/media_page.py @@ -71,7 +71,7 @@ class PageWriter(AbstractWriter): def generate_media_pages(gallery): """Generates and writes the media pages for all media in the gallery""" - writer = PageWriter(gallery.settings, index_title=gallery.title, copy_files=False) + writer = PageWriter(gallery.settings, index_title=gallery.title) for album in gallery.albums.values(): medias = album.medias diff --git a/src/sigal/writer.py b/src/sigal/writer.py index 1ab7c48..6831d58 100644 --- a/src/sigal/writer.py +++ b/src/sigal/writer.py @@ -42,7 +42,7 @@ THEMES_PATH = os.path.normpath( class AbstractWriter: template_file = None - def __init__(self, settings, index_title="", copy_files=True): + def __init__(self, settings, index_title=""): self.settings = settings self.output_dir = settings["destination"] self.theme = settings["theme"] @@ -96,33 +96,36 @@ class AbstractWriter: sys.exit(1) self.theme_path = os.path.join(self.output_dir, "static") - if copy_files: - # Copy the theme files in the output dir - if os.path.isdir(self.theme_path): - shutil.rmtree(self.theme_path) - for static_path in ( - os.path.join(THEMES_PATH, "default", "static"), - os.path.join(self.theme, "static"), - ): - shutil.copytree(static_path, self.theme_path, dirs_exist_ok=True) + def copy_theme_files(self): + """Copy the theme files to the destination""" + self.logger.info("Copying the theme files to the output dir") - # Ensure that the theme dir is writeable - for root, _, files in os.walk(self.theme_path): - st = os.stat(root) - os.chmod(root, st.st_mode | stat.S_IWUSR) - for name in files: - path = os.path.join(root, name) - st = os.stat(path) - os.chmod(path, st.st_mode | stat.S_IWUSR) + if os.path.isdir(self.theme_path): + shutil.rmtree(self.theme_path) - if self.settings["user_css"]: - if not os.path.exists(self.settings["user_css"]): - self.logger.error( - "CSS file %s could not be found", self.settings["user_css"] - ) - else: - shutil.copy(self.settings["user_css"], self.theme_path) + for static_path in ( + os.path.join(THEMES_PATH, "default", "static"), + os.path.join(self.theme, "static"), + ): + shutil.copytree(static_path, self.theme_path, dirs_exist_ok=True) + + # Ensure that the theme dir is writeable + for root, _, files in os.walk(self.theme_path): + st = os.stat(root) + os.chmod(root, st.st_mode | stat.S_IWUSR) + for name in files: + path = os.path.join(root, name) + st = os.stat(path) + os.chmod(path, st.st_mode | stat.S_IWUSR) + + if self.settings["user_css"]: + if not os.path.exists(self.settings["user_css"]): + self.logger.error( + "CSS file %s could not be found", self.settings["user_css"] + ) + else: + shutil.copy(self.settings["user_css"], self.theme_path) def generate_context(self, album): """Generate the context dict for the given path.""" diff --git a/tests/test_encrypt.py b/tests/test_encrypt.py index ea29808..075b145 100644 --- a/tests/test_encrypt.py +++ b/tests/test_encrypt.py @@ -8,6 +8,7 @@ from sigal.gallery import Gallery from sigal.plugins.encrypt import endec from sigal.plugins.encrypt.encrypt import cache_key from sigal.utils import init_plugins +from sigal.writer import AbstractWriter CURRENT_DIR = os.path.dirname(__file__)