File: __init__.py

package info (click to toggle)
python-enable 3.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 10,392 kB
  • ctags: 17,135
  • sloc: cpp: 79,151; python: 29,601; makefile: 2,926; sh: 43
file content (122 lines) | stat: -rw-r--r-- 4,037 bytes parent folder | download
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
"""
A multi-platform DisplayPDF vector drawing engine. 
Part of the Enable project of the Enthought Tool Suite.

This file pulls in the Kiva constants and does the detection of what backend
to use via the KIVA_WISHLIST environment variable.
"""

#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# some parts copyright 2002 by Space Telescope Science Institute
# All rights reserved.
# 
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#------------------------------------------------------------------------------

import os

from enthought.etsconfig.api import ETSConfig

from constants import *
from fonttools import Font   # relative import; not the fonttools project!

# The code is borrowed almost exactly from the AnyGui __init__.py
# code.  Nice code.    

# This module is really just a shell around the various core2d backends.  It 
# loops through a list of possible backends until it finds on that is 
# available.  You can override the standard list of backends by setting the 
# environment variable KIVA_WISHLIST before importing.  The environment
# variable is a space separated list of backends.  The first available
# one in the list is chosen.


_kiva_only_backends = 'gl image pdf svg ps cairo'
_backends = 'wx qt4 ' + _kiva_only_backends

_backend = None
DEBUG = 1
wishlist = os.environ.get('KIVA_WISHLIST', _backends).split()

# Symbols we import from the selected backend
backend_symbols = ('GraphicsContext', 'Canvas', 'CompiledPath',
    'font_metrics_provider')

def _dotted_import(name):
    # version of __import__ which handles dotted names
    # copied from python docs for __import__
    import string
    mod = __import__(name, globals(), locals(), [])
    components = string.split(name, '.')
    for comp in components[1:]:
        mod = getattr(mod, comp)
    return mod


def _try_backend(name):
    if name == 'null':
        # If we are given a 'null' UI toolkit, just use the image backend.
        name = 'image'
    try:
        mod = _dotted_import('backend_%s' % name)
        for key in backend_symbols:
            globals()[key] = mod.__dict__[key]
    except (ImportError, AttributeError, KeyError):
        if DEBUG and not (str(DEBUG) in _backends and not DEBUG==name):
            import traceback
            traceback.print_exc()
        return False
    else:
        global _backend
        _backend = name
        return True


def _backend_passthrough():
    global _backends

    # Remove from the list all known backends that are in the wishlist.
    _backends = _backends.split()
    _backends = [b for b in _backends if not b in wishlist]

    if wishlist:
        # Replace a '*' in the wishlist with any known backends that aren't
        # already there.
        try:
            idx = wishlist.index('*')
            wishlist[idx:idx+1] = _backends
        except ValueError: pass
        _backends = wishlist

    for name in _backends:
        if _try_backend(name):
            return

    raise RuntimeError, "no usable backend found"


def backend():
    'Return the name of the current backend'
    if not _backend:
        raise RuntimeError('no backend exists')
    return _backend


# See if the toolkit has already been selected and the user hasn't explicitly
# set their KIVA_WISHLIST environment variable
if ETSConfig.toolkit:
    if "KIVA_WISHLIST" in os.environ:
        _backend_passthrough()
    elif not _try_backend(ETSConfig.toolkit):
        raise RuntimeError("no kiva backend for %s" % ETSConfig.toolkit)
else:
    _backend_passthrough()

    # Impose the kiva selection unless it is a kiva only backend.
    if _backend not in _kiva_only_backends.split():
        ETSConfig.toolkit = _backend