|
|
@ -1,8 +1,8 @@ |
|
|
|
#!/usr/bin/python |
|
|
|
#!/usr/bin/python2.5 |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
|
|
|
|
# Mitter, a Maemo client for Twitter. |
|
|
|
# Mitter, a simple client for Twitter |
|
|
|
# Copyright (C) 2007, 2008 Julio Biason |
|
|
|
# Copyright (C) 2007, 2008 The Mitter Contributors |
|
|
|
# |
|
|
|
# |
|
|
|
# This program is free software: you can redistribute it and/or modify |
|
|
|
# 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 |
|
|
|
# it under the terms of the GNU General Public License as published by |
|
|
@ -18,64 +18,67 @@ |
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
|
|
|
|
|
|
|
|
|
import logging |
|
|
|
import logging |
|
|
|
|
|
|
|
import os.path |
|
|
|
|
|
|
|
|
|
|
|
_log = logging.getLogger('ui.__init__') |
|
|
|
from mitterlib import module_search |
|
|
|
|
|
|
|
|
|
|
|
interfaces = [ |
|
|
|
_log = logging.getLogger('mitterlib.ui.Interfaces') |
|
|
|
'pygtk', |
|
|
|
|
|
|
|
'cmd', |
|
|
|
|
|
|
|
'mh', |
|
|
|
|
|
|
|
'tty'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# List of files that are not networks |
|
|
|
|
|
|
|
SKIPPABLES = ('__init__.py') |
|
|
|
|
|
|
|
|
|
|
|
def _import_name(interface): |
|
|
|
#-------------------------------------------------------------------- |
|
|
|
"""Return the name of the module for that interface.""" |
|
|
|
# Helper functions |
|
|
|
return 'mitterlib.ui.ui_%s' % (interface) |
|
|
|
#-------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _interface_list(prefer=None): |
|
|
|
def _import_name(module): |
|
|
|
"""Return a list of UI modules.""" |
|
|
|
"""Based on the name of the module, return the proper "import" |
|
|
|
if prefer: |
|
|
|
statement.""" |
|
|
|
if prefer in interfaces: |
|
|
|
(name, _) = os.path.splitext(module) |
|
|
|
yield _import_name(prefer) |
|
|
|
return 'mitterlib.ui.%s' % (name) |
|
|
|
|
|
|
|
|
|
|
|
for interface in interfaces: |
|
|
|
|
|
|
|
module_name = _import_name(interface) |
|
|
|
|
|
|
|
_log.debug('Module %s' % (module_name)) |
|
|
|
|
|
|
|
yield module_name |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Interfaces(object): |
|
|
|
|
|
|
|
"""Interface transparency layer: Check which interfaces are available and |
|
|
|
|
|
|
|
tries to load the interface requested by the user.""" |
|
|
|
|
|
|
|
|
|
|
|
def interface(prefer): |
|
|
|
def __init__(self, options): |
|
|
|
"""Try to find an interface that works in the current user system.""" |
|
|
|
self._interfaces = [] |
|
|
|
_log.debug('Preferred interface: %s' % (prefer)) |
|
|
|
self._options = options |
|
|
|
interface = None |
|
|
|
self.options() |
|
|
|
for module_name in _interface_list(prefer): |
|
|
|
return |
|
|
|
# try to import each using __import__ |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
_log.debug('Trying to import %s' % (module_name)) |
|
|
|
|
|
|
|
interface = __import__(module_name, fromlist=[module_name]) |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
except ImportError, exc: |
|
|
|
|
|
|
|
_log.debug('Failed') |
|
|
|
|
|
|
|
_log.debug(str(exc)) |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return interface |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def interface_options(options): |
|
|
|
def options(self): |
|
|
|
"""Add options in the command line OptParser object for every |
|
|
|
"""Request all networks to add their options.""" |
|
|
|
interface (yes, every interface, even the ones the user doesn't care).""" |
|
|
|
for module_name in module_search(__file__, SKIPPABLES): |
|
|
|
|
|
|
|
import_name = _import_name(module_name) |
|
|
|
|
|
|
|
|
|
|
|
available_interfaces = [] |
|
|
|
|
|
|
|
for module in _interface_list(): |
|
|
|
|
|
|
|
try: |
|
|
|
try: |
|
|
|
_log.debug('Importing %s for options' % (module)) |
|
|
|
_log.debug('Importing module %s', import_name) |
|
|
|
interface = __import__(module, fromlist=[module]) |
|
|
|
module = __import__(import_name, fromlist=[import_name]) |
|
|
|
|
|
|
|
interface_name = module.Interface.NAMESPACE |
|
|
|
interface.options(options) |
|
|
|
module.Interface.options(self._options) |
|
|
|
available_interfaces.append(module.split('_')[-1]) |
|
|
|
self._interfaces.append(interface_name) |
|
|
|
except ImportError: |
|
|
|
except ImportError, exc: |
|
|
|
pass # so we don't care |
|
|
|
_log.debug('Cannot import module %s', import_name) |
|
|
|
|
|
|
|
_log.debug(str(exc)) |
|
|
|
|
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load(self, connection, prefer=None): |
|
|
|
|
|
|
|
"""Start the interface, using the prefered one.""" |
|
|
|
|
|
|
|
if not self._interfaces: |
|
|
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if prefer and prefer in self._interfaces: |
|
|
|
|
|
|
|
import_name = prefer |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
# So we pick the one in the top of the list ('cause we know it's |
|
|
|
|
|
|
|
# importable and the user didn't chose anything.) |
|
|
|
|
|
|
|
# [we could be mean and use random.choice(self._interfaces)] |
|
|
|
|
|
|
|
import_name = self._interfaces[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module_name = _import_name(import_name) |
|
|
|
|
|
|
|
module = __import__(module_name, fromlist[module_name]) |
|
|
|
|
|
|
|
return module.Interface(connection, self._options) |
|
|
|