2016-04-30 03:17:06 +00:00
#!/usr/bin/python
import os
2016-05-01 17:44:49 +00:00
import random
import tempfile
import subprocess
import time
2016-05-01 22:43:12 +00:00
import json
2016-05-01 17:44:49 +00:00
2016-05-01 23:34:20 +00:00
import core
2016-05-01 00:19:00 +00:00
import chatter
2016-05-03 17:14:53 +00:00
import inflect
2016-04-30 03:17:06 +00:00
2016-05-01 22:26:26 +00:00
## system globals
2016-05-01 00:19:00 +00:00
SOURCE = os . path . join ( " /home " , " endorphant " , " projects " , " ttbp " , " bin " )
2016-05-01 22:26:26 +00:00
LIVE = " http://tilde.town/~ "
FEEDBACK = os . path . join ( " /home " , " endorphant " , " ttbp-mail " )
2016-05-01 23:34:20 +00:00
USERFILE = os . path . join ( " /home " , " endorphant " , " projects " , " ttbp " , " users.txt " )
2016-05-03 17:14:53 +00:00
p = inflect . engine ( )
2016-05-01 22:26:26 +00:00
## user globals
2016-05-01 00:19:00 +00:00
USER = os . path . basename ( os . path . expanduser ( " ~ " ) )
PATH = os . path . join ( " /home " , USER , " .ttbp " )
2016-05-02 03:26:12 +00:00
PUBLIC = os . path . join ( " /home " , USER , " public_html " )
2016-05-01 00:19:00 +00:00
WWW = os . path . join ( PATH , " www " )
2016-04-30 03:48:01 +00:00
CONFIG = os . path . join ( PATH , " config " )
2016-05-01 22:43:12 +00:00
TTBPRC = os . path . join ( CONFIG , " ttbprc " )
2016-05-01 00:19:00 +00:00
DATA = os . path . join ( PATH , " entries " )
2016-05-01 22:26:26 +00:00
SETTINGS = {
2016-05-02 03:26:12 +00:00
" editor " : " none " ,
" publish dir " : False
2016-05-01 22:26:26 +00:00
}
2016-04-30 03:17:06 +00:00
2016-05-01 22:26:26 +00:00
## ui globals
2016-05-01 18:10:04 +00:00
BANNER = open ( os . path . join ( SOURCE , " config " , " banner.txt " ) ) . read ( )
2016-05-01 17:44:49 +00:00
SPACER = " \n \n \n "
2016-05-01 18:10:04 +00:00
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 "
2016-05-01 22:26:26 +00:00
## ref
EDITORS = [ " vim " , " vi " , " emacs " , " pico " , " nano " ]
2016-05-02 15:26:51 +00:00
SUBJECTS = [ " help request " , " bug report " , " feature suggestion " , " general comment " ]
2016-05-01 22:26:26 +00:00
2016-05-01 18:10:04 +00:00
##
def redraw ( leftover = " " ) :
os . system ( " clear " )
print ( BANNER )
print ( SPACER )
if leftover :
print ( " > " + leftover + " \n " )
2016-04-30 03:17:06 +00:00
def start ( ) :
2016-05-01 18:10:04 +00:00
redraw ( )
2016-05-01 17:44:49 +00:00
#print(chatter.say("greet")+", "+chatter.say("friend"))
#print("(remember, you can always press ctrl-c to come home)\n")
2016-05-01 23:34:20 +00:00
print ( " if you don ' t want to be here at any point, press <ctrl-d> and it ' ll all go away. \n just keep in mind that you might lose anything you ' ve started here. \n " )
2016-04-30 03:34:43 +00:00
print ( check_init ( ) )
2016-04-30 03:17:06 +00:00
try :
2016-05-01 18:10:04 +00:00
redraw ( )
2016-04-30 03:17:06 +00:00
print ( main_menu ( ) )
except ValueError or SyntaxError :
2016-05-01 22:26:26 +00:00
redraw ( " oh no i didn ' t understand that. let ' s go home and start over. " )
2016-04-30 03:17:06 +00:00
print ( main_menu ( ) )
except KeyboardInterrupt :
2016-05-01 22:26:26 +00:00
redraw ( " eject button fired! going home now. " )
2016-04-30 03:17:06 +00:00
print ( main_menu ( ) )
2016-04-30 03:34:43 +00:00
def stop ( ) :
2016-05-01 00:19:00 +00:00
return " \n \t " + chatter . say ( " bye " )
2016-04-30 03:34:43 +00:00
def check_init ( ) :
2016-05-01 22:43:12 +00:00
global SETTINGS
2016-05-01 23:34:20 +00:00
print ( " \n \n " )
2016-04-30 03:34:43 +00:00
if os . path . exists ( os . path . join ( os . path . expanduser ( " ~ " ) , " .ttbp " ) ) :
2016-05-01 23:34:20 +00:00
print ( chatter . say ( " greet " ) + " , " + USER + " . " )
2016-05-01 22:43:12 +00:00
while not os . path . isfile ( TTBPRC ) :
setup_handler ( )
try :
SETTINGS = json . load ( open ( TTBPRC ) )
except ValueError :
setup_handler ( )
2016-05-03 17:46:54 +00:00
raw_input ( " \n \n press <enter> to explore your feels. \n \n " )
2016-05-01 23:34:20 +00:00
core . load ( )
2016-05-01 17:44:49 +00:00
return " "
2016-04-30 03:34:43 +00:00
else :
return init ( )
def init ( ) :
2016-05-01 23:34:20 +00:00
try :
raw_input ( " i don ' t recognize you, stranger. let ' s make friends. \n \n press <enter> to begin, or <ctrl-c> to get out of here. \n \n " )
except KeyboardInterrupt :
print ( " \n \n thanks for checking in! i ' ll always be here. \n \n " )
2016-05-02 15:26:51 +00:00
quit ( )
2016-05-01 23:34:20 +00:00
users = open ( USERFILE , ' a ' )
users . write ( USER + " \n " )
users . close ( )
subprocess . call ( [ " mkdir " , PATH ] )
subprocess . call ( [ " mkdir " , CONFIG ] )
subprocess . call ( [ " mkdir " , DATA ] )
2016-05-02 03:26:12 +00:00
#subprocess.call(["cp", os.path.join(SOURCE, "config", "defaults", "header.txt"), CONFIG])
header = gen_header ( )
headerfile = open ( os . path . join ( CONFIG , " header.txt " ) , ' w ' )
for line in header :
headerfile . write ( line )
headerfile . close ( )
2016-05-01 23:34:20 +00:00
subprocess . call ( [ " cp " , os . path . join ( SOURCE , " config " , " defaults " , " footer.txt " ) , CONFIG ] )
setup ( )
2016-05-02 15:26:51 +00:00
subprocess . call ( [ " cp " , os . path . join ( SOURCE , " config " , " defaults " , " style.css " ) , WWW ] )
2016-05-01 23:34:20 +00:00
core . load ( )
2016-05-02 03:26:12 +00:00
raw_input ( " \n you ' re all good to go, " + chatter . say ( " friend " ) + " ! hit <enter> to continue. \n \n " )
2016-05-01 17:44:49 +00:00
return " "
2016-05-02 03:26:12 +00:00
def gen_header ( ) :
header = [ ]
header . append ( " <!DOCTYPE html PUBLIC \" -//W3C//DTD HTML 3.2//EN \" > " )
2016-05-02 03:44:01 +00:00
header . append ( " \n <html> " )
header . append ( " \n \t <head> " )
header . append ( " \n \t \t <title>~ " + USER + " on TTBP</title> " )
header . append ( " \n \t \t <link rel= \" stylesheet \" href= \" style.css \" /> " )
header . append ( " \n \t </head> " )
header . append ( " \n \t <body> " )
header . append ( " \n \t \t <div id= \" meta \" > " )
2016-05-03 17:14:53 +00:00
header . append ( " \n \t \t \t <h1><a href= \" index.html# \" >~ " + USER + " </a>@<a href= \" /~endorphant/ttbp \" >TTBP</a></h1> " )
2016-05-02 03:44:01 +00:00
header . append ( " \n \t \t </div> \n " )
2016-05-02 15:26:51 +00:00
header . append ( " \n \t \t <!---put your custom html here--> \n \n \n \n " )
header . append ( " \n \t \t <!---don ' t put anything after this line--> \n " )
header . append ( " \n \t \t <div id= \" tlogs \" > \n " )
2016-05-02 03:26:12 +00:00
return header
2016-05-01 22:43:12 +00:00
def setup_handler ( ) :
print ( " \n your ttbp configuration doesn ' t look right. let ' s make you a fresh copy. \n \n " )
try :
setup ( )
except KeyboardInterrupt :
print ( " \n \n sorry, trying again. \n \n " )
setup ( )
2016-05-01 22:26:26 +00:00
def setup ( ) :
global SETTINGS
# editor selection
print_menu ( EDITORS )
choice = raw_input ( " \n pick your favorite text editor: " )
while choice not in [ ' 0 ' , ' 1 ' , ' 2 ' , ' 3 ' , ' 4 ' ] :
choice = raw_input ( " \n please pick a number from the list: " )
SETTINGS [ " editor " ] = EDITORS [ int ( choice ) ]
2016-05-02 03:26:12 +00:00
redraw ( " text editor set to: " + SETTINGS [ " editor " ] )
2016-05-01 22:26:26 +00:00
# publish directory selection
2016-05-02 03:26:12 +00:00
if SETTINGS [ " publish dir " ] :
print ( " \t current publish dir: \t " + os . path . join ( PUBLIC , SETTINGS [ " publish dir " ] ) + " \n \n " )
choice = raw_input ( " \n where do you want your blog published? (leave blank to use default \" blog \" ) " )
2016-05-01 22:26:26 +00:00
if not choice :
choice = " blog "
2016-05-02 03:26:12 +00:00
publishing = os . path . join ( PUBLIC , choice )
2016-05-01 22:26:26 +00:00
while os . path . exists ( publishing ) :
second = raw_input ( " \n " + publishing + " already exists! \n if you ' re sure you want to use it, hit <enter> to confirm. otherwise, pick another location: " )
if second == " " :
break
choice = second
2016-05-02 03:26:12 +00:00
publishing = os . path . join ( PUBLIC , choice )
2016-05-01 22:26:26 +00:00
SETTINGS [ " publish dir " ] = choice
2016-05-02 03:26:12 +00:00
2016-05-01 22:26:26 +00:00
# set up publish directory
if not os . path . exists ( publishing ) :
subprocess . call ( [ " mkdir " , publishing ] )
subprocess . call ( [ " touch " , os . path . join ( publishing , " index.html " ) ] )
index = open ( os . path . join ( publishing , " index.html " ) , " w " )
index . write ( " <h1>ttbp blog placeholder</h1> " )
index . close ( )
2016-05-01 23:34:20 +00:00
if os . path . exists ( WWW ) :
subprocess . call ( [ " rm " , WWW ] )
2016-05-01 22:26:26 +00:00
subprocess . call ( [ " ln " , " -s " , publishing , WWW ] )
2016-05-02 03:26:12 +00:00
print ( " \n \t publishing to " + LIVE + USER + " / " + SETTINGS [ " publish dir " ] + " / \n \n " )
2016-05-01 22:26:26 +00:00
2016-05-01 22:43:12 +00:00
# save settings
ttbprc = open ( TTBPRC , " w " )
ttbprc . write ( json . dumps ( SETTINGS , sort_keys = True , indent = 2 , separators = ( ' , ' , ' : ' ) ) )
ttbprc . close ( )
2016-05-01 22:26:26 +00:00
return SETTINGS
2016-05-01 17:44:49 +00:00
## menus
def print_menu ( menu ) :
i = 0
for x in menu :
line = [ ]
line . append ( " \t [ " )
if i < 10 :
line . append ( " " )
line . append ( str ( i ) + " ] " + x )
print ( " " . join ( line ) )
i + = 1
2016-04-30 03:34:43 +00:00
2016-04-30 03:17:06 +00:00
def main_menu ( ) :
2016-05-01 18:10:04 +00:00
#os.system("clear")
#print(BANNER)
#redraw()
2016-05-01 22:26:26 +00:00
menuOptions = [
2016-05-03 17:46:54 +00:00
" record your feels " ,
" check out your neighbors " ,
" change your settings " ,
" send some feedback " ,
2016-05-02 03:26:12 +00:00
" (wip) see credits " ]
2016-05-01 18:10:04 +00:00
#print(SPACER)
2016-05-01 23:34:20 +00:00
print ( " you ' re at ttbp home. remember, you can always press <ctrl-c> to come back here. \n \n " )
2016-05-01 17:44:49 +00:00
print_menu ( menuOptions )
#print("how are you feeling today? ")
2016-05-01 22:26:26 +00:00
try :
choice = raw_input ( " \n tell me about your feels (enter ' none ' to quit): " )
except KeyboardInterrupt :
redraw ( " eject button fired! going home now. " )
return main_menu ( )
2016-05-01 17:44:49 +00:00
if choice == ' 0 ' :
2016-05-01 23:34:20 +00:00
redraw ( )
today = time . strftime ( " % Y % m %d " )
write_entry ( os . path . join ( DATA , today + " .txt " ) )
2016-05-01 17:44:49 +00:00
elif choice == ' 1 ' :
2016-05-03 17:20:28 +00:00
users = [ ]
for townie in os . listdir ( " /home " ) :
if os . path . exists ( os . path . join ( " /home " , townie , " .ttbp " , " config " , " ttbprc " ) ) :
users . append ( townie )
2016-05-03 17:46:54 +00:00
redraw ( " the following " + p . no ( " user " , len ( users ) ) + " " + p . plural ( " is " , len ( users ) ) + " recording feels on ttbp: \n \n " )
2016-05-03 17:20:28 +00:00
view_neighbors ( users )
2016-05-01 17:44:49 +00:00
elif choice == ' 2 ' :
2016-05-02 03:26:12 +00:00
pretty_settings = " \n \t text editor: \t " + SETTINGS [ " editor " ]
pretty_settings + = " \n \t publish dir: \t " + os . path . join ( PUBLIC , SETTINGS [ " publish dir " ] )
redraw ( " now changing your settings. press <ctrl-c> if you didn ' t mean to do this. \n \n current settings " + pretty_settings + " \n " )
setup ( )
raw_input ( " \n you ' re all good to go, " + chatter . say ( " friend " ) + " ! hit <enter> to continue. \n \n " )
redraw ( )
2016-05-01 22:26:26 +00:00
elif choice == ' 3 ' :
2016-05-01 18:10:04 +00:00
redraw ( )
feedback_menu ( )
2016-05-01 22:26:26 +00:00
elif choice == ' 4 ' :
redraw ( DUST )
2016-05-03 17:14:53 +00:00
elif choice == ' secret ' :
2016-05-03 17:46:54 +00:00
redraw ( " here are your recorded feels, listed by date: \n \n " )
2016-05-03 17:14:53 +00:00
view_entries ( )
2016-05-01 17:44:49 +00:00
elif choice == " none " :
return stop ( )
else :
2016-05-01 18:10:04 +00:00
redraw ( INVALID )
2016-05-01 17:44:49 +00:00
return main_menu ( )
2016-04-30 03:17:06 +00:00
2016-05-01 17:44:49 +00:00
def feedback_menu ( ) :
2016-05-01 18:10:04 +00:00
print ( " you ' re about to send mail to ~endorphant about ttbp \n \n " )
2016-04-30 03:17:06 +00:00
2016-05-01 22:26:26 +00:00
print_menu ( SUBJECTS )
2016-05-01 17:44:49 +00:00
choice = raw_input ( " \n pick a category for your feedback: " )
2016-04-30 03:17:06 +00:00
2016-05-01 17:44:49 +00:00
cat = " "
2016-05-03 17:14:53 +00:00
if choice in [ ' 0 ' , ' 1 ' , ' 2 ' , ' 3 ' ] :
2016-05-01 22:26:26 +00:00
cat = SUBJECTS [ int ( choice ) ]
2016-05-02 15:26:51 +00:00
raw_input ( " \n composing a " + cat + " to ~endorphant. \n \n press <enter> to open an external text editor. mail will be sent once you save and quit. \n " )
2016-05-01 18:10:04 +00:00
redraw ( send_feedback ( cat ) )
return
2016-05-01 17:44:49 +00:00
else :
2016-05-01 18:10:04 +00:00
redraw ( INVALID )
2016-05-01 17:44:49 +00:00
return feedback_menu ( )
## handlers
def write_entry ( entry = os . path . join ( DATA , " test.txt " ) ) :
2016-05-03 17:46:54 +00:00
raw_input ( " \n feels will be recorded for today, " + time . strftime ( " %d % B % Y " ) + " . \n \n if you ' ve already started recording feels for this day, you \n can pick up where you left off. \n \n press <enter> to begin recording your feels. \n \n " )
2016-05-01 22:26:26 +00:00
subprocess . call ( [ SETTINGS [ " editor " ] , entry ] )
2016-05-01 23:34:20 +00:00
core . load_files ( )
core . write ( " index.html " )
2016-05-03 17:46:54 +00:00
redraw ( " posted to " + LIVE + USER + " / " + SETTINGS [ " publish dir " ] + " /index.html \n \n thanks for sharing your feels! " )
2016-05-01 23:34:20 +00:00
return
2016-05-01 17:44:49 +00:00
2016-05-02 15:26:51 +00:00
def send_feedback ( subject = " none " , mailbox = os . path . join ( FEEDBACK , USER + " - " + time . strftime ( " % Y % m %d - % H % M " ) + " .msg " ) ) :
2016-05-01 17:44:49 +00:00
mail = " "
temp = tempfile . NamedTemporaryFile ( )
2016-05-01 22:26:26 +00:00
subprocess . call ( [ SETTINGS [ " editor " ] , temp . name ] )
2016-05-01 17:44:49 +00:00
mail = open ( temp . name , ' r ' ) . read ( )
outfile = open ( mailbox , ' w ' )
outfile . write ( " from: \t \t ~ " + USER + " \n " )
2016-05-02 03:26:12 +00:00
outfile . write ( " subject: \t " + subject + " \n " )
2016-05-02 15:26:51 +00:00
outfile . write ( " date: \t " + time . strftime ( " %d % B % y " ) + " \n " )
2016-05-01 17:44:49 +00:00
outfile . write ( mail )
outfile . close ( )
return " mail sent. thanks for writing! i ' ll try to respond to you soon. "
2016-04-30 03:48:01 +00:00
2016-05-03 17:20:28 +00:00
def view_neighbors ( users ) :
2016-05-02 15:26:51 +00:00
for user in users :
userRC = json . load ( open ( os . path . join ( " /home " , user , " .ttbp " , " config " , " ttbprc " ) ) )
url = LIVE + user + " / " + userRC [ " publish dir " ]
2016-05-03 17:14:53 +00:00
count = 0
for filename in os . listdir ( os . path . join ( " /home " , user , " .ttbp " , " entries " ) ) :
if os . path . splitext ( filename ) [ 1 ] == " .txt " and len ( os . path . splitext ( filename ) [ 0 ] ) == 8 :
count + = 1
2016-05-03 17:20:28 +00:00
pad = " "
2016-05-03 17:46:54 +00:00
if len ( user ) < 8 :
2016-05-03 17:20:28 +00:00
pad = " \t "
2016-05-03 17:14:53 +00:00
user = " ~ " + user
if len ( user ) < 8 :
user + = " \t "
2016-05-03 17:46:54 +00:00
print ( " \t " + user + " \t " + url + pad + " \t ( " + p . no ( " feel " , count ) + " ) " )
2016-05-02 15:26:51 +00:00
raw_input ( " \n \n press <enter> to go back home. \n \n " )
redraw ( )
return
2016-05-03 17:14:53 +00:00
def view_entries ( ) :
entries = [ ]
raw_input ( " \n \n press <ctrl-c> to go back home. \n \n " )
redraw ( )
return
2016-04-30 03:48:01 +00:00
#####
start ( )