-
Notifications
You must be signed in to change notification settings - Fork 24
/
proc_py.pas
executable file
·90 lines (72 loc) · 2.07 KB
/
proc_py.pas
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
unit proc_py;
{$mode objfpc}{$H+}
interface
uses
SysUtils,
PythonEngine;
type
TStringDecodeRecW = record
SFrom, STo: WideString;
end;
procedure Py_SetSysPath(const Dirs: array of string; DoAdd: boolean);
function SBegin(const s, sub: string): boolean;
function SDecodeW(const S: WideString; const Decode: array of TStringDecodeRecW): WideString;
function SWideStringToPythonString(const Str: Widestring): string;
const
cDefFixedFontName = {$ifdef windows} 'Consolas' {$else} {$ifdef linux} 'Ubuntu Mono' {$else} 'Monaco' {$endif} {$endif};
cDefFixedFontSize = {$ifdef windows} 9 {$else} 11 {$endif};
cDefVarFontName = {$ifdef windows} 'Tahoma' {$else} {$ifdef linux} 'Ubuntu' {$else} 'default' {$endif} {$endif};
cDefVarFontSize = {$ifdef windows} 9 {$else} 10 {$endif};
implementation
function SDecodeW(const S: WideString; const Decode: array of TStringDecodeRecW): WideString;
var
i, j: Integer;
DoDecode: Boolean;
begin
Result := '';
i := 1;
repeat
if i > Length(S) then Break;
DoDecode := False;
for j := Low(Decode) to High(Decode) do
with Decode[j] do
if (SFrom <> '') and (SFrom = Copy(S, i, Length(SFrom))) then
begin
DoDecode := True;
Result := Result + STo;
Inc(i, Length(SFrom));
Break
end;
if DoDecode then Continue;
Result := Result + S[i];
Inc(i);
until False;
end;
function SWideStringToPythonString(const Str: Widestring): string;
const
Decode: array[0..0] of TStringDecodeRecW =
((SFrom: '"'; STo: '"+''"''+"'));
begin
Result:= UTF8Encode(SDecodeW(Str, Decode));
Result:= 'r"'+Result+'"';
end;
function SBegin(const s, sub: string): boolean;
begin
Result:= Copy(s, 1, Length(sub))=sub;
end;
procedure Py_SetSysPath(const Dirs: array of string; DoAdd: boolean);
var
Str, Sign: string;
i: Integer;
begin
Str:= '';
for i:= 0 to Length(Dirs)-1 do
Str:= Str + 'r"' + Dirs[i] + '"' + ',';
if DoAdd then
Sign:= '+='
else
Sign:= '=';
Str:= Format('sys.path %s [%s]', [Sign, Str]);
GetPythonEngine.ExecString(Str);
end;
end.