A series of edits aimed at making dialogs.py behave better in non-mac, non-windows, non-fontlab environments. Tests are added to shed some light on where dialogs.py thinks it is running.

git-svn-id: http://svn.robofab.com/trunk@240 b5fa9d6c-a76f-4ffd-b3cb-f825fc41095c
This commit is contained in:
Erik van Blokland 2011-05-07 12:52:50 +00:00
parent 532007f86e
commit 7fe5ef02f3

View File

@ -1,20 +1,18 @@
""" """
Dialogs. Not just for FontLab any more. Dialogs. Not just for FontLab any more.
Cross-platform and cross-application compatible. Mostly anyway. Cross-platform and cross-application compatible. Some of them anyway.
(Not all dialogs work on PCs outside of FontLab. Some dialogs are for FontLab only. Sorry.) (Not all dialogs work on PCs outside of FontLab. Some dialogs are for FontLab only. Sorry.)
Mac and FontLab implementation written by the RoboFab development team. Mac and FontLab implementation written by the RoboFab development team.
PC implementation by Eigi Eigendorf and is (C)2002 Eigi Eigendorf. PC implementation by Eigi Eigendorf and is (C)2002 Eigi Eigendorf.
""" """
#to add:
#W version of TwoFields
import os import os
import sys import sys
#from robofab import RoboFabError from robofab import RoboFabError
RoboFabError = "RoboFabError"
MAC = False MAC = False
PC = False PC = False
@ -25,7 +23,7 @@ if sys.platform in ('mac', 'darwin'):
elif os.name == 'nt': elif os.name == 'nt':
PC = True PC = True
else: else:
raise RoboFabError, "dialogs.py only supports Mac and PC platforms." raise RoboFabError("dialogs.py only supports Mac and PC platforms.")
pyVersion = sys.version_info[:3] pyVersion = sys.version_info[:3]
inFontLab = False inFontLab = False
@ -34,6 +32,25 @@ try:
inFontLab = True inFontLab = True
except ImportError: pass except ImportError: pass
try:
import W
hasW = True
except ImportError:
hasW = False
try:
import dialogKit
hasDialogKit = True
except ImportError:
hasDialogKit = False
try:
import EasyDialogs
hasEasyDialogs = True
except:
hasEasyDialogs = False
if MAC: if MAC:
if pyVersion < (2, 3, 0): if pyVersion < (2, 3, 0):
import macfs import macfs
@ -50,7 +67,9 @@ def _raisePlatformError(dialog):
p = 'Macintosh' p = 'Macintosh'
elif PC: elif PC:
p = 'PC' p = 'PC'
raise RoboFabError, "%s is not currently available on the %s platform"%(dialog, p) else:
p = sys.platform
raise RoboFabError("%s is not currently available on the %s platform"%(dialog, p))
class _FontLabDialogOneList: class _FontLabDialogOneList:
@ -241,7 +260,7 @@ class _FontLabDialogGetYesNoCancel:
self.d.End() self.d.End()
class _MacOneList: class _MacOneListW:
"""A one list dialog for Macintosh. This class should not be called directly. Use the OneList function.""" """A one list dialog for Macintosh. This class should not be called directly. Use the OneList function."""
def __init__(self, list, message='Make a selection'): def __init__(self, list, message='Make a selection'):
@ -267,7 +286,8 @@ class _MacOneList:
self.selected = None self.selected = None
self.w.close() self.w.close()
class _MacTwoChecks: class _MacTwoChecksW:
""" Version using W """
def __init__(self, title_1, title_2, value1=1, value2=1, title='RoboFab'): def __init__(self, title_1, title_2, value1=1, value2=1, title='RoboFab'):
import W import W
@ -304,7 +324,7 @@ class ProgressBar:
if inFontLab: if inFontLab:
fl.BeginProgress(title, ticks) fl.BeginProgress(title, ticks)
elif MAC: elif MAC and hasEasyDialogs:
import EasyDialogs import EasyDialogs
self._bar = EasyDialogs.ProgressBar(title, maxval=ticks, label=label) self._bar = EasyDialogs.ProgressBar(title, maxval=ticks, label=label)
else: else:
@ -425,12 +445,15 @@ def OneList(list, message="Select an item:", title='RoboFab'):
except: except:
return None return None
elif MAC: elif MAC:
d = _MacOneList(list, message) if hasW:
d = _MacOneListW(list, message)
sel = d.selected sel = d.selected
if sel is None: if sel is None:
return None return None
else: else:
return list[sel] return list[sel]
else:
_raisePlatformError('OneList')
elif PC: elif PC:
_raisePlatformError('OneList') _raisePlatformError('OneList')
@ -483,8 +506,11 @@ def TwoChecks(title_1="One", title_2="Two", value1=1, value2=1, title='RoboFab'
tc = _FontLabDialogTwoChecks(title_1, title_2, value1, value2, title) tc = _FontLabDialogTwoChecks(title_1, title_2, value1, value2, title)
tc.Run() tc.Run()
elif MAC: elif MAC:
tc = _MacTwoChecks(title_1, title_2, value1, value2, title) if hasW:
elif PC: tc = _MacTwoChecksW(title_1, title_2, value1, value2, title)
else:
_raisePlatformError('TwoChecks')
else:
_raisePlatformError('TwoChecks') _raisePlatformError('TwoChecks')
c1 = tc.check1 c1 = tc.check1
c2 = tc.check2 c2 = tc.check2
@ -509,7 +535,7 @@ def Message(message, title='RoboFab'):
elif MAC: elif MAC:
import EasyDialogs import EasyDialogs
EasyDialogs.Message(message) EasyDialogs.Message(message)
elif PC: else:
_raisePlatformError('Message') _raisePlatformError('Message')
def AskString(prompt, value='', title='RoboFab'): def AskString(prompt, value='', title='RoboFab'):
@ -534,7 +560,7 @@ def AskString(prompt, value='', title='RoboFab'):
return None return None
else: else:
return askString return askString
elif PC: else:
_raisePlatformError('GetString') _raisePlatformError('GetString')
def AskYesNoCancel(prompt, title='RoboFab', default=0): def AskYesNoCancel(prompt, title='RoboFab', default=0):
@ -552,8 +578,7 @@ def AskYesNoCancel(prompt, title='RoboFab', default=0):
import EasyDialogs import EasyDialogs
gync = EasyDialogs.AskYesNoCancel(prompt, default=default) gync = EasyDialogs.AskYesNoCancel(prompt, default=default)
return gync return gync
else:
elif PC:
_raisePlatformError('GetYesNoCancel') _raisePlatformError('GetYesNoCancel')
def GetFile(message=None): def GetFile(message=None):
@ -653,16 +678,43 @@ def PutFile(message=None, defaultName=None):
_raisePlatformError('GetFile') _raisePlatformError('GetFile')
return path return path
if __name__=='__main__': if __name__=='__main__':
#print TwoFields() import traceback
print TwoChecks('hello', 'world', 1, 0, 'ugh')
print OneList(['a', 'b', 'c'], 'hello world') print "dialogs hasW", hasW
Message('hello world') print "dialogs hasDialogKit", hasDialogKit
print AskString('hello world') print "dialogs MAC", MAC
print AskYesNoCancel('hello world') print "dialogs PC", PC
print "dialogs inFontLab", inFontLab
print "dialogs hasEasyDialogs", hasEasyDialogs
def tryDialog(dialogClass, args=None):
print
print "tryDialog:", dialogClass, "with args:", args
try:
if args is not None:
apply(dialogClass, args)
else:
apply(dialogClass)
except:
traceback.print_exc(limit=0)
tryDialog(TwoChecks, ('hello', 'world', 1, 0, 'ugh'))
tryDialog(TwoFields)
tryDialog(TwoChecks, ('hello', 'world', 1, 0, 'ugh'))
tryDialog(OneList, (['a', 'b', 'c'], 'hello world'))
tryDialog(Message, ('hello world',))
tryDialog(AskString, ('hello world',))
tryDialog(AskYesNoCancel, ('hello world',))
try:
b = ProgressBar('hello', 50, 'world') b = ProgressBar('hello', 50, 'world')
for i in range(50): for i in range(50):
if i == 25: if i == 25:
b.label('ugh.') b.label('ugh.')
b.tick(i) b.tick(i)
b.close() b.close()
except:
traceback.print_exc(limit=0)
#