code cleanup
adding more code comments, cleaning up deprecated code, improving general readability
This commit is contained in:
		
							parent
							
								
									dccb1570d8
								
							
						
					
					
						commit
						f98513a079
					
				| @ -184,7 +184,7 @@ def init(): | |||||||
|         raw_input(""" |         raw_input(""" | ||||||
| i don't recognize you, stranger. let's make friends. | 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: |     except KeyboardInterrupt: | ||||||
|         print("\n\nthanks for checking in! i'll always be here.\n\n") |         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 |     builds header to insert username | ||||||
|     ''' |     ''' | ||||||
| 
 | 
 | ||||||
|     header ="""\ |     header =""" | ||||||
| <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2//EN\"> | <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2//EN\"> | ||||||
| <html> | <html> | ||||||
|     <head> |     <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") |         redraw("the following "+p.no("user", len(users))+" "+p.plural("is", len(users))+" recording feels on ttbp:\n") | ||||||
|         view_neighbors(users) |         view_neighbors(users) | ||||||
|     elif choice == '3': |     elif choice == '3': | ||||||
|         redraw("now viewing most recent 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") |         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])+") ") |         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 ten entries: \n\n") |     view_entries(metas, entries, "most recent global entries: \n\n") | ||||||
| 
 | 
 | ||||||
|     redraw() |     redraw() | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -39,8 +39,21 @@ LANG = json.load(langfile) | |||||||
| langfile.close() | langfile.close() | ||||||
| 
 | 
 | ||||||
| def say(keyword): | 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)) |     return random.choice(LANG.get(keyword)) | ||||||
| 
 | 
 | ||||||
| def month(num): | def month(num): | ||||||
|  |     ''' | ||||||
|  |     takes a MM and returns lovercase full name of that month | ||||||
|  | 
 | ||||||
|  |     TODO: validate num? | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     return LANG["months"].get(num) |     return LANG["months"].get(num) | ||||||
|  | |||||||
| @ -214,7 +214,7 @@ def meta(entries = FILES): | |||||||
| 
 | 
 | ||||||
|     * takes a list of filenames and returns a 2d list: |     * takes a list of filenames and returns a 2d list: | ||||||
|       [0] absolute path |       [0] absolute path | ||||||
|       [1] ctime |       [1] mtime | ||||||
|       [2] wc -w |       [2] wc -w | ||||||
|       [3] timestamp "DD month YYYY at HH:MM" |       [3] timestamp "DD month YYYY at HH:MM" | ||||||
|       [4] entry date YYYY-MM-DD |       [4] entry date YYYY-MM-DD | ||||||
| @ -226,14 +226,14 @@ def meta(entries = FILES): | |||||||
|     meta = [] |     meta = [] | ||||||
| 
 | 
 | ||||||
|     for filename in entries: |     for filename in entries: | ||||||
|       ctime = os.path.getctime(filename) |       mtime = os.path.getmtime(filename) | ||||||
|       wc = subprocess.check_output(["wc","-w",filename]).split()[0] |       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)) |       date = "-".join(parse_date(filename)) | ||||||
|       author = os.path.split(os.path.split(os.path.split(os.path.split(filename)[0])[0])[0])[1] |       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.sort(key = lambda filename:filename[4]) | ||||||
|     meta.reverse() |     meta.reverse() | ||||||
|  | |||||||
							
								
								
									
										102
									
								
								bin/ttbp.py
									
									
									
									
									
								
							
							
						
						
									
										102
									
								
								bin/ttbp.py
									
									
									
									
									
								
							| @ -81,6 +81,12 @@ SUBJECTS = ["help request", "bug report", "feature suggestion", "general comment | |||||||
| ## | ## | ||||||
| 
 | 
 | ||||||
| def redraw(leftover=""): | def redraw(leftover=""): | ||||||
|  |     ''' | ||||||
|  |     screen clearing | ||||||
|  | 
 | ||||||
|  |     * clears the screen and reprints the banner, plus whatever leftover text to be hilights | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     os.system("clear") |     os.system("clear") | ||||||
|     print(BANNER) |     print(BANNER) | ||||||
|     print(SPACER) |     print(SPACER) | ||||||
| @ -88,9 +94,16 @@ def redraw(leftover=""): | |||||||
|         print("> "+leftover+"\n") |         print("> "+leftover+"\n") | ||||||
| 
 | 
 | ||||||
| def start(): | def start(): | ||||||
|  |     ''' | ||||||
|  |     main engine head | ||||||
|  | 
 | ||||||
|  |     * called on program start | ||||||
|  |     * calls config check | ||||||
|  |     * proceeds to main menu | ||||||
|  |     * handles ^c and ^d ejects | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     redraw() |     redraw() | ||||||
|     #print(chatter.say("greet")+", "+chatter.say("friend")) |  | ||||||
|     #print("(remember, you can always press ctrl-c to come home)\n") |  | ||||||
|     print(""" |     print(""" | ||||||
| if you don't want to be here at any point, press <ctrl-d> and it'll all go away. | 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.\ | 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 |             break | ||||||
| 
 | 
 | ||||||
| def stop(): | def stop(): | ||||||
|  |     ''' | ||||||
|  |     closer | ||||||
|  | 
 | ||||||
|  |     * prints ending text | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     return "\n\n\t"+chatter.say("bye")+"\n\n" |     return "\n\n\t"+chatter.say("bye")+"\n\n" | ||||||
| 
 | 
 | ||||||
| def check_init(): | def check_init(): | ||||||
|  |     ''' | ||||||
|  |     user handler | ||||||
|  | 
 | ||||||
|  |     * checks for presence of ttbprc | ||||||
|  |     * checks for last run version | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     global SETTINGS |     global SETTINGS | ||||||
|  | 
 | ||||||
|     print("\n\n") |     print("\n\n") | ||||||
|     if os.path.exists(os.path.join(os.path.expanduser("~"),".ttbp")): |     if os.path.exists(os.path.join(os.path.expanduser("~"),".ttbp")): | ||||||
|         print(chatter.say("greet")+", "+USER+".\n") |         print(chatter.say("greet")+", "+USER+".\n") | ||||||
|  | 
 | ||||||
|  |         ## ttbprc validation | ||||||
|         while not os.path.isfile(TTBPRC): |         while not os.path.isfile(TTBPRC): | ||||||
|             setup_handler() |             setup_handler() | ||||||
|         try: |         try: | ||||||
| @ -135,6 +164,7 @@ def check_init(): | |||||||
|         if not updated(): |         if not updated(): | ||||||
|             print(update_version()) |             print(update_version()) | ||||||
| 
 | 
 | ||||||
|  |         ## when ready, enter main program and load core engine | ||||||
|         raw_input("press <enter> to explore your feels.\n\n") |         raw_input("press <enter> to explore your feels.\n\n") | ||||||
|         core.load() |         core.load() | ||||||
| 
 | 
 | ||||||
| @ -143,6 +173,13 @@ def check_init(): | |||||||
|         return init() |         return init() | ||||||
| 
 | 
 | ||||||
| def init(): | def init(): | ||||||
|  |     ''' | ||||||
|  |     new user creation | ||||||
|  | 
 | ||||||
|  |     * introduces user | ||||||
|  |     * calls setup functinos | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     try: |     try: | ||||||
|         raw_input(""" |         raw_input(""" | ||||||
| i don't recognize you, stranger. let's make friends. | 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") |         print("\n\nthanks for checking in! i'll always be here.\n\n") | ||||||
|         quit() |         quit() | ||||||
| 
 | 
 | ||||||
|  |     ## record user in source list | ||||||
|     users = open(USERFILE, 'a') |     users = open(USERFILE, 'a') | ||||||
|     users.write(USER+"\n") |     users.write(USER+"\n") | ||||||
|     users.close() |     users.close() | ||||||
|  | 
 | ||||||
|  |     ## make .ttbp directory structure | ||||||
|     subprocess.call(["mkdir", PATH]) |     subprocess.call(["mkdir", PATH]) | ||||||
|     subprocess.call(["mkdir", CONFIG]) |     subprocess.call(["mkdir", CONFIG]) | ||||||
|     subprocess.call(["mkdir", DATA]) |     subprocess.call(["mkdir", DATA]) | ||||||
| 
 | 
 | ||||||
|  |     ## create header file | ||||||
|     header = gen_header() |     header = gen_header() | ||||||
|     headerfile = open(os.path.join(CONFIG, "header.txt"), 'w') |     headerfile = open(os.path.join(CONFIG, "header.txt"), 'w') | ||||||
|     for line in header: |     for line in header: | ||||||
|         headerfile.write(line) |         headerfile.write(line) | ||||||
|     headerfile.close() |     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", "footer.txt"), CONFIG]) | ||||||
|     subprocess.call(["cp", os.path.join(SOURCE, "config", "defaults", "style.css"), CONFIG]) |     subprocess.call(["cp", os.path.join(SOURCE, "config", "defaults", "style.css"), CONFIG]) | ||||||
| 
 | 
 | ||||||
|  |     ## run user-interactive setup and load core engine | ||||||
|     setup() |     setup() | ||||||
|     core.load() |     core.load() | ||||||
| 
 | 
 | ||||||
| @ -176,10 +219,17 @@ press <enter> to begin, or <ctrl-c> to get out of here. | |||||||
|     return "" |     return "" | ||||||
| 
 | 
 | ||||||
| def gen_header(): | def gen_header(): | ||||||
|  |     ''' | ||||||
|  |     header generator | ||||||
|  | 
 | ||||||
|  |     builds header to insert username | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     header =""" |     header =""" | ||||||
| <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2//EN\"> | <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2//EN\"> | ||||||
| <html> | <html> | ||||||
|     <head> |     <head> | ||||||
|  |         <!--- this header automatically generated by ttbp initialization on """+time.strftime("%Y-%m-%d %h:m")+""" ---> | ||||||
|         <title>~"""+USER+""" on TTBP</title> |         <title>~"""+USER+""" on TTBP</title> | ||||||
|         <link rel=\"stylesheet\" href=\"style.css\" /> |         <link rel=\"stylesheet\" href=\"style.css\" /> | ||||||
|     </head> |     </head> | ||||||
| @ -198,6 +248,13 @@ def gen_header(): | |||||||
|     return header |     return header | ||||||
| 
 | 
 | ||||||
| def setup_handler(): | 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") |     print("\nyour ttbp configuration doesn't look right. let's make you a fresh copy.\n\n") | ||||||
|     try: |     try: | ||||||
|         setup() |         setup() | ||||||
| @ -206,6 +263,17 @@ def setup_handler(): | |||||||
|         setup() |         setup() | ||||||
| 
 | 
 | ||||||
| def setup(): | def setup(): | ||||||
|  |     ''' | ||||||
|  |     master setup function | ||||||
|  | 
 | ||||||
|  |     * editor selection | ||||||
|  |     * publishing toggle | ||||||
|  |     * publish/unpublish as needed | ||||||
|  |     * directory selection | ||||||
|  | 
 | ||||||
|  |     TODO: break this out better? | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     global SETTINGS |     global SETTINGS | ||||||
| 
 | 
 | ||||||
|     # editor selection |     # editor selection | ||||||
| @ -219,6 +287,7 @@ def setup(): | |||||||
| 
 | 
 | ||||||
|     if publishing(): |     if publishing(): | ||||||
|         print("publish directory: ~"+USER+"/public_html/"+SETTINGS.get("publish dir")) |         print("publish directory: ~"+USER+"/public_html/"+SETTINGS.get("publish dir")) | ||||||
|  | 
 | ||||||
|     # save settings |     # save settings | ||||||
|     ttbprc = open(TTBPRC, "w") |     ttbprc = open(TTBPRC, "w") | ||||||
|     ttbprc.write(json.dumps(SETTINGS, sort_keys=True, indent=2, separators=(',',':'))) |     ttbprc.write(json.dumps(SETTINGS, sort_keys=True, indent=2, separators=(',',':'))) | ||||||
| @ -231,6 +300,12 @@ def setup(): | |||||||
| ## menus | ## menus | ||||||
| 
 | 
 | ||||||
| def print_menu(menu): | def print_menu(menu): | ||||||
|  |     ''' | ||||||
|  |     pretty menu handler | ||||||
|  | 
 | ||||||
|  |     * takes list of options and prints them | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     i = 0 |     i = 0 | ||||||
|     for x in menu: |     for x in menu: | ||||||
|         line = [] |         line = [] | ||||||
| @ -242,6 +317,10 @@ def print_menu(menu): | |||||||
|         i += 1 |         i += 1 | ||||||
| 
 | 
 | ||||||
| def main_menu(): | def main_menu(): | ||||||
|  |     ''' | ||||||
|  |     main navigation menu | ||||||
|  |     ''' | ||||||
|  | 
 | ||||||
|     menuOptions = [ |     menuOptions = [ | ||||||
|             "record your feels", |             "record your feels", | ||||||
|             "review your feels", |             "review your feels", | ||||||
| @ -250,8 +329,8 @@ def main_menu(): | |||||||
|             "change your settings", |             "change your settings", | ||||||
|             "send some feedback", |             "send some feedback", | ||||||
|             "see credits"] |             "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. remember, you can always press <ctrl-c> to come back here.\n\n") | ||||||
|     #print("you're at ttbp home.\n\n") |  | ||||||
|     print_menu(menuOptions) |     print_menu(menuOptions) | ||||||
| 
 | 
 | ||||||
|     try: |     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") |         redraw("the following "+p.no("user", len(users))+" "+p.plural("is", len(users))+" recording feels on ttbp:\n") | ||||||
|         view_neighbors(users) |         view_neighbors(users) | ||||||
|     elif choice == '3': |     elif choice == '3': | ||||||
|         redraw("now viewing most recent 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") |         pretty_settings = "\n\n\ttext editor:\t" +SETTINGS.get("editor") | ||||||
| @ -299,9 +378,14 @@ def main_menu(): | |||||||
| 
 | 
 | ||||||
|     return main_menu() |     return main_menu() | ||||||
| 
 | 
 | ||||||
| ### |  | ||||||
| 
 |  | ||||||
| def feedback_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("you're about to send mail to ~endorphant about ttbp\n\n") | ||||||
| 
 | 
 | ||||||
|     print_menu(SUBJECTS) |     print_menu(SUBJECTS) | ||||||
| @ -441,7 +525,7 @@ press <enter> to begin recording your feels. | |||||||
| 
 | 
 | ||||||
|     return |     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 = "" |     message = "" | ||||||
| 
 | 
 | ||||||
| @ -517,7 +601,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 ten entries: \n\n") |     view_entries(metas, entries, "most recent global entries: \n\n") | ||||||
| 
 | 
 | ||||||
|     redraw() |     redraw() | ||||||
| 
 | 
 | ||||||
| @ -815,5 +899,5 @@ def update_version(): | |||||||
| 
 | 
 | ||||||
| ##### | ##### | ||||||
| 
 | 
 | ||||||
|  | if __name__ == '__main__': | ||||||
|     start() |     start() | ||||||
| #print("ttbp beta is out to lunch. bbl.") |  | ||||||
|  | |||||||
							
								
								
									
										12
									
								
								devnotes.txt
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								devnotes.txt
									
									
									
									
									
								
							| @ -1,9 +1,5 @@ | |||||||
| config file things: | NOTES FOR ~ENDO | ||||||
| -preferred text editor |  | ||||||
| -set www symlink |  | ||||||
| -set page title |  | ||||||
| -set colorizing pref |  | ||||||
| 
 | 
 | ||||||
| flair: | handling global feels out of order: | ||||||
| -themes/stylesheets |   -check to see if entry filename date and mtime date matches | ||||||
| -global post announcements |   -if not, process as if it was last modified on the date indicated by filename | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user