From 9ecf23c0166871e4147533147e14fc488c011f92 Mon Sep 17 00:00:00 2001 From: Ian Weller Date: Sun, 28 Sep 2008 15:46:02 -0500 Subject: Do all sorts of fun reorganization --- fedora_business_cards/__init__.py | 29 ++++ fedora_business_cards/config.ini | 3 + fedora_business_cards/config.py | 40 +++++ fedora_business_cards/exceptions.py | 37 +++++ fedora_business_cards/export.py | 52 +++++++ fedora_business_cards/frontend/__init__.py | 20 +++ fedora_business_cards/frontend/cmdline.py | 21 +++ fedora_business_cards/generate.py | 63 ++++++++ fedora_business_cards/information.py | 61 ++++++++ templates/back-northamerica.svg | 30 ++++ templates/back.svg | 30 ---- templates/bleed16-under.svg | 102 ------------- templates/front-northamerica.svg | 230 +++++++++++++++++++++++++++++ templates/front-template.svg | 230 ----------------------------- 14 files changed, 586 insertions(+), 362 deletions(-) create mode 100644 fedora_business_cards/__init__.py create mode 100644 fedora_business_cards/config.ini create mode 100644 fedora_business_cards/config.py create mode 100644 fedora_business_cards/exceptions.py create mode 100644 fedora_business_cards/export.py create mode 100644 fedora_business_cards/frontend/__init__.py create mode 100644 fedora_business_cards/frontend/cmdline.py create mode 100644 fedora_business_cards/generate.py create mode 100644 fedora_business_cards/information.py create mode 100644 templates/back-northamerica.svg delete mode 100644 templates/back.svg delete mode 100644 templates/bleed16-under.svg create mode 100644 templates/front-northamerica.svg delete mode 100644 templates/front-template.svg diff --git a/fedora_business_cards/__init__.py b/fedora_business_cards/__init__.py new file mode 100644 index 0000000..07a90f4 --- /dev/null +++ b/fedora_business_cards/__init__.py @@ -0,0 +1,29 @@ +### +# fedora-business-cards - for rendering Fedora contributor business cards +# Copyright (C) 2008 Ian Weller +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +### + +""" +The Fedora business cards generator will generate business cards for Fedora +contributors. It connects to FAS to retrieve user information and lays it out on +a SVG, then exports that to a PDF and PNG. Different templates are/will be +available for different business card sizes, or different requirements by +different companies. +""" + +__all__ = ('config', 'exceptions', 'export', 'frontend', 'generate', + 'information') diff --git a/fedora_business_cards/config.ini b/fedora_business_cards/config.ini new file mode 100644 index 0000000..eff2723 --- /dev/null +++ b/fedora_business_cards/config.ini @@ -0,0 +1,3 @@ +[location] +; this can be relative to the directory with config.py in it +templates = ../templates diff --git a/fedora_business_cards/config.py b/fedora_business_cards/config.py new file mode 100644 index 0000000..259b4db --- /dev/null +++ b/fedora_business_cards/config.py @@ -0,0 +1,40 @@ +### +# fedora-business-cards - for rendering Fedora contributor business cards +# Copyright (C) 2008 Ian Weller +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +### + +""" +Controls the locations of configuration files, and imports configurations from +all those files in a specific order. +""" + +import iniparse +import os + +# locations, in reverse-order of priority +LOCATIONS = ['./config.ini', + '/usr/share/fedora-business-cards/config.ini', + '/etc/fedora-business-cards.ini', + os.getenv('HOME')+'/.fedora-business-cards.ini'] + +parser = iniparse.ConfigParser() + +# import the configs +for i in LOCATIONS: + parser.read(LOCATIONS[i]) + +__all__ = ('parser') diff --git a/fedora_business_cards/exceptions.py b/fedora_business_cards/exceptions.py new file mode 100644 index 0000000..9e87efb --- /dev/null +++ b/fedora_business_cards/exceptions.py @@ -0,0 +1,37 @@ +### +# fedora-business-cards - for rendering Fedora contributor business cards +# Copyright (C) 2008 Ian Weller +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +### + +""" +Miscellaneous exceptions used specifically for this module. +""" + + +class NoGPGKeyError(StandardError): + """ + Exception used when the GPG key for a specific ID isn't available on the + system; usually used when we can't derive the fingerprint from the ID + because we don't have the key. + """ + + def __init__(self, keyid): + StandardError.__init__(self) + self.keyid = keyid + + def __str__(self): + return self.keyid diff --git a/fedora_business_cards/export.py b/fedora_business_cards/export.py new file mode 100644 index 0000000..d4b6c37 --- /dev/null +++ b/fedora_business_cards/export.py @@ -0,0 +1,52 @@ +### +# fedora-business-cards - for rendering Fedora contributor business cards +# Copyright (C) 2008 Ian Weller +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +### + +""" +Functions to export cards from SVGs. +""" + +# Thanks much to Jef Spaleta for this code. + +import rsvg +import cairo + +if not cairo.HAS_PDF_SURFACE: + raise SystemExit('cairo was not compiled with PDF support') +if not cairo.HAS_PNG_FUNCTIONS: + raise SystemExit('cairo was not compiled with PNG support') + + +def svg_to_pdf_png(pdfname, pngname, xmlstring, dpi=300): + """ + Export an SVG to both a PDF and PNG. + pngname = location of PNG file to export to + pdfname = location of pdf file to export to + xmlstring = the SVG XML to export + dpi = DPI to export PNG with (default: 300) + """ + svg = rsvg.Handle(data=xmlstring) + pdffile = file(pdfname, 'w') + width = int(svg.props.width/90.*dpi) + height = int(svg.props.height/90.*dpi) + surface = cairo.PDFSurface(pdffile, width, height) + ctx = cairo.Context(surface) + svg.render_cairo(ctx) + surface.write_to_png(pngname) + surface.finish() + pdffile.close() diff --git a/fedora_business_cards/frontend/__init__.py b/fedora_business_cards/frontend/__init__.py new file mode 100644 index 0000000..5fba6d4 --- /dev/null +++ b/fedora_business_cards/frontend/__init__.py @@ -0,0 +1,20 @@ +### +# fedora-business-cards - for rendering Fedora contributor business cards +# Copyright (C) 2008 Ian Weller +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +### + +__all__ = ('frontend') diff --git a/fedora_business_cards/frontend/cmdline.py b/fedora_business_cards/frontend/cmdline.py new file mode 100644 index 0000000..8144e8e --- /dev/null +++ b/fedora_business_cards/frontend/cmdline.py @@ -0,0 +1,21 @@ +### +# fedora-business-cards - for rendering Fedora contributor business cards +# Copyright (C) 2008 Ian Weller +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +### + +from optparse import OptionParser + diff --git a/fedora_business_cards/generate.py b/fedora_business_cards/generate.py new file mode 100644 index 0000000..5a92039 --- /dev/null +++ b/fedora_business_cards/generate.py @@ -0,0 +1,63 @@ +### +# fedora-business-cards - for rendering Fedora contributor business cards +# Copyright (C) 2008 Ian Weller +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +### + +""" +Generates both sides of the business card. +""" + +from xml.dom import minidom +from . import config + + +def find_node(doc_node, tag_name, attribute_name, attribute_value): + """ + Gets a specific node from a DOM tree with a certain tag name, attribute + name, and attribute value. + """ + # thanks, mizmo + elements = doc_node.getElementsByTagName(tag_name) + for element in elements: + if element.hasAttribute(attribute_name): + if element.getAttribute(attribute_name) == attribute_value: + return element + + +def gen_front(name, title, lines, template="northamerica"): + """ + Generates the front of the business card. + """ + dom = minidom.parse(config.parser.get('location', 'templates')+'/front-'+\ + template+'.svg') + namenode = find_node(dom, 'text', 'id', 'fullname') + namenode.appendChild(dom.createTextNode(name)) + titlenode = find_node(dom, 'text', 'id', 'title') + titlenode.appendChild(dom.createTextNode(title)) + for i in range(6): + node = find_node(dom, 'tspan', 'id', 'line%d' % (i+1)) + node.appendChild(dom.createTextNode(lines[i])) + return dom.toxml() + + +def gen_back(template="northamerica"): + """ + Generates the back of the business card. + """ + dom = minidom.parse(config.parser.get('location', 'templates')+'/back-'+\ + template+'.svg') + return dom.toxml() diff --git a/fedora_business_cards/information.py b/fedora_business_cards/information.py new file mode 100644 index 0000000..9fbaf48 --- /dev/null +++ b/fedora_business_cards/information.py @@ -0,0 +1,61 @@ +### +# fedora-business-cards - for rendering Fedora contributor business cards +# Copyright (C) 2008 Ian Weller +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +### + +""" +Assembles information about a person from FAS and their GPG key. +""" + +from fedora.client.fas2 import AccountSystem +import gpgme +import re +from . import exceptions + + +def get_gpg_fingerprint(keyid): + """ + Gets the GPG fingerprint from the key ID. + """ + ctx = gpgme.Context() + try: + key = ctx.get_key(keyid) + except: + raise exceptions.NoGPGKeyError(keyid) + fpr = key.subkeys[0].fpr + return ' '.join([i for i in re.split('([A-Z0-9]{4})', fpr) if i]) + + +def get_information(loginname, password, username=None): + """ + Fetches information about a certain contributor from FAS. + loginname: username to *login with* on FAS + password: password to loginname + username: username to get information on (default: same as loginname) + """ + if username == None: + username = loginname + fas = AccountSystem(username=loginname, password=password) + userinfo = fas.person_by_username(username) + infodict = {} + infodict['name'] = userinfo["human_name"] + infodict['title'] = "Fedora Project Contributor" + infodict['email'] = "%s@fedoraproject.org" % username + infodict['phone'] = "(919) 424-0063 x 5%s" % userinfo['id'] + infodict['url'] = 'fedoraproject.org' + infodict['gpgid'] = userinfo['gpg_keyid'] + return infodict diff --git a/templates/back-northamerica.svg b/templates/back-northamerica.svg new file mode 100644 index 0000000..246b883 --- /dev/null +++ b/templates/back-northamerica.svg @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/templates/back.svg b/templates/back.svg deleted file mode 100644 index 246b883..0000000 --- a/templates/back.svg +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/templates/bleed16-under.svg b/templates/bleed16-under.svg deleted file mode 100644 index c29ec21..0000000 --- a/templates/bleed16-under.svg +++ /dev/null @@ -1,102 +0,0 @@ - - - - - - - image/svg+xml - - - - - - - - - - - - - - - - - - diff --git a/templates/front-northamerica.svg b/templates/front-northamerica.svg new file mode 100644 index 0000000..c2a7a98 --- /dev/null +++ b/templates/front-northamerica.svg @@ -0,0 +1,230 @@ + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + freedom | friends | features | first + + + + + + + + + + + + + + + + + + + + + + diff --git a/templates/front-template.svg b/templates/front-template.svg deleted file mode 100644 index c2a7a98..0000000 --- a/templates/front-template.svg +++ /dev/null @@ -1,230 +0,0 @@ - - - - - - - image/svg+xml - - - - - - - - - - - - - - - - - - - - - - - freedom | friends | features | first - - - - - - - - - - - - - - - - - - - - - - -- cgit