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