try using a class method instead

This commit is contained in:
David Schultz
2024-02-22 11:42:27 -06:00
parent a148592a2f
commit 892cb267ed
4 changed files with 32 additions and 27 deletions

View File

@@ -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(),

View File

@@ -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

View File

@@ -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."""

View File

@@ -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__)