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')
USER_CONFIG = os.path.join(PATH, 'config')
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")
## UI

View File

@ -41,6 +41,7 @@ import json
from . import chatter
from . import config
from . import gopher
FEED = os.path.join("/home", "endorphant", "public_html", "ttbp", "index.html")
SETTINGS = {}
@ -75,19 +76,24 @@ def reload_ttbprc(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 = []
for filename in os.listdir(config.USER_DATA):
for filename in os.listdir(feelsdir):
if nopub(filename):
unpublish_feel(filename)
'''
link = os.path.join(config.WWW,
os.path.splitext(
os.path.basename(filename))[0]+".html")
if os.path.exists(link):
subprocess.call(["rm", link])
continue
filename = os.path.join(config.USER_DATA, filename)
'''
filename = os.path.join(feelsdir, filename)
if os.path.isfile(filename) and valid(filename):
files.append(filename)
@ -96,19 +102,18 @@ def get_files():
return files
def load_files():
def load_files(feelsdir=config.MAIN_FEELS):
'''
file loader
* 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
load_nopubs()
FILES = get_files()
FILES = get_files(feelsdir)
def load_nopubs():
"""Load a list of the user's nopub entries.
@ -127,7 +132,7 @@ def load_nopubs():
## html outputting
def write(outurl="default.html"):
def write_html(outurl="default.html"):
'''
main page renderer
@ -167,7 +172,7 @@ def write_page(filename):
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.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
'''
date = parse_date(filename)
date = util.parse_date(filename)
entry = [
"\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 = []
rawfile = open(os.path.join(config.USER_DATA, filename), "r")
rawfile = open(os.path.join(config.MAIN_FEELS, filename), "r")
for line in rawfile:
raw.append(line)
@ -316,7 +321,7 @@ def meta(entries = FILES):
except subprocess.CalledProcessError:
wc = "???"
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]
meta.append([filename, mtime, wc, timestamp, date, author])
@ -345,23 +350,6 @@ def valid(filename):
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():
'''
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)
else:
NOPUBS.append(filename)
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])
unpublish_feel(filename)
nopub_file = open(config.NOPUB, 'w')
nopub_file.write("""\
@ -475,9 +458,35 @@ def toggle_nopub(filename):
nopub_file.close()
load_files()
write_html("index.html")
gopher.publish_gopher('feels', FILES)
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
from . import util
from .core import parse_date
#from .core import parse_date
GOPHER_PROMPT = """
@ -73,7 +73,7 @@ def publish_gopher(gopher_path, entry_filenames):
if not os.path.exists(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(
file_label=label,
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())
subprocess.call(["mkdir", config.PATH])
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")
open(versionFile, "w").write(__version__)
@ -537,14 +537,14 @@ def main_menu():
if choice == '0':
redraw()
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()
elif choice == '1':
intro = "here are some options for managing your feels:"
redraw(intro)
review_menu(intro)
core.load_files()
core.write("index.html")
core.write_html("index.html")
elif choice == '2':
users = core.find_ttbps()
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))
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 = ""
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> "
if choice is not False:
@ -727,7 +727,7 @@ def view_neighbors(users, prompt):
sortedUsers.append(user[0])
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:
redraw("~{user}'s recorded feels, listed by date: \n".format(user=userIndex[choice]))
@ -765,7 +765,7 @@ def generate_feels_list(user):
showpub = False
if user == config.USER:
entryDir = config.USER_DATA
entryDir = config.MAIN_FEELS
owner = "your"
if core.publishing():
showpub = True
@ -834,7 +834,7 @@ YYYYMMDD: """)
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")
print("""\
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)
print("...")
feelscount = len(os.listdir(config.USER_DATA))
feelscount = len(os.listdir(config.MAIN_FEELS))
if feelscount > 0:
purgecode = util.genID(5)
@ -910,8 +910,8 @@ the following purge code:
print("...")
time.sleep(1)
unpublish()
if not subprocess.call(["rm", "-rf", config.USER_DATA]):
subprocess.call(["mkdir", config.USER_DATA])
if not subprocess.call(["rm", "-rf", config.MAIN_FEELS]):
subprocess.call(["mkdir", config.MAIN_FEELS])
print("ALL FEELS PURGED! you're ready to start fresh!")
else:
print("""
@ -941,7 +941,7 @@ def show_credits():
## 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
'''
@ -962,15 +962,15 @@ def write_entry(entry=os.path.join(config.USER_DATA, "test.txt")):
core.toggle_nopub(os.path.basename(entry))
else:
if core.publishing():
core.write("index.html")
core.write_html("index.html")
left = "posted to {url}/index.html\n\n> ".format(
url="/".join(
[config.LIVE+config.USER,
str(SETTINGS.get("publish dir"))]))
if SETTINGS.get('gopher'):
gopher.publish_gopher('feels', core.get_files())
left += " also posted to your ~/public_gopher!\n"
gopher.publish_gopher('feels', core.FILES)
left += "also posted to your ~/public_gopher!\n\n> "
#core.load_files()
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)
redraw(prompt)
core.write("index.html")
if SETTINGS["gopher"]:
gopher.publish_gopher('feels', core.get_files())
@ -1340,7 +1339,7 @@ def update_publishing():
subprocess.call(["rm", os.path.join(config.PUBLIC, oldDir)])
make_publish_dir(newDir)
core.load_files()
core.write("index.html")
core.write_html("index.html")
else:
unpublish()
SETTINGS.update({"publish dir": None})
@ -1444,7 +1443,7 @@ def update_user_version():
# repopulate html files
core.load_files()
core.write("index.html")
core.write_html("index.html")
# add publishing setting
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 time
from six.moves import input
import os
import colorama
import inflect
@ -214,3 +215,21 @@ def input_yn(query):
ans = input("'y' or 'n' please: ")
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