File: __init__.py

package info (click to toggle)
pyferret 7.6.5-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 138,136 kB
  • sloc: fortran: 240,609; ansic: 25,235; python: 24,026; sh: 1,618; makefile: 1,123; pascal: 569; csh: 307; awk: 18
file content (116 lines) | stat: -rw-r--r-- 4,166 bytes parent folder | download | duplicates (5)
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
'''
Maintains a dictionary of classes which can be used to bind PyFerret
graphics calls to a particular graphics engine.

When a new Window is needed, the createWindow function in this module
is called.  This function creates an instance of the appropriate
bindings class, its createWindow method is called, and the bindings 
instance is returned.

Other methods of the bindings instance are called directly to perform
graphics operations on this Window.
'''

from __future__ import print_function

from pyferret.graphbind.abstractpyferretbindings import AbstractPyFerretBindings

__pyferret_bindings_classes = { }

def addPyFerretBindings(engine_name, bindings_class):
    '''
    Adds the given class to the dictionary of available bindings.

    Arguments:
        engine_name: a string identifying the bindings class
                and graphics engine
        bindings_class: a subclass of AbstractPyFerretBindings
                providing the bindings for a graphics engine

    Raises:
        ValueError if bindings for engine_name already exist
        TypeError if bindings_class is not a subclass of
                AbstractPyFerretBindings
    '''
    try:
        _ = __pyferret_bindings_classes[engine_name]
        raise ValueError("Bindings already exist for graphics engine '%s'" \
                         % engine_name)
    except KeyError:
        pass
    if not issubclass(bindings_class, AbstractPyFerretBindings):
        raise TypeError("bindings_class argument is not a " \
                        "subclass of AbstractPyFerretBindings")
    __pyferret_bindings_classes[engine_name] = bindings_class

def knownPyFerretEngines():
    '''
    Returns a tuple of all the known engine names.
    '''
    return tuple( __pyferret_bindings_classes.keys() )
    
def createWindow(engine_name, title, visible, noalpha, rasteronly):
    '''
    Creates an instance of the bindings class associated with
    engine_name and calls the createWindow method of that
    instance of the class.  The instance of the bindings class
    is then returned.

    Arguments:
        engine_name: string identifying the bindings class and
                     graphics engine to use for the Window
        title: display title for the Window
        visible: display Window on start-up?
        noalpha: do not use the alpha channel in colors?
        rasteronly: only raster images will be used ?

    Returns the instance of the binding class associated with
    the newly created Window if the createWindow method of the
    instance returns True.  Otherwise the bindings instance is
    deleted and None is returned.

    Raises a ValueError if engine_name does not have any bindings.
    '''
    try:
        bindclass = __pyferret_bindings_classes[engine_name]
    except KeyError:
        raise ValueError("Unknown graphics engine '%s'" % engine_name)
    bindinst = bindclass()
    if not bindinst.createWindow(title, visible, noalpha, rasteronly):
        del bindinst
        return None
    return bindinst


if __name__ == "__main__":

    class TestBindings(AbstractPyFerretBindings):
        engine_name = "TestEngine"

        def __init__(self):
            super(TestBindings, self).__init__()

        def createWindow(self, title, visible, noalpha):
            return True

    addPyFerretBindings(TestBindings.engine_name, TestBindings)
    bindinst = createWindow(TestBindings.engine_name, "test", False, False, False)
    if not bindinst:
        raise RuntimeError("Unsuccessful creation of a Window")
    try:
        addPyFerretBindings(TestBindings.engine_name, TestBindings)
        raise RuntimeError("Adding bindings with the same name succeeded")
    except ValueError:
        pass
    try:
        addPyFerretBindings("GenericObject", object)
        raise RuntimeError("Adding object for a bindings class succeeded")
    except TypeError:
        pass
    known_engines = knownPyFerretEngines()
    if (len(known_engines) != 1) or \
       (known_engines[0] != TestBindings.engine_name):
        raise RuntimeError("Unexpected tuple of known engines: %s" % \
                           str(known_engines))
    print("Success")