Return to repo list

pystatsite

On-or-Offline static site generator in Python
Return to HMagellan.com

commit 07cb9e4b56137ac770e2af94fb1e9ff97332f912
parent 038acf69c9d6570613fc0bef1b62507f263a9a5c
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sun, 28 Mar 2021 23:55:19 -0500

added interface abstraction

Diffstat:
Adata/interfaces.json | 11+++++++++++
Mpystatsite.py | 57+++++++++++++++++++++++++--------------------------------
2 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/data/interfaces.json b/data/interfaces.json @@ -0,0 +1,11 @@ +{ + "main" : { + "prompt" : "==============\n| PYSTATSITE |\n==============\nPlease choose an option:", + "choices" : [ + ["Create a new web page", "new_page"], + ["Edit an existing web page", "edit_page"], + ["Sync site to a location", "sync_site"], + ["Quit", "exit"] + ] + } +} diff --git a/pystatsite.py b/pystatsite.py @@ -3,48 +3,22 @@ import json, os from src import compose, sync, add -# Values -options_file = "options.json" -loaded_options = {} - -# Loading -with open(options_file) as o: loaded_options = json.load(o) - -# Functions -def new_page(): - os.system("clear") - -def sync_site(): - os.system("clear") - -# Interface -def main(options = loaded_options): - +# Interface function +def interface(interface_name = ""): chosen = False choice = None - # NOTE: Is this horrible? - choices = [ - ["Create a new web page", new_page], - ["Sync site to a location", sync_site], - ["Quit", exit] - ] - + choices = loaded_interfaces[interface_name]["choices"] while not chosen: os.system("clear") - print("==============") - print("| PYSTATSITE |") - print("==============\n") - print("Please choose an option:\n") + print(loaded_interfaces[interface_name]["prompt"]) num = 1 for c in choices: print('\t' + str(num) + ") " + c[0]) num += 1 - try: choice = int(input()) except ValueError: choice = None - if choice != None and choice in range(1, len(choices) + 1): chosen = True else: @@ -52,8 +26,27 @@ def main(options = loaded_options): print("Press ENTER to continue...") input() - # NOTE: Maybe horrible... - choices[choice - 1][1]() + funcs[choices[choice - 1][1]][0](funcs[choices[choice - 1][1]][1]) + +# Values +data_dir = "data" +options_file = "options.json" +interfaces_file = os.path.join(data_dir, "interfaces.json") +loaded_options = {} +loaded_interfaces = {} +funcs = { + "new_page" : [interface, "new_page"], + "edit_page" : [interface, "edit_page"], + "sync_site" : [interface, "sync_site"], + "exit" : [quit, 0] +} + +# Loading +with open(options_file) as o: loaded_options = json.load(o) +with open(interfaces_file) as i: loaded_interfaces = json.load(i) + +def main(options = loaded_options): + interface("main") if __name__ == "__main__": main(loaded_options)