From 5ba5a22a73dbe0edf0929fa6d9a8c91a2feddcc5 Mon Sep 17 00:00:00 2001 From: Dan Hon Date: Thu, 12 Dec 2024 10:54:18 -0800 Subject: [PATCH 1/4] Added support for json5 * Switched the json module for pyjson5 to support json5 (comments, yay) * moved the test grammar out into its own file. --- .gitignore | 5 ++++- README.md | 0 hello.py | 6 ++++++ tests/test_grammar.json | 29 +++++++++++++++++++++++++++++ tests/test_tracery.py | 39 +++++---------------------------------- tracery/__main__.py | 4 ++-- 6 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 README.md create mode 100644 hello.py create mode 100644 tests/test_grammar.json 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/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/hello.py b/hello.py new file mode 100644 index 0000000..ea4ea3a --- /dev/null +++ b/hello.py @@ -0,0 +1,6 @@ +def main(): + print("Hello from pytracery!") + + +if __name__ == "__main__": + main() 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..b1263da 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('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) From ef453d2394113faa686317d66e2a73ce2d7b412e Mon Sep 17 00:00:00 2001 From: Dan Hon Date: Sun, 15 Dec 2024 09:55:34 -0800 Subject: [PATCH 2/4] Added pyjson5 to setuptools install_requires --- README.md | 0 hello.py | 6 ------ setup.py | 2 +- 3 files changed, 1 insertion(+), 7 deletions(-) delete mode 100644 README.md delete mode 100644 hello.py diff --git a/README.md b/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/hello.py b/hello.py deleted file mode 100644 index ea4ea3a..0000000 --- a/hello.py +++ /dev/null @@ -1,6 +0,0 @@ -def main(): - print("Hello from pytracery!") - - -if __name__ == "__main__": - main() diff --git a/setup.py b/setup.py index fef9b2a..fb68ef4 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ 'tracery', ], python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - install_requires=[], + install_requires=['pyjson5>=1.6.7'], license="Apache License 2.0", zip_safe=True, keywords='tracery', From 98d9dd1179c3d6ac2f04ca87a6229b2f62ea8988 Mon Sep 17 00:00:00 2001 From: Dan Hon Date: Sun, 15 Dec 2024 10:07:00 -0800 Subject: [PATCH 3/4] Fixed to open test grammar from the correct location. --- tests/test_tracery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tracery.py b/tests/test_tracery.py index b1263da..e4b73fb 100644 --- a/tests/test_tracery.py +++ b/tests/test_tracery.py @@ -17,7 +17,7 @@ from tracery.modifiers import base_english -with open('test_grammar.json') as data_file: +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) From 0686de9cca39ca81e7079f6a715e427d55993a8b Mon Sep 17 00:00:00 2001 From: Dan Hon Date: Sun, 15 Dec 2024 10:24:13 -0800 Subject: [PATCH 4/4] Update setup.py Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fb68ef4..c1cc7b5 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ packages=[ 'tracery', ], - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', + python_requires='>3.5', install_requires=['pyjson5>=1.6.7'], license="Apache License 2.0", zip_safe=True,