Browse Source

Interface manager seems to be working; need some interface working now

master
Julio Biason 16 years ago
parent
commit
35ed830de6
  1. 22
      mitter
  2. 12
      mitterlib/__init__.py
  3. 4
      mitterlib/network/__init__.py
  4. 131
      mitterlib/ui/__init__.py

22
mitter

@ -53,21 +53,20 @@ def main():
metavar='INTERFACE', metavar='INTERFACE',
help='Interface to be used.') help='Interface to be used.')
# Ask networks to add their options logging.basicConfig(level=logging.DEBUG)
# Start the network manager (which will add the network options)
connection = Networks(options) connection = Networks(options)
# Ask interfaces to add their options # Start the interface manager and make interfaces add their options to the
# command line
interfaces = Interfaces() interfaces = Interfaces(options)
interfaces.options(options) interfaces.options()
# Parse the command line options and the config file # Parse the command line options and the config file
options() options()
# start the logging service # start the logging service
if options['General']['debug']: if options['General']['debug']:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
else: else:
@ -87,14 +86,13 @@ def main():
preferred_interface = options.conflicts['interface'] preferred_interface = options.conflicts['interface']
log.debug('Command line interface: %s', preferred_interface) log.debug('Command line interface: %s', preferred_interface)
interface = interfaces(preferred_interface) # load the module with the selected interface (or pick one for the user if
# they don't chose anything.)
if interface is None: display = interface.load(connection, preferred_interface)
if display is None:
log.error('Sorry, no interface could be found for your system') log.error('Sorry, no interface could be found for your system')
return return
display = interface.Interface(connection, options)
# display the interface (the interface should take care of updating # display the interface (the interface should take care of updating
# itself) # itself)
display() display()

12
mitterlib/__init__.py

@ -57,16 +57,14 @@ def module_search(source, ignore):
base_dir = os.path.dirname(source) base_dir = os.path.dirname(source)
modules = glob.glob(os.path.join(base_dir, '*.py')) modules = glob.glob(os.path.join(base_dir, '*.py'))
# TODO: What if they only have the installed/compiled modules (*.pyc)?
# Shouldn't we add those to the glob list?
for module in modules: for module in modules:
module_name = os.path.basename(module) module_name = os.path.basename(module)
if module_name in ignore: if module_name in ignore:
# not a usable module # not a usable module
continue continue
try: # TODO: Maybe this is a good place to test if the module is
module_name = _import_name(module_name) # "importable"
yield module_name yield module_name
except ImportError, exc:
_log.debug('Module %s is not "importable"', module_name)
_log.debug(str(exc))
pass

4
mitterlib/network/__init__.py

@ -18,6 +18,7 @@
# 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
from mitterlib.network.networkbase import NetworkData from mitterlib.network.networkbase import NetworkData
from mitterlib import module_search from mitterlib import module_search
@ -91,7 +92,8 @@ class Networks(object):
return self._networks return self._networks
for module_name in module_search(__file__, SKIPPABLES): for module_name in module_search(__file__, SKIPPABLES):
module = __import__(module_name, fromlist=[module_name]) import_name = _import_name(module_name)
module = __import__(import_name, fromlist=[import_name])
connection = module.Connection(self._options) connection = module.Connection(self._options)
self._networks[connection.SHORTCUT] = connection self._networks[connection.SHORTCUT] = connection

131
mitterlib/ui/__init__.py

@ -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 = [
'pygtk', _log = logging.getLogger('mitterlib.ui.Interfaces')
'cmd',
'mh', # List of files that are not networks
'tty'] SKIPPABLES = ('__init__.py')
#--------------------------------------------------------------------
def _import_name(interface): # Helper functions
"""Return the name of the module for that interface.""" #--------------------------------------------------------------------
return 'mitterlib.ui.ui_%s' % (interface)
def _import_name(module):
def _interface_list(prefer=None): """Based on the name of the module, return the proper "import"
"""Return a list of UI modules.""" statement."""
if prefer: (name, _) = os.path.splitext(module)
if prefer in interfaces: return 'mitterlib.ui.%s' % (name)
yield _import_name(prefer)
for interface in interfaces: class Interfaces(object):
module_name = _import_name(interface) """Interface transparency layer: Check which interfaces are available and
_log.debug('Module %s' % (module_name)) tries to load the interface requested by the user."""
yield module_name
def __init__(self, options):
self._interfaces = []
def interface(prefer): self._options = options
"""Try to find an interface that works in the current user system.""" self.options()
_log.debug('Preferred interface: %s' % (prefer)) return
interface = None
for module_name in _interface_list(prefer): def options(self):
# try to import each using __import__ """Request all networks to add their options."""
try: for module_name in module_search(__file__, SKIPPABLES):
_log.debug('Trying to import %s' % (module_name)) import_name = _import_name(module_name)
interface = __import__(module_name, fromlist=[module_name])
break try:
except ImportError, exc: _log.debug('Importing module %s', import_name)
_log.debug('Failed') module = __import__(import_name, fromlist=[import_name])
_log.debug(str(exc)) interface_name = module.Interface.NAMESPACE
pass module.Interface.options(self._options)
self._interfaces.append(interface_name)
return interface except ImportError, exc:
_log.debug('Cannot import module %s', import_name)
_log.debug(str(exc))
def interface_options(options):
"""Add options in the command line OptParser object for every return
interface (yes, every interface, even the ones the user doesn't care)."""
def load(self, connection, prefer=None):
available_interfaces = [] """Start the interface, using the prefered one."""
for module in _interface_list(): if not self._interfaces:
try: return None
_log.debug('Importing %s for options' % (module))
interface = __import__(module, fromlist=[module]) if prefer and prefer in self._interfaces:
import_name = prefer
interface.options(options) else:
available_interfaces.append(module.split('_')[-1]) # So we pick the one in the top of the list ('cause we know it's
except ImportError: # importable and the user didn't chose anything.)
pass # so we don't care # [we could be mean and use random.choice(self._interfaces)]
import_name = self._interfaces[0]
return
module_name = _import_name(import_name)
module = __import__(module_name, fromlist[module_name])
return module.Interface(connection, self._options)

Loading…
Cancel
Save