Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
180 lines (159 loc) · 4.46 KB

File metadata and controls

180 lines (159 loc) · 4.46 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
unit PythonAction;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ActnList, PythonEngine;
type
TPythonAction = class(TAction)
private
fRegisteredMethods: TList;
fPythonModule: TPythonModule;
fClearname: string;
fRegistername: string;
fUnregistername: string;
procedure ClearMethods;
protected
function PythonClear(pself, args: PPyObject): PPyObject; cdecl;
function PythonRegister(pself, args: PPyObject): PPyObject; cdecl;
function PythonUnregister(pself, args: PPyObject): PPyObject; cdecl;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function HandlesTarget(Target: TObject): Boolean; override;
function Execute: Boolean; override;
procedure UpdateTarget(Target: TObject); override;
procedure InitializeAction;
property RegisteredMethods: TList read fRegisteredMethods;
published
property PythonModule: TPythonModule read fPythonModule write fPythonModule;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterActions('Python', [TPythonAction], TComponent);
end;
{ TPythonAction }
procedure TPythonAction.ClearMethods;
var
i: integer;
begin
if PythonOK then
with GetPythonEngine, RegisteredMethods do
begin
for i:=0 to Count-1 do
Py_XDECREF(PPyObject(Items[i]));
Clear;
end;
end;
constructor TPythonAction.Create(AOwner: TComponent);
begin
inherited;
fRegisteredMethods := TList.Create;
end;
destructor TPythonAction.Destroy;
begin
ClearMethods;
fRegisteredMethods.Free;
inherited;
end;
function TPythonAction.Execute: Boolean;
var
i: integer;
func: PPyObject;
begin
Result := inherited Execute;
with GetPythonEngine, RegisteredMethods do
begin
i:=0;
while i < Count do
begin
func := Items[i];
// The function could be deleted by reimport of the Python module
// or other reasons!
try
EvalFunctionNoArgs(func);
Inc(i);
except
Py_XDECREF(func);
Delete(i);
end;
end;
end;
end;
function TPythonAction.HandlesTarget(Target: TObject): Boolean;
begin
Result := True;
end;
procedure TPythonAction.InitializeAction;
var
docString: string;
begin
if not (csDesigning in ComponentState) and Assigned(PythonModule) then
begin
fClearname := 'Clear' + Name;
docString := 'Claer all events of "' + Owner.Name + '.' + Name + '" action';
PythonModule.AddDelphiMethod(PChar(fClearname), PythonClear, PChar(docString));
fRegistername := 'Register' + Name;
docString := 'Register an event againt the "' + Owner.Name + '.' + Name + '" action';
PythonModule.AddDelphiMethod(PChar(fRegistername), PythonRegister, PChar(docString));
fUnregistername := 'Unregister' + Name;
docString := 'Unregister an event againt the "' + Owner.Name + '.' + Name + '" action';
PythonModule.AddDelphiMethod(PChar(fUnregistername), PythonUnregister, PChar(docString));
end;
end;
function TPythonAction.PythonClear(pself, args: PPyObject): PPyObject;
begin
ClearMethods;
with GetPythonEngine, RegisteredMethods do
begin
Result := PyInt_FromLong(Count);
end;
end;
function TPythonAction.PythonRegister(pself, args: PPyObject): PPyObject;
var
func: PPyObject;
s: string;
begin
Result := nil;
with GetPythonEngine do
if (PyArg_ParseTuple( args, 'O',@func) = 0) or
( not PyFunction_Check(func)) then
begin
s := fRegistername + '(function)';
PyErr_SetString(PyExc_TypeError^,PChar(s));
end
else
with RegisteredMethods do
begin
Py_XINCREF(func);
Add(func);
Result := PyInt_FromLong(Count);
end;
end;
function TPythonAction.PythonUnregister(pself, args: PPyObject): PPyObject;
var
func: PPyObject;
s: string;
begin
Result := nil;
with GetPythonEngine do
if (PyArg_ParseTuple( args, 'O',@func) = 0) or
(RegisteredMethods.IndexOf(func) = -1) then
begin
s := fUnregistername + '(function)';
PyErr_SetString(PyExc_TypeError^,PChar(s));
end
else
with RegisteredMethods do
begin
Py_XDECREF(func);
Remove(func);
Result := PyInt_FromLong(Count);
end;
end;
procedure TPythonAction.UpdateTarget(Target: TObject);
begin
Enabled := (RegisteredMethods.Count > 0) or Assigned(OnExecute);
end;
end.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.