Commits

Radomir Dopieralski committed 478ed1c

Add a --json option for generating json output

  • Participants
  • Parent commits 42d03f6

Comments (0)

Files changed (1)

File fugue_icons/fugue_make.py

 """
 
 import argparse
+import json
 import logging
 import os
 import pygame
         return open(path_or_url, 'rb')
 
 def main(url=None, version=None, width=WIDTH, size=SIZE, base_name=OUT_NAME,
-         prefix=PREFIX, use_important=False):
+         prefix=PREFIX, use_important=False, write_json=False):
     if use_important:
         logging.info('Using !important.')
         important = '!important'
              if 'icons/' in path and path.endswith('.png')]
     merged = pygame.Surface((width * size, len(icons) / width * size),
                             pygame.SRCALPHA)
+    data = {}
     css = [
         COMMENT,
         (".%s-icon{"
         merged.blit(icon, (x * size, y * size))
         css.append('.%s-%s{background-position:-%dpx -%dpx%s}'
                    % (prefix, name, x * size, y * size, important))
-    open('%s.css' % base_name, 'wb').write('\n'.join(css))
-    logging.info('File %s.css written.' % base_name)
-    pygame.image.save(merged, '%s.png' % base_name)
-    logging.info('File %s.png written.' % base_name)
+        data[name] = [x * size, y * size]
+    open(base_name + '.css', 'wb').write('\n'.join(css))
+    logging.info('File %r written.' % (base_name + '.css'))
+    pygame.image.save(merged, base_name + '.png')
+    logging.info('File %r written.' % (base_name + '.png'))
+    if write_json:
+        json.dump(data, open(base_name + '.json', 'wb'))
+        logging.info('File %r written.' % (base_name + '.json'))
 
 
 if __name__ == '__main__':
         help="Base name of output files.")
     parser.add_argument('--prefix', default=PREFIX,
         help="Prefix to use in CSS classes.")
+    parser.add_argument('--json', default=False, action='store_true',
+        help="Also save a JSON file with all the coordinates.")
     parser.add_argument('--important', default=False, action='store_true',
         help="Use !important in the generated CSS.")
     args = parser.parse_args()
     logging.basicConfig(level=args.log)
     main(url=args.url, version=args.version, width=args.width, size=args.size,
-         base_name=args.out, prefix=args.prefix, use_important=args.important)
+         base_name=args.out, prefix=args.prefix, use_important=args.important,
+         write_json=args.json)