You can add a draft by 'draft add [name]' now and receive a simple template with current time and date and UNIX username as author. At this Time emacs is hardcoded as editor.

Also I expanded the 'blog update' program logic.
This commit is contained in:
maste9 2015-02-10 20:26:53 +01:00 committed by Homer S
parent 22df72569b
commit e52104895b
1 changed files with 53 additions and 12 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os,re,subprocess import os,re,time,getpass,subprocess
from string import Template from string import Template
from argparse import ArgumentParser from argparse import ArgumentParser
@ -57,6 +57,9 @@ print(args)
# Global Constants # # Global Constants #
#################### ####################
# logged in user
user = getpass.getuser()
# set path to blog # set path to blog
try: try:
blog_dir = os.path.abspath(os.path.expanduser(vars(args)['blog_dir'])) blog_dir = os.path.abspath(os.path.expanduser(vars(args)['blog_dir']))
@ -68,6 +71,15 @@ except:
(pbconf,blogconf,l10nconf) = (False,False,False) (pbconf,blogconf,l10nconf) = (False,False,False)
# path to blog's templates
tplDir = os.path.join(blog_dir,"templates")
# paths to configs
pbconfpath = os.path.join("/","etc","picoblogger","pb.conf") # global config
blogconfpath = os.path.join(blog_dir,"pb.conf") # local config
################## ##################
# Help Functions # # Help Functions #
################## ##################
@ -87,41 +99,43 @@ def parse_conf(path):
return aVars return aVars
###################### ######################
# Parse Config Files # # Parse Config Files #
###################### ######################
pbconfpath = os.path.join("/","etc","picoblogger","pb.conf")
try: try:
if os.path.isfile(pbconfpath): if os.path.isfile(pbconfpath):
pbconf = parse_conf(pbconfpath) pbconf = parse_conf(pbconfpath)
except: except:
print("Couldn't parse systems global picoblogger configuration (/etc/picoblogger/pb.conf).") print("Couldn't parse systems global picoblogger configuration (%s)."%pbconfpath)
raise raise
blogconfpath = os.path.join(blog_dir,"pb.conf")
try: try:
if os.path.isfile(blogconfpath): if os.path.isfile(blogconfpath):
blogconf = parse_conf(blogconfpath) blogconf = parse_conf(blogconfpath)
except: except:
print("Couldn't parse blog's local configuration ([blog-dir]/pb.conf).") print("Couldn't parse blog's local configuration (%s)."%blogconfpath)
raise raise
l10nconfpath = os.path.join(blog_dir,"l10n",blogconf['BLOG_LANG'],"static.conf") l10nconfpath = os.path.join(blog_dir,"l10n",blogconf['BLOG_LANG'],"static.conf") # localization file
try: try:
if os.path.isfile(l10nconfpath): if os.path.isfile(l10nconfpath):
l10nconf = parse_conf(l10nconfpath) l10nconf = parse_conf(l10nconfpath)
print(l10nconf)
except: except:
print("Couldn't parse localization file ([blog-dir]/l10n/['BLOG_LANG']/static.conf).") print("Couldn't parse localization file (%s)."%l10nconfpath)
raise raise
tplDir = os.path.join(blog_dir,"templates") ########################
# Templating functions #
########################
def update_blog(subsection): def update_blog(subsection):
# Joins config defined variables into templates
tplIndex = os.path.join(tplDir,"index.htm") tplIndex = os.path.join(tplDir,"index.htm")
blogIndex = os.path.join(blog_dir,"index.html") blogIndex = os.path.join(blog_dir,"index.html")
@ -151,6 +165,10 @@ def update_blog(subsection):
with open(blogIndex,"w", encoding=l10nconf['BLOG_CHARSET']) as f: with open(blogIndex,"w", encoding=l10nconf['BLOG_CHARSET']) as f:
print(tmpIndex, file=f) print(tmpIndex, file=f)
##################
# Main Functions #
##################
# blog functions # blog functions
@ -160,8 +178,20 @@ if vars(args)['target'] == 'blog':
## update subsections or whole blog ## update subsections or whole blog
if vars(args)['action'] == 'update': if vars(args)['action'] == 'update':
print("Updating your block.")
if vars(args)['subsection'] in ('recent','all'):
pass
if vars(args)['subsection'] in ('archive','all'):
pass
if vars(args)['subsection'] in ('feed','all'):
pass
# Joining all templates
update_blog(vars(args)['subsection']) update_blog(vars(args)['subsection'])
print("Updated your blog.")
## add a new blog at -d blog-directory ## add a new blog at -d blog-directory
@ -178,7 +208,18 @@ if vars(args)['target'] == 'draft':
## add new draft ## add new draft
if vars(args)['action'] == 'add': if vars(args)['action'] == 'add':
subprocess.call(["emacs",os.path.join(blog_dir,"templates/article.txt")]) draft = os.path.join(blog_dir,"drafts",vars(args)['name'] + ".txt")
tpl = os.path.join(tplDir,"article.tpl")
sTpl = open(tpl).read()
subs = {"DATE" : time.strftime("%x",time.localtime()), "TIME" : time.strftime("%X",time.localtime()),
"AUTHOR" : user
}
sTpl = Template(sTpl).safe_substitute(subs)
with open(draft,"w", encoding=l10nconf['BLOG_CHARSET']) as f:
print(sTpl, file=f)
subprocess.call(["emacs", "nw", draft])
## list drafts ## list drafts