summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIan Weller <ianweller@gmail.com>2008-11-06 20:02:32 -0600
committerIan Weller <ianweller@gmail.com>2008-11-06 20:02:32 -0600
commit7160f64fd2d506d5f9021ed6639805e9af7ce357 (patch)
tree3b0007b10c54db5dc10c8100ce279671f5bc473f
parent6a3752bf45daead85162f4b1469f8de1747f7ae2 (diff)
downloadfedora-business-cards-7160f64fd2d506d5f9021ed6639805e9af7ce357.tar.gz
fedora-business-cards-7160f64fd2d506d5f9021ed6639805e9af7ce357.tar.xz
fedora-business-cards-7160f64fd2d506d5f9021ed6639805e9af7ce357.zip
Use configuration file to determine templates
-rw-r--r--fedora_business_cards/config.py34
-rw-r--r--fedora_business_cards/frontend/cmdline.py32
-rw-r--r--fedora_business_cards/generate.py13
-rw-r--r--templates/templates.ini11
4 files changed, 55 insertions, 35 deletions
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