~facundo/encuentro/trunk

« back to all changes in this revision

Viewing changes to encuentro/__init__.py

  • Committer: facundo at com
  • Date: 2011-12-10 14:51:18 UTC
  • mfrom: (63.1.1 better-error-info)
  • Revision ID: facundo@taniquetil.com.ar-20111210145118-26p1pzx6tyy3cd29
Better info on import error and success.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
"""The package."""
22
22
 
23
 
 
24
 
def import_exit(package, version):
25
 
    """Show a nice explanation of which dependency to install and quit."""
26
 
    print
27
 
    print u"¡Falta instalar una dependencia!"
28
 
    print u"Necesitás tener instalado el paquete %r" % (package,)
29
 
    print u"(de la versión %s en adelante funciona seguro)" % (version,)
30
 
    print
31
 
    exit()
 
23
import sys
 
24
 
 
25
IMPORT_MSG = u"""
 
26
ERROR! Problema al importar %(module)r
 
27
 
 
28
Probablemente falte instalar una dependencia.  Se necesita tener instalado
 
29
el paquete %(package)r versión %(version)s o superior.
 
30
"""
 
31
 
 
32
class NiceImporter(object):
 
33
    """Show nicely successful and errored imports."""
 
34
    def __init__(self, module, package, version):
 
35
        self.module = module
 
36
        self.package = package
 
37
        self.version = version
 
38
 
 
39
    def __enter__(self):
 
40
        pass
 
41
 
 
42
    def _get_version(self):
 
43
        """Get the version of a module."""
 
44
        mod = sys.modules[self.module]
 
45
        for attr in ('version', '__version__', 'ver'):
 
46
            v = getattr(mod, attr, None)
 
47
            if v is not None:
 
48
                return v
 
49
        return "<desconocida>"
 
50
 
 
51
    def __exit__(self, exc_type, exc_value, traceback):
 
52
        if exc_type is None:
 
53
            version = self._get_version()
 
54
            print "Modulo %r importado ok, version %r" % (self.module, version)
 
55
        else:
 
56
            print IMPORT_MSG % dict(module=self.module, package=self.package,
 
57
                                    version=self.version)
 
58
 
32
59
 
33
60
# test some packages! gtk and twisted are controlled in main.py, as they
34
61
# import order is critical because of the reactor
35
62
# pylint: disable=W0611
36
63
 
37
 
try:
 
64
with NiceImporter('xdg', 'python-xdg', '0.15'):
38
65
    import xdg
39
 
except ImportError:
40
 
    import_exit('python-xdg', '0.15')
41
 
try:
 
66
with NiceImporter('mechanize', 'python-mechanize', '0.1.11'):
42
67
    import mechanize
43
 
except ImportError:
44
 
    import_exit('python-mechanize', '0.1.11')
 
68
 
 
69
 
45
70
 
46
71
from encuentro.main import MainUI as EncuentroUI