Hello,

I am attempting to index a 4D (4th Dimension) located on a network with dtSearch 
(www.dtsearch.com).

Sample code in VB, Java, C++ is provided with dtSearch and I am attempting to 
translate the VB code to Python.


The Index Form (which indexes the database) includes a button that triggers the 
following VB method:

======================

Private Sub IndexButton_Click()
        Dim ij As Object
        ' Clear flags
        fStopPressed = False
        fAbortImmediately = False
        fIndexing = True

        ' Collect values from the controls
        Set ij = TheEngine.NewIndexJob()
        
        Dim adoDatabase As SampleAdoDataSource
        Set adoDatabase = New SampleAdoDataSource
        
        adoDatabase.BrowseOpen ("")
        Set ij.DataSourceToIndex = adoDatabase
        
        ij.IndexPath = IndexPath
        ij.ActionCreate = True
        ij.ActionAdd = True
        ij.CreateRelativePaths = True
        ij.StoredFields = StoredFields
        Set ij.StatusHandler = Me
        
        StopButton.Enabled = True
        StopImmediatelyButton.Enabled = True
        CloseButton.Enabled = False
        
        ij.Execute
        
        StopButton.Enabled = False
        StopImmediatelyButton.Enabled = False
        CloseButton.Enabled = True
        fIndexing = False

        Set ij.DataSourceToIndex = Nothing
        Set adoDatabase = Nothing
        If fCloseRequested Then
            Unload Me
        End If
End Sub

===============

How do you create an OLE IDispatch object, such as SampleAdoDataSource (the VB code to 
create a SampleAdoDataSource class is listed below), in Python? 

How should I tranlate the code between the BEGIN and END tags below?

Do I need all the attributes below?


Many thanks.

Philippe de Rochambeau

==============

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "SampleAdoDataSource"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Description = "dtSearch ADO Reader"
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'  Demonstrates the use of the DataSourceToIndex member of an IndexJob
'
'  SampleAdoDataSource uses Active Data Objects 1.5
'  to demonstrate indexing of data sources using the dtSearch Engine.
'
'  The DataSourceToIndex member of an IndexJob can be any Visual Basic object
'  (or other OLE IDispatch object), that implements the following methods and 
properties:
'
'  Methods:
'     Rewind                Initialize the data source so that the next GetNextDoc 
call will
'                           return the first document.
'     GetNextDoc            Get the next document from the data source. The document
'                           information is stored in the properties.  GetNextDoc 
returns 0
'                           if it succeeds, non-zero if there are no more documents.
'  Properties
'     DocName               Name of the document.  Can be any legal Win32 filename.
'     DocDisplayName        A user-friendly version of the filename.  Usually this 
will be
'                           blank, but in this example the message subject is used
'                           since the DocName for a message is an incomprehensible hex 
string.
'                           This is what will appear in dtSearch search results lists 
if the
'                           document is retrieved in a search.
'     DocModifiedDate       The date that the document was last modified.
'     DocText               Text of the document to be indexed.
'     DocFields             Fields in the document to be indexed

Option Explicit
Public DocName As String
Public DocDisplayName As String
Public DocModifiedDate As Variant
Public DocCreatedDate As Variant
Public DocText As String
Public DocFields As String

'  By default, all of the fields are indexed using both the DocText
'  property and the DocFields property.  These flags provide a way to override
'  this behavior
Public fSkipDocFields As Boolean
Public fSkipDocText As Boolean

Dim cn As ADODB.Connection
Dim Tables() As String
Dim tableCount As Long
Dim iTable As Long
Dim iRow As Long
Dim fLogin As Integer
Dim rs As ADODB.Recordset
Dim iRowIdField As Long

Private Sub AddTable(tableName As String)
    tableCount = tableCount + 1
    If tableCount > UBound(Tables, 1) Then
        ReDim Preserve Tables(tableCount * 2) As String
    End If
    Tables(tableCount) = tableName
End Sub
        
Private Sub Logoff()
    If Not fLogin Then
        Exit Sub
    End If
    
    cn.Close
    fLogin = False
End Sub

Public Function BrowseOpen(ByVal NameToOpen As String) As Long
    If fLogin Then
        Logoff
    End If
    On Error GoTo BadOpen
    
    Set cn = Nothing
    Set cn = New ADODB.Connection
    
    cn.Properties("Prompt") = adPromptAlways
    cn.ConnectionString = NameToOpen
    cn.Open
    GetTableNames
    
    fLogin = True
    BrowseOpen = False
    Exit Function
BadOpen:
    fLogin = False
    BrowseOpen = False
End Function

' Build a list of the tables in the database.  This is used
' to iterate over each of the tables in GetNextDoc
Private Sub GetTableNames()
    tableCount = 0
    iTable = 0
    ReDim Tables(20) As String
    Dim rsSchema As ADODB.Recordset
    Set rsSchema = cn.OpenSchema(adSchemaTables)

    Do Until rsSchema.EOF
        Dim t As String
        t = rsSchema!TABLE_TYPE
        
        If (StrComp(t, "TABLE", vbTextCompare) = 0) Then
            AddTable (rsSchema!TABLE_NAME)
        End If
        rsSchema.MoveNext
    Loop
    rsSchema.Close
End Sub

Private Sub NextTable()
    On Error GoTo AdoError
    
    If (iTable = tableCount) Then
        Exit Sub
    End If
    
    iTable = iTable + 1
    iRow = 1
    
    Dim cmd As String
    cmd = "Select * from " & Chr$(34) & Tables(iTable) & Chr$(34)
    Set rs = cn.Execute(cmd)
    
    Dim f As ADODB.Field
    Dim fields As ADODB.fields
    Set fields = rs.fields
    iRowIdField = 1
    'Dim i As Integer
    'For i = 1 To fields.Count
    '    Set f = fields.item(i - 1)
    '    If (f.Properties("IsAutoIncrement") Or (f.Attributes And adFldRowID)) Then
    '        iRowIdField = i
    '    End If
    'Next i
    Exit Sub
AdoError:
    
End Sub

Private Function ValueToString(v As Variant) As String
    On Error GoTo BadVal
    If (IsNull(v)) Then
          ValueToString = ""
    Else
           ValueToString = CStr(v)
    End If
    Exit Function
BadVal:
    ValueToString = ""
        
End Function


Private Sub GetRowInfo()
    'On Error Resume Next
    DocName = Tables(iTable) + "#"
    
    Dim fields As ADODB.fields
    Set fields = rs.fields
    Dim val As Variant
    
    
    If (iRowIdField = 0) Then
        DocName = DocName + Str$(iRow)
    Else
        val = fields(iRowIdField - 1).Value
        DocName = DocName + fields(iRowIdField - 1).Name + "=" + ValueToString(val)
    End If
    
    DocModifiedDate = Now
    DocCreatedDate = Now
    
    DocText = ""
    DocFields = ""
    Dim f As ADODB.Field
    Dim i As Integer
    Dim fieldValue As String
    
    For i = 1 To fields.Count
        Set f = fields(i - 1)
        fieldValue = "" ' make sure old values are not reused
        fieldValue = ValueToString(f.Value)
        
        ' Note: after being referenced once in ValueToString, f.Value may now be null;
        ' inspected the value in the VB debugger can also set it to null.
        
        DocText = DocText & f.Name & " = " & fieldValue & Chr$(13) & Chr$(10)
        
        ' DocFields is a series of {fieldname, fieldValue} pairs delimited with the 
chr$(9) (tab)
        ' character.  To contruct this, just make sure that the field text does not 
contain
        ' any tab characters (here we convert them to spaces)
        While InStr(fieldValue, Chr$(9)) > 0
            Dim iTab
            iTab = InStr(fieldValue, Chr$(9))
            Mid$(fieldValue, iTab, 1) = " "
        Wend
        If (Len(fieldValue) = 0) Then
             fieldValue = " "
        End If
        
        DocFields = DocFields & f.Name & Chr$(9) & fieldValue & Chr$(9)
    Next i
    
    If (fSkipDocText) Then
        DocText = ""
    End If
    If (fSkipDocFields) Then
        DocFields = ""
    End If
End Sub

Public Function GetNextDoc() As Long
    On Error Resume Next
    If (iTable = 0) Then
        NextTable
    End If
    While (rs.EOF And (iTable < tableCount))
        NextTable
    Wend
    If (rs.EOF Or (iTable = 0)) Then
        GetNextDoc = -1 ' no more documents
    Else
        GetRowInfo
        rs.MoveNext
        iRow = iRow + 1
        GetNextDoc = 0
    End If
End Function

Public Sub Rewind()
    iTable = 0
    Set rs = Nothing
End Sub

===========================

_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython

Reply via email to