Commit f7c51083 authored by Andi Vajda's avatar Andi Vajda
Browse files

added wrapper for LocaleMatcher.acceptLanguageFromHTTP()

parent eb91012b
...@@ -712,6 +712,9 @@ Formattable *toFormattableArray(PyObject *arg, int *len, ...@@ -712,6 +712,9 @@ Formattable *toFormattableArray(PyObject *arg, int *len,
*len = (int) PySequence_Size(arg); *len = (int) PySequence_Size(arg);
Formattable *array = new Formattable[*len + 1]; Formattable *array = new Formattable[*len + 1];
if (!array)
return (Formattable *) PyErr_NoMemory();
for (int i = 0; i < *len; i++) { for (int i = 0; i < *len; i++) {
PyObject *obj = PySequence_GetItem(arg, i); PyObject *obj = PySequence_GetItem(arg, i);
...@@ -752,6 +755,9 @@ static UnicodeString *toUnicodeStringArray(PyObject *arg, int *len) ...@@ -752,6 +755,9 @@ static UnicodeString *toUnicodeStringArray(PyObject *arg, int *len)
*len = (int) PySequence_Size(arg); *len = (int) PySequence_Size(arg);
UnicodeString *array = new UnicodeString[*len + 1]; UnicodeString *array = new UnicodeString[*len + 1];
if (!array)
return (UnicodeString *) PyErr_NoMemory();
for (int i = 0; i < *len; i++) { for (int i = 0; i < *len; i++) {
PyObject *obj = PySequence_GetItem(arg, i); PyObject *obj = PySequence_GetItem(arg, i);
...@@ -787,6 +793,9 @@ static int *toIntArray(PyObject *arg, int *len) ...@@ -787,6 +793,9 @@ static int *toIntArray(PyObject *arg, int *len)
*len = (int) PySequence_Size(arg); *len = (int) PySequence_Size(arg);
int *array = new int[*len + 1]; int *array = new int[*len + 1];
if (!array)
return (int *) PyErr_NoMemory();
for (int i = 0; i < *len; i++) { for (int i = 0; i < *len; i++) {
PyObject *obj = PySequence_GetItem(arg, i); PyObject *obj = PySequence_GetItem(arg, i);
...@@ -837,6 +846,9 @@ static double *toDoubleArray(PyObject *arg, int *len) ...@@ -837,6 +846,9 @@ static double *toDoubleArray(PyObject *arg, int *len)
*len = (int) PySequence_Size(arg); *len = (int) PySequence_Size(arg);
double *array = new double[*len + 1]; double *array = new double[*len + 1];
if (!array)
return (double *) PyErr_NoMemory();
for (int i = 0; i < *len; i++) { for (int i = 0; i < *len; i++) {
PyObject *obj = PySequence_GetItem(arg, i); PyObject *obj = PySequence_GetItem(arg, i);
...@@ -878,6 +890,9 @@ static UBool *toUBoolArray(PyObject *arg, int *len) ...@@ -878,6 +890,9 @@ static UBool *toUBoolArray(PyObject *arg, int *len)
*len = (int) PySequence_Size(arg); *len = (int) PySequence_Size(arg);
UBool *array = new UBool[*len + 1]; UBool *array = new UBool[*len + 1];
if (!array)
return (UBool *) PyErr_NoMemory();
for (int i = 0; i < *len; i++) { for (int i = 0; i < *len; i++) {
PyObject *obj = PySequence_GetItem(arg, i); PyObject *obj = PySequence_GetItem(arg, i);
......
...@@ -45,6 +45,7 @@ DECLARE_CONSTANTS_TYPE(UResType) ...@@ -45,6 +45,7 @@ DECLARE_CONSTANTS_TYPE(UResType)
DECLARE_CONSTANTS_TYPE(ULocaleDataDelimiterType) DECLARE_CONSTANTS_TYPE(ULocaleDataDelimiterType)
DECLARE_CONSTANTS_TYPE(ULocaleDataExemplarSetType) DECLARE_CONSTANTS_TYPE(ULocaleDataExemplarSetType)
DECLARE_CONSTANTS_TYPE(UMeasurementSystem) DECLARE_CONSTANTS_TYPE(UMeasurementSystem)
DECLARE_CONSTANTS_TYPE(UAcceptResult)
#if U_ICU_VERSION_HEX >= VERSION_HEX(51, 0, 0) #if U_ICU_VERSION_HEX >= VERSION_HEX(51, 0, 0)
DECLARE_CONSTANTS_TYPE(URegionType) DECLARE_CONSTANTS_TYPE(URegionType)
...@@ -513,6 +514,8 @@ static PyObject *t_localematcher_getBestMatchResult(t_localematcher *self, PyObj ...@@ -513,6 +514,8 @@ static PyObject *t_localematcher_getBestMatchResult(t_localematcher *self, PyObj
static PyObject *t_localematcher_isMatch(t_localematcher *self, PyObject *args); static PyObject *t_localematcher_isMatch(t_localematcher *self, PyObject *args);
#endif #endif
static PyObject *t_localematcher_acceptLanguageFromHTTP(PyTypeObject *type, PyObject *args);
static PyMethodDef t_localematcher_methods[] = { static PyMethodDef t_localematcher_methods[] = {
DECLARE_METHOD(t_localematcher, getBestMatch, METH_O), DECLARE_METHOD(t_localematcher, getBestMatch, METH_O),
DECLARE_METHOD(t_localematcher, getBestMatchForListString, METH_O), DECLARE_METHOD(t_localematcher, getBestMatchForListString, METH_O),
...@@ -520,6 +523,7 @@ static PyMethodDef t_localematcher_methods[] = { ...@@ -520,6 +523,7 @@ static PyMethodDef t_localematcher_methods[] = {
#if U_ICU_VERSION_HEX >= VERSION_HEX(68, 0, 0) #if U_ICU_VERSION_HEX >= VERSION_HEX(68, 0, 0)
DECLARE_METHOD(t_localematcher, isMatch, METH_VARARGS), DECLARE_METHOD(t_localematcher, isMatch, METH_VARARGS),
#endif #endif
DECLARE_METHOD(t_localematcher, acceptLanguageFromHTTP, METH_CLASS| METH_VARARGS),
{ NULL, NULL, 0, NULL } { NULL, NULL, 0, NULL }
}; };
...@@ -2378,7 +2382,8 @@ static PyObject *t_localematcherresult_makeResolvedLocale( ...@@ -2378,7 +2382,8 @@ static PyObject *t_localematcherresult_makeResolvedLocale(
/* LocaleMatcher */ /* LocaleMatcher */
static PyObject *t_localematcher_getBestMatch(t_localematcher *self, static PyObject *t_localematcher_getBestMatch(t_localematcher *self,
PyObject *arg) { PyObject *arg)
{
const Locale *locale; const Locale *locale;
Locale **locales; Locale **locales;
int len; int len;
...@@ -2480,6 +2485,71 @@ static PyObject *t_localematcher_isMatch(t_localematcher *self, PyObject *args) ...@@ -2480,6 +2485,71 @@ static PyObject *t_localematcher_isMatch(t_localematcher *self, PyObject *args)
#endif // ICU >= 65 #endif // ICU >= 65
static PyObject *t_localematcher_acceptLanguageFromHTTP(
PyTypeObject *type, PyObject *args)
{
charsArg header_value;
UnicodeString *locales = NULL;
int num_locales = 0;
switch (PyTuple_Size(args)) {
case 2:
if (!parseArgs(args, "nT", &header_value, &locales, &num_locales))
{
const UChar **buffers =
(const UChar **) calloc(num_locales, sizeof(UChar *));
if (!buffers)
{
delete[] locales;
return PyErr_NoMemory();
}
for (int i = 0; i < num_locales; ++i)
buffers[i] = locales[i].getTerminatedBuffer();
UErrorCode status = U_ZERO_ERROR;
UEnumeration *uenum = uenum_openUCharStringsEnumeration(
buffers, num_locales, &status);
if (U_FAILURE(status))
{
free(buffers);
delete[] locales;
return ICUException(status).reportError();
}
else
status = U_ZERO_ERROR;
UAcceptResult result;
char buffer[128];
size_t size = uloc_acceptLanguageFromHTTP(
buffer, sizeof(buffer), &result, header_value.c_str(), uenum,
&status);
uenum_close(uenum);
free(buffers);
delete[] locales;
if (size >= sizeof(buffer) || U_FAILURE(status))
{
if (U_FAILURE(status))
return ICUException(status).reportError();
PyErr_SetString(
PyExc_ValueError, "resulting locale id length > 128");
return NULL;
}
return Py_BuildValue("(s#i)", buffer, (int) size, (int) result);
}
break;
}
return PyErr_SetArgsError(type, "acceptLanguageFromHTTP", args);
}
void _init_locale(PyObject *m) void _init_locale(PyObject *m)
{ {
...@@ -2498,6 +2568,7 @@ void _init_locale(PyObject *m) ...@@ -2498,6 +2568,7 @@ void _init_locale(PyObject *m)
INSTALL_CONSTANTS_TYPE(ULocaleDataDelimiterType, m); INSTALL_CONSTANTS_TYPE(ULocaleDataDelimiterType, m);
INSTALL_CONSTANTS_TYPE(ULocaleDataExemplarSetType, m); INSTALL_CONSTANTS_TYPE(ULocaleDataExemplarSetType, m);
INSTALL_CONSTANTS_TYPE(UMeasurementSystem, m); INSTALL_CONSTANTS_TYPE(UMeasurementSystem, m);
INSTALL_CONSTANTS_TYPE(UAcceptResult, m);
REGISTER_TYPE(Locale, m); REGISTER_TYPE(Locale, m);
REGISTER_TYPE(ResourceBundle, m); REGISTER_TYPE(ResourceBundle, m);
INSTALL_STRUCT(LocaleData, m); INSTALL_STRUCT(LocaleData, m);
...@@ -2559,6 +2630,10 @@ void _init_locale(PyObject *m) ...@@ -2559,6 +2630,10 @@ void _init_locale(PyObject *m)
INSTALL_ENUM(UMeasurementSystem, "SI", UMS_SI); INSTALL_ENUM(UMeasurementSystem, "SI", UMS_SI);
INSTALL_ENUM(UMeasurementSystem, "US", UMS_US); INSTALL_ENUM(UMeasurementSystem, "US", UMS_US);
INSTALL_ENUM(UAcceptResult, "FAILED", ULOC_ACCEPT_FAILED);
INSTALL_ENUM(UAcceptResult, "VALID", ULOC_ACCEPT_VALID);
INSTALL_ENUM(UAcceptResult, "FALLBACK", ULOC_ACCEPT_FALLBACK);
// options for LocaleData.getExemplarSet() // options for LocaleData.getExemplarSet()
INSTALL_MODULE_INT(m, USET_IGNORE_SPACE); INSTALL_MODULE_INT(m, USET_IGNORE_SPACE);
INSTALL_MODULE_INT(m, USET_CASE_INSENSITIVE); INSTALL_MODULE_INT(m, USET_CASE_INSENSITIVE);
......
...@@ -59,6 +59,22 @@ class TestLocaleMatcher(TestCase): ...@@ -59,6 +59,22 @@ class TestLocaleMatcher(TestCase):
self.assertEqual(Locale('de-AT'), result.getDesiredLocale()) self.assertEqual(Locale('de-AT'), result.getDesiredLocale())
self.assertEqual(Locale.getGermany(), result.getSupportedLocale()) self.assertEqual(Locale.getGermany(), result.getSupportedLocale())
def testAcceptLanguageFromHTTP(self):
locale, status = LocaleMatcher.acceptLanguageFromHTTP(
"fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5",
("de-AT", "fr-CA"))
self.assertEqual("fr_CA", locale)
self.assertEqual(UAcceptResult.FALLBACK, status)
locale, status = LocaleMatcher.acceptLanguageFromHTTP(
"fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5",
list(Locale.getAvailableLocales().keys()))
self.assertEqual("fr_CH", locale)
self.assertEqual(UAcceptResult.VALID, status)
if __name__ == "__main__": if __name__ == "__main__":
if ICU_VERSION >= '65.0': if ICU_VERSION >= '65.0':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment