Pre-generate JSON, so we don't need to require pyyaml
authorStefano Rivera <stefano@rivera.za.net>
Fri, 16 May 2014 22:32:58 +0000 (00:32 +0200)
committerStefano Rivera <stefano@rivera.za.net>
Fri, 16 May 2014 22:32:58 +0000 (00:32 +0200)
README.txt
naja/gamestate.py
setup.py
tools/gen_json.py [new file with mode: 0755]

index 837b207344364b6fdee12ae41c5f195e854ca448..5fbd41bd5375558660f8bf258d07b0f98d3cbd78 100644 (file)
@@ -75,6 +75,10 @@ points, you win the game, and there is much rejoicing.
 Development notes
 -----------------
 
+Ensure the JSON is generated from the YAML, for people with out pyyaml::
+
+   ./tools/gen_json.py
+
 Creating a source distribution with::
 
    ./scripts/build_unix.sh
index 19051a42b8c1d450d4f4b43a0662610f4ad56825..a32438170e3c5663366850cde744027507b57ceb 100644 (file)
@@ -2,15 +2,23 @@
 The current game state.
 """
 
-import yaml
+try:
+    import yaml
+except ImportError:
+    yaml = None
+    import json
 
 from naja.gameboard import GameBoard
 from naja.resources import resources
 
 
 def load_location_deck(name):
-    with resources.get_file('location_decks', '%s.yaml' % (name,)) as deck_fp:
-        return yaml.safe_load(deck_fp)
+    if yaml:
+        with resources.get_file('location_decks', '%s.yaml' % name) as deck_fp:
+            return yaml.safe_load(deck_fp)
+    else:
+        with resources.get_file('location_decks', '%s.json' % name) as deck_fp:
+            return json.load(deck_fp)
 
 
 class GameState(object):
index b35e3bf6629c605b6095bb7a365676625add4858..e8042f9e8b1d5ae967bed2da00bbeda10a72a98c 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -86,7 +86,7 @@ setup(
                 'logging', 'encodings', 'naja',
             ],
             'includes': [
-                'pygame', 'yaml'
+                'pygame',
             ],
             'excludes': [
                 'numpy', 'pygame.sdlmain_osx', 'winreg', 'AppKit', 'Foundation',
diff --git a/tools/gen_json.py b/tools/gen_json.py
new file mode 100755 (executable)
index 0000000..6ae4b51
--- /dev/null
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+import json
+import os
+
+import yaml
+
+
+def main():
+    data = os.path.join(os.path.dirname(__file__), '..', 'data')
+    deck_dir = os.path.join(data, 'location_decks')
+
+    for yaml_fn in os.listdir(deck_dir):
+        basename, extension = os.path.splitext(yaml_fn)
+        if extension != '.yaml':
+            continue
+        json_fn = basename + '.json'
+
+        yaml_path = os.path.join(deck_dir, yaml_fn)
+        json_path = os.path.join(deck_dir, json_fn)
+
+        with open(yaml_path) as yaml_f:
+            obj = yaml.safe_load(yaml_f)
+        with open(json_path, 'w') as json_f:
+            json.dump(obj, json_f, indent=2)
+
+
+if __name__ == '__main__':
+    main()