Add command execution framework

This commit is contained in:
Jaculabilis 2018-05-29 11:23:13 -07:00
parent ec1608b154
commit e1b2d7b96d
1 changed files with 58 additions and 17 deletions

View File

@ -10,17 +10,14 @@ import os
def is_lexicon(name): def is_lexicon(name):
""" """
Checks whether the given name is an extant Lexicon game. Checks whether the given folder is a Lexicon game.
Inputs: Inputs:
name The Lexicon name to check. name The Lexicon name to check. Assumed to be an existing folder.
Output: Output:
If the given name is an extant Lexicon, returns a tuple (True, status) If the given name is a Lexicon game, returns a tuple (True, status)
where status is a string with the status of the named Lexicon. where status is a string with the Lexicon's status. Otherwise, returns
If the given name is not an extant Lexicon, returns a tuple
(False, errormsg) where errormsg is a string detailing the error. (False, errormsg) where errormsg is a string detailing the error.
""" """
if not os.path.isdir(os.path.join("lexicon", name)):
return (False, "There is no Lexicon named '{}'.".format(name))
# TODO: Verify the folder is a Lexicon # TODO: Verify the folder is a Lexicon
#return (False, "'{}' is not a Lexicon game, or it may be corrupted.".format(name)) #return (False, "'{}' is not a Lexicon game, or it may be corrupted.".format(name))
return (True, "A Lexicon") return (True, "A Lexicon")
@ -34,9 +31,10 @@ def overview_all():
lexicon_names = [] lexicon_names = []
with os.scandir("lexicon") as lexicons: with os.scandir("lexicon") as lexicons:
for entry in lexicons: for entry in lexicons:
check = is_lexicon(entry.name) if entry.is_dir():
if check[0]: check = is_lexicon(entry.name)
lexicon_names.append((entry.name, check[1])) if check[0]:
lexicon_names.append((entry.name, check[1]))
# Print the results # Print the results
if len(lexicon_names) > 0: if len(lexicon_names) > 0:
l = max([len(name[0]) for name in lexicon_names]) + 4 l = max([len(name[0]) for name in lexicon_names]) + 4
@ -53,30 +51,73 @@ def overview_one(name):
given name. given name.
""" """
# Verify the name # Verify the name
if not os.path.isdir(os.path.join("lexicon", name)):
print("Error: There is no Lexicon named '{}'.".format(name))
return
check = is_lexicon(name) check = is_lexicon(name)
if not check[0]: if not check[0]:
print("Error: " + check[1]) print("Error: " + check[1])
return
# Print status and summary
# TODO
print("Lexicon {} exists, status {}".format(name, check[1]))
def run_command(name, command): def run_command(name, command):
""" """
Runs the specified command on the specified Lexicon. Runs a command on a Lexicon.
""" """
# Verify name
check = is_lexicon(name)
if not check[0]:
print("Error: " + check[1])
return
# Dispatch commands
if command == "init": if command == "init":
# Check that the folder isn't already there
if os.path.exists(os.path.join("lexicon", name)):
print("Error: Can't create '{}', it already exists.".format(name))
return
# Create the Lexicon
command_init(name)
return return
elif command == "build": elif command == "build":
if not os.path.exists(os.path.join("lexicon", name)):
print("Error: There is no Lexicon named '{}'.".format(name))
return
check = is_lexicon(name)
if not check[0]:
print("Error: " + check[1])
return
# Build the Lexicon
command_build(name)
return return
elif command == "run": elif command == "run":
if not os.path.exists(os.path.join("lexicon", name)):
print("Error: There is no Lexicon named '{}'.".format(name))
return
check = is_lexicon(name)
if not check[0]:
print("Error: " + check[1])
return
# Run a server managing the Lexicon
command_run(name)
return return
else: else:
print("Error: '{}' is not a valid command.".format(command)) print("Error: '{}' is not a valid command.".format(command))
return return
def command_init(name):
"""
Sets up a Lexicon game with the given name.
"""
pass
def command_build(name):
"""
Rebuilds the browsable pages of a Lexicon.
"""
pass
def command_run(name):
"""
Runs as a server managing a Lexicon.
"""
pass
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Lexipython is a Python application for playing the Lexicon RPG.", description="Lexipython is a Python application for playing the Lexicon RPG.",