|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +## ui_main.py |
| 4 | +## |
| 5 | +## Copyright © 2008-2010 Saeed Rasooli <[email protected]> (ilius) |
| 6 | +## This file is part of PyGlossary project, http://sourceforge.net/projects/pyglossary/ |
| 7 | +## |
| 8 | +## This program is a free software; you can redistribute it and/or modify |
| 9 | +## it under the terms of the GNU General Public License as published by |
| 10 | +## the Free Software Foundation; either version 3, or (at your option) |
| 11 | +## any later version. |
| 12 | +## |
| 13 | +## This program is distributed in the hope that it will be useful, |
| 14 | +## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +## GNU General Public License for more details. |
| 17 | +## |
| 18 | +## You should have received a copy of the GNU General Public License along |
| 19 | +## with this program. Or on Debian systems, from /usr/share/common-licenses/GPL |
| 20 | +## If not, see <http://www.gnu.org/licenses/gpl.txt>. |
| 21 | + |
| 22 | +import os, sys, getopt, __builtin__ |
| 23 | +from pyglossary.glossary import confPath, VERSION |
| 24 | +#from pyglossary.text_utils import printAsError ## No red color, plain |
| 25 | +from os.path import dirname, join |
| 26 | + |
| 27 | +try: |
| 28 | + import ui |
| 29 | +except ImportError: |
| 30 | + sys.path.append(join(dirname(dirname(__file__)), 'share', 'pyglossary', 'ui')) |
| 31 | + |
| 32 | +from ui_cmd import COMMAND, printAsError, help, parseFormatOptionsStr |
| 33 | + |
| 34 | + |
| 35 | +def dashToCamelCase(text):## converts "hello-PYTHON-user" to "helloPythonUser" |
| 36 | + parts = text.split('-') |
| 37 | + parts[0] = parts[0].lower() |
| 38 | + for i in range(1, len(parts)): |
| 39 | + parts[i] = parts[i].capitalize() |
| 40 | + return ''.join(parts) |
| 41 | + |
| 42 | +use_psyco_file = '%s_use_psyco'%confPath |
| 43 | +psyco_found = None |
| 44 | + |
| 45 | + |
| 46 | +ui_list = ('gtk', 'tk', 'qt') |
| 47 | + |
| 48 | +#print('PyGlossary %s'%VERSION) |
| 49 | + |
| 50 | +if os.path.isfile(use_psyco_file): |
| 51 | + try: |
| 52 | + import psyco |
| 53 | + except ImportError: |
| 54 | + print('Warning: module "psyco" not found. It could speed up execution.') |
| 55 | + psyco_found = False |
| 56 | + else: |
| 57 | + psyco.full() |
| 58 | + print('Using module "psyco" to speed up execution.') |
| 59 | + psyco_found = True |
| 60 | + |
| 61 | +available_options = [ |
| 62 | + 'version', |
| 63 | + 'help', |
| 64 | + 'ui=', |
| 65 | + 'read-options=', |
| 66 | + 'write-options=', |
| 67 | + 'read-format=', |
| 68 | + 'write-format=', |
| 69 | + 'reverse', |
| 70 | + 'no-progress-bar', |
| 71 | +] |
| 72 | + |
| 73 | +## no-progress-bar only for command line UI |
| 74 | +## FIXME: load ui-dependent available options from ui modules (for example ui_cmd.available_options) |
| 75 | +## the only problem is that it has to "import gtk" before it get the "ui_gtk.available_options" |
| 76 | + |
| 77 | +try: |
| 78 | + (options, arguments) = getopt.gnu_getopt( |
| 79 | + sys.argv[1:], |
| 80 | + 'vhu:r:w:', |
| 81 | + available_options, |
| 82 | + ) |
| 83 | +except getopt.GetoptError: |
| 84 | + printAsError(sys.exc_info()[1]) |
| 85 | + print 'try: %s --help'%COMMAND |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | +""" |
| 89 | +ui_type: User interface type |
| 90 | +Possible values: |
| 91 | + cmd - Command line interface, this ui will automatically selected if you give both input and output file |
| 92 | + gtk - GTK interface |
| 93 | + tk - Tkinter interface |
| 94 | + qt - Qt interface |
| 95 | + auto - Use the first available UI |
| 96 | +""" |
| 97 | + |
| 98 | +ui_type = 'auto' |
| 99 | + |
| 100 | +if len(arguments)<1:## open GUI |
| 101 | + ipath = opath = '' |
| 102 | +elif len(arguments)==1:## open GUI, in edit mode (if gui support, like DB Editor in ui_gtk) |
| 103 | + ipath = arguments[0] |
| 104 | + opath = '' |
| 105 | +else:## run the commnad line interface |
| 106 | + ui_type = 'cmd' |
| 107 | + ipath = arguments[0] |
| 108 | + opath = arguments[1] |
| 109 | + |
| 110 | + |
| 111 | +read_format = '' ## only used in ui_cmd for now |
| 112 | +write_format = '' ## only used in ui_cmd for now |
| 113 | +read_options = {} ## only used in ui_cmd for now |
| 114 | +write_options = {} ## only used in ui_cmd for now |
| 115 | +reverse = False ## only used in ui_cmd for now |
| 116 | +ui_options = {} |
| 117 | + |
| 118 | + |
| 119 | +''' |
| 120 | + examples for read and write options: |
| 121 | + --read-options testOption=stringValue |
| 122 | + --read-options enableFoo=True |
| 123 | + --read-options fooList=[1,2,3] |
| 124 | + --read-options 'fooList=[1, 2, 3]' |
| 125 | + --read-options 'testOption=stringValue; enableFoo=True; fooList=[1, 2, 3]' |
| 126 | + --read-options 'testOption=stringValue;enableFoo=True;fooList=[1,2,3]' |
| 127 | +''' |
| 128 | + |
| 129 | + |
| 130 | +for (opt, opt_arg) in options: |
| 131 | + if opt in ('-v', '--version'): |
| 132 | + print('PyGlossary %s'%VERSION) |
| 133 | + sys.exit(0) |
| 134 | + elif opt in ('-h', '--help'): |
| 135 | + help() |
| 136 | + sys.exit(0) |
| 137 | + elif opt in ('-u', '--ui'): |
| 138 | + if opt_arg in ui_list: |
| 139 | + ui_type = opt_arg |
| 140 | + else: |
| 141 | + printAsError('invalid ui type %s'%opt_arg) |
| 142 | + elif opt in ('-r', '--read-options'): |
| 143 | + read_options = parseFormatOptionsStr(opt_arg) |
| 144 | + elif opt in ('-w', '--write-options'): |
| 145 | + write_options = parseFormatOptionsStr(opt_arg) |
| 146 | + elif opt == '--read-format': |
| 147 | + read_format = opt_arg |
| 148 | + elif opt == '--write-format': |
| 149 | + write_format = opt_arg |
| 150 | + elif opt == '--reverse': |
| 151 | + reverse = True |
| 152 | + elif opt.startswith('--'): |
| 153 | + ui_options[dashToCamelCase(opt[2:])] = opt_arg ## opt_arg is not None, UI just ignores None value |
| 154 | + |
| 155 | + |
| 156 | +## FIXME |
| 157 | +## -v (verbose or version?) |
| 158 | +## -r (reverse or read-options) |
| 159 | + |
| 160 | + |
| 161 | +if ui_type == 'cmd': |
| 162 | + import ui_cmd |
| 163 | + sys.exit(ui_cmd.UI(**ui_options).run( |
| 164 | + ipath, |
| 165 | + opath=opath, |
| 166 | + read_format=read_format, |
| 167 | + write_format=write_format, |
| 168 | + read_options=read_options, |
| 169 | + write_options=write_options, |
| 170 | + reverse=reverse, |
| 171 | + )) |
| 172 | +else: |
| 173 | + if ui_type=='auto': |
| 174 | + ui_module = None |
| 175 | + for ui_type2 in ui_list: |
| 176 | + try: |
| 177 | + ui_module = getattr(__import__('ui.ui_%s'%ui_type2), 'ui_%s'%ui_type2) |
| 178 | + except ImportError: |
| 179 | + pass |
| 180 | + else: |
| 181 | + break |
| 182 | + if ui_module==None: |
| 183 | + printAsError('no user interface module found!') |
| 184 | + sys.exit(1) |
| 185 | + else: |
| 186 | + ui_module = getattr(__import__('ui.ui_%s'%ui_type), 'ui_%s'%ui_type) |
| 187 | + sys.exit(ui_module.UI(ipath, **ui_options).run()) |
| 188 | + ## don't forget to append "**options" at every UI.__init__ arguments |
| 189 | + |
0 commit comments