summaryrefslogtreecommitdiffstats
path: root/fedora_business_cards/frontend.py
blob: bb7764f1fc0c14a05d45725ee9f82cbefb575b4a (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
###
# 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.
###

"""
Frontend to the business card generator. Theoretically expandable to GUI and
whatnot, but for now just has a command-line interface.
"""

from optparse import OptionParser
import os
import sys
from getpass import getpass

# local imports
import config
import information
import generate
import export


def cmdline_card_line(data):
    """
    Print a line of the business card for the cmdline frontend.
    """
    return "| %s%s |" % (data, ' '*(59-len(data)))


def cmdline():
    """
    Command-line interface to business card generator. Takes no arguments; uses
    optparser.OptionParser instead.
    """
    # setup option parser
    parser = OptionParser()
    parser.usage = "%prog [options]"
    parser.add_option("-d", "--dpi", dest="dpi", default=300, type="int",
                      help="DPI of exported file")
    parser.add_option("-t", "--template", dest="template",
                      default="northamerica", help="Name of template to use,"+\
                      " run with --list-templates to see a list")
    parser.add_option("--list-templates", action="store_true", default=False,
                      dest="listtemplates", help="List available templates")
    parser.add_option("-u", "--username", dest="username", default="",
                      help="If set, use a different name than the one logged"+\
                      " in with to fill out business card information")
    parser.add_option("--pdf", dest="output", default="png", const="pdf",
                      action="store_const", help="Export as PDF")
    parser.add_option("--png", dest="output", default="png", const="png",
                      action="store_const", help="Export as PNG (default)")
    parser.add_option("--svg", dest="output", default="png", const="svg",
                      action="store_const", help="Export as SVG")
    parser.add_option("-c", "--config", dest="config_location", default="",
                      help="Location of config.ini configuration file")
    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)
    if options.listtemplates:
        print "Available templates:"
        for i in available_templates:
            print "  %s" % i
        sys.exit(0)
    if options.template not in available_templates:
        print "%s not an available template" % options.template
        sys.exit(1)
    # ask for FAS login
    print "Login to FAS:"
    print "Username:",
    username = raw_input()
    password = getpass()
    if options.username == "":
        options.username = username
    infodict = information.get_information(username, password,
                                           options.username)
    # setup default content
    name = infodict['name']
    title = infodict['title']
    if infodict['gpgid'] == None:
        gpg = ''
    else:
        gpg = "GPG key ID: %s" % infodict['gpgid']
    if infodict['irc'] == None:
        lines = [infodict['email'],
                 infodict['phone'],
                 infodict['url'],
                 '',
                 gpg,
                 '']
    else:
        lines = [infodict['email'],
                 infodict['phone'],
                 infodict['irc']+" on irc.freenode.net",
                 infodict['url'],
                 '',
                 "GPG key ID: "+infodict['gpgid']]
    done_editing = False
    while not done_editing:
        print "Current business card layout:"
        print "   +"+"-"*61+"+"
        print " n "+cmdline_card_line(name)
        print " t "+cmdline_card_line(title)
        print "   "+cmdline_card_line('')
        for i in range(6):
            print (" %i " % i)+cmdline_card_line(lines[i])
        print "   "+cmdline_card_line('')
        print "   "+cmdline_card_line('')
        print "   "+cmdline_card_line('fedora'+' '*17+\
                                      'freedom | friends | features | first')
        print "   +"+"-"*61+"+"
        print "Enter a line number to edit, or [y] to accept:",
        lineno = raw_input()
        if lineno == "" or lineno == "y":
            done_editing = True
        else:
            print ("Enter new data for line %s:" % lineno),
            newdata = raw_input()
            if lineno == 'n':
                name = newdata
            elif lineno == 't':
                title = newdata
            elif lineno == '0' or lineno == '1' or lineno == '2' or \
                    lineno == '3' or lineno == '4' or lineno == '5':
                lines[int(lineno)] = newdata
    # generate front of business card
    print "Generating front...",
    sys.stdout.flush()
    xml = generate.gen_front(name, title, lines, options.template)
    if options.output == "svg":
        export.svg_to_file(xml, options.username+'-front.'+options.output)
    else:
        export.svg_to_pdf_png(xml, options.username+'-front.'+options.output,
                              options.output, options.dpi)
    # generate back of business card
    print "Generating back...",
    sys.stdout.flush()
    xml = generate.gen_back(options.template)
    if options.output == "svg":
        export.svg_to_file(xml, options.username+'-back.'+options.output)
    else:
        export.svg_to_pdf_png(xml, options.username+'-back.'+options.output,
                              options.output, options.dpi)
    print "Done."
    sys.stdout.flush()