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