~dsoulayrol/duo/dreadful_ui

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python
#
# Duo, A crazy eights game.
# Copyright (C) 2003-2008 The Duo developers.
#
# This file is part of Duo.
#
# Duo 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""
[TODO: write this]
"""

import getopt
import locale
import logging
import sys

# Install internationalization, once for all (defines _() ).
import gettext
gettext.install('duo', 'locale')


import duo

from duo import config
from duo import ui


# Textual data
NOTICE = 'Duo version ' + config.VERSION + """
Copyright (C) 2004,2008 The Duo developpers.
Duo comes with ABSOLUTELY NO WARRANTY; for details type `duo -w'.
This is free software, and you are welcome to redistribute it under
certain conditions; see the COPYING file.
"""

WARRANTY = """  NO WARRANTY

  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.

  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
"""


# Generic command line options.
# Game options are available in ui package.
OPT_UI = 'ui'
OPT_HELP = 'help'
OPT_VERBOSE = 'verbose'
OPT_QUIET = 'quiet'
OPT_CONDITIONS = 'conditions'
OPT_WARRANTY = 'warranty'


def print_usage():
    """Prints a short notice about Duo usage.
    """
    print NOTICE
    print 'Usage:\tduo [-q] [-v] [--players nb] [--cards nb] [--name nm]'
    print '\tduo -h|--help | -c|--conditions | -w|--warranty'


def print_help():
    """Prints a detailed notice about Duo usage.
    """
    print_usage()
    print ''
    print '  -h, --' + OPT_HELP + '\t\tthis screen.'
    print '  -u, --' + OPT_UI + '\t\tthe user interface selection.'
    print '  -v, --' + OPT_VERBOSE + '\t\tincrease output on console'
    print '  -q, --' + OPT_QUIET + '\t\tlimits console output to warnings and errors'
    print '  -c, --' + OPT_CONDITIONS + '\tdisplay the licence conditions'
    print '  -w, --' + OPT_WARRANTY + '\tdisplay the licence warranties'
    print '\nGame options:'
    print '      --' + ui.OPT_GAME_PLAYER_NAME + '\t\tthe name of the player'
    print '      --' + ui.OPT_GAME_TRANSPORT + '\tthe game transport layer to use'
    print '      --' + ui.OPT_GAME_NB_PLAYERS + '\t\tthe number of players'
    print '      --' + ui.OPT_GAME_NB_CARDS + '\t\tthe number of cards'
    print '      --' + ui.OPT_GAME_START + '\t\timmediately start to play'


def print_gpl_conditions():
    """Prints the GPL license.
    """
    print "[TODO] conditions..."


def print_gpl_warranty():
    """Prints the warranty section of the GPL license.
    """
    print WARRANTY


def read_command_line():
    """Analyse command line.
    """
    # In the future, packages should be called to complete the list of
    # options they propose.
    OPT_LIST = 'u:hvqcw'
    LONG_OPT_LIST = [OPT_UI + '=',
                     OPT_HELP,
                     OPT_VERBOSE, OPT_QUIET,
                     OPT_CONDITIONS, OPT_WARRANTY,

                     ui.OPT_GAME_PLAYER_NAME + '=',
                     ui.OPT_GAME_TRANSPORT + '=',
                     ui.OPT_GAME_NB_PLAYERS + '=',
                     ui.OPT_GAME_NB_CARDS + '=',
                     ui.OPT_GAME_START]

    # Default options
    options = {
        OPT_UI: 'tk',
        OPT_VERBOSE: logging.WARNING,
    }

    try:
        opt_list, args = getopt.getopt(
            sys.argv[1:], OPT_LIST, LONG_OPT_LIST)
        if args:
            print_usage()
            print 'Unknown argument:', args
            sys.exit()

    except getopt.GetoptError, err:
        print_usage()
        print err
        sys.exit()

    # In depth analyse
    for opt_name, opt_arg in opt_list:
        if opt_name in ('-h', '--' + OPT_HELP):
            print_help()
            sys.exit()

        if opt_name in ('-c', '--' + OPT_CONDITIONS):
            print_gpl_conditions()
            sys.exit()

        if opt_name in ('-w', '--' + OPT_WARRANTY):
            print_gpl_warranty()
            sys.exit()

        if opt_name in ('-u', '--' + OPT_UI):
            options[OPT_UI] = opt_arg

        if opt_name == ('--' + ui.OPT_GAME_PLAYER_NAME):
            options[ui.OPT_GAME_PLAYER_NAME] = opt_arg

        if opt_name == ('--' + ui.OPT_GAME_TRANSPORT):
            options[ui.OPT_GAME_TRANSPORT] = opt_arg

        if opt_name == ('--' + ui.OPT_GAME_NB_PLAYERS):
            options[ui.OPT_GAME_NB_PLAYERS] = int(opt_arg)

        if opt_name == ('--' + ui.OPT_GAME_NB_CARDS):
            options[ui.OPT_GAME_NB_CARDS] = int(opt_arg)

        if opt_name == ('--' + ui.OPT_GAME_START):
            options[ui.OPT_GAME_START] = True

        if opt_name in ('-v', '--' + OPT_VERBOSE):
            if options[OPT_VERBOSE] != 0:
                options[OPT_VERBOSE] = logging.DEBUG
            else:
                print 'verbose option conflicts with quiet.'
                sys.exit()

        if opt_name in ('-q', '--' + OPT_QUIET):
            if options[OPT_VERBOSE] != logging.DEBUG:
                options[OPT_VERBOSE] = 0
            else:
                print 'verbose option conflicts with quiet.'
                sys.exit()

    # Configure all future instances of Logger
    handler = logging.FileHandler('duo.log', 'w')
    formatter = logging.Formatter(
        '%(asctime)s %(name)-25s:%(lineno) 4d\t%(levelname)s\t%(message)s')

    handler.setFormatter(formatter)

    logger = logging.getLogger('duo')
    logger.addHandler(handler)
    logger.setLevel(options[OPT_VERBOSE])

    # Return behaviour
    return options




# Main
try:
    options = read_command_line()

    # Set default language.
    (lang, encoding) = locale.getdefaultlocale()
    if not lang is None:
        duo.switch_to_language(lang)

    ui = ui.create_ui(options[OPT_UI], options)
    ui.launch()

except KeyboardInterrupt, e:
    print e
except Exception, e:
    print e