-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.py
executable file
·144 lines (117 loc) · 3.87 KB
/
shell.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /usr/bin/env python3
# A simple shell interface for Adventure Core
# This software is licensed under the NCSA License. Details in LICENSE.txt
# Copyright 2011 Dylan Nugent. All rights reserved.
import textwrap
from adventure.game import Game
# Declares whether debugging signals should be used within shell.py
# For semantic reasons, a -O compile will override DEBUG to false
DEBUG = True
if __debug__ == False:
DEBUG = False
class ShellInterface:
"""Class for a very simple shell interface."""
shellname = "Adventure Core Testing Shell ver. NaN"
initmessage = """\
Copyright (c) 2011 Dylan Nugent. All Rights Reserved.
This program is licensed freely under the NCSA license.
Source code is available, and a copy of the license should be included.
"""
initmessage = textwrap.dedent(initmessage)
prompt = "command> "
case_insensitive = True
command_list = {}
alias_list = {}
def __init__(self):
"""Initializes the shell interface.
Loads shell builtins and prints the initialization (welcome)
message for the shell.
"""
print(self.shellname)
print(self.initmessage)
if DEBUG:
print("Debug mode is active. Bumpy road ahead.")
self.load()
self.running = False
def run(self):
"""Begins running the shell interface.
Note that this will take over an application until run terminates.
"""
self.running = True
while(self.running):
indata = self.get_command()
try:
self.process_command(indata)
except NotImplementedError:
print("Error: Command not yet implemented")
except KeyError as e:
print("Error: {}".format(e))
def load(self):
"""Load up the commands for the shell interface."""
# TODO: Come up with a better way to do this than hardcoded commands
self.command_list["help"] = "Display this help message"
self.command_list["quit"] = "Exit the shell"
self.command_list["load"] = "Load an Adventure Core game into the shell"
self.alias_list["q"] = "quit"
self.alias_list["exit"] = "quit"
self.currgame = None
def get_command(self):
"""Display a shell prompt and get the command from it"""
command = input(self.prompt)
if self.case_insensitive:
command = command.lower()
return command
def process_command(self, linein):
"""Process a command on the active shell
Arguments:
linein - The command to be processed
"""
# For now, it's a hard coded limitation that a command must be
# distinguished by its first word alone, and all following data
# is to be treated as an argument to the command
command = linein.split()[0]
args = linein.split()[1:]
if(command in self.alias_list):
command = self.alias_list[command]
if(command in self.command_list):
# Process the command, currently manual
if(command == "help"):
if args == []:
self.print_help()
else:
self.print_help(args[0])
elif(command == "quit"):
self.running = False
elif(command == "load"):
self.currgame = Game(name="My Game")
print("Loaded", self.currgame.name)
print(self.currgame.display_room())
else:
raise NotImplementedError()
else:
raise KeyError("No such command")
def print_help(self, command="shell"):
"""Access the help system and print the requested help to the shell.
Arguments:
command - (default: shell) The command to get help on. If command is
'shell', prints help on all shell commands.
"""
if command == "shell":
print()
print(self.shellname)
print("Shell commands:")
for command in self.command_list:
print(command, ": ", self.command_list[command], sep="")
print()
elif command in self.command_list:
print(command, ": ", self.command_list[command], sep="")
else:
print("No help for that command")
# For current testing purposes, allow the shell to be run directly
if __name__ == '__main__':
if DEBUG:
print("WARNING: Running non fully implemented shell")
print("Quit with 'quit'")
print()
shell = ShellInterface()
shell.run()