Improve Lexicon status checking
This commit is contained in:
parent
deeaeafb39
commit
82106e3deb
|
@ -8,6 +8,8 @@ if sys.version_info[0] < 3:
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import json
|
||||||
|
import src.utils as utils
|
||||||
|
|
||||||
def is_lexicon(name):
|
def is_lexicon(name):
|
||||||
"""
|
"""
|
||||||
|
@ -15,13 +17,25 @@ def is_lexicon(name):
|
||||||
Inputs:
|
Inputs:
|
||||||
name The Lexicon name to check. Assumed to be an existing folder.
|
name The Lexicon name to check. Assumed to be an existing folder.
|
||||||
Output:
|
Output:
|
||||||
If the given name is a Lexicon game, returns a tuple (True, status)
|
Returns a tuple (result, msg, status), where result is True if the
|
||||||
where status is a string with the Lexicon's status. Otherwise, returns
|
given name is a Lexicon game and False otherwise, msg is the Lexicon's
|
||||||
(False, errormsg) where errormsg is a string detailing the error.
|
status or an error message, and status is the status dictionary of the
|
||||||
|
Lexicon or None.
|
||||||
"""
|
"""
|
||||||
# TODO: Verify the folder is a Lexicon
|
if not os.path.isfile(os.path.join("lexicon", name, "lexicon.cfg")):
|
||||||
#return (False, "'{}' is not a Lexicon game, or it may be corrupted.".format(name))
|
return (False, "'{}' is not a Lexicon game, or its config file may be missing.".format(name), None)
|
||||||
return (True, "A Lexicon")
|
if not os.path.isfile(os.path.join("lexicon", name, "status")):
|
||||||
|
return (True, "status missing", None)
|
||||||
|
with open(os.path.join("lexicon", name, "status")) as statusfile:
|
||||||
|
raw = statusfile.read()
|
||||||
|
if len(raw) == 0:
|
||||||
|
return (True, "unbuilt", {})
|
||||||
|
try:
|
||||||
|
status = json.loads(raw)
|
||||||
|
except:
|
||||||
|
return (True, "status corrupted", None)
|
||||||
|
return (True, "ye", status) # TODO
|
||||||
|
return (False, "Error checking Lexicon status", None)
|
||||||
|
|
||||||
def overview_all():
|
def overview_all():
|
||||||
"""
|
"""
|
||||||
|
@ -33,15 +47,15 @@ def overview_all():
|
||||||
with os.scandir("lexicon") as lexicons:
|
with os.scandir("lexicon") as lexicons:
|
||||||
for entry in lexicons:
|
for entry in lexicons:
|
||||||
if entry.is_dir():
|
if entry.is_dir():
|
||||||
check = is_lexicon(entry.name)
|
result, msg, status = is_lexicon(entry.name)
|
||||||
if check[0]:
|
if result:
|
||||||
lexicon_names.append((entry.name, check[1]))
|
lexicon_names.append((entry.name, msg))
|
||||||
# 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) for name, msg in lexicon_names]) + 4
|
||||||
print("Lexicons:")
|
print("Lexicons:")
|
||||||
for name, status in sorted(lexicon_names):
|
for name, msg in sorted(lexicon_names):
|
||||||
print(" {}{}{}".format(name, " " * (l - len(name)), status))
|
print(" {}{}{}".format(name, " " * (l - len(name)), msg))
|
||||||
else:
|
else:
|
||||||
print("There are no Lexicons yet. Create one with:\n\n"\
|
print("There are no Lexicons yet. Create one with:\n\n"\
|
||||||
" lexipython.py [name] init")
|
" lexipython.py [name] init")
|
||||||
|
@ -55,13 +69,14 @@ def overview_one(name):
|
||||||
if not os.path.isdir(os.path.join("lexicon", name)):
|
if not os.path.isdir(os.path.join("lexicon", name)):
|
||||||
print("Error: There is no Lexicon named '{}'.".format(name))
|
print("Error: There is no Lexicon named '{}'.".format(name))
|
||||||
return
|
return
|
||||||
check = is_lexicon(name)
|
result, msg, status = is_lexicon(name)
|
||||||
if not check[0]:
|
if not result:
|
||||||
print("Error: " + check[1])
|
print("Error: " + msg)
|
||||||
return
|
return
|
||||||
# Print status and summary
|
# Print status and summary
|
||||||
|
print(msg)
|
||||||
|
print(status)
|
||||||
# TODO
|
# TODO
|
||||||
print("Lexicon {} exists, status {}".format(name, check[1]))
|
|
||||||
|
|
||||||
def run_command(name, command):
|
def run_command(name, command):
|
||||||
"""
|
"""
|
||||||
|
@ -79,9 +94,9 @@ def run_command(name, command):
|
||||||
if not os.path.exists(os.path.join("lexicon", name)):
|
if not os.path.exists(os.path.join("lexicon", name)):
|
||||||
print("Error: There is no Lexicon named '{}'.".format(name))
|
print("Error: There is no Lexicon named '{}'.".format(name))
|
||||||
return
|
return
|
||||||
check = is_lexicon(name)
|
result, msg, status = is_lexicon(name)
|
||||||
if not check[0]:
|
if not result:
|
||||||
print("Error: " + check[1])
|
print("Error: " + msg)
|
||||||
return
|
return
|
||||||
# Build the Lexicon
|
# Build the Lexicon
|
||||||
command_build(name)
|
command_build(name)
|
||||||
|
@ -90,9 +105,9 @@ def run_command(name, command):
|
||||||
if not os.path.exists(os.path.join("lexicon", name)):
|
if not os.path.exists(os.path.join("lexicon", name)):
|
||||||
print("Error: There is no Lexicon named '{}'.".format(name))
|
print("Error: There is no Lexicon named '{}'.".format(name))
|
||||||
return
|
return
|
||||||
check = is_lexicon(name)
|
result, msg, status = is_lexicon(name)
|
||||||
if not check[0]:
|
if not result:
|
||||||
print("Error: " + check[1])
|
print("Error: " + msg)
|
||||||
return
|
return
|
||||||
# Run a server managing the Lexicon
|
# Run a server managing the Lexicon
|
||||||
command_run(name)
|
command_run(name)
|
||||||
|
@ -116,18 +131,20 @@ def command_init(name):
|
||||||
os.mkdir(os.path.join(lex_path, "session"))
|
os.mkdir(os.path.join(lex_path, "session"))
|
||||||
os.mkdir(os.path.join(lex_path, "statistics"))
|
os.mkdir(os.path.join(lex_path, "statistics"))
|
||||||
# Open the default config file
|
# Open the default config file
|
||||||
config = None
|
config = utils.load_resource("lexicon.cfg")
|
||||||
with open(os.path.join("src", "resources", "lexicon.cfg")) as def_cfg:
|
#with open(os.path.join("src", "resources", "lexicon.cfg")) as def_cfg:
|
||||||
config = def_cfg.read()
|
# config = def_cfg.read()
|
||||||
# Edit the name field
|
# Edit the name field
|
||||||
config = re.sub("Lexicon Title", "Lexicon {}".format(name), config)
|
config = re.sub("Lexicon Title", "Lexicon {}".format(name), config)
|
||||||
# Create the Lexicon's config file
|
# Create the Lexicon's config file
|
||||||
with open(os.path.join(lex_path, "lexicon.cfg"), "w") as config_file:
|
with open(os.path.join(lex_path, "lexicon.cfg"), "w") as config_file:
|
||||||
config_file.write(config)
|
config_file.write(config)
|
||||||
# Create an example page
|
# Create an example page
|
||||||
with open(os.path.join("src", "resources", "example-page.txt")) as srcfile:
|
#with open(os.path.join("src", "resources", "example-page.txt")) as srcfile:
|
||||||
with open(os.path.join(lex_path, "src", "example-page.txt"), "w") as destfile:
|
with open(os.path.join(lex_path, "src", "example-page.txt"), "w") as destfile:
|
||||||
destfile.write(srcfile.read())
|
destfile.write(utils.load_resource("example-page.txt"))
|
||||||
|
# Create an empty status file
|
||||||
|
open(os.path.join(lex_path, "status"), "w").close()
|
||||||
print("Created Lexicon {}".format(name))
|
print("Created Lexicon {}".format(name))
|
||||||
|
|
||||||
def command_build(name):
|
def command_build(name):
|
||||||
|
|
Loading…
Reference in New Issue