code cleanup

adding more code comments, cleaning up deprecated code, improving
general readability
master
endorphant 2016-05-22 00:45:04 -04:00
parent dccb1570d8
commit f98513a079
5 changed files with 122 additions and 29 deletions

View File

@ -184,7 +184,7 @@ def init():
raw_input("""
i don't recognize you, stranger. let's make friends.
press <enter> to begin, or <ctrl-c> to get out of here.\
press <enter> to begin, or <ctrl-c> to get out of here.
""")
except KeyboardInterrupt:
print("\n\nthanks for checking in! i'll always be here.\n\n")
@ -225,7 +225,7 @@ def gen_header():
builds header to insert username
'''
header ="""\
header ="""
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2//EN\">
<html>
<head>
@ -354,7 +354,7 @@ def main_menu():
redraw("the following "+p.no("user", len(users))+" "+p.plural("is", len(users))+" recording feels on ttbp:\n")
view_neighbors(users)
elif choice == '3':
redraw("now viewing most recent entries\n")
redraw("most recent global entries\n")
view_feed()
elif choice == '4':
pretty_settings = "\n\n\ttext editor:\t" +SETTINGS.get("editor")
@ -603,7 +603,7 @@ def view_feed():
entries.append("~"+entry[5]+pad+"\ton "+entry[3]+" ("+p.no("word", entry[2])+") ")
#print_menu(entries)
view_entries(metas, entries, "most recent ten entries: \n\n")
view_entries(metas, entries, "most recent global entries: \n\n")
redraw()
@ -903,4 +903,4 @@ def update_version():
if __name__ == '__main__':
start()
#print("ttbp beta is out to lunch. bbl.")
#print("ttbp beta is out to lunch. bbl.")

View File

@ -39,8 +39,21 @@ LANG = json.load(langfile)
langfile.close()
def say(keyword):
'''
takes a keyword and randomly returns from language dictionary to match that keyword
returns None if keyword doesn't exist
TODO: validate keyword?
'''
return random.choice(LANG.get(keyword))
def month(num):
'''
takes a MM and returns lovercase full name of that month
TODO: validate num?
'''
return LANG["months"].get(num)

View File

@ -214,7 +214,7 @@ def meta(entries = FILES):
* takes a list of filenames and returns a 2d list:
[0] absolute path
[1] ctime
[1] mtime
[2] wc -w
[3] timestamp "DD month YYYY at HH:MM"
[4] entry date YYYY-MM-DD
@ -226,14 +226,14 @@ def meta(entries = FILES):
meta = []
for filename in entries:
ctime = os.path.getctime(filename)
mtime = os.path.getmtime(filename)
wc = subprocess.check_output(["wc","-w",filename]).split()[0]
timestamp = time.strftime("%Y-%m-%d at %H:%M", time.localtime(ctime))
timestamp = time.strftime("%Y-%m-%d at %H:%M", time.localtime(mtime))
date = "-".join(parse_date(filename))
author = os.path.split(os.path.split(os.path.split(os.path.split(filename)[0])[0])[0])[1]
meta.append([filename, ctime, wc, timestamp, date, author])
meta.append([filename, mtime, wc, timestamp, date, author])
meta.sort(key = lambda filename:filename[4])
meta.reverse()

View File

@ -81,6 +81,12 @@ SUBJECTS = ["help request", "bug report", "feature suggestion", "general comment
##
def redraw(leftover=""):
'''
screen clearing
* clears the screen and reprints the banner, plus whatever leftover text to be hilights
'''
os.system("clear")
print(BANNER)
print(SPACER)
@ -88,9 +94,16 @@ def redraw(leftover=""):
print("> "+leftover+"\n")
def start():
'''
main engine head
* called on program start
* calls config check
* proceeds to main menu
* handles ^c and ^d ejects
'''
redraw()
#print(chatter.say("greet")+", "+chatter.say("friend"))
#print("(remember, you can always press ctrl-c to come home)\n")
print("""
if you don't want to be here at any point, press <ctrl-d> and it'll all go away.
just keep in mind that you might lose anything you've started here.\
@ -117,13 +130,29 @@ just keep in mind that you might lose anything you've started here.\
break
def stop():
'''
closer
* prints ending text
'''
return "\n\n\t"+chatter.say("bye")+"\n\n"
def check_init():
'''
user handler
* checks for presence of ttbprc
* checks for last run version
'''
global SETTINGS
print("\n\n")
if os.path.exists(os.path.join(os.path.expanduser("~"),".ttbp")):
print(chatter.say("greet")+", "+USER+".\n")
## ttbprc validation
while not os.path.isfile(TTBPRC):
setup_handler()
try:
@ -135,6 +164,7 @@ def check_init():
if not updated():
print(update_version())
## when ready, enter main program and load core engine
raw_input("press <enter> to explore your feels.\n\n")
core.load()
@ -143,6 +173,13 @@ def check_init():
return init()
def init():
'''
new user creation
* introduces user
* calls setup functinos
'''
try:
raw_input("""
i don't recognize you, stranger. let's make friends.
@ -153,22 +190,28 @@ press <enter> to begin, or <ctrl-c> to get out of here.
print("\n\nthanks for checking in! i'll always be here.\n\n")
quit()
## record user in source list
users = open(USERFILE, 'a')
users.write(USER+"\n")
users.close()
## make .ttbp directory structure
subprocess.call(["mkdir", PATH])
subprocess.call(["mkdir", CONFIG])
subprocess.call(["mkdir", DATA])
## create header file
header = gen_header()
headerfile = open(os.path.join(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])
## run user-interactive setup and load core engine
setup()
core.load()
@ -176,10 +219,17 @@ press <enter> to begin, or <ctrl-c> to get out of here.
return ""
def gen_header():
'''
header generator
builds header to insert username
'''
header ="""
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2//EN\">
<html>
<head>
<!--- this header automatically generated by ttbp initialization on """+time.strftime("%Y-%m-%d %h:m")+""" --->
<title>~"""+USER+""" on TTBP</title>
<link rel=\"stylesheet\" href=\"style.css\" />
</head>
@ -198,6 +248,13 @@ def gen_header():
return header
def setup_handler():
'''
setup wrapper function
* calls setup()
* handles ^c
'''
print("\nyour ttbp configuration doesn't look right. let's make you a fresh copy.\n\n")
try:
setup()
@ -206,6 +263,17 @@ def setup_handler():
setup()
def setup():
'''
master setup function
* editor selection
* publishing toggle
* publish/unpublish as needed
* directory selection
TODO: break this out better?
'''
global SETTINGS
# editor selection
@ -219,6 +287,7 @@ def setup():
if publishing():
print("publish directory: ~"+USER+"/public_html/"+SETTINGS.get("publish dir"))
# save settings
ttbprc = open(TTBPRC, "w")
ttbprc.write(json.dumps(SETTINGS, sort_keys=True, indent=2, separators=(',',':')))
@ -231,6 +300,12 @@ def setup():
## menus
def print_menu(menu):
'''
pretty menu handler
* takes list of options and prints them
'''
i = 0
for x in menu:
line = []
@ -242,6 +317,10 @@ def print_menu(menu):
i += 1
def main_menu():
'''
main navigation menu
'''
menuOptions = [
"record your feels",
"review your feels",
@ -250,8 +329,8 @@ def main_menu():
"change your settings",
"send some feedback",
"see credits"]
print("you're at ttbp home. remember, you can always press <ctrl-c> to come back here.\n\n")
#print("you're at ttbp home.\n\n")
print_menu(menuOptions)
try:
@ -273,7 +352,7 @@ def main_menu():
redraw("the following "+p.no("user", len(users))+" "+p.plural("is", len(users))+" recording feels on ttbp:\n")
view_neighbors(users)
elif choice == '3':
redraw("now viewing most recent entries\n")
redraw("most recent global entries\n")
view_feed()
elif choice == '4':
pretty_settings = "\n\n\ttext editor:\t" +SETTINGS.get("editor")
@ -299,9 +378,14 @@ def main_menu():
return main_menu()
###
def feedback_menu():
'''
feedback handling menu
* selects feedback type
* calls feedback writing function
'''
print("you're about to send mail to ~endorphant about ttbp\n\n")
print_menu(SUBJECTS)
@ -441,7 +525,7 @@ press <enter> to begin recording your feels.
return
def send_feedback(entered, subject="none", mailbox=os.path.join(FEEDBACK, USER+"-"+time.strftime("%Y%m%d-%H%M")+".msg")):
def send_feedback(entered, subject="none"):
message = ""
@ -517,7 +601,7 @@ def view_feed():
entries.append("~"+entry[5]+pad+"\ton "+entry[3]+" ("+p.no("word", entry[2])+") ")
#print_menu(entries)
view_entries(metas, entries, "most recent ten entries: \n\n")
view_entries(metas, entries, "most recent global entries: \n\n")
redraw()
@ -815,5 +899,5 @@ def update_version():
#####
start()
#print("ttbp beta is out to lunch. bbl.")
if __name__ == '__main__':
start()

View File

@ -1,9 +1,5 @@
config file things:
-preferred text editor
-set www symlink
-set page title
-set colorizing pref
NOTES FOR ~ENDO
flair:
-themes/stylesheets
-global post announcements
handling global feels out of order:
-check to see if entry filename date and mtime date matches
-if not, process as if it was last modified on the date indicated by filename