From b52c70d07c2ebcefc0b5ec5cb964e0ae0f9974b8 Mon Sep 17 00:00:00 2001 From: Ian Weller Date: Sun, 2 Nov 2008 11:18:22 -0600 Subject: Modularize frontend system --- fedora-business-cards | 18 +-- fedora_business_cards/frontend.py | 174 ----------------------------- fedora_business_cards/frontend/__init__.py | 0 fedora_business_cards/frontend/cmdline.py | 173 ++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+), 183 deletions(-) delete mode 100644 fedora_business_cards/frontend.py create mode 100644 fedora_business_cards/frontend/__init__.py create mode 100644 fedora_business_cards/frontend/cmdline.py diff --git a/fedora-business-cards b/fedora-business-cards index 5f7183f..a11d473 100755 --- a/fedora-business-cards +++ b/fedora-business-cards @@ -24,12 +24,12 @@ module. """ import sys -try: - from fedora_business_cards import frontend - frontend.cmdline() -except ImportError: - print >> sys.stderr, """\ -There was a problem importing the fedora_business_cards module. Please make -sure that you have fedora-business-cards installed properly. -""" - sys.exit(1) +#try: +from fedora_business_cards.frontend import cmdline +cmdline.main() +#except ImportError: +# print >> sys.stderr, """\ +#There was a problem importing the fedora_business_cards module. Please make +#sure that you have fedora-business-cards installed properly. +#""" +# sys.exit(1) diff --git a/fedora_business_cards/frontend.py b/fedora_business_cards/frontend.py deleted file mode 100644 index bb7764f..0000000 --- a/fedora_business_cards/frontend.py +++ /dev/null @@ -1,174 +0,0 @@ -### -# 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. -### - -""" -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() diff --git a/fedora_business_cards/frontend/__init__.py b/fedora_business_cards/frontend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/fedora_business_cards/frontend/cmdline.py b/fedora_business_cards/frontend/cmdline.py new file mode 100644 index 0000000..1b36998 --- /dev/null +++ b/fedora_business_cards/frontend/cmdline.py @@ -0,0 +1,173 @@ +### +# 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. +### + +""" +Command-line interface to business card generator. Takes no arguments; uses +optparser.OptionParser instead. +""" + +from optparse import OptionParser +import os +import sys +from getpass import getpass + +# local imports +from .. import config +from .. import information +from .. import generate +from .. 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 main(): + """ + Call this to make things happen. + """ + # 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() -- cgit