From 57f7091fea0adb9d67529cae4b61e2bfbd438b02 Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Tue, 28 Jan 2014 12:35:52 +0200 Subject: [PATCH] Added config file features --- Qalbum-thumbnailer.py | 2 +- Qalbum.py | 70 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/Qalbum-thumbnailer.py b/Qalbum-thumbnailer.py index ca2b3b4..e72c8ed 100755 --- a/Qalbum-thumbnailer.py +++ b/Qalbum-thumbnailer.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# Copyright 2011 Ville Rantanen +# Copyright 2014 Ville Rantanen # # this program is free software: you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published diff --git a/Qalbum.py b/Qalbum.py index 6460b38..378456c 100755 --- a/Qalbum.py +++ b/Qalbum.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# Copyright 2012 Ville Rantanen +# Copyright 2014 Ville Rantanen # # This program is free software: you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published @@ -22,14 +22,34 @@ import shutil import csv import subprocess import string +import configobj from math import ceil from datetime import datetime # (c) ville.rantanen@helsinki.fi -__version__='2.5' +__version__='2.6' -webfilesearch=re.compile('.*index.html$|gallerystyle.css$|galleryscript.js$|descriptions.csv$|^info.txt$|\..*',re.I) +FILECONFIG=".config" +FILEDESC="descriptions.csv" +FILEINFO="info.txt" +SAVEDCONFIG="""attachments=boolean +gallery=string +infofile=string +link=boolean +parent=string +reverse=boolean +startpath=string +timesort=boolean +clean=boolean +force=boolean +gravity=string +recursive=boolean +width=string""".split('\n') +CONFIGCOMMENTS=""" +config values: +""".split('\n') +webfilesearch=re.compile('.*index.html$|gallerystyle.css$|galleryscript.js$|'+FILEDESC+'$|^'+FILEINFO+'$|\..*',re.I) imagesearch=re.compile('.*\.jpg$|.*\.jpeg$|.*\.gif$|.*\.png$|.*\.svg$|.*\.pdf$',re.I) vectorsearch=re.compile('.*\.svg$|.*\.pdf$',re.I) nonconvertiblesearch=re.compile('.*\.html$|.*\.htm$|.*\.php$',re.I) @@ -312,10 +332,10 @@ def create_thumb_bitmap(infile,outfile,vector=False,gravity='Center'): def getdescriptions(path,list): ''' Read descriptions.csv file and returns a list of descriptions. Missing descriptions are replaced with the file name. ''' - if not os.path.exists(os.path.join(path,'descriptions.csv')): + if not os.path.exists(os.path.join(path,FILEDESC)): return list desc=[i for i in list] - reader = csv.reader(open(os.path.join(path,'descriptions.csv'),'rb'), + reader = csv.reader(open(os.path.join(path,FILEDESC),'rb'), delimiter='\t', doublequote=False, escapechar='\\', @@ -437,6 +457,10 @@ def setupoptions(): parser=ArgumentParser() parser.add_argument("-v",action='version', version=__version__) parser.add_argument("--version",action='version', version=__version__) + parser.add_argument("-c",action="store_true",dest="writeconfig",default=False, + help="Write current configuration to file "+FILECONFIG+ + ". If file exists, Qalbum and thumbnail handling read from the file, "+ + "overriding switches.") parser.add_argument("-r",action="store_true",dest="reverse",default=False, help="Reverse sort orded") parser.add_argument("-L",action="store_false",dest="recurselink",default=True, @@ -447,7 +471,7 @@ def setupoptions(): help="Sort by file modification time") parser.add_argument("-a",action="store_false",dest="attachments",default=True, help="Disable attachments") - parser.add_argument("-i",type=str,dest="infofile",default="info.txt", + parser.add_argument("-i",type=str,dest="infofile",default=FILEINFO, help="File name for info files in all folders. (Default: %(default)s)") parser.add_argument("-g",type=str,dest="gallery",default="Gallery", help="Name for the root gallery (Default: %(default)s)") @@ -473,7 +497,7 @@ def setupdefaultoptions(options): if 'force' not in options: options.force=False if 'infofile' not in options: - options.infofile="info.txt" + options.infofile=FILEINFO if 'gallery' not in options: options.gallery="Gallery" if 'gravity' not in options: @@ -494,10 +518,42 @@ def setupdefaultoptions(options): options.width=850 return options +def readconfig(options): + """ Set up the options via config file """ + if os.path.exists(FILECONFIG): + try: + cfg=configobj.ConfigObj(FILECONFIG, configspec=SAVEDCONFIG, unrepr=True) + except configobj.UnreprError as err: + print("Config file "+FILECONFIG+" syntax error!") + print(err) + sys.exit(1) + for opt in cfg.keys(): + setattr(options,opt,cfg[opt]) + print("Read config from file") + + return options + +def writeconfig(options): + """ Write the options to config file """ + + cfg=configobj.ConfigObj(configspec=SAVEDCONFIG, unrepr=True) + cfg.initial_comment=CONFIGCOMMENTS + cfg.filename=FILECONFIG + for opt in SAVEDCONFIG: + if opt.startswith('#'): + continue + optname=opt.split("=")[0] + cfg[optname]=getattr(options,optname) + cfg.write() + print('Wrote '+FILECONFIG) + def execute_plain(): ''' Main execution function ''' options=setupoptions() + options=readconfig(options) options=setupdefaultoptions(options) + if options.writeconfig: + writeconfig(options) # Copy all resources to target folder pathname=os.path.dirname(os.path.realpath(sys.argv[0])) fullpath=os.path.abspath(pathname)