1'''
2Python mapping for the SearchKit framework.
3
4This module does not contain docstrings for the wrapped code, check Apple's
5documentation for details on how to use these functions and classes.
6'''
7
8import objc as _objc
9from CoreFoundation import *
10
11__bundle__ = _objc.initFrameworkWrapper("SearchKit",
12    frameworkIdentifier="com.apple.SearchKit",
13    frameworkPath=_objc.pathForFramework(
14        "/System/Library/Frameworks/CoreServices.framework/Frameworks/SearchKit.framework"),
15    globals=globals())
16
17try:
18    SKIndexGetTypeID
19    SKDocumentRef
20
21except NameError:
22    # SKIndexGetTypeID is documented, but not actually exported by Leopard. Try to
23    # emulate the missing functionality.
24    #
25    # See also radar:6525606.
26    #
27    def workaround():
28        from Foundation import NSMutableData, NSAutoreleasePool
29
30        pool = NSAutoreleasePool.alloc().init()
31        try:
32            rI = SKIndexCreateWithMutableData(NSMutableData.data(),
33                    None, kSKIndexInverted, None)
34
35            indexID = CFGetTypeID(rI)
36
37            r = SKIndexDocumentIteratorCreate(rI, None)
38            iterID = CFGetTypeID(r)
39            del r
40
41            r = SKSearchGroupCreate([rI])
42            groupID = CFGetTypeID(r)
43
44            r = SKSearchResultsCreateWithQuery(r, ".*", kSKSearchRanked, 1, None, None)
45            resultID = CFGetTypeID(r)
46
47            if SKSearchGetTypeID() == 0:
48                # Type doesn't get registered unless you try to use it.
49                # That's no good for PyObjC, therefore forcefully create
50                # a SKSearch object
51                SKSearchCreate(rI, "q", 0)
52                searchref = objc.registerCFSignature(
53                        "SKSearchRef", b"^{__SKSearch=}", SKSearchGetTypeID())
54            else:
55                searchref = SKSearchRef
56
57            del r
58            del rI
59
60            r = SKSummaryCreateWithString("foo")
61            summaryID = CFGetTypeID(r)
62            del r
63
64        finally:
65            del pool
66
67        def SKIndexGetTypeID():
68            return indexID
69
70        def SKIndexDocumentIteratorGetTypeID():
71            return iterID
72
73        def SKSearchGroupGetTypeID():
74            return groupID
75
76        def SKSearchResultsGetTypeID():
77            return resultID
78
79        def SKSummaryGetTypeID():
80            return summaryID
81
82        indexType = objc.registerCFSignature(
83                "SKIndexRef", b"^{__SKIndex=}", indexID)
84        iterType = objc.registerCFSignature(
85                "SKIndexDocumentIteratorRef", b"^{__SKIndexDocumentIterator=}", iterID)
86        groupType = objc.registerCFSignature(
87                "SKSearchGroupRef", b"^{__SKSearchGroup=}", groupID)
88        resultType = objc.registerCFSignature(
89                "SKSearchResultsRef", b"^{__SKSearchResults=}", resultID)
90        summaryType = objc.registerCFSignature(
91                "SKSummaryRef", b"^{__SKSummary=}", summaryID)
92
93
94        # For some reason SKDocumentGetTypeID doesn't return the right value
95        # when the framework loader calls it the first time around,
96        # by this time the framework is fully initialized and we get
97        # the correct result.
98        SKDocumentRef = objc.registerCFSignature(
99                "SKDocumentRef", b"@", SKDocumentGetTypeID())
100
101
102        return (SKIndexGetTypeID, indexType, SKIndexDocumentIteratorGetTypeID, iterType,
103                SKSearchGroupGetTypeID, groupType, SKSearchResultsGetTypeID, resultType,
104                SKSummaryGetTypeID, summaryType, iterType,
105                SKDocumentRef, searchref)
106
107    (SKIndexGetTypeID, SKIndexRef,
108        SKIndexDocumentIteratorGetTypeID, SKIndexDocumentRef,
109        SKSearchGroupGetTypeID, SKSearchGroupRef,
110        SKSearchResultsGetTypeID, SKSearchResultsRef,
111        SKSummaryGetTypeID, SKSummaryRef,
112        SKIndexDocumentIteratorRef,
113        SKDocumentRef, SKSearchRef,
114    ) = workaround()
115
116    del workaround
117