Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for json5 #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,7 @@ celerybeat-schedule
.env

# Spyder project settings
.spyderproject
.spyderproject
pyproject.toml
uv.lock
.DS_Store
Empty file added README.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's delete this empty file, there's already a README.rst

Empty file.
6 changes: 6 additions & 0 deletions hello.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can delete this file too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure how that got in there.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def main():
print("Hello from pytracery!")


if __name__ == "__main__":
main()
29 changes: 29 additions & 0 deletions tests/test_grammar.json
Original file line number Diff line number Diff line change
@@ -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#"]
}
39 changes: 5 additions & 34 deletions tests/test_tracery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
danhon marked this conversation as resolved.
Show resolved Hide resolved
"""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"""
Expand Down
4 changes: 2 additions & 2 deletions tracery/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down