Skip to content

Commit

Permalink
Add new format json
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Bastida committed Dec 8, 2013
1 parent cc2e8a6 commit 3f08225
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ Using the ``--html`` option, ``Glue`` will also generate a test html per sprite
$ glue source output --html
--json
-----------
Using the ``--json`` option, ``Glue`` will generate both a sprite image and a json metadata file.
.. code-block:: bash
$ glue source output --json
-l --less
---------
Expand Down
4 changes: 3 additions & 1 deletion glue/formats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from .cocos2d import Cocos2dFormat
from .img import ImageFormat
from .html import HtmlFormat
from .json_format import JSONFormat

formats = {'css': CssFormat,
'cocos2d': Cocos2dFormat,
'img': ImageFormat,
'html': HtmlFormat}
'html': HtmlFormat,
'json': JSONFormat}
36 changes: 36 additions & 0 deletions glue/formats/json_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import json
import codecs

from base import BaseTextFormat


class JSONFormat(BaseTextFormat):

extension = 'json'

@classmethod
def populate_argument_parser(cls, parser):
group = parser.add_argument_group("JSON format options")

group.add_argument("--json",
dest="json_dir",
nargs='?',
const=True,
default=os.environ.get('GLUE_JSON', False),
metavar='DIR',
help="Generate JSON files and optionally where")

def needs_rebuild(self):
if os.path.exists(self.output_path()):
try:
with codecs.open('output/simple.css', 'r', 'utf-8-sig') as f:
data = json.loads(f.read())
assert data['hash'] == self.sprite.hash
return False
except Exception:
pass
return True

def render(self, *args, **kwargs):
return json.dumps(self.get_context())
12 changes: 12 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import json
import codecs
import shutil
import unittest
Expand Down Expand Up @@ -927,6 +928,17 @@ def test_html(self):
self.assertExists("output/simple.css")
self.assertExists("output/simple.html")

def test_json(self):
self.create_image("simple/red.png", RED)
self.create_image("simple/blue.png", BLUE)
code = self.call("glue simple output --json")
self.assertEqual(code, 0)

self.assertExists("output/simple.png")
self.assertExists("output/simple.json")
with codecs.open('output/simple.json', 'r', 'utf-8-sig') as f:
json.loads(f.read())

def test_img(self):
self.create_image("simple/red.png", RED)
self.create_image("simple/blue.png", BLUE)
Expand Down

0 comments on commit 3f08225

Please sign in to comment.