CLASS_METHODS = {} 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 'super_class'. CONVENIENCE_METHODS is a global variable containing a mapping from
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 # ensure that NSDecimal is around global Foundation if Foundation is None: import Foundation # return NSDecimal return Foundation.NSDecimal(obj) try: tp = obj.objCType()
""" Convenience interface for NSDictionary/NSMutableDictionary """ __all__ = () from objc._convenience_mapping import addConvenienceForBasicMapping from objc._convenience import container_wrap, container_unwrap, addConvenienceForClass from _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):
__slots__=('__pyobjc_object__',) 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 # ensure that NSDecimal is around global Foundation if Foundation is None: import Foundation # return NSDecimal return Foundation.NSDecimal(obj) try: tp = obj.objCType() except AttributeError:
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 'super_class'.
""" 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 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
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 isNative(sel): return not hasattr(sel, "callable") 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
""" Convenience interface for NSSet/NSMutableSet """ __all__ = () from objc._convenience import addConvenienceForClass, container_wrap, container_unwrap from _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 NSArray/NSMutableArray """ __all__ = () from objc._convenience import addConvenienceForClass, container_wrap, container_unwrap from _objc import lookUpClass, registerMetaDataForSelector, _C_NSInteger, _C_ID from _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={ 2: { 'callable': {
# # 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