def __new__(cls, obj, value): self = int.__new__(cls, value) self.__pyobjc_object__ = obj return self __class__ = property(lambda self: self.__pyobjc_object__.__class__) def __getattr__(self, attr): return getattr(self.__pyobjc_object__, attr) def __reduce__(self): return (int, (int(self), )) NSNumber = _objc.lookUpClass('NSNumber') NSDecimalNumber = _objc.lookUpClass('NSDecimalNumber') def numberWrapper(obj): if isinstance(obj, NSDecimalNumber): return obj try: tp = obj.objCType() except AttributeError: import warnings warnings.warn( "NSNumber instance doesn't implement objCType? %r" % (obj, ), RuntimeWarning) return obj
# # Helper functions for converting data item to/from a representation # that is usable inside Cocoa data structures. # # In particular: # # - Python "None" is stored as +[NSNull null] because Cocoa containers # won't store NULL as a value (and this transformation is undone when # retrieving data) # # - When a getter returns NULL in Cocoa the queried value is not present, # that's converted to an exception in Python. # _NULL = lookUpClass('NSNull').null() def container_wrap(v): if v is None: return _NULL return v def container_unwrap(v, exc_type, *exc_args): if v is None: raise exc_type(*exc_args) elif v is _NULL: return None return v
def addConvenienceForSelector(selector, methods): """ Add the list with methods to every class that has a selector with the given name. """ CONVENIENCE_METHODS[selector] = methods def addConvenienceForClass(classname, methods): """ Add the list with methods to the class with the specified name """ CLASS_METHODS[classname] = methods NSObject = lookUpClass("NSObject") def add_convenience_methods(super_class, name, type_dict): try: return _add_convenience_methods(super_class, name, type_dict) except: import traceback traceback.print_exc() raise def _add_convenience_methods(super_class, name, type_dict): """ Add additional methods to the type-dict of subclass 'name' of
def __new__(cls, obj, value): self = int.__new__(cls, value) self.__pyobjc_object__ = obj return self __class__ = property(lambda self: self.__pyobjc_object__.__class__) def __getattr__(self, attr): return getattr(self.__pyobjc_object__, attr) def __reduce__(self): return (int, (int(self),)) NSNumber = _objc.lookUpClass('NSNumber') NSDecimalNumber = _objc.lookUpClass('NSDecimalNumber') def numberWrapper(obj): if isinstance(obj, NSDecimalNumber): return obj try: tp = obj.objCType() except AttributeError: import warnings warnings.warn("NSNumber instance doesn't implement objCType? %r" % (obj,), RuntimeWarning) return obj if tp in b'qQLfd': if tp == b'q':
# # Helper functions for converting data item to/from a representation # that is usable inside Cocoa data structures. # # In particular: # # - Python "None" is stored as +[NSNull null] because Cocoa containers # won't store NULL as a value (and this transformation is undone when # retrieving data) # # - When a getter returns NULL in Cocoa the queried value is not present, # that's converted to an exception in Python. # _NULL = lookUpClass("NSNull").null() def container_wrap(v): if v is None: return _NULL return v def container_unwrap(v, exc_type, *exc_args): if v is None: raise exc_type(*exc_args) elif v is _NULL: return None return v
""" Convenience interface for NSArray/NSMutableArray """ __all__ = () from objc._convenience import addConvenienceForClass, container_wrap, container_unwrap from objc._objc import lookUpClass, registerMetaDataForSelector, _C_NSInteger, _C_ID from objc._objc import _NSNotFound as NSNotFound import sys, collections.abc NSArray = lookUpClass("NSArray") NSMutableArray = lookUpClass("NSMutableArray") collections.abc.Sequence.register(NSArray) collections.abc.MutableSequence.register(NSMutableArray) registerMetaDataForSelector( b"NSObject", b"sortUsingFunction:context:", dict( arguments={ 2: { "callable": { "retval": { "type": _C_NSInteger }, "arguments": { 0: { "type": _C_ID },
def __new__(cls, obj, value): self = int.__new__(cls, value) self.__pyobjc_object__ = obj return self __class__ = property(lambda self: self.__pyobjc_object__.__class__) def __getattr__(self, attr): return getattr(self.__pyobjc_object__, attr) def __reduce__(self): return (int, (int(self),)) NSNumber = _objc.lookUpClass("NSNumber") NSDecimalNumber = _objc.lookUpClass("NSDecimalNumber") def numberWrapper(obj): if isinstance(obj, NSDecimalNumber): return obj try: tp = obj.objCType() except AttributeError: import warnings warnings.warn( "NSNumber instance doesn't implement objCType? %r" % (obj,), RuntimeWarning )
def __new__(cls, obj, value): self = int.__new__(cls, value) self.__pyobjc_object__ = obj return self __class__ = property(lambda self: self.__pyobjc_object__.__class__) def __getattr__(self, attr): return getattr(self.__pyobjc_object__, attr) def __reduce__(self): return (int, (int(self),)) NSNumber = _objc.lookUpClass("NSNumber") NSDecimalNumber = _objc.lookUpClass("NSDecimalNumber") Foundation = None def numberWrapper(obj): if isinstance(obj, NSDecimalNumber): return obj try: tp = obj.objCType() except AttributeError: import warnings warnings.warn("NSNumber instance doesn't implement objCType? %r" % (obj,), RuntimeWarning) return obj
""" Convenience interface for NSSet/NSMutableSet """ __all__ = () from objc._convenience import addConvenienceForClass, container_wrap, container_unwrap from objc._objc import lookUpClass import collections import sys NSSet = lookUpClass('NSSet') NSMutableSet = lookUpClass('NSMutableSet') collections.Set.register(NSSet) collections.MutableSet.register(NSMutableSet) def nsset_isdisjoint(self, other): if not hasattr(other, '__contains__'): other = list(other) for item in self: if item in other: return False return True def nsset_union(self, *other): result = NSMutableSet() result.unionSet_(self) for val in other: if isinstance(val, collections.Set):
""" Support for NSDecimalNumber. The actual class is defined in Foundation, but having the wrapper here is much more convenient. """ __all__ = () from objc._convenience import addConvenienceForClass from objc._objc import lookUpClass, NSDecimal import sys import operator NSDecimalNumber = lookUpClass('NSDecimalNumber') def decimal_new(cls, value=None): if value is None: return cls.numberWithInt_(0) else: if isinstance(value, NSDecimal): return cls.decimalNumberWithDecimal_(value) elif isinstance(value, NSDecimalNumber): return cls.decimalNumberWithDecimal_(value.decimalValue()) elif isinstance(value, float): return cls.numberWithDouble_(value) elif isinstance(value, str): value = NSDecimal(value) return cls.decimalNumberWithDecimal_(value) else: # The value is either an integer, or
Add the list with methods to every class that has a selector with the given name. """ warnings.warn("addConvenienceForSelector is on the way out", DeprecationWarning) _CONVENIENCE_METHODS[selector] = methods def addConvenienceForClass(classname, methods): """ Add the list with methods to the class with the specified name """ CLASS_METHODS[classname] = methods NSObject = lookUpClass('NSObject') def add_convenience_methods(super_class, name, type_dict): """ Add additional methods to the type-dict of subclass 'name' of 'super_class'. _CONVENIENCE_METHODS is a global variable containing a mapping from an Objective-C selector to a Python method name and implementation. CLASS_METHODS is a global variable containing a mapping from class name to a list of Python method names and implementation. Matching entries from both mappings are added to the 'type_dict'. """
def addConvenienceForSelector(selector, methods): """ Add the list with methods to every class that has a selector with the given name. """ warnings.warn("addConvenienceForSelector is on the way out", DeprecationWarning) _CONVENIENCE_METHODS[selector] = methods def addConvenienceForClass(classname, methods): """ Add the list with methods to the class with the specified name """ CLASS_METHODS[classname] = methods NSObject = lookUpClass('NSObject') def add_convenience_methods(super_class, name, type_dict): """ Add additional methods to the type-dict of subclass 'name' of 'super_class'. _CONVENIENCE_METHODS is a global variable containing a mapping from an Objective-C selector to a Python method name and implementation. CLASS_METHODS is a global variable containing a mapping from class name to a list of Python method names and implementation. Matching entries from both mappings are added to the 'type_dict'. """ if type_dict.get('__objc_python_subclass__'):
""" Convenience interface for NSSet/NSMutableSet """ __all__ = () import collections.abc from objc._convenience import addConvenienceForClass, container_unwrap, container_wrap from objc._objc import lookUpClass NSSet = lookUpClass("NSSet") NSMutableSet = lookUpClass("NSMutableSet") collections.abc.Set.register(NSSet) collections.abc.MutableSet.register(NSMutableSet) def nsset_isdisjoint(self, other): if not hasattr(other, "__contains__"): other = list(other) for item in self: if item in other: return False return True def nsset_union(self, *other): result = NSMutableSet() result.unionSet_(self) for val in other:
# # Helper functions for converting data item to/from a representation # that is usable inside Cocoa data structures. # # In particular: # # - Python "None" is stored as +[NSNull null] because Cocoa containers # won't store NULL as a value (and this transformation is undone when # retrieving data) # # - When a getter returns NULL in Cocoa the queried value is not present, # that's converted to an exception in Python. # _NULL = lookUpClass('NSNull').null() def container_wrap(v): if v is None: return _NULL return v def container_unwrap(v, exc_type, *exc_args): if v is None: raise exc_type(*exc_args) elif v is _NULL: return None return v # #
""" Convenience interface for NSArray/NSMutableArray """ __all__ = () from objc._convenience import addConvenienceForClass, container_wrap, container_unwrap from objc._objc import lookUpClass, registerMetaDataForSelector, _C_NSInteger, _C_ID from objc._objc import _NSNotFound as NSNotFound import collections import sys NSArray = lookUpClass('NSArray') NSMutableArray = lookUpClass('NSMutableArray') collections.Sequence.register(NSArray) collections.MutableSequence.register(NSMutableArray) if sys.version_info[0] == 2: # pragma: no 3.x cover INT_TYPES = (int, long) STR_TYPES = (str, unicode) else: # pragma: no 2.x cover INT_TYPES = int STR_TYPES = str registerMetaDataForSelector( b"NSObject", b"sortUsingFunction:context:", dict( arguments={
""" Support for NSDecimalNumber. The actual class is defined in Foundation, but having the wrapper here is much more convenient. """ __all__ = () from objc._convenience import addConvenienceForClass from objc._objc import lookUpClass, NSDecimal import sys import operator NSDecimalNumber = lookUpClass("NSDecimalNumber") def decimal_new(cls, value=None): if value is None: return cls.numberWithInt_(0) else: if isinstance(value, NSDecimal): return cls.decimalNumberWithDecimal_(value) elif isinstance(value, NSDecimalNumber): return cls.decimalNumberWithDecimal_(value.decimalValue()) elif isinstance(value, float): return cls.numberWithDouble_(value) elif isinstance(value, str): value = NSDecimal(value) return cls.decimalNumberWithDecimal_(value) else: # The value is either an integer, or
""" Convenience interface for NSDictionary/NSMutableDictionary """ __all__ = () from objc._convenience_mapping import addConvenienceForBasicMapping from objc._convenience import container_wrap, container_unwrap, addConvenienceForClass from objc._objc import lookUpClass import collections import sys NSDictionary = lookUpClass('NSDictionary') NSMutableDictionary = lookUpClass('NSMutableDictionary') addConvenienceForBasicMapping('NSDictionary', True) addConvenienceForBasicMapping('NSMutableDictionary', False) def _all_contained_in(inner, outer): """ Return True iff all items in ``inner`` are also in ``outer``. """ for v in inner: if v not in outer: return False return True def nsdict__len__(self):
""" Convenience interface for NSSet/NSMutableSet """ __all__ = () from objc._convenience import addConvenienceForClass, container_wrap, container_unwrap from objc._objc import lookUpClass import collections import sys NSSet = lookUpClass('NSSet') NSMutableSet = lookUpClass('NSMutableSet') collections.Set.register(NSSet) collections.MutableSet.register(NSMutableSet) def nsset_isdisjoint(self, other): if not hasattr(other, '__contains__'): other = list(other) for item in self: if item in other: return False return True def nsset_union(self, *other): result = NSMutableSet() result.unionSet_(self)
""" Convenience interface for NSDictionary/NSMutableDictionary """ __all__ = () from objc._convenience_mapping import addConvenienceForBasicMapping from objc._convenience import container_wrap, container_unwrap, addConvenienceForClass from objc._objc import lookUpClass import collections import sys, os NSDictionary = lookUpClass('NSDictionary') NSMutableDictionary = lookUpClass('NSMutableDictionary') addConvenienceForBasicMapping('NSDictionary', True) addConvenienceForBasicMapping('NSMutableDictionary', False) def _all_contained_in(inner, outer): """ Return True iff all items in ``inner`` are also in ``outer``. """ for v in inner: if v not in outer: return False return True def nsdict__len__(self):
""" Convenience interface for NSArray/NSMutableArray """ __all__ = () from objc._convenience import addConvenienceForClass, container_wrap, container_unwrap from objc._objc import lookUpClass, registerMetaDataForSelector, _C_NSInteger, _C_ID from objc._objc import _NSNotFound as NSNotFound import collections import sys NSArray = lookUpClass('NSArray') NSMutableArray = lookUpClass('NSMutableArray') collections.Sequence.register(NSArray) collections.MutableSequence.register(NSMutableArray) if sys.version_info[0] == 2: # pragma: no 3.x cover INT_TYPES = (int, int) STR_TYPES = (str, str) else: # pragma: no 2.x cover INT_TYPES = int STR_TYPES = str registerMetaDataForSelector( b"NSObject", b"sortUsingFunction:context:", dict(arguments={ 2: { 'callable': {
""" Convenience interface for NSDictionary/NSMutableDictionary """ __all__ = () from objc._convenience_mapping import addConvenienceForBasicMapping from objc._convenience import container_wrap, container_unwrap, addConvenienceForClass from objc._objc import lookUpClass import sys, os, collections.abc NSDictionary = lookUpClass("NSDictionary") NSMutableDictionary = lookUpClass("NSMutableDictionary") addConvenienceForBasicMapping("NSDictionary", True) addConvenienceForBasicMapping("NSMutableDictionary", False) def _all_contained_in(inner, outer): """ Return True iff all items in ``inner`` are also in ``outer``. """ for v in inner: if v not in outer: return False return True def nsdict__len__(self): return self.count()
""" Support for NSDecimalNumber. The actual class is defined in Foundation, but having the wrapper here is much more convenient. """ __all__ = () from objc._convenience import addConvenienceForClass from objc._objc import lookUpClass, NSDecimal import sys import operator NSDecimalNumber = lookUpClass('NSDecimalNumber') def decimal_new(cls, value=None): if value is None: return cls.numberWithInt_(0) else: if isinstance(value, NSDecimal): return cls.decimalNumberWithDecimal_(value) elif isinstance(value, NSDecimalNumber): return cls.decimalNumberWithDecimal_(value.decimalValue()) elif isinstance(value, float): return cls.numberWithDouble_(value) elif isinstance(value, str): value = NSDecimal(value) return cls.decimalNumberWithDecimal_(value) else: # The value is either an integer, or # invalid (and numberWithLongLong_ wil raise