diff --git a/ssm/ssm.py b/ssm/ssm.py index 9ab4054..b831e43 100644 --- a/ssm/ssm.py +++ b/ssm/ssm.py @@ -15,6 +15,7 @@ import sys import os from typing import List +from .__version__ import __version__ SVG_XMLNS = {"xmlns": "http://www.w3.org/2000/svg"} @@ -294,27 +295,39 @@ def _list_sprites(*, def _export_sprites(*sprites, show_use: bool, + export_dir: str = "", spritesheet_filename: str) -> None: if not os.path.exists(spritesheet_filename): raise RuntimeError(f"Spritesheet '{spritesheet_filename}' " "does not exist.") + if export_dir != "" and not os.path.isdir(export_dir): + raise RuntimeError("The directory specified does not exist.") sprites = list(sprites) spritesheet = Spritesheet(spritesheet_filename) for sprite in spritesheet.sprites: - if sprite.id in sprites: - print(sprite.export(show_use=show_use, - spritesheet_filename=spritesheet_filename), - end="") + if sprite.id not in sprites: + continue + + sprite_data = sprite.export(show_use=show_use, + spritesheet_filename=spritesheet_filename) + if export_dir != "": + with open(f"{export_dir}/{sprite.id}.svg", "w") as f: + f.write(sprite_data) + else: + print(sprite_data, end="") def main() -> None: """Runs script. """ - parser = ArgumentParser(prog="ssm.py", + parser = ArgumentParser(prog="ssm", description=__doc__, formatter_class=RawDescriptionHelpFormatter) + parser.add_argument("--version", + action="version", + version=f"%(prog)s {__version__}") subparsers = parser.add_subparsers() parser_create = subparsers.add_parser("create", @@ -388,6 +401,11 @@ def main() -> None: "-u", action="store_true", help="Print for HTML") + parser_export.add_argument("--dir", + type=str, + metavar="EXPORT_DIR", + default="", + help="Write sprites to directory") parser_export.add_argument("sprites", type=str, metavar="SVG_SPRITES", @@ -420,6 +438,7 @@ def main() -> None: elif args._parser == "export": _export_sprites(*args.sprites, show_use=args.use, + export_dir=args.dir, spritesheet_filename=(args.f.name if args.f is not None else "")) diff --git a/tests/menu.svg b/tests/menu.svg index 073d863..c61ff15 100644 --- a/tests/menu.svg +++ b/tests/menu.svg @@ -1,3 +1,3 @@ - - + + diff --git a/tests/search.svg b/tests/search.svg index a4b8ab0..860702f 100644 --- a/tests/search.svg +++ b/tests/search.svg @@ -1,4 +1,4 @@ - - + + diff --git a/tests/test_ssm.py b/tests/test_ssm.py index 56a4f3e..4eccb83 100644 --- a/tests/test_ssm.py +++ b/tests/test_ssm.py @@ -17,6 +17,7 @@ MENU_SPRITE = f"{TEST_DIR}/menu.svg" SEARCH_ID = "search" MENU_ID = "menu" +EXPORTED_SVG = f"{ARTIFACT_DIR}/{SEARCH_ID}.svg" class TestSsm(unittest.TestCase): @@ -135,6 +136,23 @@ def test_export_with_use(self): self.assertEqual(len(tree.xpath(f"/svg/use[@href='{Path(SPRITESHEET_TWO_SPRITES).name}#{SEARCH_ID}']")), 1, # noqa "Malformed spritesheet.") + def test_export_to_file(self): + completed_process = subprocess.run(["python", + "-m", + "ssm", + "export", + "-f", + f"{SPRITESHEET_TWO_SPRITES}", + f"{SEARCH_ID}" + "--dir", + f"{ARTIFACT_DIR}"]) + self.assertTrue(completed_process.returncode == 0, + "Failed to export sprite from spritesheet.") + tree = etree.parse(f"{EXPORTED_SVG}") + self.assertEqual(len(tree.xpath("/xmlns:svg/xmlns:path", + namespaces=SVG_XMLNS)), 1, + "Malformed spritesheet.") + if __name__ == '__main__': unittest.main()