added command line arguments support

This commit is contained in:
Louis Heredero 2024-01-28 13:52:45 +01:00
parent 946f4a3b5c
commit 9a032b1ebd
3 changed files with 44 additions and 12 deletions

29
main.py
View File

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

View File

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

View File

@ -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)
renderer.save(path)
if self.display:
input("Press ENTER to quit")