1"""
2Port of "function defines".
3"""
4from Foundation import NSBundle, NSProcessInfo
5
6def NSLocalizedString(key, comment):
7    return NSBundle.mainBundle().localizedStringForKey_value_table_(key, '', None)
8
9def NSLocalizedStringFromTable(key, tbl, comment):
10    return NSBundle.mainBundle().localizedStringForKey_value_table_(key, '', tbl)
11
12def NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment):
13    return bundle.localizedStringForKey_value_table_(key, '', tbl)
14
15def NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment):
16    return bundle.localizedStringForKey_value_table_(key, val, tbl)
17
18
19def MIN(a, b):
20    if a < b:
21        return a
22    else:
23        return b
24
25def MAX(a, b):
26    if a < b:
27        return b
28    else:
29        return a
30
31ABS = abs
32
33class _OC_DisabledSuddenTermination (object):
34    """
35    Helper class to implement NSDisabledSuddenTermination
36
37    Usage::
38
39        with NSDisabledSuddenTermination:
40            pass
41
42    Inside the with block sudden termination is disabled.
43
44    This only has an effect on OSX 10.6 or later.
45    """
46    if hasattr(NSProcessInfo, 'disableSuddenTermination'):
47        def __enter__(self):
48            NSProcessInfo.processInfo().disableSuddenTermination()
49
50        def __exit__(self, type, value, tb):
51            NSProcessInfo.processInfo().enableSuddenTermination()
52
53    else:
54        def __enter__(self):
55            pass
56
57        def __exit__(self, type, value, tb):
58            pass
59
60
61NSDisabledSuddenTermination = _OC_DisabledSuddenTermination()
62
63if hasattr(NSProcessInfo, 'disableSuddenTermination_'):
64    class NSDisabledAutomaticTermination (object):
65        """
66        Helper class to implement NSDisabledAutomaticTermination
67
68        Usage::
69
70            with NSDisabledAutomaticTermination:
71                pass
72
73        Inside the with block sudden termination is disabled.
74
75        This only has an effect on OSX 10.6 or later.
76        """
77        def __init__(self, reason):
78            self._reason = reason
79
80        def __enter__(self):
81            NSProcessInfo.processInfo().disableAutomaticTermination_(self._reason)
82
83        def __exit__(self, type, value, tb):
84            NSProcessInfo.processInfo().enableAutomaticTermination_(self._reason)
85