Skip to content

Commit

Permalink
Add export dir support
Browse files Browse the repository at this point in the history
Add version option

Add xml declaration to SVG sprites
  • Loading branch information
obeezzy committed May 2, 2024
1 parent 72cfbe5 commit f8d69b1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
29 changes: 24 additions & 5 deletions ssm/ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -388,6 +401,11 @@ def main() -> None:
"-u",
action="store_true",
help="Print <use> 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",
Expand Down Expand Up @@ -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 ""))
Expand Down
4 changes: 2 additions & 2 deletions tests/menu.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions tests/test_ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit f8d69b1

Please sign in to comment.