summaryrefslogtreecommitdiffstats
path: root/fedora_business_cards/generate.py
blob: cf2b4e16df464831b39e306a33c4407c2c1bd659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
###
# fedora-business-cards - for rendering Fedora contributor business cards
# Copyright (C) 2008  Ian Weller <ianweller@gmail.com>
#
# 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


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_loc, width=None, height=None):
    """
    Generates the front of the business card.
    """
    dom = minidom.parse(template_loc)

    namenode = find_node(dom, 'text', 'id', 'fullname')
    namenode.appendChild(dom.createTextNode(name))
    titlenode = find_node(dom, 'text', 'id', 'title')
    titlenode.appendChild(dom.createTextNode(title))
    if width or height:
        svg = find_node(dom, 'svg', 'id', 'svg')
        svg.setAttribute('width', width)
        svg.setAttribute('height', height)
        svg.setAttribute('viewBox', '0 0 '+width+' '+height)
        whiteness = find_node(dom, 'rect', 'id', 'whiteness')
        whiteness.setAttribute('height', height)
        blueband = find_node(dom, 'rect', 'id', 'blueband')
        blueband.setAttribute('height', height)
        blueband.setAttribute('width', width)
    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_loc, width=None, height=None):
    """
    Generates the back of the business card.
    """
    dom = minidom.parse(template_loc)
    if width or height:
        svg = find_node(dom, 'svg', 'id', 'svg')
        svg.setAttribute('width', width)
        svg.setAttribute('height', height)
        svg.setAttribute('viewBox', '0 0 '+width+' '+height)
        background = find_node(dom, 'rect', 'id', 'background')
        background.setAttribute('width', width)
        background.setAttribute('height', height)
    return dom.toxml()