Ver Fonte

generate song slides async + add more colors to logger

Noah Vogt há 2 anos atrás
pai
commit
2f51da31f5
4 ficheiros alterados com 79 adições e 31 exclusões
  1. 1 2
      README.md
  2. 8 2
      input/parse_prompt.py
  3. 68 25
      slides/engine/song_slides.py
  4. 2 2
      utils/log.py

+ 1 - 2
README.md

@@ -305,8 +305,7 @@ These are some issues and possible changes that will be addressed or at least co
 
 - prevent all crashes:
     - safe `PROMPT_INPUT` parsing
-    - handle possibly incorrect or insensible configurations safely
-- asynchronous slide generation
+- asynchronous slide generation for start slide
 - use caching, with checksum checks for changes in the source file and the `PROMPT_INPUT`
 - provide ssync with the song structure, display it to the user and prevent him from entering a prompt that would cause slidegen to terminate unsuccessfully
 - add more documentation, especially explaining the slide generation, but also dependencies and deployment

+ 8 - 2
input/parse_prompt.py

@@ -25,10 +25,16 @@ from utils import (
 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)))
+        log(
+            "chosen structure: {}".format(str(slidegen.metadata["structure"])),
+            color="cyan",
+        )
         return structure_as_list(slidegen.metadata["structure"])
     if not "-" in slidegen.chosen_structure:
-        log("chosen structure: {}".format(str(slidegen.chosen_structure)))
+        log(
+            "chosen structure: {}".format(str(slidegen.chosen_structure)),
+            color="cyan",
+        )
         return structure_as_list(str(slidegen.chosen_structure))
 
     dash_index = str(slidegen.chosen_structure).find("-")

+ 68 - 25
slides/engine/song_slides.py

@@ -15,6 +15,7 @@ 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 threading import Thread
 from os import path
 
 from wand.exceptions import BlobError
@@ -38,6 +39,8 @@ def generate_song_slides(
 
     current_slide_index: int = 0
 
+    threads = []
+
     for index, structure in enumerate(slidegen.chosen_structure):
         structure_element_splitted: list = slidegen.songtext[
             structure
@@ -70,9 +73,10 @@ def generate_song_slides(
             current_slide_index += 1
 
             log(
-                "generating song slide [{} / {}]...".format(
+                "spawning subprocess for song slide [{} / {}]...".format(
                     current_slide_index, slide_count
-                )
+                ),
+                color="yellow",
             )
 
             if inner_slide_count == 1:
@@ -90,27 +94,66 @@ def generate_song_slides(
 
                 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,
-                    )
+            threads.append(
+                Thread(
+                    target=generate_song_slide,
+                    args=(
+                        slidegen.song_slide_form,
+                        template_img,
+                        structure_element_value,
+                        slidegen,
+                        index,
+                        inner_slide_count,
+                        inner_slide,
+                        current_slide_index,
+                        zfill_length,
+                    ),
                 )
-            except BlobError:
-                error_msg("could not write slide to target directory")
+            )
+
+    for thread in threads:
+        thread.start()
+
+    for thread in threads:
+        thread.join()
+
+
+def generate_song_slide(
+    song_slide,
+    template_img,
+    structure_element_value,
+    slidegen,
+    index,
+    inner_slide_count,
+    inner_slide,
+    current_slide_index,
+    zfill_length,
+):
+    song_slide_img = song_slide.get_slide(
+        self=slidegen.song_slide_form(),
+        template_img=template_img,
+        slide_text=structure_element_value,
+        song_structure=slidegen.chosen_structure,
+        index=index,
+        use_arrow=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,
+            )
+        )
+        log("song slide {} generated and saved".format(current_slide_index))
+    except BlobError:
+        error_msg(
+            "could not write song slide {} to target directory".format(
+                current_slide_index
+            )
+        )

+ 2 - 2
utils/log.py

@@ -25,5 +25,5 @@ def error_msg(msg: str):
     sys.exit(1)
 
 
-def log(message: str) -> None:
-    print(colored("[*] {}".format(message), "green"))
+def log(message: str, color: str = "green") -> None:
+    print(colored("[*] {}".format(message), color))