From: Stefano Rivera Date: Fri, 16 May 2014 22:32:58 +0000 (+0200) Subject: Pre-generate JSON, so we don't need to require pyyaml X-Git-Tag: 0.1~167 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=44b9016415981987c6c5fe9e637a860b67a786a7;p=naja.git Pre-generate JSON, so we don't need to require pyyaml --- diff --git a/README.txt b/README.txt index 837b207..5fbd41b 100644 --- a/README.txt +++ b/README.txt @@ -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 diff --git a/naja/gamestate.py b/naja/gamestate.py index 19051a4..a324381 100644 --- a/naja/gamestate.py +++ b/naja/gamestate.py @@ -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): diff --git a/setup.py b/setup.py index b35e3bf..e8042f9 100644 --- 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 index 0000000..6ae4b51 --- /dev/null +++ b/tools/gen_json.py @@ -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()