forked from Embarcadero/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonDocs.pas
More file actions
480 lines (423 loc) · 14.7 KB
/
Copy pathPythonDocs.pas
File metadata and controls
480 lines (423 loc) · 14.7 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
{$I Definition.Inc}
unit PythonDocs;
interface
uses
System.Rtti,
System.Types,
System.TypInfo,
System.Classes,
System.SysUtils,
System.SyncObjs,
System.Threading,
System.Generics.Collections,
Xml.xmldom,
Xml.XMLIntf,
Xml.XMLDoc,
Xml.omnixmldom;
type
TSymbol = string;
TDocScanResult = class;
{$SCOPEDENUMS ON}
TPythonDocSource = (Xml);
TPythonDocSymbolType = (Constant, Variable, &Constructor, &Destructor, &Procedure, &Function, Enum, &Set, &Record, &Class);
{$SCOPEDENUMS OFF}
TDiscoverPredicate = reference to function(const ARttiType: TRttiType; out ATypeInfo: PTypeInfo): boolean;
IPythonDocProvider = interface
['{4763349F-A25B-41E9-8811-2A6BD5933834}']
/// <summary>
/// Find the doc of a given symbol.
/// </summary>
function Find(const ASymbolName: TSymbol;
const ASymbolType: TPythonDocSymbolType;
const ADeclaringUnitName: string): TDocScanResult;
end;
TPythonDocServer = class
private
class var FInstance: TPythonDocServer;
const DOC_DIR_NAME = 'doc';
const DOC_FILE_NAME = 'docs.xml';
private
FProvider: IPythonDocProvider;
constructor Create();
class function GetInstance: TPythonDocServer; static;
class function GetDocDir(): string; static;
class function GetDocFile(): string; static;
public
class destructor Destroy();
public
/// <summary>
/// Bufferize all symbols.
/// </summary>
procedure Bufferize();
/// <summary>
/// Clear all symbol info off buffer.
/// </summary>
procedure ClearBuffer();
/// <summary>
/// Reads the docs of a symbol.
/// </summary>
function ReadTypeDocStr(const ASymbolName: TSymbol;
const ASymbolType: TPythonDocSymbolType;
const ADeclaringUnitName: string; out ADocStr: string): boolean; overload;
/// <summary>
/// Reads the docs of a type.
/// </summary>
function ReadTypeDocStr(const ATypeInfo: PTypeInfo; out ADocStr: string): boolean; overload;
/// <summary>
/// Reads the docs of a type.
/// </summary>
function ReadTypeDocStr<T>(out ADocStr: string): boolean; overload;
/// <summary>
/// Reads the docs of a member of a type.
/// </summary>
function ReadMemberDocStr(const AParent: PTypeInfo; const AMember: TRttiMember; out ADocStr: string): boolean; overload;
/// <summary>
/// Reads the docs of a member of a type.
/// </summary>
function ReadMemberDocStr<T>(const AMember: TRttiMember; out ADocStr: string): boolean; overload;
/// <summary>
/// Doc provider instance.
/// </summary>
property Provider: IPythonDocProvider read FProvider write FProvider;
class property Instance: TPythonDocServer read GetInstance;
end;
TPythonDocXML = class(TInterfacedObject, IPythonDocProvider)
private type
TDiscoveredDocs = TDocScanResult;
TDiscoveredSymbols = TObjectDictionary<string, TDiscoveredDocs>;
TDiscoveredFiles = TObjectDictionary<string, TDiscoveredSymbols>;
private
class var FDiscoveredFiles: TDiscoveredFiles;
/// <summary>
/// Creates a XML document instance with the necessary settings.
/// </summary>
class function CreateXmlDoc(const AFileName: string): IXMLDocument;
/// <summary>
/// Create a buffer with all nodes of the XML file.
/// </summary>
class procedure BufferizeClasses(const AClassNodes: IXMLNode; const ASymbolMap: TDiscoveredSymbols);
class procedure BufferizeMembers(const AMemberNodes: IXMLNode; const ADocMap: TDiscoveredDocs);
public
class constructor Create();
class destructor Destroy();
/// <summary>
/// Find a symbol in the buffered map
/// </summary>
function Find(const ASymbolName: TSymbol;
const ASymbolType: TPythonDocSymbolType;
const ADeclaringUnitName: string): TDocScanResult;
/// <summary>
/// Bufferizes the XML doc file.
/// </summary>
class procedure Bufferize(const AFileName: string);
/// <summary>
/// Clear all discovered files with buffered xml from buffer.
/// </summary>
class procedure ClearBuffer();
end;
TDocScanResult = class(TDictionary<string, string>)
public
class function ExtractMemberPrefix(const AXmlMember: IXMLNode): string; overload; static; inline;
class function BuildMemberIdentifier(const AXmlMember: IXMLNode): string; overload; static; inline;
class function ExtractMemberPrefix(const ARttiNamedType: TRttiNamedObject): string; overload; static;
class function BuildMemberIdentifier(const ARttiNamedType: TRttiNamedObject): string; overload; static; inline;
class function BuildMemberIdentifier(const ATypeInfo: PTypeInfo): string; overload; static; inline;
class function BuildMemberIdentifier(const ASymbolName: string; const ASymbolType: TPythonDocSymbolType): string; overload; static; inline;
end;
implementation
uses
System.IOUtils,
System.StrUtils;
{ TPythonDocXML }
class constructor TPythonDocXML.Create;
begin
FDiscoveredFiles := TDiscoveredFiles.Create([doOwnsValues]);
end;
class destructor TPythonDocXML.Destroy;
begin
FDiscoveredFiles.Free();
end;
function TPythonDocXML.Find(const ASymbolName: TSymbol;
const ASymbolType: TPythonDocSymbolType;
const ADeclaringUnitName: string): TDocScanResult;
var
LSymbols: TDiscoveredSymbols;
begin
//Do we have the given unit bufferized?
if not FDiscoveredFiles.TryGetValue(ADeclaringUnitName, LSymbols) then
Exit(nil);
case ASymbolType of
TPythonDocSymbolType.Class:
//Do we have the given symbol bufferized?
if LSymbols.ContainsKey(ASymbolName) then
Exit(LSymbols[ASymbolName]);
//These docs will be enhanced as needed
else raise ENotSupportedException.Create('Type doesn''t supports documentation.');
end;
Result := nil;
end;
class function TPythonDocXML.CreateXmlDoc(const AFileName: string): IXMLDocument;
var
LXMLDocument: TXMLDocument;
begin
LXMLDocument := TXMLDocument.Create(nil);
try
LXMLDocument.DOMVendor := GetDOMVendor(sOmniXmlVendor);
LXMLDocument.FileName := AFileName;
LXMLDocument.Active := true;
except
LXMLDocument.Free();
raise;
end;
Result := LXMLDocument;
end;
class procedure TPythonDocXML.Bufferize(const AFileName: string);
var
LXMLDoc: IXMLDocument;
LXmlDOMNodeSelect: IDOMNodeSelect;
LXmlDOMNodes: IDOMNodeList;
I: Integer;
LSymbols: TDiscoveredSymbols;
begin
if not TFile.Exists(AFileName) then
Exit;
LXMLDoc := CreateXmlDoc(AFileName);
LXmlDOMNodeSelect := (LXMLDoc.DOMDocument.documentElement as IDOMNodeSelect);
if not Assigned(LXmlDOMNodeSelect) then
Exit;
//We currently only support classes
LXmlDOMNodes := LXmlDOMNodeSelect.selectNodes('/docs/class');
if not Assigned(LXmlDOMNodes) then
Exit;
//Bufferize all classes
for I := 0 to Pred(LXmlDOMNodes.Length) do begin
//This must be fast
if not FDiscoveredFiles.TryGetValue(
LXmlDOMNodes.Get_Item(I).attributes.getNamedItem('unit').nodeValue,
LSymbols) then
begin
LSymbols := TDiscoveredSymbols.Create([doOwnsValues]);
FDiscoveredFiles.Add(
LXmlDOMNodes.Get_Item(I).attributes.getNamedItem('unit').nodeValue,
LSymbols);
end;
//Add a class and its members into the buffer
BufferizeClasses(TXMLNode.Create(LXmlDOMNodes.Get_Item(I), nil,
LXMLDoc as TXMLDocument) as IXMLNode, LSymbols);
end;
end;
class procedure TPythonDocXML.BufferizeClasses(const AClassNodes: IXMLNode;
const ASymbolMap: TDiscoveredSymbols);
var
LDiscoveredDocs: TDiscoveredDocs;
LXmlDocStr: IXMLNode;
LXmlMembers: IXMLNode;
begin
LDiscoveredDocs := TDiscoveredDocs.Create();
try
//Add a discovered doc map to the class
ASymbolMap.Add(AClassNodes.GetAttribute('name'), LDiscoveredDocs);
//Look for class doc
LXmlDocStr := AClassNodes.ChildNodes.FindNode('docstr');
if Assigned(LXmlDocStr) then
LDiscoveredDocs.Add(
TDocScanResult.BuildMemberIdentifier(AClassNodes), LXmlDocStr.Text);
//Scan class members
LXmlMembers := AClassNodes.ChildNodes.FindNode('members');
if Assigned(LXmlMembers) then
BufferizeMembers(LXmlMembers, LDiscoveredDocs);
except
FreeAndNil(LDiscoveredDocs);
raise;
end;
end;
class procedure TPythonDocXML.BufferizeMembers(const AMemberNodes: IXMLNode;
const ADocMap: TDiscoveredDocs);
var
I: Integer;
LIdentifier: string;
begin
for I := 0 to AMemberNodes.ChildNodes.Count - 1 do begin
LIdentifier := TDocScanResult.BuildMemberIdentifier(AMemberNodes.ChildNodes[I]);
if not ADocMap.ContainsKey(LIdentifier) then
ADocMap.Add(LIdentifier, AMemberNodes.ChildNodes[I].ChildNodes.FindNode('docstr').Text)
//Concat docstr for overloaded methods. We are considering any duplicates as overloads.
else begin
ADocMap[LIdentifier] := ADocMap[LIdentifier]
+ #13#10
+ AMemberNodes.ChildNodes[I].ChildNodes.FindNode('docstr').Text;
end;
end;
end;
class procedure TPythonDocXML.ClearBuffer;
begin
FDiscoveredFiles.Clear();
end;
{ TPythonDocServer }
constructor TPythonDocServer.Create;
begin
inherited;
FProvider := TPythonDocXML.Create();
end;
class destructor TPythonDocServer.Destroy;
begin
FInstance.Free();
end;
class function TPythonDocServer.GetInstance: TPythonDocServer;
begin
if not Assigned(FInstance) then
FInstance := TPythonDocServer.Create();
Result := FInstance;
end;
class function TPythonDocServer.GetDocDir(): string;
begin
{$IFDEF ANDROID}
Exit(TPath.GetDocumentsPath());
{$ENDIF ANDROID}
{$IFDEF DEBUG}
Result := TPath.Combine(ExtractFilePath(
GetModuleName(HInstance)), DOC_DIR_NAME);
{$ELSE}
Result := TPath.Combine(
TDirectory.GetParent(ExcludeTrailingPathDelimiter(
ExtractFilePath(GetModuleName(HInstance)))), DOC_DIR_NAME);
{$ENDIF}
end;
class function TPythonDocServer.GetDocFile: string;
begin
Result := TPath.Combine(TPythonDocServer.GetDocDir(), DOC_FILE_NAME);
end;
procedure TPythonDocServer.Bufferize;
begin
TPythonDocXML.Bufferize(TPythonDocServer.GetDocFile());
end;
procedure TPythonDocServer.ClearBuffer;
begin
TPythonDocXML.ClearBuffer();
end;
function TPythonDocServer.ReadTypeDocStr(const ASymbolName: TSymbol;
const ASymbolType: TPythonDocSymbolType;
const ADeclaringUnitName: string; out ADocStr: string): boolean;
var
LDocs: TDocScanResult;
begin
LDocs := FProvider.Find(ASymbolName, ASymbolType, ADeclaringUnitName);
if not Assigned(LDocs) then
Exit(false);
Result := LDocs.TryGetValue(
TDocScanResult.BuildMemberIdentifier(ASymbolName, ASymbolType), ADocStr);
end;
function TPythonDocServer.ReadTypeDocStr(const ATypeInfo: PTypeInfo;
out ADocStr: string): boolean;
begin
case ATypeInfo^.Kind of
tkClass: Result := ReadTypeDocStr(TSymbol(ATypeInfo^.Name),
TPythonDocSymbolType.Class,
String(ATypeInfo^.TypeData^.UnitName), ADocStr);
//These docs will be enhanced as needed
else raise ENotSupportedException.Create('Type doesn''t supports documentation.');
end;
end;
function TPythonDocServer.ReadTypeDocStr<T>(out ADocStr: string): boolean;
begin
Result := ReadTypeDocStr(TypeInfo(T), ADocStr);
end;
function TPythonDocServer.ReadMemberDocStr(const AParent: PTypeInfo;
const AMember: TRttiMember; out ADocStr: string): boolean;
var
LDocs: TDocScanResult;
begin
case AParent^.Kind of
tkClass: begin
LDocs := FProvider.Find(TSymbol(AParent^.Name), TPythonDocSymbolType.Class,
String(AParent^.TypeData^.UnitName));
if not Assigned(LDocs) then
Exit(false);
Result := LDocs.TryGetValue(
TDocScanResult.BuildMemberIdentifier(AMember), ADocStr);
end;
//These docs will be enhanced as needed
else raise ENotSupportedException.Create('Type doesn''t supports documentation.');
end;
end;
function TPythonDocServer.ReadMemberDocStr<T>(const AMember: TRttiMember;
out ADocStr: string): boolean;
begin
Result := ReadMemberDocStr(TypeInfo(T), AMember, ADocStr);
end;
{ TDocScanResult }
class function TDocScanResult.BuildMemberIdentifier(
const AXmlMember: IXMLNode): string;
begin
Result := ExtractMemberPrefix(AXmlMember) + '_' + AXmlMember.GetAttribute('name');
end;
class function TDocScanResult.BuildMemberIdentifier(
const ARttiNamedType: TRttiNamedObject): string;
begin
Result := TDocScanResult.ExtractMemberPrefix(ARttiNamedType) + '_' + ARttiNamedType.Name;
end;
class function TDocScanResult.ExtractMemberPrefix(
const AXmlMember: IXMLNode): string;
begin
Result := AXmlMember.NodeName;
end;
class function TDocScanResult.BuildMemberIdentifier(
const ATypeInfo: PTypeInfo): string;
var
LRttiContext: TRttiContext;
begin
LRttiContext := TRttiContext.Create();
try
Result := BuildMemberIdentifier(LRttiContext.GetType(ATypeInfo));
finally
LRttiContext.Free();
end;
end;
class function TDocScanResult.ExtractMemberPrefix(
const ARttiNamedType: TRttiNamedObject): string;
begin
Result := String.Empty;
if ARttiNamedType is TRttiType then begin
if TRttiType(ARttiNamedType).IsInstance then
Result := 'class';
end else if ARttiNamedType is TRttiMethod then begin
if TRttiMethod(ARttiNamedType).IsConstructor then
Result := 'constructor'
else if TRttiMethod(ARttiNamedType).IsDestructor then
Result := 'destructor'
else if Assigned(TRttiMethod(ARttiNamedType).ReturnType) then
Result := 'function'
else
Result := 'procedure';
end else if ARttiNamedType is TRttiField then
Result := 'field'
else if ARttiNamedType is TRttiProperty then
Result := 'property';
end;
class function TDocScanResult.BuildMemberIdentifier(const ASymbolName: string;
const ASymbolType: TPythonDocSymbolType): string;
begin
case ASymbolType of
TPythonDocSymbolType.Constant:
Result := 'const_' + ASymbolName;
TPythonDocSymbolType.Variable:
Result := 'var_' + ASymbolName;
TPythonDocSymbolType.Constructor:
Result := 'constructor_' + ASymbolName;
TPythonDocSymbolType.Destructor:
Result := 'destructor_' + ASymbolName;
TPythonDocSymbolType.Procedure:
Result := 'procedure_' + ASymbolName;
TPythonDocSymbolType.Function:
Result := 'function_';
TPythonDocSymbolType.Enum:
Result := 'enum_' + ASymbolName;
TPythonDocSymbolType.Set:
Result := 'set_' + ASymbolName;
TPythonDocSymbolType.Record:
Result := 'record_' + ASymbolName;
TPythonDocSymbolType.Class:
Result := 'class_' + ASymbolName;
end;
end;
end.