~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to linux/miro.real

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- mode: python -*-
 
3
 
 
4
# Miro - an RSS based video player application
 
5
# Copyright (C) 2005-2010 Participatory Culture Foundation
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
20
#
 
21
# In addition, as a special exception, the copyright holders give
 
22
# permission to link the code of portions of this program with the OpenSSL
 
23
# library.
 
24
#
 
25
# You must obey the GNU General Public License in all respects for all of
 
26
# the code used other than OpenSSL. If you modify file(s) with this
 
27
# exception, you may extend this exception to your version of the file(s),
 
28
# but you are not obligated to do so. If you do not wish to do so, delete
 
29
# this exception statement from your version. If you delete this exception
 
30
# statement from all source files in the program, then also delete it here.
 
31
 
 
32
 
 
33
# This comes first.  Seriously.
 
34
from miro.plat import xlibhelper
 
35
if xlibhelper.XInitThreads() == 0:
 
36
    print "WARNING: XInitThreads() failed!"
 
37
 
 
38
import os.path
 
39
import sys
 
40
import string
 
41
 
 
42
import optparse
 
43
 
 
44
import pygtk
 
45
pygtk.require('2.0')
 
46
 
 
47
import glib
 
48
from miro.plat import upgrade
 
49
try:
 
50
    upgrade.upgrade()
 
51
except glib.GError, ge:
 
52
    # this handles when the user tries to run Miro but gconf isn't
 
53
    # started or something along those lines.  ge has the full
 
54
    # message, so we print that out.
 
55
    print "Gconf error."
 
56
    print str(ge)
 
57
    sys.exit(1)
 
58
 
 
59
usage = "usage: %prog [options] [torrent files] [video files]"
 
60
parser = optparse.OptionParser(usage=usage)
 
61
parser.add_option('--version',
 
62
                  dest='version', action='store_true',
 
63
                  help='Print version info.')
 
64
parser.set_defaults(version=False)
 
65
 
 
66
parser.add_option('--theme',
 
67
                  dest='theme', metavar='<THEME>',
 
68
                  help='Theme to use.')
 
69
parser.add_option('--frontend',
 
70
                  dest='frontend', metavar='<FRONTEND>',
 
71
                  help='Frontend to use (widgets, cli, shell).')
 
72
parser.set_defaults(frontend="widgets")
 
73
 
 
74
group = optparse.OptionGroup(parser, "Settings options")
 
75
group.add_option('--list',
 
76
                 action="store_true",
 
77
                 dest="list_options",
 
78
                 help="Lists all preferences that can be set by command line.")
 
79
parser.set_defaults(list_options=False)
 
80
group.add_option('--set',
 
81
                 action="append",
 
82
                 dest="set_prefs",
 
83
                 metavar="<PREF>",
 
84
                 nargs=1,
 
85
                 help="Sets a preference and saves the new value for future Miro runs.  Example: miro --set renderer=gstreamer")
 
86
parser.set_defaults(set_prefs=[])
 
87
parser.add_option_group(group)
 
88
 
 
89
group = optparse.OptionGroup(parser, "Debugging options")
 
90
group.add_option('--debug',
 
91
                  action="store_true",
 
92
                  help='Starts Miro in a gdb session for better debugging.')
 
93
group.add_option('--unittest',
 
94
                  dest='unittest', action='store_true',
 
95
                  help='Run unittests instead of launching the program.')
 
96
parser.set_defaults(unittest=False)
 
97
group.add_option('--convert',
 
98
                  dest='convert', action='store_true',
 
99
                  help='Command line conversion.')
 
100
parser.set_defaults(unittest=False)
 
101
group.add_option('-v',
 
102
                  dest='unittest_verbose', action='store_true',
 
103
                  help='Run unittests in verbose mode.')
 
104
parser.set_defaults(unittest_verbose=False)
 
105
group.add_option('--profile',
 
106
                  metavar='<FILE>',
 
107
                  help='Write profiling information to FILE.')
 
108
group.add_option('--bias',
 
109
                  metavar='<BIAS>', type=float,
 
110
                  help='Set profiling bias to BIAS.')
 
111
parser.set_defaults(bias=None)
 
112
group.add_option('--sync',
 
113
                  action="store_true",
 
114
                  help='Use X synchronously.')
 
115
parser.set_defaults(sync=False)
 
116
group.add_option('--gconf-name',
 
117
                  dest='gconf_name',
 
118
                  metavar='<NAME>',
 
119
                  help='Use a different name for gconf values than "miro"')
 
120
parser.set_defaults(gconf_name='miro')
 
121
parser.add_option_group(group)
 
122
 
 
123
(parsed_options, args) = parser.parse_args()
 
124
 
 
125
# Setup gconf_name early on so we can load config values
 
126
from miro.plat import options
 
127
options.gconf_name = parsed_options.gconf_name
 
128
 
 
129
from miro.plat import utils
 
130
utils.initialize_locale()
 
131
 
 
132
from miro import gtcache
 
133
gtcache.init()
 
134
 
 
135
# This fixes some/all problems with Python 2.6 support but should be
 
136
# re-addressed when we have a system with Python 2.6 to test with.
 
137
# (bug 11262)
 
138
if sys.version_info[0] == 2 and sys.version_info[1] > 5:
 
139
    import miro.feedparser
 
140
    import miro.storedatabase
 
141
    sys.modules['feedparser'] = miro.feedparser
 
142
    sys.modules['storedatabase'] = miro.storedatabase
 
143
 
 
144
from miro import config
 
145
from miro import prefs
 
146
from miro import commandline
 
147
 
 
148
if '--unittest' in sys.argv:
 
149
    # handle this outside optparse, because unittest has its own option set
 
150
    sys.argv.remove('--unittest')
 
151
    import logging
 
152
    logging.basicConfig(level=logging.CRITICAL)
 
153
    from miro import test
 
154
    test.run_tests()
 
155
    sys.exit(0)
 
156
    
 
157
if '--convert' in sys.argv:
 
158
    sys.argv.remove('--convert')
 
159
    from miro.plat import clconverter
 
160
    clconverter.convert(sys.argv[1:])
 
161
    sys.exit(0)
 
162
    
 
163
def load_frontend(globals_, locals_, frontend):
 
164
    try:
 
165
        _temp = __import__(frontend, globals_, locals_, ['application'], -1)
 
166
        return _temp.application
 
167
    except ImportError, ie:
 
168
        # this is goofy, but to quell messages that happen when
 
169
        # trying to find which package the frontend is in
 
170
        if "No module" not in str(ie):
 
171
            print "ImportError on %s: %s" % (frontend, ie)
 
172
        return None
 
173
    
 
174
def startup(props_to_set):
 
175
    frontend = parsed_options.frontend
 
176
    goodchars = string.letters + "."
 
177
    for c in frontend:
 
178
        if c not in goodchars:
 
179
            raise ValueError("Unknown frontend: %s" % parsed_options.frontend)
 
180
 
 
181
    application = None
 
182
    attempts = [
 
183
        "miro.plat.frontends.%s" % frontend,
 
184
        "miro.frontends.%s" % frontend,
 
185
        "%s" % frontend
 
186
        ]
 
187
 
 
188
    for att in attempts:
 
189
        application = load_frontend(globals(), locals(), att)
 
190
        if application is not None:
 
191
            break
 
192
    else:
 
193
        raise ValueError("Cannot load frontend: %s" % frontend)
 
194
 
 
195
    application.run_application(props_to_set, parsed_options.theme)
 
196
 
 
197
def get_keyval(keyval):
 
198
    key, val = keyval.split("=")
 
199
    if not key in options.PREFERENCES:
 
200
        print "Option '%s' does not exist." % (key)
 
201
        print "Exiting...."
 
202
        sys.exit(1)
 
203
 
 
204
    p = options.PREFERENCES[key]
 
205
 
 
206
    try:
 
207
        defaulttype = type(p.default)
 
208
        if defaulttype == unicode or defaulttype == str:
 
209
            pass
 
210
        elif defaulttype == float:
 
211
            val = float(val)
 
212
        elif defaulttype == int:
 
213
            val = int(val)
 
214
        elif defaulttype == bool:
 
215
            if val[0].lower() in ["t", "y", "1" ]:
 
216
                val = True
 
217
            else:
 
218
                val = False
 
219
    except Exception, e:
 
220
        print "Problem converting value to correct type: %s" % e
 
221
        print "Exiting...."
 
222
        sys.exit(1)
 
223
 
 
224
    return p, val
 
225
 
 
226
def startapp():
 
227
    # Create miro directories in the user's home
 
228
    support_dir = config.get(prefs.SUPPORT_DIRECTORY)
 
229
    if not os.path.exists(support_dir):
 
230
        os.makedirs(support_dir)
 
231
 
 
232
    props_to_set = []
 
233
 
 
234
    for mem in parsed_options.set_prefs:
 
235
        p, val = get_keyval(mem)
 
236
        props_to_set.append((p, val))
 
237
 
 
238
    options.shouldSyncX = parsed_options.sync
 
239
    options.frontend = parsed_options.frontend
 
240
    if parsed_options.theme:
 
241
        options.themeName = parsed_options.theme
 
242
        print 'Theme is %s' % parsed_options.theme
 
243
 
 
244
    for i in xrange(len(args)):
 
245
        if args[i].startswith('file://'):
 
246
            args[i] = args[i][len('file://'):]
 
247
 
 
248
    commandline.set_command_line_args(args)
 
249
    startup(props_to_set)
 
250
 
 
251
def print_version():
 
252
    print """
 
253
Miro (%s)
 
254
 
 
255
Miro comes with ABSOLUTELY NO WARRANTY.
 
256
 
 
257
This is free software, and you are welcome to redistribute it under certain
 
258
conditions.  See license.txt for details.
 
259
""" % config.get(prefs.APP_VERSION)
 
260
 
 
261
if parsed_options.version:
 
262
    print_version()
 
263
 
 
264
elif parsed_options.list_options:
 
265
    print_version()
 
266
 
 
267
    print "Options that can be set:"
 
268
    for p in options.PREFERENCES.values():
 
269
        print "   %s\n      %s\n" % (p.alias, p.helptext)
 
270
 
 
271
elif parsed_options.profile:
 
272
    import sys
 
273
    try:
 
274
        import cProfile as profile
 
275
        sys.modules['profile'] = profile
 
276
    except ImportError:
 
277
        import profile
 
278
    from miro import eventloop
 
279
    def main():
 
280
        startapp()
 
281
    if (parsed_options.bias):
 
282
        profile.Profile.bias = parsed_options.bias
 
283
    eventloop.profile_file = parsed_options.profile
 
284
    profile.run ('main()', parsed_options.profile)
 
285
 
 
286
else:
 
287
    try:
 
288
        import dbus
 
289
        from miro.plat import onetime
 
290
        try:
 
291
            onetime.OneTime()
 
292
            startapp()
 
293
        except dbus.NameExistsException:
 
294
            bus = dbus.SessionBus()
 
295
            proxy_obj = bus.get_object(
 
296
                'org.participatoryculture.dtv.onetime',
 
297
                '/org/participatoryculture/dtv/OneTime')
 
298
            iface = dbus.Interface(
 
299
                proxy_obj, 'org.participatoryculture.dtv.OneTimeIFace')
 
300
            for i, arg in enumerate(args):
 
301
                args[i] = arg.decode('latin1')
 
302
            dbusargs = dbus.Array(args, signature="s")
 
303
 
 
304
            try:
 
305
                iface.handle_args(dbusargs)
 
306
            except dbus.DBusException:
 
307
                startapp()
 
308
        except dbus.DBusException:
 
309
            startapp()
 
310
    except ImportError:
 
311
        startapp()