ttbp/bin/core.py

101 lines
2.4 KiB
Python
Raw Normal View History

2016-04-30 01:56:40 +00:00
#!/usr/bin/python
import os
2016-04-30 23:47:49 +00:00
SOURCE = os.path.join("/home", "endorphant", "projects", "ttbp", "bin")
USER = os.path.basename(os.path.expanduser("~"))
PATH = os.path.join("/home", USER, ".ttbp")
LIVE = "http://tilde.town/~"
WWW = os.path.join(PATH, "www")
2016-04-30 04:00:55 +00:00
CONFIG = os.path.join(PATH, "config")
2016-04-30 23:47:49 +00:00
DATA = os.path.join(PATH, "entries")
2016-04-30 01:56:40 +00:00
2016-04-30 02:22:21 +00:00
MONTHS = {
"01":"january",
"02":"february",
"03":"march",
"04":"april",
"05":"may",
"06":"june",
"07":"july",
"08":"august",
"09":"september",
"10":"october",
"11":"november",
"12":"december"
}
2016-04-30 01:56:40 +00:00
HEADER = open(os.path.join(CONFIG, "header.txt")).read()
FOOTER = open(os.path.join(CONFIG, "footer.txt")).read()
FILES = []
for file in os.listdir(DATA):
filename = os.path.join(DATA, file)
if os.path.isfile(filename) and os.path.splitext(filename)[1] == ".txt":
FILES.append(file)
2016-05-01 00:19:00 +00:00
#print(file)
2016-04-30 01:56:40 +00:00
2016-04-30 23:47:49 +00:00
FILES.sort()
FILES.reverse()
2016-05-01 00:19:00 +00:00
#print("found: "+str(FILES))
2016-04-30 01:56:40 +00:00
def write(outurl="default.html"):
outfile = open(os.path.join(WWW, outurl), "w")
for line in HEADER:
outfile.write(line)
#for line in write_placeholder():
# outfile.write(line)
for file in FILES:
for line in write_entry(file):
outfile.write(line)
for line in FOOTER:
outfile.write(line)
outfile.close()
2016-04-30 23:47:49 +00:00
return os.path.join(LIVE+USER,os.path.basename(os.path.realpath(WWW)),outurl)
2016-04-30 04:00:55 +00:00
2016-04-30 01:56:40 +00:00
def write_entry(file):
# dump given file into entry format, return as list of strings
2016-04-30 02:09:30 +00:00
date = parse_date(file)
2016-04-30 01:56:40 +00:00
entry = [
2016-04-30 02:09:30 +00:00
"\t\t<p><a name=\""+date[0]+date[1]+date[2]+"\"></a><br /><br /></p>\n",
2016-04-30 01:56:40 +00:00
"\t\t<div class=\"entry\">\n",
2016-04-30 02:22:21 +00:00
"\t\t\t<h5><a href=\"#"+date[0]+date[1]+date[2]+"\">"+date[2]+"</a> "+MONTHS[date[1]]+" "+date[0]+"</h5>\n",
2016-04-30 01:56:40 +00:00
"\t\t\t<P>"
]
raw = []
rawfile = open(os.path.join(DATA, file), "r")
for line in rawfile:
raw.append(line)
rawfile.close()
for line in raw:
entry.append(line+"\t\t\t")
if line == "\n":
entry.append("</p>\n\t\t\t<p>")
entry.append("</p>\n\t\t</div>\n")
return entry
def parse_date(file):
# assuming a filename of YYYYMMDD.txt, returns a list of
# ['YYYY', 'MM', 'DD']
2016-04-30 02:09:30 +00:00
rawdate = os.path.splitext(os.path.basename(file))[0]
date = [rawdate[0:4], rawdate[4:6], rawdate[6:]]
return date