From 9a032b1ebd6af91043f90d4e2cabe93505d4a278 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Sun, 28 Jan 2024 13:52:45 +0100 Subject: [PATCH] added command line arguments support --- main.py | 29 ++++++++++++++++++++++++++--- renderer.py | 18 ++++++++++++------ schema.py | 9 ++++++--- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index fd826eb..5d562a2 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,29 @@ +import argparse + from schema import InstructionSetSchema +description = """Examples: + - Default theme (black on white): + python main.py schema.xml -o out.jpg + + - Dark theme (white on black): + python main.py schema.xml -o out.jpg -c dark.json + + - Blueprint theme (white on blue): + python main.py schema.xml -o out.jpg -c blueprint.json + + - Transparent background: + python main.py schema.xml -o out.png -c transparent.json +""" + if __name__ == "__main__": - schema = InstructionSetSchema("example1.yaml") - schema.save("example1_v2.jpg") - input() \ No newline at end of file + parser = argparse.ArgumentParser(description=description, formatter_class=argparse.RawTextHelpFormatter) + parser.add_argument("schema", help="Path to the schema description. Accepted formats are: YAML, JSON and XML") + parser.add_argument("-o", "--output", help="Output path", default="out.jpg") + parser.add_argument("-c", "--config", help="Path to the config file", default="config.json") + parser.add_argument("-D", "--display", help="Enable pygame display of the result", action="store_true") + + args = parser.parse_args() + + schema = InstructionSetSchema(args.schema, args.config, args.display) + schema.save(args.output) \ No newline at end of file diff --git a/renderer.py b/renderer.py index 29b17b6..197b82f 100644 --- a/renderer.py +++ b/renderer.py @@ -1,4 +1,5 @@ from __future__ import annotations +import os from typing import TYPE_CHECKING if TYPE_CHECKING: from config import Config @@ -14,10 +15,13 @@ class Renderer: WIDTH = 1200 HEIGHT = 800 - def __init__(self, config: Config) -> None: + def __init__(self, config: Config, display: bool = False) -> None: self.config = config + self.display = display pygame.init() - self.win = pygame.display.set_mode([Renderer.WIDTH, Renderer.HEIGHT]) + if self.display: + self.win = pygame.display.set_mode([Renderer.WIDTH, Renderer.HEIGHT]) + self.surf = pygame.Surface([Renderer.WIDTH, Renderer.HEIGHT], pygame.SRCALPHA) self.font = pygame.font.SysFont(self.config.DEFAULT_FONT_FAMILY, self.config.DEFAULT_FONT_SIZE) self.italicFont = pygame.font.SysFont(self.config.ITALIC_FONT_FAMILY, self.config.ITALIC_FONT_SIZE, italic=True) @@ -25,14 +29,16 @@ class Renderer: self.margins = self.config.MARGINS def render(self, schema: InstructionSetSchema) -> None: - - self.win.fill(self.config.BACKGROUND_COLOR) self.surf.fill(self.config.BACKGROUND_COLOR) self.drawStructure(schema.structures["main"], schema.structures, self.margins[3], self.margins[0]) - self.win.blit(self.surf, [0, 0]) - pygame.display.flip() + if self.display: + name = os.path.basename(schema.path) + pygame.display.set_caption(f"Rivet - {name}") + self.win.fill(self.config.BACKGROUND_COLOR) + self.win.blit(self.surf, [0, 0]) + pygame.display.flip() def save(self, path: str) -> None: pygame.image.save(self.surf, path) diff --git a/schema.py b/schema.py index 2959fa0..798eaf8 100644 --- a/schema.py +++ b/schema.py @@ -13,8 +13,9 @@ class UnsupportedFormatException(Exception): class InstructionSetSchema: VALID_EXTENSIONS = ("yaml", "json", "xml") - def __init__(self, path: str, configPath: str = "config.json") -> None: + def __init__(self, path: str, configPath: str = "config.json", display: bool = False) -> None: self.config = Config(configPath) + self.display = display self.path = path self.load() @@ -43,6 +44,8 @@ class InstructionSetSchema: self.structures[id_] = Structure.load(id_, data) def save(self, path: str) -> None: - renderer = Renderer(self.config) + renderer = Renderer(self.config, self.display) renderer.render(self) - renderer.save(path) \ No newline at end of file + renderer.save(path) + if self.display: + input("Press ENTER to quit") \ No newline at end of file