consolidate variables in config module

This commit is contained in:
nathaniel smith
2017-11-20 22:02:10 -08:00
parent e910ce5518
commit ab17642361
5 changed files with 171 additions and 120 deletions

View File

@@ -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 <endorphant@tilde.town)"
## system globals
SOURCE = os.path.join("/home", "endorphant", "projects", "ttbp", "bin")
LIVE = "http://tilde.town/~"
FEEDBACK = os.path.join("/home", "endorphant", "ttbp-mail")
FEEDBOX = "endorphant@tilde.town"
USERFILE = os.path.join("/home", "endorphant", "projects", "ttbp", "users.txt")
GRAFF_DIR = os.path.join(SOURCE, "graffiti")
WALL = os.path.join(GRAFF_DIR, "wall.txt")
WALL_LOCK = os.path.join(GRAFF_DIR, ".lock")
p = inflect.engine()
## user globals
USER = os.path.basename(os.path.expanduser("~"))
PATH = os.path.join("/home", USER, ".ttbp")
PUBLIC = os.path.join("/home", USER, "public_html")
WWW = os.path.join(PATH, "www")
CONFIG = os.path.join(PATH, "config")
TTBPRC = os.path.join(CONFIG, "ttbprc")
DATA = os.path.join(PATH, "entries")
SETTINGS = {
"editor": "none",
"publish dir": False
}
## ui globals
BANNER = util.attach_rainbow()+open(os.path.join(SOURCE, "config", "banner.txt")).read()+util.attach_reset()
BANNER = util.attach_rainbow() + config.BANNER + util.attach_reset()
SPACER = "\n"
INVALID = "please pick a number from the list of options!\n\n"
DUST = "sorry about the dust, but this part is still under construction. check back later!\n\n"
@@ -231,14 +215,14 @@ def check_init():
print("\n\n")
if os.path.exists(os.path.join(os.path.expanduser("~"),".ttbp")):
print(chatter.say("greet")+", "+USER+".\n")
print(chatter.say("greet")+", "+config.USER+".\n")
'''
## ttbprc validation
while not os.path.isfile(TTBPRC):
while not os.path.isfile(config.TTBPRC):
setup_repair()
try:
SETTINGS = json.load(open(TTBPRC))
SETTINGS = json.load(open(config.TTBPRC))
except ValueError:
setup_repair()
'''
@@ -281,28 +265,30 @@ press <enter> to begin, or <ctrl-c> 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():
<html>
<head>
<!--- this header automatically generated by ttbp initialization on """+time.strftime("%Y-%m-%d %h:m")+""" --->
<title>~"""+USER+""" on TTBP</title>
<title>~"""+config.USER+""" on TTBP</title>
<link rel=\"stylesheet\" href=\"style.css\" />
</head>
<body>
<div id=\"meta\">
<h1><a href=\"index.html#\">~"""+USER+"""</a>@<a href=\"/~endorphant/ttbp\">TTBP</a></h1>
<h1><a href=\"index.html#\">~"""+config.USER+"""</a>@<a href=\"/~endorphant/ttbp\">TTBP</a></h1>
</div>
<!---put your custom html here-->
@@ -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 <enter> 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("<h1>ttbp blog placeholder</h1>")
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()