summaryrefslogtreecommitdiff
path: root/misc/pylib/robofab/interface/all/dialogs.py
blob: 8fdfe847b5726d49dcdc290b7d60cccd3a01eb20 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
    
    
    Restructured dialogs for Robofab
    
                            dialog          file dialogs
                                
*   FontLab 5.04    10.6    dialogKit       fl internal     * theoretically that should work under windows as well, untested
*   FontLab 5.1     10.6    dialogKit       fl internal
*   FontLab 5.1     10.7    raw cocao       fl internal
    Glyphs          any     vanilla         vanilla
    RoboFont        any     vanilla         vanilla
    
    This module does a fair amount of sniffing in order to guess
    which dialogs to load. Linux and Windows environments are
    underrepresented at the moment. Following the prototypes in dialogs_default.py
    it is possible (with knowledge of the platform) to extend support.
    
    The platformApplicationSupport table contains very specific
    versions with which certain apps need to work. Moving forward,
    it is likely that these versions will change and need to be updated.
    
    # this calls the new dialogs infrastructure:
    from robofab.interface.all.dialogs import Message
    
    # this calls the old original legacy dialogs infrastructure:
    from robofab.interface.all.dialogs_legacy import Message

"""



# determine platform and application
import sys, os
import platform as _platform

__verbose__ = False

platform = None
platformVersion = None
application = None
applicationVersion = None

if sys.platform in (    
        'mac',
        'darwin',
        ):
	platform = "mac"
	v = _platform.mac_ver()[0]
	platformVersion = float('.'.join(v.split('.')[:2]))
elif sys.platform in (
        'linux1',
        'linux2',       # Ubuntu = others?
        ):
    platform = "linux"
elif os.name == 'nt':
	platform = "win"
	
# determine application

try:
    # FontLab
    # alternative syntax to cheat on the FL import filtering in RF
    __import__("FL")
    application = "fontlab"
    #applicationVersion = fl.version
except ImportError:
    pass

if application is None:
    try:
        # RoboFont
        import mojo
        application = 'robofont'
        try:
            from AppKit import NSBundle
            b = NSBundle.mainBundle()
            applicationVersion = b.infoDictionary()["CFBundleVersion"]
        except ImportError:
            pass
    except ImportError:
        pass
    
if application is None:
    try:
        # Glyphs
        import GlyphsApp
        application = "glyphs"
    except ImportError:
        pass

if application is None:
    try:
        # fontforge
        # note that in some configurations, fontforge can be imported in other pythons as well
        # so the availability of the fontforge module is no garuantee that we are in fontforge.
        import fontforge
        application = "fontforge"
    except ImportError:
        pass
        
pyVersion = sys.version_info[:3]

# with that out of the way, perhaps we can have a look at where we are
# and which modules we have available. This maps any number of platform / application
# combinations so an independent list of module names. That should make it 
# possible to map multiple things to one module. 

platformApplicationSupport = [
    # 
    # switchboard for platform, application, python version -> dialog implementations
    # platform  applicatiom     python      sub module
    # |         |               |           |
    ('mac',     'fontlab',      (2,3,5),    "dialogs_fontlab_legacy1"),
    # because FontLab 5.01 and earlier on 2.3.5 can run EasyDialogs
    # |         |               |           |
    # because FontLab 5.1 on mac 10.6 should theoretically be able to run cocoa dialogs,
    # but they are very unreliable. So until we know what's going on, FL5.1 on 10.6
    # is going to have to live with DialogKit dialogs. 
    # |         |               |           |
    ('mac',     'fontlab',      None,       "dialogs_fontlab_legacy2"),
    # because FontLab 5.1 on mac, 10.7+ should run cocoa / vanilla
    # |         |               |           |
    ('mac',     None,           None,       "dialogs_mac_vanilla"),
    # perhaps nonelab scripts can run vanilla as well?
    # |         |               |           |
    ('win',     None,           None,       "dialogs_legacy"),
    # older windows stuff might be able to use the legacy dialogs
]

platformModule = None
foundPlatformModule = False
dialogs = {}

if __verbose__:
    print "robofab.interface.all __init__ - finding out where we were."
 
# do we have a support module?
for pl, app, py, platformApplicationModuleName in platformApplicationSupport:
    if __verbose__:
        print "looking at", pl, app, py, platformApplicationModuleName
    if pl is None or pl == platform:
        if app is None or app == application:
            if py is None or py == pyVersion:
                break
    if __verbose__:
        print "nope"

if __verbose__:
    print "searched for", pl, app, py, platformApplicationModuleName
    
# preload the namespace with default functions that do nothing but raise NotImplementedError
from robofab.interface.all.dialogs_default import *

# now import the module we selected. 
if platformApplicationModuleName == "dialogs_fontlab_legacy1":
    try:
        from robofab.interface.all.dialogs_fontlab_legacy1 import *
        foundPlatformModule = True
        if __verbose__:
            print "loaded robofab.interface.all.dialogs_fontlab_legacy1"
        if platform == "mac":
            from robofab.interface.mac.getFileOrFolder import GetFile, GetFileOrFolder
    except ImportError:
        print "can't import", platformApplicationModuleName

elif platformApplicationModuleName == "dialogs_fontlab_legacy2":
    try:
        from robofab.interface.all.dialogs_fontlab_legacy2 import *
        foundPlatformModule = True
        if __verbose__:
            print "loaded robofab.interface.all.dialogs_fontlab_legacy2"
        if platform == "mac":
            #   
            #
            #
            #
            #
            from robofab.interface.all.dialogs_legacy import AskString, TwoChecks, TwoFields, SelectGlyph, FindGlyph, OneList, SearchList, SelectFont, SelectGlyph
    except ImportError:
        print "can't import", platformApplicationModuleName

elif platformApplicationModuleName == "dialogs_mac_vanilla":
    try:
        from robofab.interface.all.dialogs_mac_vanilla import *
        foundPlatformModule = True
        if __verbose__:
            print "loaded robofab.interface.all.dialogs_mac_vanilla"
    except ImportError:
        print "can't import", platformApplicationModuleName

elif platformApplicationModuleName == "dialogs_legacy":
   try:
       from robofab.interface.all.dialogs_legacy import *
       foundPlatformModule = True
       if __verbose__:
           print "loaded robofab.interface.all.dialogs_legacy"
   except ImportError:
       print "can't import", platformApplicationModuleName
    

__all__ = [
    "AskString",
    "AskYesNoCancel",
    "FindGlyph",
    "GetFile",
    "GetFolder",
    "GetFileOrFolder",
    "Message",
    "OneList",
    "PutFile",
    "SearchList",
    "SelectFont",
    "SelectGlyph",
    "TwoChecks",
    "TwoFields",
    "ProgressBar",
]

    
def test():
    """ This is a test that prints the available functions and where they're imported from.
        The report can be useful for debugging.

        For instance:

        from robofab.interface.all.dialogs import test
        test()
        
        testing RoboFab Dialogs:
            python version: (2, 7, 1)
            platform: mac
            application: None
            applicationVersion: None
            platformVersion: 10.7
            looking for module: dialogs_mac_vanilla
                did we find it? True

        Available dialogs and source:
            AskString       robofab.interface.all.dialogs_mac_vanilla
            AskYesNoCancel  robofab.interface.all.dialogs_mac_vanilla
            FindGlyph       robofab.interface.all.dialogs_mac_vanilla
            GetFile         robofab.interface.all.dialogs_mac_vanilla
            GetFolder       robofab.interface.all.dialogs_mac_vanilla
            GetFileOrFolder robofab.interface.all.dialogs_mac_vanilla
            Message         robofab.interface.all.dialogs_mac_vanilla
            OneList         robofab.interface.all.dialogs_mac_vanilla
            PutFile         robofab.interface.all.dialogs_mac_vanilla
            SearchList      robofab.interface.all.dialogs_mac_vanilla
            SelectFont      robofab.interface.all.dialogs_mac_vanilla
            SelectGlyph     robofab.interface.all.dialogs_mac_vanilla
            TwoChecks       robofab.interface.all.dialogs_default
            TwoFields       robofab.interface.all.dialogs_default
            ProgressBar     robofab.interface.all.dialogs_mac_vanilla

    """

    print
    print "testing RoboFab Dialogs:"
    print "\tpython version:", pyVersion
    print "\tplatform:", platform
    print "\tapplication:", application
    print "\tapplicationVersion:", applicationVersion
    print "\tplatformVersion:", platformVersion
    print "\tlooking for module:", platformApplicationModuleName
    print "\t\tdid we find it?", foundPlatformModule
    
    print
    print "Available dialogs and source:"
    for name in __all__:
        if name in globals().keys():
            print "\t", name, "\t", globals()[name].__module__
        else:
            print "\t", name, "\t not loaded."

if __name__ == "__main__":
    test()