From 7160f64fd2d506d5f9021ed6639805e9af7ce357 Mon Sep 17 00:00:00 2001 From: Ian Weller Date: Thu, 6 Nov 2008 20:02:32 -0600 Subject: Use configuration file to determine templates --- fedora_business_cards/config.py | 34 ++++++++++++++++++++++++++----- fedora_business_cards/frontend/cmdline.py | 32 ++++++++++------------------- fedora_business_cards/generate.py | 13 ++++-------- templates/templates.ini | 11 ++++++++++ 4 files changed, 55 insertions(+), 35 deletions(-) create mode 100644 templates/templates.ini diff --git a/fedora_business_cards/config.py b/fedora_business_cards/config.py index 61b1a32..b88d6f2 100644 --- a/fedora_business_cards/config.py +++ b/fedora_business_cards/config.py @@ -22,9 +22,34 @@ Controls the locations of configuration files, and imports configurations from all those files in a specific order. """ -import iniparse +from iniparse import ConfigParser import os + +def available_templates(config): + """ + Takes the main ConfigParser as the argument. + """ + templates_dir = config.get('location', 'templates') + templates = ConfigParser() + templates.read(templates_dir+"/templates.ini") + filelist = os.listdir(templates_dir) + for section in templates.sections(): + if templates.options(section) == ["humandesc", "front", "back", "type"]: + if templates.get(section, "front") in filelist: + if templates.get(section, "back") in filelist: + # only SVG templates are currently supported + if templates.get(section, "type") == "svg": + continue + elif templates.options(section) == ["humandesc", "front", "type"]: + if templates.get(section, "front") in filelist: + # only SVG templates are currently supported + if templates.get(section, "type") == "svg": + continue + templates.remove_section(section) + return templates + + # locations, in reverse-order of priority LOCATIONS = ['/'.join(__file__.split('/')[:-1]+['config.ini']), 'config.ini', # in current working directory @@ -32,10 +57,9 @@ LOCATIONS = ['/'.join(__file__.split('/')[:-1]+['config.ini']), '/etc/fedora-business-cards.ini', os.getenv('HOME')+'/.fedora-business-cards.ini'] -parser = iniparse.ConfigParser() - -# import the configs +parser = ConfigParser() for i in LOCATIONS: parser.read(i) -__all__ = ('parser') + +__all__ = ('parser', 'available_templates') diff --git a/fedora_business_cards/frontend/cmdline.py b/fedora_business_cards/frontend/cmdline.py index 1b36998..8d1921c 100644 --- a/fedora_business_cards/frontend/cmdline.py +++ b/fedora_business_cards/frontend/cmdline.py @@ -69,28 +69,13 @@ def main(): options = parser.parse_args()[0] # check what templates are available config.parser.read(options.config_location) - templates_dir = config.parser.get('location', 'templates') - contents = os.listdir(templates_dir) - checked_once = [] - available_templates = [] - for i in contents: - if i[-4:] == '.svg': - if i[:6] == 'front-': - name = i[6:-4] - elif i[:5] == 'back-': - name = i[5:-4] - else: - continue - if name in checked_once: - available_templates.append(name) - else: - checked_once.append(name) + templates = config.available_templates(config.parser) if options.listtemplates: print "Available templates:" - for i in available_templates: - print " %s" % i + for section in templates.sections(): + print " %s (%s)" % (section, templates.get(section, 'humandesc')) sys.exit(0) - if options.template not in available_templates: + if options.template not in templates.sections(): print "%s not an available template" % options.template sys.exit(1) # ask for FAS login @@ -151,10 +136,15 @@ def main(): elif lineno == '0' or lineno == '1' or lineno == '2' or \ lineno == '3' or lineno == '4' or lineno == '5': lines[int(lineno)] = newdata + # figure out template locations + frontloc = config.parser.get('location', 'templates')+'/'+\ + templates.get(options.template, 'front') + backloc = config.parser.get('location', 'templates')+'/'+\ + templates.get(options.template, 'back') # generate front of business card print "Generating front...", sys.stdout.flush() - xml = generate.gen_front(name, title, lines, options.template) + xml = generate.gen_front(name, title, lines, frontloc) if options.output == "svg": export.svg_to_file(xml, options.username+'-front.'+options.output) else: @@ -163,7 +153,7 @@ def main(): # generate back of business card print "Generating back...", sys.stdout.flush() - xml = generate.gen_back(options.template) + xml = generate.gen_back(backloc) if options.output == "svg": export.svg_to_file(xml, options.username+'-back.'+options.output) else: diff --git a/fedora_business_cards/generate.py b/fedora_business_cards/generate.py index aa22485..91f1e14 100644 --- a/fedora_business_cards/generate.py +++ b/fedora_business_cards/generate.py @@ -23,9 +23,6 @@ Generates both sides of the business card. from xml.dom import minidom -# local imports -import config - def find_node(doc_node, tag_name, attribute_name, attribute_value): """ @@ -40,12 +37,11 @@ def find_node(doc_node, tag_name, attribute_name, attribute_value): return element -def gen_front(name, title, lines, template="northamerica"): +def gen_front(name, title, lines, template_loc): """ Generates the front of the business card. """ - dom = minidom.parse(config.parser.get('location', 'templates')+'/front-'+\ - template+'.svg') + dom = minidom.parse(template_loc) namenode = find_node(dom, 'text', 'id', 'fullname') namenode.appendChild(dom.createTextNode(name)) titlenode = find_node(dom, 'text', 'id', 'title') @@ -56,10 +52,9 @@ def gen_front(name, title, lines, template="northamerica"): return dom.toxml() -def gen_back(template="northamerica"): +def gen_back(template_loc): """ Generates the back of the business card. """ - dom = minidom.parse(config.parser.get('location', 'templates')+'/back-'+\ - template+'.svg') + dom = minidom.parse(template_loc) return dom.toxml() diff --git a/templates/templates.ini b/templates/templates.ini new file mode 100644 index 0000000..82c3c0f --- /dev/null +++ b/templates/templates.ini @@ -0,0 +1,11 @@ +[northamerica] +humandesc = North America +front = front-northamerica.svg +back = back-northamerica.svg +type = svg + +[overnightprints] +humandesc = OvernightPrints.com (1/16" bleed) +front = front-overnightprints.svg +back = back-overnightprints.svg +type = svg -- cgit