-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalization_blue.cpp
More file actions
809 lines (716 loc) · 26.8 KB
/
localization_blue.cpp
File metadata and controls
809 lines (716 loc) · 26.8 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// Copyright © 2014 CCP ehf.
#include "stdafx.h"
#include "localization.h"
#include "parser.h"
const Be::VarChooser TokenFlagChooser[] =
{
{ "LINKIFY", BeCast( TOKENFLAG_LINKIFY ), "This token should be turned into a link" },
{ "CAPITALIZE", BeCast( TOKENFLAG_CAPITALIZE ), "This token should be capitalized" },
{ "UPPERCASE", BeCast( TOKENFLAG_UPPERCASE ), "This token should be uppercased" },
{ "LOWERCASE", BeCast( TOKENFLAG_LOWERCASE ), "This token should be lowercased" },
{ "TITLECASE", BeCast( TOKENFLAG_TITLECASE ), "This token should be titlecased" },
{ "USEGROUPING", BeCast( TOKENFLAG_USEGROUPING ), "This token should use numeric grouping" },
{ "CONDITIONAL", BeCast( TOKENFLAG_CONDITIONAL ), "This token includes a conditional value" },
{ "LINKINFO", BeCast( TOKENFLAG_LINKINFO ), "This token includes a linkinfo tag" },
{ "QUANTITY", BeCast( TOKENFLAG_QUANTITY ), "This token includes a quantity tag" },
{ "DECIMALPLACES", BeCast( TOKENFLAG_DECIMALPLACES ), "This token includes a decimalPlaces keyword for number formatting." },
{ "LEADINGZEROES", BeCast( TOKENFLAG_LEADINGZEROES ), "This token includes a leadingZeroes keyword for number formatting." },
{ 0 }
};
BLUE_REGISTER_ENUM_EX("TOKEN_FLAG", TokenFlag, TokenFlagChooser, ENUM_REG_ENUM_OBJECT_ON_MODULE);
const Be::VarChooser VariableTypeChooser[] =
{
{ "CHARACTER", BeCast( VARIABLETYPE_CHARACTER ), "This token should be handled by character handlers." },
{ "NPCORGANIZATION", BeCast( VARIABLETYPE_NPCORGANIZATION ), "This token should be handled by npcorganization handlers." },
{ "ITEM", BeCast( VARIABLETYPE_ITEM ), "This token should be handled by item handlers." },
{ "LOCATION", BeCast( VARIABLETYPE_LOCATION ), "This token should be handled by location handlers." },
{ "CHARACTERLIST", BeCast( VARIABLETYPE_CHARACTERLIST ), "This token should be handled by characterlist handlers." },
{ "MESSAGE", BeCast( VARIABLETYPE_MESSAGE ), "This token should be handled by message handlers." },
{ "DATETIME", BeCast( VARIABLETYPE_DATETIME ), "This token should be handled by datetime handlers." },
{ "FORMATTEDTIME", BeCast( VARIABLETYPE_FORMATTEDTIME ), "This token should be handled by formattedtime handlers." },
{ "TIMEINTERVAL", BeCast( VARIABLETYPE_TIMEINTERVAL ), "This token should be handled by timeinterval handlers." },
{ "NUMERIC", BeCast( VARIABLETYPE_NUMERIC ), "This token should be handled by numeric handlers." },
{ "GENERIC", BeCast( VARIABLETYPE_GENERIC ), "This token should be handled by generic handlers." },
{ 0 }
};
BLUE_REGISTER_ENUM_EX("VARIABLE_TYPE", VariableType, VariableTypeChooser, ENUM_REG_ENUM_OBJECT_ON_MODULE);
// -------------------------------------------------------------
// Description:
// Creates the eveLocalization storage for the given language if it does not exist yet, otherwise
// it updates an existing storage with the provided information.
// Arguments:
// module - Ignored
// args - The language for which we provide the messages and a dictionary describing message data.
// The format of the message data dictionary must be this: {messageID:(text,metadata,tokens),...}
// Return value:
// None.
// -------------------------------------------------------------
PyObject* PyLoadMessageData( PyObject* module, PyObject* args )
{
CCP_STATS_ZONE( __FUNCTION__ );
char* languageCode = 0;
PyObject* dataDict = 0;
if ( PyArg_ParseTuple( args, "sO", &languageCode, &dataDict ) )
{
if ( !PyDict_Check( dataDict ) )
{
PyErr_SetString( PyExc_TypeError, "Localization::LoadMessageData expects a dictionary." );
return NULL;
}
LanguageID langID = CodeToLanguageID( languageCode );
Language* lang = CCP_NEW( "language" ) Language( langID );
MessageMap& mm = lang->data;
PyObject* key;
PyObject* value;
Py_ssize_t pos = 0;
while ( PyDict_Next( dataDict, &pos, &key, &value ) )
{
if( !key )
{
PyErr_SetString( PyExc_TypeError, "Localization::LoadMessageData - key is NULL" );
return NULL;
}
if ( ! PyTuple_Check( value ) || PyTuple_Size( value ) != 3 )
{
PyErr_SetString( PyExc_TypeError,
"Localization::LoadMessageData - dictionary value must be a 3 element tuple describing sourceText, metaData and tokens." );
return NULL;
}
MessageID id;
if ( PyLong_Check( key ) )
{
id = PyLong_AsUnsignedLongLong( key );
}
else
{
PyErr_SetString( PyExc_TypeError, "Localization::LoadMessageData - dictionary must be keyed on int or long" );
return NULL;
}
MessageData* md = CCP_NEW( "PyLoadMessageData/md" ) MessageData;
// We use PyTuple_GET_ITEM for speed - we've already checked that we have a tuple
// and that it is of the correct size. Also remember that PyTuple_GET_ITEM returns
// a borrowed reference - we're not leaking the objects.
PyObject* text = PyTuple_GET_ITEM( value, 0 );
if ( ! PyUnicode_Check( text ) )
{
PyErr_SetString( PyExc_TypeError, "Localization::LoadMessageData - first tuple entry must be unicode string" );
return NULL;
}
PyObject* metaData = PyTuple_GET_ITEM( value, 1 );
if ( metaData != Py_None )
{
if ( ! PyDict_Check( metaData ) )
{
PyErr_SetString( PyExc_TypeError, "Localization::LoadMessageData - second tuple entry must be a dictionary or None" );
return NULL;
}
md->metaData = CCP_NEW( "eveLocalization/MetaData" ) MetaData();
PyObject* key;
PyObject* val;
Py_ssize_t pos = 0;
while ( PyDict_Next( metaData, &pos, &key, &val ) )
{
// temporary bug in pickle processing means that metadata keys are being exported as unicode.
// Convert to string until this is fixed.
PyObject *tmp = PyObject_Str(key);
if (tmp)
{
std::string k( PyUnicode_AsUTF8( tmp ) );
Py_DECREF(tmp);
std::wstring v( PyUnicodeToWString( val ) );
md->metaData->insert( MetaData::value_type( k, v ) );
}
}
}
PyObject* token = PyTuple_GET_ITEM( value, 2 );
if ( token != Py_None )
{
if ( ! PyDict_Check( token ) )
{
PyErr_SetString( PyExc_TypeError, "Localization::LoadMessageData - third tuple entry must be a dictionary or None" );
return NULL;
}
if ( ! LoadTokens( token, *md ) )
{
// Error state was set from within ParseTokens
return NULL;
}
}
md->text = PyUnicodeToWString( text );
mm[id] = md;
}
LanguageMap::iterator oldLang = g_settings.languages.find( langID );
if ( oldLang != g_settings.languages.end() )
{
CCP_STATS_ZONE( "Updating language" );
// update the already-existing language with new data
for ( MessageMap::iterator i = lang->data.begin(); i != lang->data.end(); ++i )
{
oldLang->second->data[i->first] = i->second;
}
}
else
{
CCP_STATS_ZONE( "Inserting language" );
// insert the new language
g_settings.languages.insert( LanguageMap::value_type( langID, lang ) );
}
}
Py_RETURN_NONE;
}
MAP_FUNCTION( "LoadMessageData", PyLoadMessageData, "Load the message data we are operating on." );
// -------------------------------------------------------------
// Description:
// Clear the whole eveLocalization storage.
// Arguments:
// module - Ignored
// args - Ignored
// Return value:
// None
// -------------------------------------------------------------
PyObject* PyUnloadAllMessageData( PyObject* module, PyObject* args )
{
for( auto it = g_settings.languages.begin(); it != g_settings.languages.end(); ++it )
{
CCP_DELETE it->second;
}
g_settings.languages.clear();
// And make sure that we do have the default language, as it won't be reinitialized elsewhere
Language* lang = CCP_NEW( "language" ) Language( LANGUAGEID_SYSTEM_DEFAULT );
g_settings.languages.insert( LanguageMap::value_type( LANGUAGEID_SYSTEM_DEFAULT, lang ));
Py_RETURN_NONE;
}
MAP_FUNCTION( "UnloadAllMessageData", PyUnloadAllMessageData, "Unloads all stored message data." );
// -------------------------------------------------------------
// Description:
// Clear the eveLocalization storage for the specified language.
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. Must be the language we want to unload.
// Return value:
// None
// -------------------------------------------------------------
PyObject* PyUnloadMessageData( PyObject* module, PyObject* args )
{
char* languageCode;
if ( ! PyArg_ParseTuple( args, "s", &languageCode ) )
{
return NULL;
}
LanguageID langID = CodeToLanguageID( languageCode );
LanguageMapIt it = g_settings.languages.find( langID );
if ( it != g_settings.languages.end() )
{
g_settings.languages.erase( it );
}
Py_RETURN_NONE;
}
MAP_FUNCTION( "UnloadMessageData", PyUnloadMessageData, "Unloads message data for the specified language, if it's loaded." );
// -------------------------------------------------------------
// Description:
// Determine if a message for the given ID exists in the specified language.
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. Must be message ID and language.
// Return value:
// True if the message exists, false otherwise.
// -------------------------------------------------------------
PyObject* PyIsValidMessageID( PyObject* module, PyObject* args )
{
MessageID messageID;
char* languageCode;
if ( ! PyArg_ParseTuple( args, "Ks", &messageID, &languageCode ) )
{
return NULL;
}
LanguageID langID = CodeToLanguageID( languageCode );
LanguageMapCit cit = g_settings.languages.find( langID );
if ( cit == g_settings.languages.cend() )
{
Py_RETURN_FALSE;
}
MessageMapCit msg = cit->second->data.find( messageID );
if ( msg == cit->second->data.cend() )
{
Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
}
MAP_FUNCTION( "IsValidMessageID", PyIsValidMessageID, "Checks if messageID exists in the specified language." );
// -------------------------------------------------------------
// Description:
// Return the raw text for a specified message id and language.
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. Must be message ID and language.
// Return value:
// A unicode string with the raw message text.
// -------------------------------------------------------------
PyObject* PyGetRawByMessageID( PyObject* module, PyObject* args )
{
MessageID messageID;
char* languageCode;
if ( ! PyArg_ParseTuple( args, "Ks", &messageID, &languageCode ) )
{
return NULL;
}
LanguageID langID = CodeToLanguageID( languageCode );
LanguageMapCit cit = g_settings.languages.find( langID );
if ( cit == g_settings.languages.cend() )
{
PyErr_SetString( PyExc_KeyError, "No such language." );
return NULL;
}
MessageMapCit msg = cit->second->data.find( messageID );
if ( msg == cit->second->data.cend() )
{
PyErr_SetString( PyExc_KeyError, "No such message." );
return NULL;
}
return PyUnicode_FromWideChar( msg->second->text.c_str(), msg->second->text.size() );
}
MAP_FUNCTION( "GetRawByMessageID", PyGetRawByMessageID, "Returns the raw, unparsed message by ID" );
// -------------------------------------------------------------
// Description:
// Fetch the actual data for a given messageID and language.
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. Must include messageID and language.
// Return value:
// A 3-tuple describing (text, metadata, tokens)
// -------------------------------------------------------------
PyObject* PyGetMessageDataByID( PyObject* module, PyObject* args )
{
MessageID messageID;
char* languageCode;
PyObject* ret = NULL;
if ( PyArg_ParseTuple( args, "Ks", &messageID, &languageCode ) )
{
LanguageID lid = CodeToLanguageID( languageCode );
Language& lang = *g_settings.languages[lid];
MessageMapCit mdit = lang.data.find(messageID);
if ( mdit == lang.data.cend() )
{
PyErr_SetString( PyExc_KeyError, "No such element" );
return NULL;
}
const MessageData& msg = *mdit->second;
PyObject* text = PyUnicode_FromWideChar( msg.text.c_str(), msg.text.size() );
PyObject* tokens = NULL;
PyObject* metaData = NULL;
if ( msg.metaData )
{
metaData = PyDict_New();
for ( MetaDataCit cit = msg.metaData->cbegin(); cit != msg.metaData->cend(); ++cit )
{
PyObject* tmp = PyUnicode_FromWideChar( cit->second.c_str(), cit->second.size() );
PyDict_SetItemString( metaData, cit->first.c_str(), tmp );
Py_XDECREF( tmp );
}
}
if ( msg.tokens )
{
tokens = PyDict_New();
for ( TokenContainerCit cit = msg.tokens->cbegin(); cit != msg.tokens->cend(); ++cit )
{
const Token& tok = *cit->second;
PyObject* temp = NULL;
PyObject* token = PyDict_New();
temp = PyUnicode_FromStringAndSize( tok.variableName.c_str(), tok.variableName.size() );
PyDict_SetItemString( token, "variableName", temp);
Py_XDECREF( temp );
temp = PyLong_FromLong( (long) tok.variableType );
PyDict_SetItemString( token, "variableType", temp);
Py_XDECREF( temp );
if ( tok.propertyName.size() > 0 )
{
temp = PyUnicode_FromStringAndSize( tok.propertyName.c_str(), tok.propertyName.size() );
}
else
{
temp = Py_None;
Py_INCREF( temp );
}
PyDict_SetItemString( token, "propertyName", temp );
Py_XDECREF( temp );
temp = PyLong_FromLong( tok.flags );
PyDict_SetItemString( token, "args", temp );
Py_XDECREF( temp );
temp = PyDict_New();
if ( tok.kwargs )
{
for ( KeywordArgsCit kw = tok.kwargs->cbegin(); kw != tok.kwargs->cend(); ++kw )
{
PyDict_SetItemString( temp, kw->first.c_str(), kw->second );
}
}
PyDict_SetItemString( token, "kwargs", temp );
Py_XDECREF( temp );
temp = PyList_New(0);
for ( size_t i = 0; i < 3; ++i )
{
if ( 0 == tok.conditionalValues[i].size() )
{
break;
}
PyObject* tmp = PyUnicode_FromWideChar( tok.conditionalValues[i].c_str(), tok.conditionalValues[i].size() );
PyList_Append( temp, tmp );
Py_XDECREF( tmp );
}
PyDict_SetItemString( token, "conditionalValues", temp );
Py_XDECREF( temp );
temp = PyUnicode_FromWideChar( tok.tagName.c_str(), tok.tagName.size() );
PyDict_SetItem( tokens, temp, token );
Py_XDECREF( temp );
Py_XDECREF( token );
}
}
ret = Py_BuildValue("(OOO)", text, metaData ? metaData : Py_None, tokens ? tokens : Py_None );
Py_XDECREF( text );
Py_XDECREF( metaData );
Py_XDECREF( tokens );
}
return ret;
}
MAP_FUNCTION( "GetMessageDataByID", PyGetMessageDataByID, "Returns the message data tuple for a specific message ID." );
// -------------------------------------------------------------
// Description:
// Checks if this Language exists in the eveLocalization storage.
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. Must include the language.
// Return value:
// True if there is MessageData for this language, False other.
// -------------------------------------------------------------
PyObject* PyHasLanguage( PyObject* module, PyObject* args )
{
char* languageCode;
if ( PyArg_ParseTuple( args, "s", &languageCode ) )
{
LanguageID langID = CodeToLanguageID( languageCode );
LanguageMapCit lang = g_settings.languages.find( langID );
if ( lang == g_settings.languages.cend() )
{
Py_RETURN_FALSE;
}
else
{
Py_RETURN_TRUE;
}
}
return NULL;
}
MAP_FUNCTION( "HasLanguage", PyHasLanguage, "Returns true if we have data for the specified language, false otherwise." );
// -------------------------------------------------------------
// Description:
// Set the default (a.k.a. primary) language that should be used as fallback.
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. Must include the language.
// Return value:
// None
// -------------------------------------------------------------
PyObject* PySetPrimaryLanguage( PyObject* module, PyObject* args )
{
char* languageCode;
if ( PyArg_ParseTuple( args, "s", &languageCode ) )
{
g_settings.defaultLanguage = CodeToLanguageID( languageCode );
Py_RETURN_NONE;
}
return NULL;
}
MAP_FUNCTION( "SetPrimaryLanguage", PySetPrimaryLanguage, "Set the default fallback language." );
// -------------------------------------------------------------
// Description:
// Get the currently set default language
// Arguments:
// module - Ignored
// args - Ignored
// Return value:
// Python representation of the default language currently set
// -------------------------------------------------------------
PyObject* PyGetPrimaryLanguage( PyObject* module, PyObject* args )
{
return PyUnicode_FromString( LanguageIDToCode( g_settings.defaultLanguage ) ) ;
}
MAP_FUNCTION( "GetPrimaryLanguage", PyGetPrimaryLanguage, "Returns the default fallback language." );
// -------------------------------------------------------------
// Description:
// Returns the unicode symbol representing a decimal point.
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. Must include the language.
// Return value:
// A unicode string containing the symbol to use as decimal point for the given language.
// -------------------------------------------------------------
PyObject* PyGetDecimalSeparator( PyObject* module, PyObject* args )
{
PyObject *retVal = NULL;
char* languageCode;
if ( PyArg_ParseTuple( args, "s", &languageCode ) )
{
LanguageID langID = CodeToLanguageID( languageCode );
LanguageMapCit lang = g_settings.languages.find( langID );
if ( lang != g_settings.languages.cend() )
{
const Language& l = *lang->second;
wchar_t w = l.decimalSep;
retVal = PyUnicode_FromWideChar( &w, 1 );
}
else
{
PyErr_SetString( PyExc_KeyError, "Could not find language." );
}
}
else
{
PyErr_SetString( PyExc_TypeError, "Method expects the language code to be passed in." );
}
return retVal;
}
MAP_FUNCTION( "GetDecimalSeparator", PyGetDecimalSeparator, "Returns the symbol to be used as decimal point in this locale." );
// -------------------------------------------------------------
// Description:
// Returns the unicode symbol to use when grouping numbers.
// Note: In general you would not want to use this directly because different locales have different rules for number grouping.
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. Must include the language we want to know the thousand separator for.
// Return value:
// Unicode string describing the symbol to use for number grouping.
// -------------------------------------------------------------
PyObject* PyGetThousandSeparator( PyObject* module, PyObject* args )
{
PyObject *retVal = NULL;
char* languageCode;
if ( PyArg_ParseTuple( args, "s", &languageCode ) )
{
LanguageID langID = CodeToLanguageID( languageCode );
LanguageMapCit lang = g_settings.languages.find( langID );
if ( lang != g_settings.languages.cend() )
{
const Language& l = *lang->second;
wchar_t w = l.thousandSep;
retVal = PyUnicode_FromWideChar( &w, 1 );
}
else
{
PyErr_SetString( PyExc_KeyError, "Could not find language." );
}
}
else
{
PyErr_SetString( PyExc_TypeError, "Method expects the language code to be passed in." );
}
return retVal;
}
MAP_FUNCTION( "GetThousandSeparator", PyGetThousandSeparator, "Returns the symbol to be used as thousand separator in this locale." );
// -------------------------------------------------------------
// Description:
// Python-exposed function to retrieve metadata associated with a message.
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. This must include the messageID, property name
// and language we want to fetch metadata for.
// Return value:
// Metadata for the specified message, or a KeyError if not found
// -------------------------------------------------------------
PyObject* PyGetMetaDataByID( PyObject* module, PyObject* args )
{
MessageID messageID;
char* languageCode;
char* propertyName;
if ( PyArg_ParseTuple( args, "Kss", &messageID, &propertyName, &languageCode ) )
{
LanguageID langID = CodeToLanguageID( languageCode );
LanguageMapCit lang = g_settings.languages.find( langID );
if ( lang == g_settings.languages.cend() )
{
char tmp[128];
sprintf_s(tmp, "Language %s does not exist.", languageCode );
PyErr_SetString( PyExc_KeyError, tmp );
return NULL;
}
const MessageMap& mm = lang->second->data;
MessageMapCit cit = mm.find(messageID);
// Not in the language we requested? Ah well, let's try with the fallback language then
if ( cit == mm.cend() )
{
const MessageMap& mm = g_settings.languages[g_settings.defaultLanguage]->data;
cit = mm.find(messageID);
if ( cit == mm.cend() )
{
char tmp[128];
sprintf_s(tmp, "Message ID %llu does not exists neither in requested nor in fallback language.", messageID );
PyErr_SetString( PyExc_KeyError, tmp );
return NULL;
}
}
const MessageData& msg = *cit->second;
if ( msg.metaData )
{
MetaDataCit cit = msg.metaData->find( propertyName );
if ( cit != msg.metaData->cend() )
{
return PyUnicode_FromWideChar( cit->second.c_str(), cit->second.size() );
}
}
char tmp[128];
sprintf_s(tmp, "Message ID %llu does not have metadata for property %s.", messageID, propertyName );
PyErr_SetString( PyExc_KeyError, tmp );
return NULL;
}
return NULL;
}
MAP_FUNCTION( "GetMetaDataByID", PyGetMetaDataByID, "Returns the meta data associated with the given ID and property name."\
"Raises KeyError if no metaData for the supplied parameters exists." );
// -------------------------------------------------------------
// Description:
// Set the time delta for date/time formatting. Since we are always running in GMT but need to display GMT+8 for China
// then we are adding
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. This should contain the amount of seconds we add to the current time.
// Return value:
// None if successful, raises an exception in error case
// -------------------------------------------------------------
PyObject* PySetTimeDelta( PyObject* module, PyObject* args )
{
if ( ! PyArg_ParseTuple( args, "L", &g_settings.timeDelta ) )
{
return NULL;
}
Py_RETURN_NONE;
}
MAP_FUNCTION( "SetTimeDelta", PySetTimeDelta, "Adjust the actual time by timeDelta seconds when formatting date/time.");
// -------------------------------------------------------------
// Description:
// Return the currently specified timeDelta, relative to GMT, in seconds.
// Arguments:
// module - Ignored
// args - Ignored
// Return value:
// The currently specified timeDelta in seconds.
// -------------------------------------------------------------
PyObject* PyGetTimeDelta( PyObject* module, PyObject* args )
{
PyObject* ret = PyLong_FromLongLong( (long long) g_settings.timeDelta );
return ret;
}
MAP_FUNCTION( "GetTimeDelta", PyGetTimeDelta, "Returns the current timeDelta, in seconds.");
// -------------------------------------------------------------
// Description:
// Allow python to fetch a localized message by ID and language.
// Note: This is exposed from localization.cpp because the blue macros don't deal with kwargs
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python. This should contain the ID of the message we are requesting as well as the
// language the message should be in.
// kwargs - Keyword arguments passed in from Python. These are the parameters for message replacement.
// Return value:
// A unicode string containing the processed message, or a KeyError if not found.
// -------------------------------------------------------------
PyObject* PyGetMessageByID( PyObject* module, PyObject* args, PyObject* kwargs )
{
MessageID messageID;
char* languageCode;
if ( PyArg_ParseTuple( args, "Ks", &messageID, &languageCode ) )
{
LanguageID langID = CodeToLanguageID( languageCode );
LanguageMapCit lang = g_settings.languages.find( langID );
if ( lang == g_settings.languages.cend() )
{
char tmp[128];
sprintf_s(tmp, "Language %s does not exist.", languageCode );
PyErr_SetString( PyExc_KeyError, tmp );
return NULL;
}
const MessageMap& mm = lang->second->data;
MessageMapCit cit = mm.find(messageID);
// Not in the language we requested? Ah well, let's try with the fallback language then
if ( cit == mm.cend() )
{
const MessageMap& mm = g_settings.languages[g_settings.defaultLanguage]->data;
cit = mm.find(messageID);
if ( cit == mm.cend() )
{
char tmp[128];
sprintf_s(tmp, "Message ID %llu does not exists neither in requested nor in fallback language.", messageID );
PyErr_SetString( PyExc_KeyError, tmp );
return NULL;
}
}
const MessageData& msg = *cit->second;
if ( msg.tokens && kwargs )
{
std::wstringstream text;
bool ret = Parse( msg.text, *lang->second, *( msg.tokens ), kwargs, text );
if ( ! ret )
{
// Actual exception object should be set in Parse.
return NULL;
}
std::wstring tmp = text.str();
return PyUnicode_FromWideChar( tmp.c_str(), tmp.size() );
}
else
{
return PyUnicode_FromWideChar( msg.text.c_str(), msg.text.size() );
}
}
return NULL;
}
// -------------------------------------------------------------
// Description:
// Python-exposed utility function for locale-aware number formatting.
// Note: This is exposed from localization.cpp because the blue macros don't deal with kwargs
// Arguments:
// module - Ignored
// args - Argument tuple passed in from python
// kwargs - Keyword arguments passed in from Python.
// Return value:
// A unicode string containing the formatted number.
// See also:
// NumericFormatter
// -------------------------------------------------------------
PyObject* PyFormatNumeric( PyObject* module, PyObject* args, PyObject* kwargs )
{
PyObject* value = NULL;
char* languageCode;
size_t result = 0;
wchar_t number[STACK_BUFFER_SIZE_LARGE];
PyObject* tmp = NULL;
if ( ! PyArg_ParseTuple( args, "Os", &value, &languageCode ) )
{
return NULL;
}
// Initialize our local variables...
bool useGrouping = false;
int decimalPlaces = 2;
int leadingZeroes = 0;
if ( kwargs )
{
tmp = PyDict_GetItemString( kwargs, "useGrouping" );
if ( tmp )
{
useGrouping = tmp == Py_True;
}
tmp = PyDict_GetItemString( kwargs, "decimalPlaces" );
if ( tmp && tmp != Py_None && PyLong_Check( tmp ) )
{
decimalPlaces = std::max( 0, std::min( 9, (int) PyLong_AS_LONG( tmp ) ) );
}
tmp = PyDict_GetItemString( kwargs, "leadingZeroes" );
if ( tmp && tmp != Py_None && PyLong_Check( tmp ) )
{
leadingZeroes = std::max( 0, std::min( 9, (int) PyLong_AS_LONG( tmp ) ) );
}
}
result = FormatNumber( number, value, decimalPlaces, leadingZeroes, useGrouping );
if ( 0 == result )
{
return NULL;
}
return PyUnicode_FromWideChar( number, result );
}