forked from aichaos/rivescript-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
63 lines (49 loc) · 1.62 KB
/
Copy pathparse.py
File metadata and controls
63 lines (49 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
from __future__ import print_function
import codecs
import os
import json
import sys
from rivescript.parser import Parser
"""Example use of the RiveScript Parser module.
Usage: python parse.py [path/to/file.rive]
By default it will parse ../brain/clients.rive but you can pass it any
RiveScript file you want.
"""
def main():
# Get the command line argument.
filename = "../brain/clients.rive"
if len(sys.argv) > 1:
filename = sys.argv[1]
# Make sure it's really a file.
if not os.path.isfile(filename):
print("{}: Not a file.".format(filename))
sys.exit(1)
# Make sure it looks like a RiveScript file.
if not filename.lower().endswith(".rive"):
print("{}: Doesn't look like a RiveScript file (no .rive extension)")
sys.exit(1)
# Create a parser instance.
if os.environ.get("PARSER_DEBUG"):
parser = Parser(
on_debug=on_debug,
on_warn=on_warn,
)
else:
parser = Parser()
# Read the file's contents.
with codecs.open(filename, "r", "utf-8") as fh:
source = fh.readlines()
# Create a parser and parse it.
ast = parser.parse(filename, source)
# Dump the "Abstract Syntax Tree" to the console as JSON.
print(json.dumps(ast, indent=2, sort_keys=True))
def on_debug(message):
print("[DEBUG]", message)
def on_warn(message, filename=None, lineno=None):
if filename is not None and lineno is not None:
print("[WARN]", message, "at", filename, "line", lineno)
else:
print("[WARN]", message)
if __name__ == "__main__":
main()