diff --git a/.gitignore b/.gitignore index ea8ad88..cfdc646 100644 --- a/.gitignore +++ b/.gitignore @@ -78,4 +78,7 @@ celerybeat-schedule .env # Spyder project settings -.spyderproject \ No newline at end of file +.spyderproject +pyproject.toml +uv.lock +.DS_Store diff --git a/setup.py b/setup.py index fef9b2a..c1cc7b5 100644 --- a/setup.py +++ b/setup.py @@ -24,8 +24,8 @@ packages=[ 'tracery', ], - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - install_requires=[], + python_requires='>3.5', + install_requires=['pyjson5>=1.6.7'], license="Apache License 2.0", zip_safe=True, keywords='tracery', diff --git a/tests/test_grammar.json b/tests/test_grammar.json new file mode 100644 index 0000000..faa3706 --- /dev/null +++ b/tests/test_grammar.json @@ -0,0 +1,29 @@ + // json5 supports comments + + { + 'deepHash': ["\\#00FF00", "\\#FF00FF"], + 'deeperHash': ["#deepHash#"], + 'animal': ["bear", "cat", "dog", "fox", "giraffe", "hippopotamus"], + 'mood': ["quiet", "morose", "gleeful", "happy", "bemused", "clever", + "jovial", "vexatious", "curious", "anxious", "obtuse", "serene", + "demure"], + 'nonrecursiveStory': ["The #pet# went to the beach."], + 'recursiveStory': [ + "The #pet# opened a book about[pet:#mood# #animal#] #pet.a#. #story#[pet:POP] The #pet# closed the book."], + 'svgColor': ["rgb(120,180,120)", "rgb(240,140,40)", "rgb(150,45,55)", + "rgb(150,145,125)", "rgb(220,215,195)", "rgb(120,250,190)"], + 'svgStyle': ['style="fill:#svgColor#;stroke-width:3;stroke:#svgColor#"'], + "name": ["Cheri", "Fox", "Morgana", "Jedoo", "Brick", "Shadow", "Krox", + "Urga", "Zelph"], + "story": ["#hero.capitalize# was a great #occupation#, and this song tells of #heroTheir# adventure. #hero.capitalize# #didStuff#, then #heroThey# #didStuff#, then #heroThey# went home to read a book."], + "monster": ["dragon", "ogre", "witch", "wizard", "goblin", "golem", + "giant", "sphinx", "warlord"], + "setPronouns": [ + "[heroThey:they][heroThem:them][heroTheir:their][heroTheirs:theirs]", + "[heroThey:she][heroThem:her][heroTheir:her][heroTheirs:hers]", + "[heroThey:he][heroThem:him][heroTheir:his][heroTheirs:his]"], + "setOccupation": [ + "[occupation:baker][didStuff:baked bread,decorated cupcakes,folded dough,made croissants,iced a cake]", + "[occupation:warrior][didStuff:fought #monster.a#,saved a village from #monster.a#,battled #monster.a#,defeated #monster.a#]"], + "origin": ["#[#setPronouns#][#setOccupation#][hero:#name#]story#"] + } \ No newline at end of file diff --git a/tests/test_tracery.py b/tests/test_tracery.py index b709bdb..e4b73fb 100644 --- a/tests/test_tracery.py +++ b/tests/test_tracery.py @@ -11,44 +11,15 @@ sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) +import pyjson5 + import tracery from tracery.modifiers import base_english -test_grammar = { - 'deepHash': ["\\#00FF00", "\\#FF00FF"], - 'deeperHash': ["#deepHash#"], - 'animal': ["bear", "cat", "dog", "fox", "giraffe", "hippopotamus"], - 'mood': ["quiet", "morose", "gleeful", "happy", "bemused", "clever", - "jovial", "vexatious", "curious", "anxious", "obtuse", "serene", - "demure"], - 'nonrecursiveStory': ["The #pet# went to the beach."], - 'recursiveStory': [ - "The #pet# opened a book about[pet:#mood# #animal#] #pet.a#. " - "#story#[pet:POP] The #pet# closed the book."], - 'svgColor': ["rgb(120,180,120)", "rgb(240,140,40)", "rgb(150,45,55)", - "rgb(150,145,125)", "rgb(220,215,195)", "rgb(120,250,190)"], - 'svgStyle': ['style="fill:#svgColor#;stroke-width:3;stroke:#svgColor#"'], - "name": ["Cheri", "Fox", "Morgana", "Jedoo", "Brick", "Shadow", "Krox", - "Urga", "Zelph"], - "story": ["#hero.capitalize# was a great #occupation#, and this song " - "tells of #heroTheir# adventure. #hero.capitalize# #didStuff#, " - "then #heroThey# #didStuff#, then #heroThey# went home " - "to read a book."], - "monster": ["dragon", "ogre", "witch", "wizard", "goblin", "golem", - "giant", "sphinx", "warlord"], - "setPronouns": [ - "[heroThey:they][heroThem:them][heroTheir:their][heroTheirs:theirs]", - "[heroThey:she][heroThem:her][heroTheir:her][heroTheirs:hers]", - "[heroThey:he][heroThem:him][heroTheir:his][heroTheirs:his]"], - "setOccupation": [ - "[occupation:baker][didStuff:baked bread,decorated cupcakes," - "folded dough,made croissants,iced a cake]", - "[occupation:warrior][didStuff:fought #monster.a#,saved a village " - "from #monster.a#,battled #monster.a#,defeated #monster.a#]"], - "origin": ["#[#setPronouns#][#setOccupation#][hero:#name#]story#"] - } - +with open('tests/test_grammar.json') as data_file: + """We'll open the test grammar as a file now, instead of having it inline""" + test_grammar = pyjson5.decode_io(data_file) class TestPytracery(unittest.TestCase): """Common setUp and helpers""" diff --git a/tracery/__main__.py b/tracery/__main__.py index b04faac..8e80f28 100755 --- a/tracery/__main__.py +++ b/tracery/__main__.py @@ -5,7 +5,7 @@ """ from __future__ import print_function, unicode_literals import argparse -import json +import pyjson5 import tracery from tracery.modifiers import base_english @@ -22,7 +22,7 @@ args = parser.parse_args() with open(args.json) as data_file: - rules = json.load(data_file) + rules = pyjson5.decode_io(data_file) grammar = tracery.Grammar(rules) grammar.add_modifiers(base_english)