moving around a bunch of file processing backend stuff

master
Vincent Zeng 2018-03-15 22:41:22 -04:00
parent d001c14bc9
commit 8e35fd079f
5 changed files with 87 additions and 59 deletions

View File

@ -71,7 +71,8 @@ WWW = os.path.join(PATH, 'www')
GOPHER_PATH = os.path.join(USER_HOME, 'public_gopher', 'feels') GOPHER_PATH = os.path.join(USER_HOME, 'public_gopher', 'feels')
USER_CONFIG = os.path.join(PATH, 'config') USER_CONFIG = os.path.join(PATH, 'config')
TTBPRC = os.path.join(USER_CONFIG, 'ttbprc') TTBPRC = os.path.join(USER_CONFIG, 'ttbprc')
USER_DATA = os.path.join(PATH, 'entries') MAIN_FEELS = os.path.join(PATH, 'entries')
BURIED_FEELS = os.path.join(MAIN_FEELS, 'buried')
NOPUB = os.path.join(USER_CONFIG, "nopub") NOPUB = os.path.join(USER_CONFIG, "nopub")
## UI ## UI

View File

@ -41,6 +41,7 @@ import json
from . import chatter from . import chatter
from . import config from . import config
from . import gopher
FEED = os.path.join("/home", "endorphant", "public_html", "ttbp", "index.html") FEED = os.path.join("/home", "endorphant", "public_html", "ttbp", "index.html")
SETTINGS = {} SETTINGS = {}
@ -75,19 +76,24 @@ def reload_ttbprc(ttbprc={}):
SETTINGS = ttbprc SETTINGS = ttbprc
def get_files(feelsdir=config.MAIN_FEELS):
"""Returns a list of user's feels in the given directory (defaults to main
feels dir)"""
def get_files():
"""Returns a list of user's feels."""
files = [] files = []
for filename in os.listdir(config.USER_DATA): for filename in os.listdir(feelsdir):
if nopub(filename): if nopub(filename):
unpublish_feel(filename)
'''
link = os.path.join(config.WWW, link = os.path.join(config.WWW,
os.path.splitext( os.path.splitext(
os.path.basename(filename))[0]+".html") os.path.basename(filename))[0]+".html")
if os.path.exists(link): if os.path.exists(link):
subprocess.call(["rm", link]) subprocess.call(["rm", link])
continue continue
filename = os.path.join(config.USER_DATA, filename) '''
filename = os.path.join(feelsdir, filename)
if os.path.isfile(filename) and valid(filename): if os.path.isfile(filename) and valid(filename):
files.append(filename) files.append(filename)
@ -96,19 +102,18 @@ def get_files():
return files return files
def load_files(feelsdir=config.MAIN_FEELS):
def load_files():
''' '''
file loader file loader
* reads user's nopub file * reads user's nopub file
* loads all valid filenames that are not excluded in nopub to global files list * calls get_files() to load all files for given directory
''' '''
global FILES global FILES
load_nopubs() load_nopubs()
FILES = get_files() FILES = get_files(feelsdir)
def load_nopubs(): def load_nopubs():
"""Load a list of the user's nopub entries. """Load a list of the user's nopub entries.
@ -127,7 +132,7 @@ def load_nopubs():
## html outputting ## html outputting
def write(outurl="default.html"): def write_html(outurl="default.html"):
''' '''
main page renderer main page renderer
@ -167,7 +172,7 @@ def write_page(filename):
url url
''' '''
outurl = os.path.join(config.WWW, "".join(parse_date(filename))+".html") outurl = os.path.join(config.WWW, "".join(util.parse_date(filename))+".html")
outfile = open(outurl, "w") outfile = open(outurl, "w")
outfile.write("<!--generated by the tilde.town blogging platform on "+time.strftime("%d %B %y")+"\nhttp://tilde.town/~endorphant/ttbp/-->\n\n") outfile.write("<!--generated by the tilde.town blogging platform on "+time.strftime("%d %B %y")+"\nhttp://tilde.town/~endorphant/ttbp/-->\n\n")
@ -197,7 +202,7 @@ def write_entry(filename):
* return as list of strings * return as list of strings
''' '''
date = parse_date(filename) date = util.parse_date(filename)
entry = [ entry = [
"\t\t<p><a name=\""+date[0]+date[1]+date[2]+"\"></a><br /><br /></p>\n", "\t\t<p><a name=\""+date[0]+date[1]+date[2]+"\"></a><br /><br /></p>\n",
@ -207,7 +212,7 @@ def write_entry(filename):
] ]
raw = [] raw = []
rawfile = open(os.path.join(config.USER_DATA, filename), "r") rawfile = open(os.path.join(config.MAIN_FEELS, filename), "r")
for line in rawfile: for line in rawfile:
raw.append(line) raw.append(line)
@ -316,7 +321,7 @@ def meta(entries = FILES):
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
wc = "???" wc = "???"
timestamp = time.strftime("%Y-%m-%d at %H:%M", time.localtime(mtime)) timestamp = time.strftime("%Y-%m-%d at %H:%M", time.localtime(mtime))
date = "-".join(parse_date(filename)) date = "-".join(util.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, mtime, wc, timestamp, date, author]) meta.append([filename, mtime, wc, timestamp, date, author])
@ -345,23 +350,6 @@ def valid(filename):
return True return True
def parse_date(file):
'''
parses date out of pre-validated filename
* assumes a filename of YYYYMMDD.txt
* returns a list:
[0] 'YYYY'
[1] 'MM'
[2] 'DD'
'''
rawdate = os.path.splitext(os.path.basename(file))[0]
date = [rawdate[0:4], rawdate[4:6], rawdate[6:]]
return date
def find_ttbps(): def find_ttbps():
''' '''
returns a list of users with a ttbp by checking for a valid ttbprc returns a list of users with a ttbp by checking for a valid ttbprc
@ -457,12 +445,7 @@ def toggle_nopub(filename):
NOPUBS.remove(filename) NOPUBS.remove(filename)
else: else:
NOPUBS.append(filename) NOPUBS.append(filename)
live_html = os.path.join(config.WWW, filename.split(".")[0]+".html") unpublish_feel(filename)
if os.path.exists(live_html):
subprocess.call(["rm", live_html])
live_gopher = os.path.join(config.GOPHER_PATH, filename)
if os.path.exists(live_gopher):
subprocess.call(["rm", live_gopher])
nopub_file = open(config.NOPUB, 'w') nopub_file = open(config.NOPUB, 'w')
nopub_file.write("""\ nopub_file.write("""\
@ -475,9 +458,35 @@ def toggle_nopub(filename):
nopub_file.close() nopub_file.close()
load_files() load_files()
write_html("index.html")
gopher.publish_gopher('feels', FILES)
return action return action
def bury_feel(filename):
"""buries given filename; this removes the feel from any publicly-readable
location, and moves the textfile to user's private feels directory"""
pass
def delete_feel(filename):
"""deletes given filename; removes the feel from publicly-readable
locations, then deletes the original file."""
pass
def unpublish_feel(filename):
"""takes given filename and removes it from public_html and gopher_html, if
those locations exists. afterwards, regenerate index files appropriately."""
live_html = os.path.join(config.WWW,
os.path.splitext(os.path.basename(filename))[0]+".html")
#live_html = os.path.join(config.WWW, filename.split(".")[0]+".html")
if os.path.exists(live_html):
subprocess.call(["rm", live_html])
live_gopher = os.path.join(config.GOPHER_PATH, filename)
if os.path.exists(live_gopher):
subprocess.call(["rm", live_gopher])
############# #############
############# #############

View File

@ -7,7 +7,7 @@ import time
import subprocess import subprocess
from . import util from . import util
from .core import parse_date #from .core import parse_date
GOPHER_PROMPT = """ GOPHER_PROMPT = """
@ -73,7 +73,7 @@ def publish_gopher(gopher_path, entry_filenames):
if not os.path.exists(gopher_entry_symlink): if not os.path.exists(gopher_entry_symlink):
subprocess.call(["ln", "-s", entry_filename, gopher_entry_symlink]) subprocess.call(["ln", "-s", entry_filename, gopher_entry_symlink])
label = "-".join(parse_date(entry_filename)) label = "-".join(util.parse_date(entry_filename))
gophermap.write('0{file_label}\t{filename}\n'.format( gophermap.write('0{file_label}\t{filename}\n'.format(
file_label=label, file_label=label,
filename=filename)) filename=filename))

View File

@ -272,7 +272,7 @@ press <enter> to begin, or <ctrl-c> to get out of here.""")
print("\ngenerating feels at {path}...".format(path=config.PATH).rstrip()) print("\ngenerating feels at {path}...".format(path=config.PATH).rstrip())
subprocess.call(["mkdir", config.PATH]) subprocess.call(["mkdir", config.PATH])
subprocess.call(["mkdir", config.USER_CONFIG]) subprocess.call(["mkdir", config.USER_CONFIG])
subprocess.call(["mkdir", config.USER_DATA]) subprocess.call(["mkdir", config.MAIN_FEELS])
versionFile = os.path.join(config.PATH, "version") versionFile = os.path.join(config.PATH, "version")
open(versionFile, "w").write(__version__) open(versionFile, "w").write(__version__)
@ -537,14 +537,14 @@ def main_menu():
if choice == '0': if choice == '0':
redraw() redraw()
today = time.strftime("%Y%m%d") today = time.strftime("%Y%m%d")
write_entry(os.path.join(config.USER_DATA, today+".txt")) write_entry(os.path.join(config.MAIN_FEELS, today+".txt"))
core.www_neighbors() core.www_neighbors()
elif choice == '1': elif choice == '1':
intro = "here are some options for managing your feels:" intro = "here are some options for managing your feels:"
redraw(intro) redraw(intro)
review_menu(intro) review_menu(intro)
core.load_files() core.load_files()
core.write("index.html") core.write_html("index.html")
elif choice == '2': elif choice == '2':
users = core.find_ttbps() users = core.find_ttbps()
prompt = "the following {usercount} {are} recording feels on ttbp:".format( prompt = "the following {usercount} {are} recording feels on ttbp:".format(
@ -620,10 +620,10 @@ def review_menu(intro=""):
util.print_menu(menuOptions, SETTINGS.get("rainbows", False)) util.print_menu(menuOptions, SETTINGS.get("rainbows", False))
choice = util.list_select(menuOptions, "what would you like to do with your feels? (or 'back' to return home) ") choice = util.list_select(menuOptions, "what would you like to do with your feels? (or 'q' to return home) ")
top = "" top = ""
hasfeels = len(os.listdir(config.USER_DATA)) > 0 hasfeels = len(os.listdir(config.MAIN_FEELS)) > 0
nofeels = "you don't have any feels to work with, "+chatter.say("friend")+"\n\n> " nofeels = "you don't have any feels to work with, "+chatter.say("friend")+"\n\n> "
if choice is not False: if choice is not False:
@ -727,7 +727,7 @@ def view_neighbors(users, prompt):
sortedUsers.append(user[0]) sortedUsers.append(user[0])
userIndex.append(user[2]) userIndex.append(user[2])
choice = menu_handler(sortedUsers, "pick a townie to browse their feels, or type 'back' or 'q' to go home: ", 15, SETTINGS.get("rainbows", False), prompt) choice = menu_handler(sortedUsers, "pick a townie to browse their feels, or type 'q' to go home: ", 15, SETTINGS.get("rainbows", False), prompt)
if choice is not False: if choice is not False:
redraw("~{user}'s recorded feels, listed by date: \n".format(user=userIndex[choice])) redraw("~{user}'s recorded feels, listed by date: \n".format(user=userIndex[choice]))
@ -765,7 +765,7 @@ def generate_feels_list(user):
showpub = False showpub = False
if user == config.USER: if user == config.USER:
entryDir = config.USER_DATA entryDir = config.MAIN_FEELS
owner = "your" owner = "your"
if core.publishing(): if core.publishing():
showpub = True showpub = True
@ -834,7 +834,7 @@ YYYYMMDD: """)
here's a preview of that feel. press <q> when you're done reviewing! here's a preview of that feel. press <q> when you're done reviewing!
-------------------------------------------------------------""") -------------------------------------------------------------""")
if subprocess.call(["less", os.path.join(config.USER_DATA, feel+".txt")]): if subprocess.call(["less", os.path.join(config.MAIN_FEELS, feel+".txt")]):
redraw("deleting feels") redraw("deleting feels")
print("""\ print("""\
sorry, i couldn't find feels for {date}! sorry, i couldn't find feels for {date}!
@ -889,7 +889,7 @@ just in case a future version of you still wants to look them over.
time.sleep(1) time.sleep(1)
print("...") print("...")
feelscount = len(os.listdir(config.USER_DATA)) feelscount = len(os.listdir(config.MAIN_FEELS))
if feelscount > 0: if feelscount > 0:
purgecode = util.genID(5) purgecode = util.genID(5)
@ -910,8 +910,8 @@ the following purge code:
print("...") print("...")
time.sleep(1) time.sleep(1)
unpublish() unpublish()
if not subprocess.call(["rm", "-rf", config.USER_DATA]): if not subprocess.call(["rm", "-rf", config.MAIN_FEELS]):
subprocess.call(["mkdir", config.USER_DATA]) subprocess.call(["mkdir", config.MAIN_FEELS])
print("ALL FEELS PURGED! you're ready to start fresh!") print("ALL FEELS PURGED! you're ready to start fresh!")
else: else:
print(""" print("""
@ -941,7 +941,7 @@ def show_credits():
## handlers ## handlers
def write_entry(entry=os.path.join(config.USER_DATA, "test.txt")): def write_entry(entry=os.path.join(config.MAIN_FEELS, "test.txt")):
''' '''
main feels-recording handler main feels-recording handler
''' '''
@ -962,15 +962,15 @@ def write_entry(entry=os.path.join(config.USER_DATA, "test.txt")):
core.toggle_nopub(os.path.basename(entry)) core.toggle_nopub(os.path.basename(entry))
else: else:
if core.publishing(): if core.publishing():
core.write("index.html") core.write_html("index.html")
left = "posted to {url}/index.html\n\n> ".format( left = "posted to {url}/index.html\n\n> ".format(
url="/".join( url="/".join(
[config.LIVE+config.USER, [config.LIVE+config.USER,
str(SETTINGS.get("publish dir"))])) str(SETTINGS.get("publish dir"))]))
if SETTINGS.get('gopher'): if SETTINGS.get('gopher'):
gopher.publish_gopher('feels', core.get_files()) gopher.publish_gopher('feels', core.FILES)
left += " also posted to your ~/public_gopher!\n" left += "also posted to your ~/public_gopher!\n\n> "
#core.load_files() #core.load_files()
redraw(left + "thanks for sharing your feels!") redraw(left + "thanks for sharing your feels!")
@ -1015,7 +1015,6 @@ none of your feels will be viewable outside of this server)"""
action = core.toggle_nopub(target) action = core.toggle_nopub(target)
redraw(prompt) redraw(prompt)
core.write("index.html")
if SETTINGS["gopher"]: if SETTINGS["gopher"]:
gopher.publish_gopher('feels', core.get_files()) gopher.publish_gopher('feels', core.get_files())
@ -1340,7 +1339,7 @@ def update_publishing():
subprocess.call(["rm", os.path.join(config.PUBLIC, oldDir)]) subprocess.call(["rm", os.path.join(config.PUBLIC, oldDir)])
make_publish_dir(newDir) make_publish_dir(newDir)
core.load_files() core.load_files()
core.write("index.html") core.write_html("index.html")
else: else:
unpublish() unpublish()
SETTINGS.update({"publish dir": None}) SETTINGS.update({"publish dir": None})
@ -1444,7 +1443,7 @@ def update_user_version():
# repopulate html files # repopulate html files
core.load_files() core.load_files()
core.write("index.html") core.write_html("index.html")
# add publishing setting # add publishing setting
print("\nnew feature!\n") print("\nnew feature!\n")

View File

@ -26,6 +26,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import random import random
import time import time
from six.moves import input from six.moves import input
import os
import colorama import colorama
import inflect import inflect
@ -214,3 +215,21 @@ def input_yn(query):
ans = input("'y' or 'n' please: ") ans = input("'y' or 'n' please: ")
return ans == "y" return ans == "y"
def parse_date(file):
'''
parses date out of pre-validated filename
* assumes a filename of YYYYMMDD.txt
* returns a list:
[0] 'YYYY'
[1] 'MM'
[2] 'DD'
'''
rawdate = os.path.splitext(os.path.basename(file))[0]
date = [rawdate[0:4], rawdate[4:6], rawdate[6:]]
return date