picoblogger/picoblogger/main.py

118 lines
3.7 KiB
Python
Executable File

#!/usr/bin/python3
import os,re
from string import Template
from argparse import ArgumentParser
# check for arguments and options
parser = ArgumentParser(description="Manage your weblog via commandline. Creates a static html5 compliant website with your publications.")
# general options
parser.add_argument('-d','--blog-dir', dest="blog_dir", default='~/public_html/blog', help='path to blog directory (default: %(default)s)')
### subroutines of pb
# 'update' command
subparsers = parser.add_subparsers(help="subcommands")
parser_update = subparsers.add_parser('update', help='updates blog to current state')
parser_update.add_argument('subsection', choices=['all','recent','archive','feed'], help='sections of blog you may want to update')
# 'draft' command
parser_draft = subparsers.add_parser('draft', help='create, list, edit or remove drafts')
parser_draft.add_argument('action', choices=['add','list','edit','remove'], help='define what to do with a certain draft')
parser_draft.add_argument('nameorid', choices=['name','id']
# 'article' command
parser_article = subparsers.add_parser('article', help='import, list, edit or delete [article]')
args = parser.parse_args()
print(vars(args))
# set path to blog
try:
blog_dir = os.path.abspath(os.path.expanduser(vars(args)['blog_dir']))
if not os.path.isdir(blog_dir):
print("The path you offered is not a directory!")
exit
except:
raise
# parse unix-config-style files
def parse_conf(path):
aVars = {}
f = open(path, "r")
for line in f:
if "#" in line:
line = line[0:line.find("#")]
pattern = '^(\w+)\s*?=\s*(.+)[^#]*$'
match = re.search(pattern, line)
if match:
aVars[match.group(1)] = match.group(2)
return aVars
(pbconf,blogconf,l10nconf) = (False,False,False)
pbconfpath = os.path.join("/","etc","picoblogger","pb.conf")
try:
if os.path.isfile(pbconfpath):
pbconf = parse_conf(pbconfpath)
except:
print("Couldn't parse systems global picoblogger configuration (/etc/picoblogger/pb.conf).")
raise
blogconfpath = os.path.join(blog_dir,"pb.conf")
try:
if os.path.isfile(blogconfpath):
blogconf = parse_conf(blogconfpath)
except:
print("Couldn't parse blog's local configuration ([blog-dir]/pb.conf).")
raise
l10nconfpath = os.path.join(blog_dir,"l10n",blogconf['BLOG_LANG'],"static.conf")
try:
if os.path.isfile(l10nconfpath):
l10nconf = parse_conf(l10nconfpath)
print(l10nconf)
except:
print("Couldn't parse localization file ([blog-dir]/l10n/['BLOG_LANG']/static.conf).")
raise
tplDir = os.path.join(blog_dir,"templates")
def update_blog():
tplIndex = os.path.join(tplDir,"index.htm")
blogIndex = os.path.join(blog_dir,"index.html")
tpl = Template(open(tplIndex,'r').read())
tmpIndex = tpl.safe_substitute(l10nconf)
if blogconf:
tmpIndex = Template(tmpIndex).safe_substitute(blogconf)
if pbconf:
tmpIndex = Template(tmpIndex).safe_substitute(pbconf)
sNAV = open(os.path.join(tplDir,"nav.htm")).read()
sNAV = Template(sNAV).safe_substitute(l10nconf)
sMAIN = open(os.path.join(tplDir,"main.htm")).read()
sASIDE = open(os.path.join(tplDir,"aside.htm")).read()
sFOOTER = open(os.path.join(tplDir,"footer.htm")).read()
tplSub = {
"TEMPLATE_NAV":sNAV,
"TEMPLATE_MAIN":sMAIN,
"TEMPLATE_ASIDE":sASIDE,
"TEMPLATE_FOOTER":sFOOTER
}
tmpIndex = Template(tmpIndex).safe_substitute(tplSub)
with open(blogIndex,"w", encoding=l10nconf['BLOG_CHARSET']) as f:
print(tmpIndex, file=f)
if vars(args)['action'] == 'update':
print("Updating your block.")
update_blog()