ソースを参照

better modularisation + change file naming default config

Noah Vogt 2 年 前
コミット
c0d4d959b0

+ 1 - 1
config/default_config.py

@@ -17,7 +17,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 IMAGE_FORMAT = "jpeg"
 FILE_EXTENSION = "jpg"
-FILE_NAMEING = "slide"
+FILE_NAMEING = "slide-"
 
 WIDTH = 1920
 HEIGHT = 1080

+ 18 - 0
prompt/__init__.py

@@ -0,0 +1,18 @@
+"""
+Copyright © 2022 Noah Vogt <noah@noahvogt.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+from .input import parse_input

+ 64 - 0
prompt/input.py

@@ -0,0 +1,64 @@
+"""
+Copyright © 2022 Noah Vogt <noah@noahvogt.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+from utils import (
+    log,
+    error_msg,
+    structure_as_list,
+)
+
+
+def parse_input(full_song_structure: str, chosen_structure: list | str) -> list:
+    full_structure_list = structure_as_list(full_song_structure)
+    if len(chosen_structure) == 0:
+        log("chosen structure: {}".format(str(chosen_structure)))
+        return structure_as_list(full_song_structure)
+    if not "-" in chosen_structure:
+        log("chosen structure: {}".format(str(chosen_structure)))
+        return structure_as_list(str(chosen_structure))
+
+    dash_index = str(chosen_structure).find("-")
+    start_verse = str(chosen_structure[:dash_index]).strip()
+    end_verse = str(chosen_structure[dash_index + 1 :]).strip()
+
+    try:
+        if int(start_verse) >= int(end_verse):
+            error_msg("{} < {} must be true".format(start_verse, end_verse))
+        if start_verse not in full_song_structure:
+            error_msg("structure {} unknown".format(start_verse))
+        if end_verse not in full_song_structure:
+            error_msg("structure {} unknown".format(end_verse))
+    except (ValueError, IndexError):
+        error_msg("please choose a valid integer for the song structure")
+
+    start_index = full_structure_list.index(start_verse)
+    if start_index != 0:
+        if (
+            full_structure_list[0] == "R"
+            and full_structure_list[start_index - 1] == "R"
+        ):
+            start_index -= 1
+    end_index = full_structure_list.index(end_verse)
+    if end_index != len(full_structure_list) - 1:
+        if (
+            full_structure_list[-1] == "R"
+            and full_structure_list[end_index + 1] == "R"
+        ):
+            end_index += 1
+
+    log("chosen structure: {}".format(str(chosen_structure)))
+    return full_structure_list[start_index : end_index + 1]

+ 16 - 164
slidegen.py

@@ -17,13 +17,11 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 """
 
-from os import path
 import re
 import sys
 
 import colorama
 
-from wand.exceptions import BlobError
 from wand.image import Image
 
 from utils import (
@@ -34,10 +32,19 @@ from utils import (
     get_unique_structure_elements,
 )
 
-from slides import ClassicSongTemplate, ClassicStartSlide, ClassicSongSlide
+from slides import (
+    ClassicSongTemplate,
+    ClassicStartSlide,
+    ClassicSongSlide,
+    generate_start_slide,
+    generate_song_slides,
+    generate_song_template,
+)
 
 import config as const
 
+from prompt import parse_input
+
 
 class Slidegen:
     def __init__(
@@ -74,127 +81,13 @@ class Slidegen:
         return slide_count
 
     def generate_slides(self) -> None:
-        template_img: Image = self.generate_song_template()
+        template_img: Image = generate_song_template(self)
 
         slide_count: int = self.count_number_of_slides_to_be_generated()
         zfill_length: int = len(str(slide_count))
 
-        self.generate_start_slide(template_img, zfill_length)
-        self.generate_song_slides(slide_count, template_img, zfill_length)
-
-    def generate_song_template(self) -> Image:
-        song_template = self.song_template_form()
-        log("generating template...")
-        return song_template.get_template(self.metadata["title"])
-
-    def generate_song_slides(
-        self, slide_count, template_img, zfill_length
-    ) -> None:
-        log("generating song slides...")
-        # unique_structures: list = list(set(self.chosen_structure))
-
-        current_slide_index: int = 0
-
-        for index, structure in enumerate(self.chosen_structure):
-            structure_element_splitted: list = self.songtext[
-                structure
-            ].splitlines()
-            line_count = len(structure_element_splitted)
-            use_line_ranges_per_index = []
-            use_lines_per_index = []
-            if line_count <= const.STRUCTURE_ELEMENT_MAX_LINES:
-                inner_slide_count = 1
-            else:
-                inner_slide_count: int = (
-                    line_count // const.STRUCTURE_ELEMENT_MAX_LINES + 1
-                )
-                use_lines_per_index = [
-                    line_count // inner_slide_count
-                ] * inner_slide_count
-
-                for inner_slide in range(inner_slide_count):
-                    if sum(use_lines_per_index) == line_count:
-                        break
-                    use_lines_per_index[inner_slide] = (
-                        use_lines_per_index[inner_slide] + 1
-                    )
-                for inner_slide in range(inner_slide_count):
-                    use_line_ranges_per_index.append(
-                        sum(use_lines_per_index[:inner_slide])
-                    )
-
-            for inner_slide in range(inner_slide_count):
-                current_slide_index += 1
-
-                log(
-                    "generating song slide [{} / {}]...".format(
-                        current_slide_index, slide_count
-                    )
-                )
-
-                if inner_slide_count == 1:
-                    structure_element_value: str = self.songtext[structure]
-                else:
-                    splitted_wanted_range: list = structure_element_splitted[
-                        use_line_ranges_per_index[
-                            inner_slide
-                        ] : use_line_ranges_per_index[inner_slide]
-                        + use_lines_per_index[inner_slide]
-                    ]
-                    structure_element_value: str = ""
-                    for element in splitted_wanted_range:
-                        structure_element_value += element + "\n"
-
-                    structure_element_value = structure_element_value[:-1]
-
-                song_slide = self.song_slide_form()
-                song_slide_img = song_slide.get_slide(
-                    template_img,
-                    structure_element_value,
-                    self.chosen_structure,
-                    index,
-                    bool(
-                        inner_slide_count != 1
-                        and inner_slide != inner_slide_count - 1
-                    ),
-                )
-                song_slide_img.format = const.IMAGE_FORMAT
-                try:
-                    song_slide_img.save(
-                        filename=path.join(
-                            self.output_dir,
-                            const.FILE_NAMEING
-                            + str(current_slide_index + 1).zfill(zfill_length)
-                            + "."
-                            + const.FILE_EXTENSION,
-                        )
-                    )
-                except BlobError:
-                    error_msg("could not write slide to target directory")
-
-    def generate_start_slide(self, template_img, zfill_length) -> None:
-        log("generating start slide...")
-
-        first_slide = self.start_slide_form()
-        start_slide_img = first_slide.get_slide(
-            template_img,
-            self.metadata["book"],
-            self.metadata["text"],
-            self.metadata["melody"],
-        )
-        start_slide_img.format = const.IMAGE_FORMAT
-        try:
-            start_slide_img.save(
-                filename=path.join(
-                    self.output_dir,
-                    const.FILE_NAMEING
-                    + "1".zfill(zfill_length)
-                    + "."
-                    + const.FILE_EXTENSION,
-                )
-            )
-        except BlobError:
-            error_msg("could not write start slide to target directory")
+        generate_start_slide(self, template_img, zfill_length)
+        generate_song_slides(self, slide_count, template_img, zfill_length)
 
     def parse_file(self) -> None:
         self.parse_metadata()
@@ -260,50 +153,9 @@ class Slidegen:
         self.songtext = output_dict
 
     def calculate_desired_structures(self) -> None:
-        full_structure_str = str(self.metadata["structure"])
-        full_structure_list = structure_as_list(full_structure_str)
-        if len(self.chosen_structure) == 0:
-            self.chosen_structure = structure_as_list(full_structure_str)
-            log("chosen structure: {}".format(str(self.chosen_structure)))
-            return
-        if not "-" in self.chosen_structure:
-            self.chosen_structure = structure_as_list(
-                str(self.chosen_structure)
-            )
-            log("chosen structure: {}".format(str(self.chosen_structure)))
-            return
-
-        dash_index = str(self.chosen_structure).find("-")
-        start_verse = str(self.chosen_structure[:dash_index]).strip()
-        end_verse = str(self.chosen_structure[dash_index + 1 :]).strip()
-
-        try:
-            if int(start_verse) >= int(end_verse):
-                error_msg("{} < {} must be true".format(start_verse, end_verse))
-            if start_verse not in full_structure_str:
-                error_msg("structure {} unknown".format(start_verse))
-            if end_verse not in full_structure_str:
-                error_msg("structure {} unknown".format(end_verse))
-        except (ValueError, IndexError):
-            error_msg("please choose a valid integer for the song structure")
-
-        start_index = full_structure_list.index(start_verse)
-        if start_index != 0:
-            if (
-                full_structure_list[0] == "R"
-                and full_structure_list[start_index - 1] == "R"
-            ):
-                start_index -= 1
-        end_index = full_structure_list.index(end_verse)
-        if end_index != len(full_structure_list) - 1:
-            if (
-                full_structure_list[-1] == "R"
-                and full_structure_list[end_index + 1] == "R"
-            ):
-                end_index += 1
-
-        self.chosen_structure = full_structure_list[start_index : end_index + 1]
-        log("chosen structure: {}".format(str(self.chosen_structure)))
+        self.chosen_structure: list | str = parse_input(
+            str(self.metadata["structure"]), self.chosen_structure
+        )
 
     def parse_argv(self) -> None:
         try:

+ 4 - 0
slides/__init__.py

@@ -22,3 +22,7 @@ from .song_slide import SongSlide
 from .classic_song_template import ClassicSongTemplate
 from .classic_start_slide import ClassicStartSlide
 from .classic_song_slide import ClassicSongSlide
+
+from .engine.start_slide import generate_start_slide
+from .engine.song_slides import generate_song_slides
+from .engine.song_template import generate_song_template

+ 116 - 0
slides/engine/song_slides.py

@@ -0,0 +1,116 @@
+"""
+Copyright © 2022 Noah Vogt <noah@noahvogt.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+from os import path
+
+from wand.exceptions import BlobError
+
+from utils import (
+    log,
+    error_msg,
+)
+
+import config as const
+
+
+def generate_song_slides(
+    slidegen,
+    slide_count,
+    template_img,
+    zfill_length,
+) -> None:
+    log("generating song slides...")
+    # unique_structures: list = list(set(chosen_structure))
+
+    current_slide_index: int = 0
+
+    for index, structure in enumerate(slidegen.chosen_structure):
+        structure_element_splitted: list = slidegen.songtext[
+            structure
+        ].splitlines()
+        line_count = len(structure_element_splitted)
+        use_line_ranges_per_index = []
+        use_lines_per_index = []
+        if line_count <= const.STRUCTURE_ELEMENT_MAX_LINES:
+            inner_slide_count = 1
+        else:
+            inner_slide_count: int = (
+                line_count // const.STRUCTURE_ELEMENT_MAX_LINES + 1
+            )
+            use_lines_per_index = [
+                line_count // inner_slide_count
+            ] * inner_slide_count
+
+            for inner_slide in range(inner_slide_count):
+                if sum(use_lines_per_index) == line_count:
+                    break
+                use_lines_per_index[inner_slide] = (
+                    use_lines_per_index[inner_slide] + 1
+                )
+            for inner_slide in range(inner_slide_count):
+                use_line_ranges_per_index.append(
+                    sum(use_lines_per_index[:inner_slide])
+                )
+
+        for inner_slide in range(inner_slide_count):
+            current_slide_index += 1
+
+            log(
+                "generating song slide [{} / {}]...".format(
+                    current_slide_index, slide_count
+                )
+            )
+
+            if inner_slide_count == 1:
+                structure_element_value: str = slidegen.songtext[structure]
+            else:
+                splitted_wanted_range: list = structure_element_splitted[
+                    use_line_ranges_per_index[
+                        inner_slide
+                    ] : use_line_ranges_per_index[inner_slide]
+                    + use_lines_per_index[inner_slide]
+                ]
+                structure_element_value: str = ""
+                for element in splitted_wanted_range:
+                    structure_element_value += element + "\n"
+
+                structure_element_value = structure_element_value[:-1]
+
+            song_slide = slidegen.song_slide_form()
+            song_slide_img = song_slide.get_slide(
+                template_img,
+                structure_element_value,
+                slidegen.chosen_structure,
+                index,
+                bool(
+                    inner_slide_count != 1
+                    and inner_slide != inner_slide_count - 1
+                ),
+            )
+            song_slide_img.format = const.IMAGE_FORMAT
+            try:
+                song_slide_img.save(
+                    filename=path.join(
+                        slidegen.output_dir,
+                        const.FILE_NAMEING
+                        + str(current_slide_index + 1).zfill(zfill_length)
+                        + "."
+                        + const.FILE_EXTENSION,
+                    )
+                )
+            except BlobError:
+                error_msg("could not write slide to target directory")

+ 28 - 0
slides/engine/song_template.py

@@ -0,0 +1,28 @@
+"""
+Copyright © 2022 Noah Vogt <noah@noahvogt.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+from wand.image import Image
+
+from utils import (
+    log,
+)
+
+
+def generate_song_template(slidegen) -> Image:
+    song_template = slidegen.song_template_form()
+    log("generating template...")
+    return song_template.get_template(slidegen.metadata["title"])

+ 52 - 0
slides/engine/start_slide.py

@@ -0,0 +1,52 @@
+"""
+Copyright © 2022 Noah Vogt <noah@noahvogt.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+from os import path
+
+from wand.exceptions import BlobError
+
+from utils import (
+    log,
+    error_msg,
+)
+
+import config as const
+
+
+def generate_start_slide(slidegen, template_img, zfill_length) -> None:
+    log("generating start slide...")
+
+    first_slide = slidegen.start_slide_form()
+    start_slide_img = first_slide.get_slide(
+        template_img,
+        slidegen.metadata["book"],
+        slidegen.metadata["text"],
+        slidegen.metadata["melody"],
+    )
+    start_slide_img.format = const.IMAGE_FORMAT
+    try:
+        start_slide_img.save(
+            filename=path.join(
+                slidegen.output_dir,
+                const.FILE_NAMEING
+                + "1".zfill(zfill_length)
+                + "."
+                + const.FILE_EXTENSION,
+            )
+        )
+    except BlobError:
+        error_msg("could not write start slide to target directory")