added command line arguments support
This commit is contained in:
parent
946f4a3b5c
commit
9a032b1ebd
29
main.py
29
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()
|
||||
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)
|
12
renderer.py
12
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()
|
||||
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,12 +29,14 @@ 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])
|
||||
|
||||
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()
|
||||
|
||||
|
@ -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)
|
||||
if self.display:
|
||||
input("Press ENTER to quit")
|
Loading…
Reference in New Issue
Block a user