[game-data-packager] 03/07: Compile YAML game data into JSON, and read that instead of the YAML

Simon McVittie smcv at debian.org
Fri Mar 13 00:24:18 UTC 2015


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch master
in repository game-data-packager.

commit d723e66290b3fd8f1e71897b7564228b4d988ca9
Author: Simon McVittie <smcv at debian.org>
Date:   Thu Mar 12 23:51:02 2015 +0000

    Compile YAML game data into JSON, and read that instead of the YAML
    
    On my laptop, this is about 20 times as fast as parsing YAML with the
    CLoader, or about 400 times as fast as the pure-Python YAML loader.
---
 Makefile                        |  8 ++++++--
 game_data_packager/__init__.py  | 14 +++++++-------
 game_data_packager/yaml2json.py | 29 +++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/Makefile b/Makefile
index 3096a38..ce607a2 100644
--- a/Makefile
+++ b/Makefile
@@ -16,16 +16,19 @@ TEST_SUITE += rott spear-of-destiny wolf3d heretic
 png       := $(patsubst ./data/%.xpm,./out/%.png,$(wildcard ./data/*.xpm))
 svgz      := $(patsubst ./data/%.svg,./out/%.svgz,$(wildcard ./data/*.svg))
 in_yaml   := $(wildcard ./data/*.yaml)
-yaml      := $(patsubst ./data/%,./out/%,$(wildcard ./data/*.yaml))
+json      := $(patsubst ./data/%.yaml,./out/%.json,$(in_yaml))
 copyright := $(patsubst ./data/%,./out/%,$(wildcard ./data/*.copyright))
 dot_in    := $(patsubst ./data/%,./out/%,$(wildcard ./data/*.in))
 
-default: $(DIRS) $(png) $(svgz) $(yaml) $(copyright) $(dot_in) \
+default: $(DIRS) $(png) $(svgz) $(json) $(copyright) $(dot_in) \
       out/bash_completion out/changelog.gz out/copyright out/game-data-packager
 
 out/%: data/%
 	if [ -L $< ]; then cp -a $< $@ ; else install -m644 $< $@ ; fi
 
+out/%.json: data/%.yaml
+	python3 game_data_packager/yaml2json.py $< > $@
+
 out/bash_completion: $(in_yaml)
 	python3 game_data_packager/bash_completion.py > ./out/bash_completion
 	chmod 0644 ./out/bash_completion
@@ -58,6 +61,7 @@ clean:
 	rm -f ./out/*.preinst.in
 	rm -f ./out/*.png
 	rm -f ./out/*.svgz
+	rm -f ./out/*.json
 	rm -f ./out/*.yaml
 	rm -rf game_data_packager/__pycache__
 	for d in $(DIRS); do [ ! -d "$$d" ]  || rmdir "$$d"; done
diff --git a/game_data_packager/__init__.py b/game_data_packager/__init__.py
index 9372c53..abafbfc 100644
--- a/game_data_packager/__init__.py
+++ b/game_data_packager/__init__.py
@@ -23,6 +23,7 @@ import glob
 import hashlib
 import importlib
 import io
+import json
 import logging
 import os
 import random
@@ -2393,14 +2394,13 @@ class GameData(object):
 def load_games(workdir=None):
     games = {}
 
-    for yamlfile in glob.glob(os.path.join(DATADIR, '*.yaml')):
+    for jsonfile in glob.glob(os.path.join(DATADIR, '*.json')):
         try:
-            g = os.path.basename(yamlfile)
+            g = os.path.basename(jsonfile)
             g = g[:len(g) - 5]
 
-            yaml_data = yaml.load(open(yamlfile, encoding='utf-8'))
-
-            plugin = yaml_data.get('plugin', g)
+            data = json.load(open(jsonfile, encoding='utf-8'))
+            plugin = data.get('plugin', g)
 
             try:
                 plugin = importlib.import_module('game_data_packager.games.%s' %
@@ -2410,9 +2410,9 @@ def load_games(workdir=None):
                 logger.debug('No special code for %s: %s', g, e)
                 game_data_constructor = GameData
 
-            games[g] = game_data_constructor(g, yaml_data, workdir=workdir)
+            games[g] = game_data_constructor(g, data, workdir=workdir)
         except:
-            print('Error loading %s:\n' % yamlfile)
+            print('Error loading %s:\n' % jsonfile)
             raise
 
     return games
diff --git a/game_data_packager/yaml2json.py b/game_data_packager/yaml2json.py
new file mode 100644
index 0000000..f941630
--- /dev/null
+++ b/game_data_packager/yaml2json.py
@@ -0,0 +1,29 @@
+#!/usr/bin/python3
+# encoding=utf-8
+#
+# Copyright © 2015 Simon McVittie <smcv at debian.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+#
+# You can find the GPL license text on a Debian system under
+# /usr/share/common-licenses/GPL-2.
+
+import json
+import sys
+
+import yaml
+
+def main():
+    for f in sys.argv[1:]:
+        json.dump(yaml.load(open(f, encoding='utf-8'), Loader=yaml.CLoader),
+                sys.stdout, sort_keys=True)
+
+if __name__ == '__main__':
+    main()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/game-data-packager.git



More information about the Pkg-games-commits mailing list