Skip to content

Commit c80df2d

Browse files
author
borisz
committed
Add new methods last_netmask, charset and set_charset. Add attributes GeoIP.GEOIP_CHARSET_ISO_8859_1 and GeoIP.GEOIP_CHARSET_UTF8 and the example script test_city_charset.py ( Boris Zentner )
1 parent 2734f94 commit c80df2d

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2+
* Add charset and set_charset methods , as well as
3+
the new attributes GeoIP.GEOIP_CHARSET_ISO_8859_1 and
4+
GeoIP.GEOIP_CHARSET_UTF8 ( Boris Zentner )
5+
* Add test_city_charset.py script showing howto use
6+
charset and set_charset ( Boris Zentner )
7+
* Add last_netmask ( Boris Zentner )
8+
* Fix compile warnings ( Boris Zentner )
19
1.2.4
210
* fix small memoryleak in database_info, org_by_name and org_by_addr ( Boris Zentner )
311
* fix version number in setup.py ( Boris Zentner )

py_GeoIP.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static PyObject * GeoIP_country_name_by_addr_Py(PyObject *self, PyObject *args)
129129

130130
static PyObject * GeoIP_org_by_addr_Py(PyObject *self, PyObject *args) {
131131
char * name;
132-
const char * org;
132+
char * org;
133133
PyObject * ret;
134134
GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self;
135135
if (!PyArg_ParseTuple(args, "s", &name)) {
@@ -143,7 +143,7 @@ static PyObject * GeoIP_org_by_addr_Py(PyObject *self, PyObject *args) {
143143

144144
static PyObject * GeoIP_org_by_name_Py(PyObject *self, PyObject *args) {
145145
char * name;
146-
const char * org;
146+
char * org;
147147
PyObject * ret;
148148
GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self;
149149
if (!PyArg_ParseTuple(args, "s", &name)) {
@@ -308,6 +308,26 @@ static PyObject * GeoIP_range_by_ip_Py(PyObject *self, PyObject *args) {
308308
return retval;
309309
}
310310

311+
static PyObject * GeoIP_charset_Py(PyObject *self, PyObject * args) {
312+
GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self;
313+
return Py_BuildValue("i", GeoIP_charset(GeoIP->gi) );
314+
}
315+
316+
static PyObject * GeoIP_set_charset_Py(PyObject *self, PyObject * args) {
317+
GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self;
318+
int charset;
319+
if (!PyArg_ParseTuple(args, "i", &charset)) {
320+
return NULL;
321+
}
322+
return Py_BuildValue("i", GeoIP_set_charset(GeoIP->gi, charset));
323+
324+
}
325+
326+
static PyObject * GeoIP_last_netmask_Py(PyObject *self, PyObject * args) {
327+
GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self;
328+
return Py_BuildValue("i", GeoIP_last_netmask(GeoIP->gi) );
329+
}
330+
311331
static PyMethodDef GeoIP_Object_methods[] = {
312332
{"country_code_by_name", GeoIP_country_code_by_name_Py, 1, "Lookup Country Code By Name"},
313333
{"country_name_by_name", GeoIP_country_name_by_name_Py, 1, "Lookup Country Name By Name"},
@@ -320,6 +340,9 @@ static PyMethodDef GeoIP_Object_methods[] = {
320340
{"record_by_addr", GeoIP_record_by_addr_Py, 1, "Lookup City Region By IP Address"},
321341
{"record_by_name", GeoIP_record_by_name_Py, 1, "Lookup City Region By Name"},
322342
{"range_by_ip", GeoIP_range_by_ip_Py, 1, "Lookup start and end IP's for a given IP"},
343+
{"charset", GeoIP_charset_Py, 1, "Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )"},
344+
{"set_charset", GeoIP_set_charset_Py, 1, "Set the charset for city records"},
345+
{"last_netmask", GeoIP_last_netmask_Py, 1, "return the netmask depth of the last lookup"},
323346
{NULL, NULL, 0, NULL}
324347
};
325348

@@ -421,4 +444,13 @@ initGeoIP(void)
421444
tmp = PyInt_FromLong(GEOIP_INDEX_CACHE);
422445
PyDict_SetItemString(d, "GEOIP_INDEX_CACHE", tmp);
423446
Py_DECREF(tmp);
447+
448+
tmp = PyInt_FromLong(GEOIP_CHARSET_ISO_8859_1);
449+
PyDict_SetItemString(d, "GEOIP_CHARSET_ISO_8859_1", tmp);
450+
Py_DECREF(tmp);
451+
452+
tmp = PyInt_FromLong(GEOIP_CHARSET_UTF8);
453+
PyDict_SetItemString(d, "GEOIP_CHARSET_UTF8", tmp);
454+
Py_DECREF(tmp);
455+
424456
}

test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#gi = GeoIP.open("/usr/local/share/GeoIP/GeoIP.data",GeoIP.GEOIP_STANDARD)
88

99
print gi.country_code_by_name("yahoo.com")
10+
print gi.last_netmask()
1011
print gi.country_name_by_name("www.bundestag.de")
1112
print gi.country_code_by_addr("24.24.24.24")
1213
print gi.country_name_by_addr("24.24.24.24")

test_city_charset.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python
2+
3+
import GeoIP
4+
5+
# open the citydatabase. All cities return in iso-8859-1 by default
6+
gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPCity.dat",GeoIP.GEOIP_STANDARD)
7+
8+
# lookup a record, where cityname contains chars > 127 ( eg != ascii )
9+
gir = gi.record_by_name("www.osnabrueck.de")
10+
11+
# print the cityname with iso-8859-1 charset
12+
print gir['city'];
13+
14+
# print the cityname transformed to utf8
15+
print unicode(gir['city'], 'iso-8859-1')
16+
17+
# however, at your option GeoIP can return the cityname in utf8
18+
# just put GeoIP into utf8 mode
19+
#
20+
# from now on all records returns in UTF8 until you change the charset again
21+
# Notice, that all previous records return in the previous charset
22+
gi.set_charset(GeoIP.GEOIP_CHARSET_UTF8);
23+
24+
# get a new record, now in utf8
25+
gir2 = gi.record_by_name("www.osnabrueck.de")
26+
27+
# and print it ( should be the same output as on line 2 )
28+
print gir2['city']
29+
30+
## Some more charset examples
31+
# current_charset = gi.charset()
32+
# old_charset = gi.set_charset(GeoIP.GEOIP_CHARSET_ISO_8859_1);
33+
34+
35+

0 commit comments

Comments
 (0)