From ab17642361b656ea5cb364b504117f49624d89fe Mon Sep 17 00:00:00 2001 From: nathaniel smith Date: Mon, 20 Nov 2017 22:02:10 -0800 Subject: [PATCH] consolidate variables in config module --- ttbp/config/__init__.py | 82 ++++++++++++++++++++++ ttbp/config/banner.txt | 8 --- ttbp/core.py | 45 +++++------- ttbp/ttbp.py | 150 ++++++++++++++++++---------------------- ttbp/util.py | 6 +- 5 files changed, 171 insertions(+), 120 deletions(-) create mode 100644 ttbp/config/__init__.py delete mode 100644 ttbp/config/banner.txt diff --git a/ttbp/config/__init__.py b/ttbp/config/__init__.py new file mode 100644 index 0000000..6d8639d --- /dev/null +++ b/ttbp/config/__init__.py @@ -0,0 +1,82 @@ +from __future__ import absolute_import +import os + +from . import util + +## System config + +# We refer to some package files (ie .css stuff), so we save a reference to the +# path. +INSTALL_PATH = dirname(sys.modules['ttbp'].__file__) + +# We use this to store any persisted, global state. +VAR = '/var/global/ttbp' +VAR_WWW = os.path.join(VAR, 'www') + +if not os.path.isdir('/var/global'): + raise Exception('bad system state: /var/global does not exist.') + +if not os.path.isdir(VAR): + os.mkdir(VAR) + +if not os.path.isdir(VAR_WWW): + os.mkdir(VAR_WWW) + +LIVE = 'https://tilde.town/~' +FEEDBOX = "endorphant@tilde.town" +USERFILE = os.path.join(VAR, "users.txt") +GRAFF_DIR = os.path.join(VAR, "graffiti") +WALL = os.path.join(GRAFF_DIR, "wall.txt") +WALL_LOCK = os.path.join(GRAFF_DIR, ".lock") + +## Defaults + +DEFAULT_HEADER = ''' + + + + $USER on TTBP + + + +
+

~$USER@TTBP

+
+ +
+'''.ltrim() + +DEFAULT_FOOTER = ''' +
+ + +'''.ltrim() + +with open(os.path.join(INSTALL_PATH, 'config', 'defaults', 'style.css')) as f: + DEFAULT_STYLE = f.read() + + +## User config + +USER = os.path.basename(os.path.expanduser('~')) +USER_HOME = os.path.expanduser('~') +PATH = os.path.join(USER_HOME, '.ttbp') +PUBLIC = os.path.join(USER_HOME, 'public_html') +WWW = os.path.join(PATH, 'www') +USER_CONFIG = os.path.join(PATH, 'config') +TTBPRC = os.path.join(USER_CONFIG, 'ttbprc') +USER_DATA = os.path.join(PATH, 'entries') +NOPUB = os.path.join(USER_CONFIG, "nopub") + +## UI + +BANNER = ''' +__________________________________________________________ +| | +| the tilde.town | +| ____ ____ ____ _ ____ ____ _ _ ____ _ _ _ ____ | +| |___ |___ |___ | [__ |___ |\ | | __ | |\ | |___ | +| | |___ |___ |___ ___] |___ | \| |__] | | \| |___ | +| ver 0.10.0 (almost stable) | +|__________________________________________________________| +'''.ltrim() diff --git a/ttbp/config/banner.txt b/ttbp/config/banner.txt deleted file mode 100644 index 53f8893..0000000 --- a/ttbp/config/banner.txt +++ /dev/null @@ -1,8 +0,0 @@ - __________________________________________________________ -| | -| the tilde.town | -| ____ ____ ____ _ ____ ____ _ _ ____ _ _ _ ____ | -| |___ |___ |___ | [__ |___ |\ | | __ | |\ | |___ | -| | |___ |___ |___ ___] |___ | \| |__] | | \| |___ | -| ver 0.9.2 (almost stable) | -|__________________________________________________________| diff --git a/ttbp/core.py b/ttbp/core.py index 852883f..31f164d 100644 --- a/ttbp/core.py +++ b/ttbp/core.py @@ -39,19 +39,10 @@ import re import mistune import json -import chatter +from . import chatter +from . import config -SOURCE = os.path.join("/home", "endorphant", "projects", "ttbp", "bin") -USER = os.path.basename(os.path.expanduser("~")) -PATH = os.path.join("/home", USER, ".ttbp") - -LIVE = "http://tilde.town/~" -WWW = os.path.join(PATH, "www") -CONFIG = os.path.join(PATH, "config") -DATA = os.path.join(PATH, "entries") -FEED = os.path.join(SOURCE, "www", "index.html") -DOCS = os.path.join(SOURCE, "www", "help.html") -NOPUB = os.path.join(CONFIG, "nopub") +FEED = os.path.join(config.VAR_WWW, "index.html") SETTINGS = {} HEADER = "" @@ -67,8 +58,8 @@ def load(ttbprc={}): global FOOTER global SETTINGS - HEADER = open(os.path.join(CONFIG, "header.txt")).read() - FOOTER = open(os.path.join(CONFIG, "footer.txt")).read() + HEADER = open(os.path.join(config.USER_CONFIG, "header.txt")).read() + FOOTER = open(os.path.join(config.USER_CONFIG, "footer.txt")).read() SETTINGS = ttbprc load_files() @@ -94,13 +85,13 @@ def load_files(): FILES = [] - for filename in os.listdir(DATA): + for filename in os.listdir(config.USER_DATA): if nopub(filename): - link = os.path.join(WWW, os.path.splitext(os.path.basename(filename))[0]+".html") + link = os.path.join(config.WWW, os.path.splitext(os.path.basename(filename))[0]+".html") if os.path.exists(link): subprocess.call(["rm", link]) continue - filename = os.path.join(DATA, filename) + filename = os.path.join(config.USER_DATA, filename) if os.path.isfile(filename) and valid(filename): FILES.append(filename) @@ -119,7 +110,7 @@ def write(outurl="default.html"): * calls write_page() on each file to make permalinks ''' - outfile = open(os.path.join(WWW, outurl), "w") + outfile = open(os.path.join(config.WWW, outurl), "w") outfile.write("\n\n") @@ -140,7 +131,7 @@ def write(outurl="default.html"): outfile.close() - return os.path.join(LIVE+USER,os.path.basename(os.path.realpath(WWW)),outurl) + return os.path.join(config.LIVE+config.USER,os.path.basename(os.path.realpath(config.WWW)),outurl) def write_page(filename): ''' @@ -150,7 +141,7 @@ def write_page(filename): url ''' - outurl = os.path.join(WWW, "".join(parse_date(filename))+".html") + outurl = os.path.join(config.WWW, "".join(parse_date(filename))+".html") outfile = open(outurl, "w") outfile.write("\n\n") @@ -190,7 +181,7 @@ def write_entry(filename): ] raw = [] - rawfile = open(os.path.join(DATA, filename), "r") + rawfile = open(os.path.join(config.USER_DATA, filename), "r") for line in rawfile: raw.append(line) @@ -244,7 +235,7 @@ def write_global_feed(blogList): ## docs outfile.write("""\
""") - outfile.write(mistune.markdown(open(os.path.join(SOURCE, "..", "README.md"), "r").read())) + outfile.write(mistune.markdown(open(os.path.join(config.INSTALL_PATH, "..", "README.md"), "r").read())) outfile.write("""\
""") @@ -354,14 +345,14 @@ def find_ttbps(): return users -def publishing(username = USER): +def publishing(username=config.USER): ''' checks .ttbprc for whether or not user opted for www publishing ''' ttbprc = {} - if username == USER: + if username == config.USER: ttbprc = SETTINGS else: @@ -384,7 +375,7 @@ def www_neighbors(): url = "" if userRC["publish dir"]: - url = LIVE+user+"/"+userRC["publish dir"] + url = config.LIVE+user+"/"+userRC["publish dir"] lastfile = "" files = os.listdir(os.path.join("/home", user, ".ttbp", "entries")) @@ -418,8 +409,8 @@ def nopub(filename): exclude = [] - if os.path.isfile(NOPUB): - for line in open(NOPUB, "r"): + if os.path.isfile(config.NOPUB): + for line in open(config.NOPUB, "r"): exclude.append(line.rstrip()) return os.path.basename(filename) in exclude diff --git a/ttbp/ttbp.py b/ttbp/ttbp.py index 28b8231..85bbdf5 100644 --- a/ttbp/ttbp.py +++ b/ttbp/ttbp.py @@ -44,6 +44,7 @@ import re import inflect +from . import config from . import core from . import chatter from . import util @@ -51,33 +52,16 @@ from . import util __version__ = "0.10.0" __author__ = "endorphant to begin, or to get out of here. quit() ## record user in source list - users = open(USERFILE, 'a') - users.write(USER+"\n") + users = open(config.USERFILE, 'a') + users.write(config.USER+"\n") users.close() ## make .ttbp directory structure - subprocess.call(["mkdir", PATH]) - subprocess.call(["mkdir", CONFIG]) - subprocess.call(["mkdir", DATA]) + subprocess.call(["mkdir", config.PATH]) + subprocess.call(["mkdir", config.USER_CONFIG]) + subprocess.call(["mkdir", config.USER_DATA]) - versionFile = os.path.join(PATH, "version") + versionFile = os.path.join(config.PATH, "version") open(versionFile, "w").write(__version__) ## create header file header = gen_header() - headerfile = open(os.path.join(CONFIG, "header.txt"), 'w') + headerfile = open(os.path.join(config.USER_CONFIG, "header.txt"), 'w') for line in header: headerfile.write(line) headerfile.close() ## copy footer and default stylesheet - subprocess.call(["cp", os.path.join(SOURCE, "config", "defaults", "footer.txt"), CONFIG]) - subprocess.call(["cp", os.path.join(SOURCE, "config", "defaults", "style.css"), CONFIG]) + with open(os.path.join(config.USER_CONFIG, 'footer.txt', 'w')) as f: + f.write(config.DEFAULT_FOOTER) + with open(os.path.join(config.USER_CONFIG, 'style.css', 'w')) as f: + f.write(config.DEFAULT_STYLE) ## run user-interactive setup and load core engine setup() @@ -323,12 +309,12 @@ def gen_header(): - ~"""+USER+""" on TTBP + ~"""+config.USER+""" on TTBP @@ -347,11 +333,11 @@ def valid_setup(): global SETTINGS - if not os.path.isfile(TTBPRC): + if not os.path.isfile(config.TTBPRC): return False try: - SETTINGS = json.load(open(TTBPRC)) + SETTINGS = json.load(open(config.TTBPRC)) except ValueError: return False @@ -359,10 +345,10 @@ def valid_setup(): if not SETTINGS.get("publish dir"): return False - if not os.path.exists(WWW): + if not os.path.exists(config.WWW): return False - if not os.path.exists(os.path.join(WWW, SETTINGS.get("pubish dir"))): + if not os.path.exists(os.path.join(config.WWW, SETTINGS.get("pubish dir"))): return False return True @@ -398,7 +384,7 @@ def setup(): print("\n\ttext editor:\t" +SETTINGS.get("editor")) if core.publishing(): - print("\tpublish dir:\t" +os.path.join(PUBLIC, str(SETTINGS.get("publish dir")))) + print("\tpublish dir:\t" +os.path.join(config.PUBLIC, str(SETTINGS.get("publish dir")))) print("\tpubishing:\t"+str(SETTINGS.get("publishing"))+"\n") # editor selection @@ -412,10 +398,10 @@ def setup(): redraw("blog publishing: "+str(core.publishing())) if core.publishing(): - print("publish directory: ~"+USER+"/public_html/"+SETTINGS.get("publish dir")) + print("publish directory: ~"+config.USER+"/public_html/"+SETTINGS.get("publish dir")) # save settings - ttbprc = open(TTBPRC, "w") + ttbprc = open(config.TTBPRC, "w") ttbprc.write(json.dumps(SETTINGS, sort_keys=True, indent=2, separators=(',',':'))) ttbprc.close() @@ -454,7 +440,7 @@ def main_menu(): if choice == '0': redraw() today = time.strftime("%Y%m%d") - write_entry(os.path.join(DATA, today+".txt")) + write_entry(os.path.join(config.USER_DATA, today+".txt")) core.www_neighbors() elif choice == '1': if core.publishing(): @@ -465,7 +451,7 @@ def main_menu(): core.write("index.html") else: redraw("your recorded feels, listed by date:") - view_feels(USER) + view_feels(config.USER) elif choice == '2': users = core.find_ttbps() prompt = "the following "+p.no("user", len(users))+" "+p.plural("is", len(users))+" recording feels on ttbp:" @@ -490,7 +476,7 @@ def main_menu(): redraw() show_credits() elif choice == '8': - subprocess.call(["lynx", os.path.join(SOURCE, "..", "README.html")]) + subprocess.call(["lynx", os.path.join(config.INSTALL_PATH, "..", "README.html")]) redraw() elif choice in QUITS: return stop() @@ -543,7 +529,7 @@ def review_menu(intro=""): if choice is not False: if choice == 0: redraw("your recorded feels, listed by date:") - view_feels(USER) + view_feels(config.USER) elif choice == 1: redraw("here's your current nopub status:") set_nopubs() @@ -570,7 +556,7 @@ def view_neighbors(users, prompt): ## retrieve publishing url, if it exists url="\t\t\t" if userRC.get("publish dir"): - url = LIVE+user+"/"+userRC.get("publish dir") + url = config.LIVE+user+"/"+userRC.get("publish dir") ## find last entry files = os.listdir(os.path.join("/home", user, ".ttbp", "entries")) @@ -630,8 +616,8 @@ def view_feels(townie): filenames = [] showpub = False - if townie == USER: - entryDir = DATA + if townie == config.USER: + entryDir = config.USER_DATA owner = "your" if core.publishing(): showpub = True @@ -685,7 +671,7 @@ thanks to everyone who reads, listens, writes, and feels.\ ## handlers -def write_entry(entry=os.path.join(DATA, "test.txt")): +def write_entry(entry=os.path.join(config.USER_DATA, "test.txt")): ''' main feels-recording handler ''' @@ -715,7 +701,7 @@ editor. if core.publishing(): core.load_files() core.write("index.html") - left = "posted to "+LIVE+USER+"/"+str(SETTINGS.get("publish dir"))+"/index.html\n\n>" + left = "posted to "+config.LIVE+config.USER+"/"+str(SETTINGS.get("publish dir"))+"/index.html\n\n>" redraw(left + " thanks for sharing your feels!") return @@ -745,8 +731,8 @@ def send_feedback(entered, subject="none"): if message: id = "#"+util.genID(3) mail = MIMEText(message) - mail['To'] = FEEDBOX - mail['From'] = USER+"@tilde.town" + mail['To'] = config.FEEDBOX + mail['From'] = config.USER+"@tilde.town" mail['Subject'] = " ".join(["[ttbp]", subject, id]) m = os.popen("/usr/sbin/sendmail -t -oi", 'w') m.write(mail.as_string()) @@ -838,10 +824,10 @@ def graffiti_handler(): Main graffiti handler. ''' - if os.path.isfile(WALL_LOCK): + if os.path.isfile(config.WALL_LOCK): redraw("sorry, "+chatter.say("friend")+", but someone's there right now. try again in a few!") else: - subprocess.call(["touch", WALL_LOCK]) + subprocess.call(["touch", config.WALL_LOCK]) redraw() print("""\ the graffiti wall is a world-writeable text file. anyone can @@ -855,8 +841,8 @@ your changes by exiting without saving. """) raw_input("press to visit the wall\n\n") - subprocess.call([SETTINGS.get("editor"), WALL]) - subprocess.call(["rm", WALL_LOCK]) + subprocess.call([SETTINGS.get("editor"), config.WALL]) + subprocess.call(["rm", config.WALL_LOCK]) redraw("thanks for visiting the graffiti wall!") @@ -886,14 +872,14 @@ def select_publish_dir(): republish = False if current: - print("\ncurrent publish dir:\t"+os.path.join(PUBLIC, SETTINGS["publish dir"])) + print("\ncurrent publish dir:\t"+os.path.join(config.PUBLIC, SETTINGS["publish dir"])) republish = True choice = raw_input("\nwhere do you want your blog published? (leave blank to use default \"blog\") ") if not choice: choice = "blog" - publishDir = os.path.join(PUBLIC, choice) + publishDir = os.path.join(config.PUBLIC, choice) while os.path.exists(publishDir): second = raw_input("\n"+publishDir+"""\ already exists! @@ -907,7 +893,7 @@ otherwise, pick another location: """) if second == "": break choice = second - publishDir = os.path.join(PUBLIC, choice) + publishDir = os.path.join(config.PUBLIC, choice) return choice @@ -939,9 +925,9 @@ def unpublish(): remove user's published directory, if it exists. this should only remove the symlink in public_html. ''' - dir = SETTINGS.get("publish dir") + directory = SETTINGS.get("publish dir") if dir: - publishDir = os.path.join(PUBLIC, dir) + publishDir = os.path.join(config.PUBLIC, directory) subprocess.call(["rm", publishDir]) def update_publishing(): @@ -956,7 +942,7 @@ def update_publishing(): newDir = select_publish_dir() SETTINGS.update({"publish dir": newDir}) if oldDir: - subprocess.call(["rm", os.path.join(PUBLIC, oldDir)]) + subprocess.call(["rm", os.path.join(config.PUBLIC, oldDir)]) make_publish_dir(newDir) core.load_files() core.write("index.html") @@ -971,21 +957,21 @@ def make_publish_dir(dir): setup helper to create publishing directory ''' - if not os.path.exists(WWW): - subprocess.call(["mkdir", WWW]) - subprocess.call(["ln", "-s", os.path.join(CONFIG, "style.css"), os.path.join(WWW, "style.css")]) - subprocess.call(["touch", os.path.join(WWW, "index.html")]) - index = open(os.path.join(WWW, "index.html"), "w") + if not os.path.exists(config.WWW): + subprocess.call(["mkdir", config.WWW]) + subprocess.call(["ln", "-s", os.path.join(config.USER_CONFIG, "style.css"), os.path.join(config.WWW, "style.css")]) + subprocess.call(["touch", os.path.join(config.WWW, "index.html")]) + index = open(os.path.join(config.WWW, "index.html"), "w") index.write("

ttbp blog placeholder

") index.close() - publishDir = os.path.join(PUBLIC, dir) + publishDir = os.path.join(config.PUBLIC, dir) if os.path.exists(publishDir): subprocess.call(["rm", publishDir]) - subprocess.call(["ln", "-s", WWW, publishDir]) + subprocess.call(["ln", "-s", config.WWW, publishDir]) - print("\n\tpublishing to "+LIVE+USER+"/"+SETTINGS.get("publish dir")+"/\n\n") + print("\n\tpublishing to "+config.LIVE+config.USER+"/"+SETTINGS.get("publish dir")+"/\n\n") ##### PATCHING UTILITIES @@ -994,7 +980,7 @@ def build_mismatch(): checks to see if user's last run build is the same as this session ''' - versionFile = os.path.join(PATH, "version") + versionFile = os.path.join(config.PATH, "version") if not os.path.exists(versionFile): return False @@ -1020,7 +1006,7 @@ def switch_build(ver): print("\nswitching you over to the "+build+" version...\n") time.sleep(1) print("...") - versionFile = os.path.join(PATH, "version") + versionFile = os.path.join(config.PATH, "version") open(versionFile, "w").write(ver) time.sleep(1) #print("\nall good!\n") @@ -1030,7 +1016,7 @@ def updated(): checks to see if current user is up to the same version as system ''' - versionFile = os.path.join(PATH, "version") + versionFile = os.path.join(config.PATH, "version") if not os.path.exists(versionFile): return False @@ -1048,7 +1034,7 @@ def update_version(): global SETTINGS - versionFile = os.path.join(PATH, "version") + versionFile = os.path.join(config.PATH, "version") print("ttbp had some updates!") @@ -1065,22 +1051,22 @@ def update_version(): # change style.css location if core.publishing(): - if os.path.isfile(os.path.join(WWW, "style.css")): - subprocess.call(["mv", os.path.join(WWW, "style.css"), CONFIG]) + if os.path.isfile(os.path.join(config.WWW, "style.css")): + subprocess.call(["mv", os.path.join(config.WWW, "style.css"), config.USER_CONFIG]) # change www symlink - if os.path.exists(WWW): - subprocess.call(["rm", WWW]) + if os.path.exists(config.WWW): + subprocess.call(["rm", config.WWW]) - subprocess.call(["mkdir", WWW]) + subprocess.call(["mkdir", config.WWW]) - subprocess.call(["ln", "-s", os.path.join(CONFIG, "style.css"), os.path.join(WWW, "style.css")]) + subprocess.call(["ln", "-s", os.path.join(config.USER_CONFIG, "style.css"), os.path.join(config.WWW, "style.css")]) - publishDir = os.path.join(PUBLIC, SETTINGS.get("publish dir")) + publishDir = os.path.join(config.PUBLIC, SETTINGS.get("publish dir")) if os.path.exists(publishDir): subprocess.call(["rm", "-rf", publishDir]) - subprocess.call(["ln", "-s", WWW, os.path.join(PUBLIC, SETTINGS.get("publish dir"))]) + subprocess.call(["ln", "-s", config.WWW, os.path.join(config.PUBLIC, SETTINGS.get("publish dir"))]) # repopulate html files core.load_files() @@ -1090,7 +1076,7 @@ def update_version(): print("\nnew feature!\n") SETTINGS.update({"publishing":select_publishing()}) update_publishing() - ttbprc = open(TTBPRC, "w") + ttbprc = open(config.TTBPRC, "w") ttbprc.write(json.dumps(SETTINGS, sort_keys=True, indent=2, separators=(',',':'))) ttbprc.close() @@ -1102,7 +1088,7 @@ def update_version(): print("\nresetting your publishing settings...\n") SETTINGS.update({"publishing":select_publishing()}) update_publishing() - ttbprc = open(TTBPRC, "w") + ttbprc = open(config.TTBPRC, "w") ttbprc.write(json.dumps(SETTINGS, sort_keys=True, indent=2, separators=(',',':'))) ttbprc.close() diff --git a/ttbp/util.py b/ttbp/util.py index 269e692..567fa8c 100644 --- a/ttbp/util.py +++ b/ttbp/util.py @@ -23,11 +23,11 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' - -import inflect -import time import random +import time + import colorama +import inflect ## misc globals BACKS = ['back', 'b', 'q']