forked from JoshRosen/cmps140_creative_cooking_assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_line_interface.py
57 lines (51 loc) · 1.79 KB
/
command_line_interface.py
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
#!/usr/bin/env python
"""
Command line interface to the chatbot.
"""
import logging
from optparse import OptionParser
PARSER = OptionParser()
PARSER.add_option("--database", dest="database_url",
default='sqlite:///test_database.sqlite')
PARSER.add_option("--logfile", dest="log_filename",
default='cookingbot.log')
PARSER.add_option("--creative", dest="log_filename",
default='cookingbot.log')
def main():
"""
Main loop for the command line interface.
"""
(options, args) = PARSER.parse_args()
# Configure logging
logging.basicConfig(filename=options.log_filename, level=logging.DEBUG,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logging.getLogger('py4j.java_gateway').setLevel(logging.INFO)
# These imports are performed after the logging setup because basicConfig
# will not work if other modules call logging functions before it's called.
from chatbot import Chatbot
from database import Database
# Setup the database
db = Database(options.database_url)
# Setup the chatbot
logger = logging.getLogger('chatbot')
bot = Chatbot(db, logger)
greeting = bot.get_greeting()
print greeting
while 1:
# Chatbot instances should not call exit() themselves.
# If they need to exit, they should signal so, not exit
# themselves.
PROMPT = bot.prompt # prompt from chatbot, shows mode: creative/regular
try:
user_input = raw_input(PROMPT)
except EOFError:
return
bot_output = bot.handle_input(user_input)
print bot_output
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
# Any code that needs to run on shutdown should go here.
exit()