adding neighbor feels browsing

made the feels browser generalized so that you can now plug in anyone
with a ttbp. neighbor view now allows you to pick a ttbp to browse.

also cleaned up some code with more docs, better function names.
master
endorphant 2016-05-22 22:14:16 -04:00
parent 03cffe3e30
commit d300c19a2c
1 changed files with 97 additions and 41 deletions

View File

@ -279,6 +279,11 @@ def setup():
global SETTINGS global SETTINGS
print("\n\ttext editor:\t" +SETTINGS.get("editor"))
if publishing():
print("\tpublish dir:\t" +os.path.join(PUBLIC, SETTINGS.get("publish dir")))
print("\tpubishing:\t"+str(SETTINGS.get("publishing"))+"\n")
# editor selection # editor selection
SETTINGS.update({"editor": select_editor()}) SETTINGS.update({"editor": select_editor()})
redraw("text editor set to: "+SETTINGS["editor"]) redraw("text editor set to: "+SETTINGS["editor"])
@ -298,6 +303,7 @@ def setup():
raw_input("\nyou're all good to go, "+chatter.say("friend")+"! hit <enter> to continue.\n\n") raw_input("\nyou're all good to go, "+chatter.say("friend")+"! hit <enter> to continue.\n\n")
redraw() redraw()
return SETTINGS return SETTINGS
## menus ## menus
@ -351,8 +357,8 @@ def main_menu():
write_entry(os.path.join(DATA, today+".txt")) write_entry(os.path.join(DATA, today+".txt"))
www_neighbors(find_ttbps()) www_neighbors(find_ttbps())
elif choice == '1': elif choice == '1':
redraw("here are your recorded feels, listed by date:\n") redraw("your recorded feels, listed by date:\n")
view_own() view_feels(USER)
elif choice == '2': elif choice == '2':
users = find_ttbps() users = find_ttbps()
redraw("the following "+p.no("user", len(users))+" "+p.plural("is", len(users))+" recording feels on ttbp:\n") redraw("the following "+p.no("user", len(users))+" "+p.plural("is", len(users))+" recording feels on ttbp:\n")
@ -361,12 +367,7 @@ def main_menu():
redraw("most recent global entries\n") redraw("most recent global entries\n")
view_feed() view_feed()
elif choice == '4': elif choice == '4':
pretty_settings = "\n\n\ttext editor:\t" +SETTINGS.get("editor") redraw("now changing your settings. press <ctrl-c> if you didn't mean to do this.")
if publishing():
pretty_settings += "\n\tpublish dir:\t" +os.path.join(PUBLIC, SETTINGS.get("publish dir"))
pretty_settings += "\n\tpubishing:\t"+str(SETTINGS.get("publishing"))
redraw("now changing your settings. press <ctrl-c> if you didn't mean to do this."+pretty_settings+"\n")
try: try:
setup() setup()
except KeyboardInterrupt(): except KeyboardInterrupt():
@ -417,23 +418,32 @@ press <enter> to open an external text editor. mail will be sent once you save a
return feedback_menu() return feedback_menu()
def view_neighbors(users): def view_neighbors(users):
'''
generates list of all users on ttbp, sorted by most recent post
* if user is publishing, list publish directory
'''
userList = [] userList = []
## assumes list of users passed in all have valid config files
for user in users: for user in users:
userRC = json.load(open(os.path.join("/home", user, ".ttbp", "config", "ttbprc"))) userRC = json.load(open(os.path.join("/home", user, ".ttbp", "config", "ttbprc")))
## retrieve publishing url, if it exists
url="\t\t\t" url="\t\t\t"
if userRC.get("publish dir"): if userRC.get("publish dir"):
url = LIVE+user+"/"+userRC.get("publish dir") url = LIVE+user+"/"+userRC.get("publish dir")
count = 0
lastfile = "" ## find last entry
files = os.listdir(os.path.join("/home", user, ".ttbp", "entries")) files = os.listdir(os.path.join("/home", user, ".ttbp", "entries"))
files.sort() files.sort()
lastfile = ""
for filename in files: for filename in files:
if core.valid(filename): if core.valid(filename):
count += 1
lastfile = os.path.join("/home", user, ".ttbp", "entries", filename) lastfile = os.path.join("/home", user, ".ttbp", "entries", filename)
## generate human-friendly timestamp
ago = "never" ago = "never"
if lastfile: if lastfile:
last = os.path.getctime(lastfile) last = os.path.getctime(lastfile)
@ -442,44 +452,73 @@ def view_neighbors(users):
else: else:
last = 0 last = 0
pad = "" ## some formatting handwavin
if len(user) < 8: urlpad = ""
pad = "\t" if ago == "never":
user = "~"+user urlpad = "\t"
if len(user) < 8:
user += "\t"
userList.append(["\t"+user+"\t"+url+pad+"\t("+ago+")", last]) userpad = ""
if len(user) < 7:
userpad = "\t"
# sort user by most recent entry userList.append(["\t~"+user+userpad+"\t("+ago+")"+urlpad+"\t"+url, last, user])
# sort user by most recent entry for display
userList.sort(key = lambda userdata:userdata[1]) userList.sort(key = lambda userdata:userdata[1])
userList.reverse() userList.reverse()
sortedUsers = [] sortedUsers = []
userIndex = []
for user in userList: for user in userList:
sortedUsers.append(user[0]) sortedUsers.append(user[0])
userIndex.append(user[2])
print_menu(sortedUsers) print_menu(sortedUsers)
raw_input("\n\npress <enter> to go back home.\n\n") #raw_input("\n\npress <enter> to go back home.\n\n")
redraw() choice = list_select(sortedUsers, "pick a townie to browse their feels, or type 'back' to go home: ")
return if choice is not False:
redraw("~"+userIndex[choice]+"'s recorded feels, listed by date: \n")
view_feels(userIndex[choice])
view_neighbors(users)
else:
redraw()
return
def view_own(): def view_feels(townie):
'''
generates a list of all feels by given townie and displays in
date order
* calls list_entries() to select feel to read
'''
filenames = [] filenames = []
for entry in os.listdir(DATA): if townie == USER:
filenames.append(os.path.join(DATA, entry)) entryDir = DATA
owner = "your"
else:
owner = "~"+townie+"'s"
entryDir = os.path.join("/home", townie, ".ttbp", "entries")
for entry in os.listdir(entryDir):
filenames.append(os.path.join(entryDir, entry))
metas = core.meta(filenames) metas = core.meta(filenames)
entries = [] if len(filenames) > 0:
for entry in metas: entries = []
entries.append(""+entry[4]+" ("+p.no("word", entry[2])+") ") for entry in metas:
entries.append(""+entry[4]+" ("+p.no("word", entry[2])+") ")
return view_entries(metas, entries, "here are your recorded feels, listed by date: \n\n") return list_entries(metas, entries, owner+" recorded feels, listed by date: \n")
else:
redraw("no feels recorded by ~"+townie)
def show_credits(): def show_credits():
'''
prints author acknowledgements and commentary
'''
print(""" print("""
ttbp was written by ~endorphant in python. the codebase is ttbp was written by ~endorphant in python. the codebase is
@ -489,8 +528,10 @@ for the full changelog, see ~endorphant/projects/ttbp/changelog.txt
if you have ideas for ttbp, you are welcome to fork the repo and if you have ideas for ttbp, you are welcome to fork the repo and
work on it. i'm only a neophyte dev, so i apologize for any work on it. i'm only a neophyte dev, so i apologize for any
horrendously ugly coding habits i have. i'd love to hear about your bad style and practices of mine; i'm always open to suggestions for
ideas and brainstorm about new features! improvement.
i'd love to hear about your ideas and brainstorm about new features!
thanks to everyone who reads, listens, writes, and feels.\ thanks to everyone who reads, listens, writes, and feels.\
""") """)
@ -500,21 +541,24 @@ thanks to everyone who reads, listens, writes, and feels.\
return return
## handlers ## handlers
def write_entry(entry=os.path.join(DATA, "test.txt")): def write_entry(entry=os.path.join(DATA, "test.txt")):
'''
main feels-recording handler
'''
entered = raw_input(""" entered = raw_input("""
"""+util.hilight("new feature!")+""" you can now use standard markdown in your entry text!
raw html is still valid, and you can mix them together.
feels will be recorded for today, """+time.strftime("%d %B %Y")+""". feels will be recorded for today, """+time.strftime("%d %B %Y")+""".
if you've already started recording feels for this day, you if you've already started recording feels for this day, you
can pick up where you left off. can pick up where you left off.
press <enter> to begin recording your feels. you can write your feels in plaintext, markdown, html, or a mixture of
these.
press <enter> to begin recording your feels in your chosen text
editor.
""") """)
@ -535,6 +579,9 @@ press <enter> to begin recording your feels.
return return
def send_feedback(entered, subject="none"): def send_feedback(entered, subject="none"):
'''
main feedback/bug report handler
'''
message = "" message = ""
@ -558,13 +605,16 @@ def send_feedback(entered, subject="none"):
return """\ return """\
thanks for writing! for your reference, it's been recorded thanks for writing! for your reference, it's been recorded
> as """+ " ".join([subject, id])+""". i'll try to respond to you soon.\ > as """+ " ".join([subject, id])+""". i'll try to respond to you soon.\
""" """
def view_entries(metas, entries, prompt): def list_entries(metas, entries, prompt):
'''
displays a list of entries for reading selection
'''
print_menu(entries) print_menu(entries)
choice = list_select(entries, "pick an entry from the list, or type 'back' to go home: ") choice = list_select(entries, "pick an entry from the list, or type 'back' to go back: ")
if choice is not False: if choice is not False:
@ -573,19 +623,25 @@ def view_entries(metas, entries, prompt):
show_entry(metas[choice][0]) show_entry(metas[choice][0])
redraw(prompt) redraw(prompt)
return view_entries(metas, entries, prompt) return list_entries(metas, entries, prompt)
else: else:
redraw() redraw()
return return
def show_entry(filename): def show_entry(filename):
'''
call less on passed in filename
'''
subprocess.call(["less", filename]) subprocess.call(["less", filename])
return return
def view_feed(): def view_feed():
'''
generate and display list of most recent global entries
'''
feedList = [] feedList = []
@ -610,7 +666,7 @@ def view_feed():
entries.append("~"+entry[5]+pad+"\ton "+entry[3]+" ("+p.no("word", entry[2])+") ") entries.append("~"+entry[5]+pad+"\ton "+entry[3]+" ("+p.no("word", entry[2])+") ")
#print_menu(entries) #print_menu(entries)
view_entries(metas, entries, "most recent global entries: \n\n") list_entries(metas, entries, "most recent global entries: \n\n")
redraw() redraw()