فهرست منبع

modularisation is now so good as to remove the point from the roadmap

Noah Vogt 2 سال پیش
والد
کامیت
15584235fd
7فایلهای تغییر یافته به همراه181 افزوده شده و 125 حذف شده
  1. 3 1
      input/__init__.py
  2. 36 0
      input/parse_argv.py
  3. 88 0
      input/parse_file.py
  4. 14 14
      input/parse_prompt.py
  5. 9 110
      slidegen.py
  6. 1 0
      slides/__init__.py
  7. 30 0
      slides/engine/calc_slide_count.py

+ 3 - 1
prompt/__init__.py → input/__init__.py

@@ -15,4 +15,6 @@ 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
+from .parse_prompt import parse_prompt_input
+from .parse_file import parse_metadata, parse_songtext
+from .parse_argv import parse_argv

+ 36 - 0
input/parse_argv.py

@@ -0,0 +1,36 @@
+"""
+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/>.
+"""
+
+import sys
+
+from utils import log, error_msg
+
+
+def parse_argv(slidegen) -> None:
+    try:
+        slidegen.song_file_path = sys.argv[1]
+        slidegen.output_dir = sys.argv[2]
+    except IndexError:
+        error_msg("incorrect amount of arguments provided, exiting...")
+    try:
+        slidegen.chosen_structure = sys.argv[3]
+        if slidegen.chosen_structure.strip() == "":
+            slidegen.chosen_structure = ""
+    except IndexError:
+        slidegen.chosen_structure = ""
+
+    log("parsing {}...".format(slidegen.song_file_path))

+ 88 - 0
input/parse_file.py

@@ -0,0 +1,88 @@
+"""
+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 re import match
+
+from utils import (
+    error_msg,
+    structure_as_list,
+    get_unique_structure_elements,
+    get_songtext_by_structure,
+)
+
+import config as const
+
+
+def parse_metadata(slidegen) -> None:
+    metadata_dict = dict.fromkeys(const.METADATA_STRINGS)
+    try:
+        with open(slidegen.song_file_path, mode="r", encoding="utf8") as opener:
+            content = opener.readlines()
+    except IOError:
+        error_msg(
+            "could not read the the song input file: '{}'".format(
+                slidegen.song_file_path
+            )
+        )
+    valid_metadata_strings = list(const.METADATA_STRINGS)
+
+    for line_nr, line in enumerate(content):
+        if len(valid_metadata_strings) == 0:
+            content = content[line_nr:]
+            break
+        if not match(
+            r"^(?!structure)\S+: .+|^structure: ([0-9]+|R)(,([0-9]+|R))*$",
+            line,
+        ):
+            if line[-1] == "\n":
+                line = line[:-1]
+            missing_metadata_strs = ""
+            for metadata_str in valid_metadata_strings:
+                missing_metadata_strs += ", " + metadata_str
+            missing_metadata_strs = missing_metadata_strs[2:]
+            error_msg(
+                "invalid metadata syntax on line {}:\n{}\nThe ".format(
+                    line_nr + 1, line
+                )
+                + "following metadata strings are still missing: {}".format(
+                    missing_metadata_strs
+                )
+            )
+        metadata_str = line[: line.index(":")]
+        if metadata_str in valid_metadata_strings:
+            metadata_dict[metadata_str] = line[line.index(": ") + 2 : -1]
+            valid_metadata_strings.remove(metadata_str)
+            continue
+
+        error_msg("invalid metadata string '{}'".format(metadata_str))
+
+    slidegen.metadata = metadata_dict
+    slidegen.song_file_content = content
+
+
+def parse_songtext(slidegen) -> None:
+    unique_structures = get_unique_structure_elements(
+        structure_as_list(slidegen.metadata["structure"])
+    )
+    output_dict = dict.fromkeys(unique_structures)
+
+    for structure in unique_structures:
+        output_dict[structure] = get_songtext_by_structure(
+            slidegen.song_file_content, structure
+        )
+
+    slidegen.songtext = output_dict

+ 14 - 14
prompt/input.py → input/parse_prompt.py

@@ -22,25 +22,25 @@ from utils import (
 )
 
 
-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))
+def parse_prompt_input(slidegen) -> list:
+    full_structure_list = structure_as_list(slidegen.metadata["structure"])
+    if len(slidegen.chosen_structure) == 0:
+        log("chosen structure: {}".format(str(slidegen.chosen_structure)))
+        return structure_as_list(slidegen.metadata["structure"])
+    if not "-" in slidegen.chosen_structure:
+        log("chosen structure: {}".format(str(slidegen.chosen_structure)))
+        return structure_as_list(str(slidegen.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()
+    dash_index = str(slidegen.chosen_structure).find("-")
+    start_verse = str(slidegen.chosen_structure[:dash_index]).strip()
+    end_verse = str(slidegen.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:
+        if start_verse not in slidegen.metadata["structure"]:
             error_msg("structure {} unknown".format(start_verse))
-        if end_verse not in full_song_structure:
+        if end_verse not in slidegen.metadata["structure"]:
             error_msg("structure {} unknown".format(end_verse))
     except (ValueError, IndexError):
         error_msg("please choose a valid integer for the song structure")
@@ -60,5 +60,5 @@ def parse_input(full_song_structure: str, chosen_structure: list | str) -> list:
         ):
             end_index += 1
 
-    log("chosen structure: {}".format(str(chosen_structure)))
+    log("chosen structure: {}".format(str(slidegen.chosen_structure)))
     return full_structure_list[start_index : end_index + 1]

+ 9 - 110
slidegen.py

@@ -17,21 +17,10 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 """
 
-import re
-import sys
-
 import colorama
 
 from wand.image import Image
 
-from utils import (
-    log,
-    error_msg,
-    get_songtext_by_structure,
-    structure_as_list,
-    get_unique_structure_elements,
-)
-
 from slides import (
     ClassicSongTemplate,
     ClassicStartSlide,
@@ -39,11 +28,10 @@ from slides import (
     generate_start_slide,
     generate_song_slides,
     generate_song_template,
+    count_number_of_slides_to_be_generated,
 )
 
-import config as const
-
-from prompt import parse_input
+from input import parse_prompt_input, parse_metadata, parse_songtext, parse_argv
 
 
 class Slidegen:
@@ -60,118 +48,29 @@ class Slidegen:
         self.song_template_form = song_template_form
         self.start_slide_form = start_slide_form
         self.song_slide_form = song_slide_form
-        self.parse_argv()
+        parse_argv(self)
 
     def execute(self) -> None:
         self.parse_file()
         self.calculate_desired_structures()
         self.generate_slides()
 
-    def count_number_of_slides_to_be_generated(self) -> int:
-        slide_count: int = 0
-        for structure in self.chosen_structure:
-            line_count: int = len(self.songtext[structure].splitlines())
-            if line_count > const.STRUCTURE_ELEMENT_MAX_LINES:
-                slide_count += (
-                    line_count // const.STRUCTURE_ELEMENT_MAX_LINES + 1
-                )
-            else:
-                slide_count += 1
+    def parse_file(self):
+        parse_metadata(self)
+        parse_songtext(self)
 
-        return slide_count
+    def calculate_desired_structures(self) -> None:
+        self.chosen_structure = parse_prompt_input(self)
 
     def generate_slides(self) -> None:
         template_img: Image = generate_song_template(self)
 
-        slide_count: int = self.count_number_of_slides_to_be_generated()
+        slide_count: int = count_number_of_slides_to_be_generated(self)
         zfill_length: int = len(str(slide_count))
 
         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()
-        self.parse_songtext()
-
-    def parse_metadata(self) -> None:
-        metadata_dict = dict.fromkeys(const.METADATA_STRINGS)
-        try:
-            with open(self.song_file_path, mode="r", encoding="utf8") as opener:
-                content = opener.readlines()
-        except IOError:
-            error_msg(
-                "could not read the the song input file: '{}'".format(
-                    self.song_file_path
-                )
-            )
-        valid_metadata_strings = list(const.METADATA_STRINGS)
-
-        for line_nr, line in enumerate(content):
-            if len(valid_metadata_strings) == 0:
-                content = content[line_nr:]
-                break
-            if not re.match(
-                r"^(?!structure)\S+: .+|^structure: ([0-9]+|R)(,([0-9]+|R))*$",
-                line,
-            ):
-                if line[-1] == "\n":
-                    line = line[:-1]
-                missing_metadata_strs = ""
-                for metadata_str in valid_metadata_strings:
-                    missing_metadata_strs += ", " + metadata_str
-                missing_metadata_strs = missing_metadata_strs[2:]
-                error_msg(
-                    "invalid metadata syntax on line {}:\n{}\nThe ".format(
-                        line_nr + 1, line
-                    )
-                    + "following metadata strings are still missing: {}".format(
-                        missing_metadata_strs
-                    )
-                )
-            metadata_str = line[: line.index(":")]
-            if metadata_str in valid_metadata_strings:
-                metadata_dict[metadata_str] = line[line.index(": ") + 2 : -1]
-                valid_metadata_strings.remove(metadata_str)
-                continue
-
-            error_msg("invalid metadata string '{}'".format(metadata_str))
-
-        self.metadata = metadata_dict
-        self.song_file_content = content
-
-    def parse_songtext(self) -> None:
-        unique_structures = get_unique_structure_elements(
-            structure_as_list(self.metadata["structure"])
-        )
-        output_dict = dict.fromkeys(unique_structures)
-
-        for structure in unique_structures:
-            output_dict[structure] = get_songtext_by_structure(
-                self.song_file_content, structure
-            )
-
-        self.songtext = output_dict
-
-    def calculate_desired_structures(self) -> None:
-        self.chosen_structure: list | str = parse_input(
-            str(self.metadata["structure"]), self.chosen_structure
-        )
-
-    def parse_argv(self) -> None:
-        try:
-            self.song_file_path = sys.argv[1]
-            self.output_dir = sys.argv[2]
-        except IndexError:
-            error_msg("incorrect amount of arguments provided, exiting...")
-        try:
-            self.chosen_structure = sys.argv[3]
-            if self.chosen_structure.strip() == "":
-                self.chosen_structure = ""
-        except IndexError:
-            self.chosen_structure = ""
-
-        log("parsing {}...".format(self.song_file_path))
-
 
 def main() -> None:
     colorama.init()

+ 1 - 0
slides/__init__.py

@@ -26,3 +26,4 @@ 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
+from .engine.calc_slide_count import count_number_of_slides_to_be_generated

+ 30 - 0
slides/engine/calc_slide_count.py

@@ -0,0 +1,30 @@
+"""
+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/>.
+"""
+
+import config as const
+
+
+def count_number_of_slides_to_be_generated(slidegen) -> int:
+    slide_count: int = 0
+    for structure in slidegen.chosen_structure:
+        line_count: int = len(slidegen.songtext[structure].splitlines())
+        if line_count > const.STRUCTURE_ELEMENT_MAX_LINES:
+            slide_count += line_count // const.STRUCTURE_ELEMENT_MAX_LINES + 1
+        else:
+            slide_count += 1
+
+    return slide_count