-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonLogOutputStream.cpp
More file actions
481 lines (393 loc) · 13.3 KB
/
Copy pathPythonLogOutputStream.cpp
File metadata and controls
481 lines (393 loc) · 13.3 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
#include "PythonUtil/PythonLogOutputStream.h"
#include "PythonUtil/PythonInfos.h"
#include <TkUtil/InformationLog.h>
#include <TkUtil/UTF8String.h>
#include <TkUtil/Exception.h>
#include <TkUtil/InternalError.h>
#include <patchlevel.h> // PY_VERSION_HEX
#include <string.h>
#include <assert.h>
USING_STD
BEGIN_NAMESPACE_UTIL
static const Charset charset ("àéèùô");
string PythonLogOutputStream::defaultPythonShell ("/usr/bin/python");
PythonLogOutputStream::PythonLogOutputStream (
const UTF8String& fileName, const Charset& charset, Log::TYPE mask, bool enableDate, bool enableTime, bool executable)
: ScriptLogOutputStream (fileName, mask, enableDate, enableTime, executable),
_indentLevel (0)
{
setCharset (charset);
setShell (defaultPythonShell);
} // PythonLogOutputStream::PythonLogOutputStream ( )
PythonLogOutputStream::PythonLogOutputStream (const PythonLogOutputStream& stream)
: ScriptLogOutputStream (""),
_indentLevel (0)
{
assert (0 && "PythonLogOutputStream copy constructor is not allowed.");
} // PythonLogOutputStream::PythonLogOutputStream (const PythonLogOutputStream&)
PythonLogOutputStream& PythonLogOutputStream::operator = (const PythonLogOutputStream&)
{
assert (0 && "PythonLogOutputStream assignment operator is not allowed.");
return *this;
} // PythonLogOutputStream::operator =
PythonLogOutputStream::~PythonLogOutputStream ( )
{
} // PythonLogOutputStream::~PythonLogOutputStream
void PythonLogOutputStream::writeShellDeclaration ( )
{
ScriptLogOutputStream::writeShellDeclaration ( );
UTF8String encoding (charset);
switch (getCharset ( ).charset ( ))
{
case Charset::ASCII :
encoding = "-*- coding: ascii -*-";
break;
case Charset::ISO_8859 :
encoding = "-*- coding: iso-8859-1 -*-";
break;
case Charset::UTF_8 :
encoding = "-*- coding: utf-8 -*-";
break;
case Charset::UTF_16 :
encoding = "-*- coding: utf-16 -*-";
break;
} // switch (getCharset ( ).charset ( ))
if (0 != encoding.length ( ))
logComment (InformationLog (encoding));
} // PythonLogOutputStream::writeShellDeclaration
void PythonLogOutputStream::writeScriptComments ( )
{
ScriptLogOutputStream::writeScriptComments ( );
UTF8String pythonVersion (charset);
pythonVersion << "Version de Python : " << PYTHON_VERSION;
logComment (InformationLog (pythonVersion));
UTF8String swigVersion (charset);
swigVersion << "Version de Swig : " << SWIG_VERSION;
logComment (InformationLog (swigVersion));
UTF8String packageVersion (charset);
packageVersion << "Version API PythonUtil : "
<< PythonInfos::getVersion ( ).getVersion ( );
logComment (InformationLog (packageVersion));
} // PythonLogOutputStream::writeScriptComments
void PythonLogOutputStream::writePackagesDeclaration ( )
{
LOCK_LOG_STREAM
write ("import sys");
// Déclaration des chemins d'accès :
const vector<string> paths = getPackagePaths ( );
if (0 != paths.size ( ))
{
logComment (InformationLog (UTF8String (
"Déclaration des chemins d'accès aux paquetages utilisés.", charset)));
declareTryBlock ( );
for (vector<string>::const_iterator it = paths.begin ( );
paths.end ( ) != it; it++)
{
if (0 == (*it).length ( ))
{
UTF8String message (charset);
message << "Déclaration d'un chemin d'accès aux paquetages utilisés du script python "
<< getFileName ( ) << " impossible : chemin nul.";
throw Exception (message);
} // if (0 == (*it).length ( ))
ScriptingLog sl1 ("sys.path", "append", "");
sl1 << *it;
logInstruction (sl1);
} // for (vector<string>::const_iterator it = paths.begin ( ); ...
closeUtilExcTryBlock ( );
} // if (0 != paths.size ( ))
addBlankLine ( );
// Déclaration des paquetages :
const vector<string> packages = getPackages ( );
if (0 != packages.size ( ))
{
logComment (InformationLog (
UTF8String ("Déclaration des paquetages utilisés.", charset)));
declareTryBlock ( );
for (vector<string>::const_iterator it = packages.begin ( );
packages.end ( ) != it; it++)
{
if (0 == (*it).length ( ))
{
UTF8String message (charset);
message << "Déclaration d'un paquetage au script python "
<< getFileName ( ) << " impossible : nom de paquetage nul.";
throw Exception (message);
} // if (0 == (*it).length ( ))
UTF8String instruction (charset);
instruction << "from " << *it << " import *";
write (instruction);
} // for (vector<string>::const_iterator it = packages.begin ( );
closeUtilExcTryBlock ( );
} // if (0 != packages.size ( ))
} // PythonLogOutputStream::writePackagesDeclaration
string PythonLogOutputStream::formatInstruction (const ScriptingLog& log)
{
UTF8String instruction (charset);
instruction << formatResult (log);
if (0 != log.getName ( ).length ( ))
{
if (false == log.useObjectManagerApi ( ))
instruction << log.getName ( );
else
instruction << formatInstanceAccess (log);
if (false == log.getMethodName ( ).empty ( )) // v 5.6.0
instruction << "." << log.getMethodName ( ) << " (";
} // if (0 != log.getName ( ).length ( ))
else
if (false == log.getMethodName ( ).empty ( )) // v 5.6.0
instruction << log.getMethodName ( ) << " (";
const size_t argCount = log.getMethodArgumentsCount ( );
for (size_t i = 0; i < argCount; i++)
{
pair <ScriptingLog::ARG_TYPE, string> arg = log.getMethodArgument (i);
// if ((0 == arg.second.length ( )) && (ScriptingLog::INSTANCE != arg.first))
if ((0 == arg.second.length ( )) && (ScriptingLog::INSTANCE != arg.first) && (ScriptingLog::STRING != arg.first)) // v 5.5.9
{
UTF8String message (charset);
message << (unsigned long)i << "-ème argument de type inconnu ("
<< (unsigned long)arg.first << ") pour l'appel python "
<< log.getName ( ) << "." << log.getMethodName ( )
<< ").";
INTERNAL_ERROR (exc, message, "PythonLogOutputStream::formatInstruction")
throw exc;
} // if (0 == arg.second.length ( ))
if (0 != i)
instruction << ", ";
switch (arg.first)
{
case ScriptingLog::STRING :
instruction << "\"" << arg.second << "\"";
break;
case ScriptingLog::BOOL :
instruction << (0 == strcasecmp (arg.second.c_str ( ), "true") ? "True" : "False"); // v 5.0.0
// 0 == strcasecmp (arg.second.c_str ( ), "true") ?
// (short)1 : (short)0);
break;
case ScriptingLog::LONG :
case ScriptingLog::DOUBLE :
instruction << arg.second; // v 5.6.3
break;
case ScriptingLog::INSTANCE :
instruction << formatInstanceVariableAccess (arg.second);
break;
default :
{
UTF8String message (charset);
message << (unsigned long)i << "-ème argument de type inconnu ("
<< (unsigned long)arg.first << ") pour l'appel python "
<< log.getName ( ) << "." << log.getMethodName ( )
<< ").";
INTERNAL_ERROR (exc, message,
"PythonLogOutputStream::formatInstruction")
throw exc;
} // default
} // switch (arg.first)
} // for (size_t i = 0; i < argCount; i++)
if (false == log.getMethodName ( ).empty ( )) // v 5.6.0
instruction << ")";
// return instruction.iso ( );
return instruction.ascii ( );
} // PythonLogOutputStream::formatInstruction
string PythonLogOutputStream::formatResult (const ScriptingLog& log)
{
if (false == log.hasResult ( ))
return "";
return log.getResult ( ) + " = ";
} // PythonLogOutputStream::formatResult
string PythonLogOutputStream::formatInstanceAccess (const ScriptingLog& log)
{
if (false == log.useObjectManagerApi ( ))
{
UTF8String message (charset);
message << "Le log " << log.getName ( ) << "." << log.getMethodName ( )
<< " n'est pas associé à un objet.";
INTERNAL_ERROR (exc, message,
"PythonLogOutputStream::formatInstanceAccess")
throw exc;
} // if (false == log.useObjectManagerApi ( ))
UTF8String access (charset);
access << "getObjectManagerInstance ( ).getInstance (\""
<< log.getName ( ) << "\")";
// return access.iso ( );
return access.ascii ( );
} // PythonLogOutputStream::formatInstanceAccess
string PythonLogOutputStream::formatInstanceVariableAccess (
const string& uniqueName)
{
if (0 == uniqueName.length ( ))
return "None";
try
{
// On s'assure que l'instance est bien recensée :
getObjectManagerInstance ( ).getInstance (uniqueName);
UTF8String access (charset);
access << "getObjectManagerInstance ( ).getInstance (\""
<< uniqueName << "\")";
// return access.iso ( );
return access.ascii ( );
}
catch (...)
{ // Objet non référencé : variable du script ?
return uniqueName;
}
} // PythonLogOutputStream::formatInstanceVariableAccess
UTF8String PythonLogOutputStream::formatComment (const Log& log, bool strict)
{
return toComment (log.getText ( ));
} // PythonLogOutputStream::formatComment
void PythonLogOutputStream::write (const UTF8String& str)
{
if (true == isCommentLine (str))
{
ScriptLogOutputStream::write (str);
return;
} // if (0 == strncmp (str.c_str ( ), "#", 1))
if (0 == _indentLevel)
ScriptLogOutputStream::write (str);
else
{
UTF8String line (charset);
for (size_t i = 0; i < _indentLevel; i++)
line << "\t";
line << str;
ScriptLogOutputStream::write (line);
} // else if (0 == _indentLevel)
} // PythonLogOutputStream::write
void PythonLogOutputStream::declareBlock ( )
{
indent ( );
} // PythonLogOutputStream::declareBlock
void PythonLogOutputStream::closeBlock ( )
{
try
{
unindent ( );
}
catch (const Exception& exc)
{
UTF8String message (charset);
message << "PythonLogOutputStream::closeBlock : Erreur : "
<< exc.getFullMessage ( );
throw Exception (message);
}
catch (...)
{
throw Exception ("PythonLogOutputStream::closeBlock : erreur non documentée.");
}
} // PythonLogOutputStream::closeBlock
void PythonLogOutputStream::declareTryBlock ( )
{
write (UTF8String ("try :", charset));
declareBlock ( );
} // PythonLogOutputStream::declareTryBlock
void PythonLogOutputStream::closeTryBlock ( )
{
closeBlock ( );
#if PY_VERSION_HEX >= 0x03000000 // v 5.6.2
write (UTF8String ("except RuntimeError as re :", charset));
#else // #if PY_VERSION_HEX >= 0x03000000
write (UTF8String ("except RuntimeError , re :", charset));
#endif // #if PY_VERSION_HEX >= 0x03000000
declareBlock ( );
ScriptingLog i1 ("sys.stderr", "write", "");
i1 << "Erreur rencontrée : ";
logInstruction (i1);
ScriptingLog i2 ("sys.stderr", "write", "");
i2.addVariable ("str (re)");
logInstruction (i2);
closeBlock ( );
} // PythonLogOutputStream::closeTryBlock
void PythonLogOutputStream::closeUtilExcTryBlock ( )
{
closeBlock ( );
#if PY_VERSION_HEX >= 0x03000000 // v 5.6.2
write (UTF8String ("except Exception as exc :", charset));
#else // #if PY_VERSION_HEX >= 0x03000000
write (UTF8String ("except Exception , exc :", charset));
#endif // #if PY_VERSION_HEX >= 0x03000000
declareBlock ( );
ScriptingLog i1 ("sys.stderr", "write", "");
i1 << "Erreur rencontrée : ";
logInstruction (i1);
ScriptingLog i2 ("sys.stderr", "write", "");
i2.addVariable ("str (exc)");
logInstruction (i2);
// Exception standard :
closeTryBlock ( );
} // PythonLogOutputStream::closeUtilExcTryBlock
const string& PythonLogOutputStream::getCommentDeclaration ( )
{
static const string commentDeclaration ("#");
return commentDeclaration;
} // PythonLogOutputStream::getCommentDeclaration
bool PythonLogOutputStream::isCommentLine (const string& line)
{
return 0 == line.find (getCommentDeclaration ( )) ? true : false;
} // PythonLogOutputStream::isCommentLine
UTF8String PythonLogOutputStream::toComment (const UTF8String& t)
{
const char newline ('\n');
UTF8String text (t);
UTF8String comment (charset);
string::size_type pos = text.find (newline);
const string cd = getCommentDeclaration ( ); // v 3.6.0
while (string::npos != pos)
{ // Rem : chaque ligne commence peut être déjà par un
// getCommentDeclaration ( ) : on ne le rajoute que si nécessaire.
if (0 != text.find (cd)) // v 3.6.0
comment << cd << ' ';
comment << text.substring (0, pos);// << "\n";
const string::size_type len = text.length ( );
if (len - 1 == pos) // v 4.5.1 : la chaine finit par '\n' !
{
text.clear ( );
pos = string::npos;
}
else
{
text = text.substring (pos + 1);
pos = text.find (newline);
}
} // while (string::npos != pos)
if (false == text.empty ( )) // v 4.5.1
{
if (0 != text.find (cd)) // v 3.6.0
comment << cd << ' ';
comment << text;
} // if (false == text.empty ( ))
return comment;
} // PythonLogOutputStream::toComment
bool PythonLogOutputStream::isMultiLines (const string& block, vector<string>& lines)
{
lines.clear ( );
bool completed = false;
string s = block;
while (false == completed)
{
string::size_type nl = s.find ('\n');
if (string::npos == nl)
{
lines.push_back (s);
completed = true;
} // if (string::npos == nl)
else
{
string s2 = s.substr (0, nl);
lines.push_back (s2);
s = s.substr (nl + 1);
} // else if (string::npos == nl)
} // while (false == completed)
return 1 <= lines.size ( ) ? false : true;
} // PythonLogOutputStream::isMultiLines
void PythonLogOutputStream::indent ( )
{
_indentLevel++;
} // PythonLogOutputStream::indent
void PythonLogOutputStream::unindent ( )
{
if (0 == _indentLevel)
throw Exception ("PythonLogOutputStream::unindent : décrémentation de l'indentation impossible, niveau nul.");
_indentLevel--;
} // PythonLogOutputStream::unindent
END_NAMESPACE_UTIL