Return to repo list

pystatsite

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

commit 63b6637617a19f48506a9bc7826018e894a0f79d
parent 92703e3a7bc2070313f9be2c043c3ba4a50a2564
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Mon, 29 Mar 2021 11:19:16 -0500

compartmentalization

Diffstat:
A.gitignore | 10++++++++++
Mpystatsite.py | 48++----------------------------------------------
Asrc/interface.py | 35+++++++++++++++++++++++++++++++++++
Asrc/share.py | 20++++++++++++++++++++
4 files changed, 67 insertions(+), 46 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,10 @@ +__pycache__/ +build +*.zip +*.swp + +# For those who use PyCharm +.idea/ + +# For venv +env diff --git a/pystatsite.py b/pystatsite.py @@ -1,50 +1,6 @@ #! /usr/bin/env python -import json, os -from src import compose, sync, add - -# Interface function -def interface(interface_name = ""): - chosen = False - choice = None - choices = loaded_interfaces[interface_name]["choices"] - choices.append(["Quit", "exit"]) - while not chosen: - os.system("clear") - 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: - print("Invalid value. Please choose from the provided choices.") - print("Press ENTER to continue...") - input() - - 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) +from src import interface if __name__ == "__main__": - interface("main") + interface.launch_interface("main") diff --git a/src/interface.py b/src/interface.py @@ -0,0 +1,35 @@ +#! /usr/bin/env python + +import os +from .share import * + +# Function def +def launch_interface(interface_name = ""): + chosen = False + choice = None + choices = LOADED_INTERFACES[interface_name]["choices"] + choices.append(["Quit", "exit"]) + while not chosen: + os.system("clear") + 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: + print("Invalid value. Please choose from the provided choices.") + print("Press ENTER to continue...") + input() + + INTERFACE_FUNCS[choices[choice - 1][1]][0](INTERFACE_FUNCS[choices[choice - 1][1]][1]) + +# Procedural part to format interface funcs dict from constants +for k in INTERFACE_FUNCS.keys(): + if INTERFACE_FUNCS[k][0] == 0: + INTERFACE_FUNCS[k][0] = launch_interface diff --git a/src/share.py b/src/share.py @@ -0,0 +1,20 @@ +#! /usr/bin/env python + +import os, json + +# Values +DATA_DIR = "data" +OPTIONS_FILE = "options.json" +INTERFACES_FILE = os.path.join(DATA_DIR, "interfaces.json") +LOADED_OPTIONS = {} +LOADED_INTERFACES = {} +INTERFACE_FUNCS = { + "new_page" : [0, "new_page"], + "edit_page" : [0, "edit_page"], + "sync_site" : [0, "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)