forked from lmbelo/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit1.pas
More file actions
157 lines (139 loc) · 4.31 KB
/
Unit1.pas
File metadata and controls
157 lines (139 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
PythonEngine, StdCtrls, ComCtrls, ExtCtrls, PythonGUIInputOutput, Db,
DBTables, Grids, DBGrids;
type
TForm1 = class(TForm)
PythonEngine1: TPythonEngine;
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
PythonGUIInputOutput1: TPythonGUIInputOutput;
Panel2: TPanel;
Panel3: TPanel;
RichEdit1: TRichEdit;
Splitter1: TSplitter;
Memo1: TMemo;
DBGrid1: TDBGrid;
Table1: TTable;
DataSource1: TDataSource;
PythonModule1: TPythonModule;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure PythonModule1AfterInitialization(Sender: TObject);
procedure PythonModule1Initialization(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
function CreateTable( const DBName, TblName : String ) : TDataset;
function DoCreateTable( Self, Args : PPyObject ) : PPyObject; cdecl; // Don't forget the cdecl keyword !!!
var
Form1: TForm1;
implementation
uses pyDB;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
PythonEngine1.ExecStrings( Memo1.Lines );
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
with OpenDialog1 do
begin
if Execute then
Memo1.Lines.LoadFromFile( FileName );
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
with SaveDialog1 do
begin
if Execute then
Memo1.Lines.SaveToFile( FileName );
end;
end;
procedure TForm1.PythonModule1AfterInitialization(Sender: TObject);
var
ds : TDataset;
obj : PPyObject;
begin
// The table is created while Python initializes our modules.
// But we could do it just before the ExecStrings method.
with Sender as TPythonModule do
begin
// Create our TDataset subclass
ds := CreateTable( 'DBDemos', 'Animals.dbf' );
// Use it in the Datasource of the DBGrid
Datasource1.Dataset := ds;
// Create a Python object that will let your script access your table
obj := NewDataset(ds, True);
// Put the Python object in a module var called 'Dataset'
// To access this var in your script, import the module and prefix the var name with
// the module name:
// import MyModule
// print MyModule.Dataset.Active
PythonModule1.SetVar( 'Dataset', obj );
// Don't forget to decrement the object, because SetVar has incremented it,
// in order to acquire property.
GetPythonEngine.Py_XDecRef(obj);
end;
end;
procedure TForm1.PythonModule1Initialization(Sender: TObject);
begin
// Here we register our new module functions
with Sender as TPythonModule do
AddMethod( 'CreateTable', DoCreateTable, 'CreateTable( DBName, TbleName ) -> Dataset' );
end;
function CreateTable( const DBName, TblName : String ) : TDataset;
var
ds : TTable;
begin
// Note that here we use a simple TTable, but replace the TTable with anything that
// inherits from a TDataset.
ds := TTable.Create(nil);
ds.DatabaseName := DBName;
ds.TableName := TblName;
ds.Active := True;
Result := ds;
end;
{
This function will let your Python script create its own instances of your
TDataset subclass !
}
function DoCreateTable( Self, Args : PPyObject ) : PPyObject; cdecl;
var
DBName, TblName : PAnsiChar;
ds : TDataset;
begin
with GetPythonEngine do
begin
// Extract our arguments
if PyArg_ParseTuple( args, 'ss:CreateTable',@DBName, @TblName ) <> 0 then
begin
try
// Try to create our dataset
ds := CreateTable( DBName, TblName );
// Create our Python wrapper
Result := NewDataset( ds, True );
except
on E : Exception do
begin
if Assigned(gDBModule) then
gDBModule.RaiseError( 'DBError', E.Message );
Result := nil;
end;
end;
end
else // the arguments were not right
Result := nil;
end;
end;
end.