Added easy to write articles

This commit is contained in:
Morten Delenk 2016-06-12 15:59:44 +02:00
parent 358049646c
commit 6571d498e4
5 changed files with 61 additions and 2 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
__pycache__/
*.pyc
article.py
article.pyc

1
data/.htaccess Normal file
View file

@ -0,0 +1 @@
deny from all

BIN
data/articles-0.pickle Normal file

Binary file not shown.

View file

@ -1,7 +1,13 @@
#!/usr/bin/env python3
from htmlgen import *
import storage
print("Content-type: text/html\r\n\r\n")
html=htmlgen.HTMLgen(pagelayout.getLayoutXML().decode('utf-8'),"Home Page")
count=storage.count("articles")
for i in range(count):
html.addArticle(**(storage.get("articles",i)))
print("<!DOCTYPE html>")
print(html.renderSite())
html.addArticle("Markdown test","""*
* 1
* 2
@ -109,5 +115,3 @@ test
**TEST**""")
print("<!DOCTYPE html>")
print(html.renderSite())

52
storage.py Normal file
View file

@ -0,0 +1,52 @@
import pickle
import os
def get(name, num):
if not num < count (name):
raise IndexError
storagename="data/%s-%i.pickle"%(name,num/50)
f=open(storagename, "rb")
data=pickle.load(f)
f.close()
if not isinstance(data, list):
raise ValueError
return data[num%50]
def put(name, num, val):
if not num < count (name):
raise IndexError
storagename="data/%s-%i.pickle"%(name,num/50)
f=open(storagename, "rb")
data=pickle.load(f)
f.close()
if not isinstance(data, list):
raise ValueError
data[num%50]=val
f=open(storagename, "wb")
pickle.dump(data,f,-1)
f.close()
def append(name, val):
num=count(name)
storagename="data/%s-%i.pickle"%(name,num/50)
data=[]
try:
f=open(storagename, "rb")
data=pickle.load(f)
f.close()
except:
pass
data.append(val)
f=open(storagename, "wb")
pickle.dump(data,f,-1)
f.close()
def count(name):
path = "data/"
files = [i for i in os.listdir(path) if os.path.isfile(os.path.join(path,i))
and name in i]
storagename="data/%s-%i.pickle"%(name,len(files)-1)
count=(len(files)-1)*50
if len(files) == 0:
return 0
f=open(storagename, "rb")
count = count + len(pickle.load(f))
f.close()
return count