Skip to content

Commit 3addd7c

Browse files
author
borisz
committed
Add database_info, database_edition to GeoIP obj. Add region_name, time_zone to GeoIPRecord and export country list, country code -> country name mapping as well as country->continent mapping
1 parent 62b6304 commit 3addd7c

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Add database_info and database_edition attributes to GeoIP object
2+
(James Henstridge)
3+
* Add region_name and time_zone keys to GeoIPRecord wrapper (James
4+
Henstridge).
5+
* Export the country list, country code -> country name mapping, and
6+
country->continent mapping ( Ignacio Vazquez-Abrams )
17
* Raise country code counter from 251 to 253 ( Boris Zentner )
28
!! record_by_addr and record_by_name return None instead of throwing a
39
exception. See test_city.py ( Boris Zentner )

py_GeoIP.c

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static PyObject * GeoIP_org_by_name_Py(PyObject *self, PyObject *args) {
149149
return Py_BuildValue("s", retval);
150150
}
151151

152-
void GeoIP_SetItemString(PyObject *dict, const char * name, char * value) {
152+
void GeoIP_SetItemString(PyObject *dict, const char * name, const char * value) {
153153
PyObject * nameObj;
154154
PyObject * valueObj;
155155
nameObj = Py_BuildValue("s",name);
@@ -201,6 +201,10 @@ static PyObject * GeoIP_populate_dict(GeoIPRecord *gir) {
201201
GeoIP_SetItemFloat(retval,"longitude",gir->longitude);
202202
GeoIP_SetItemInt(retval,"dma_code",gir->dma_code);
203203
GeoIP_SetItemInt(retval,"area_code",gir->area_code);
204+
GeoIP_SetItemString(retval, "region_name",
205+
GeoIP_region_name_by_code(gir->country_code, gir->region));
206+
GeoIP_SetItemString(retval, "time_zone",
207+
GeoIP_time_zone_by_country_and_region(gir->country_code, gir->region));
204208
GeoIPRecord_delete(gir);
205209
return retval;
206210
}
@@ -274,9 +278,15 @@ static PyMethodDef GeoIP_Object_methods[] = {
274278
static PyObject *
275279
GeoIP_GetAttr(PyObject *self, char *attrname)
276280
{
281+
GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self;
277282
if (strcmp(attrname, "GEOIP_STANDARD") == 0) {
278283
return Py_BuildValue("i", 0);
279-
}
284+
} else if (strcmp(attrname, "database_info") == 0) {
285+
return Py_BuildValue("z", GeoIP_database_info(GeoIP->gi));
286+
} else if (strcmp(attrname, "database_edition") == 0) {
287+
return Py_BuildValue("z", GeoIPDBDescription[
288+
GeoIP_database_edition(GeoIP->gi)]);
289+
}
280290
return Py_FindMethod(GeoIP_Object_methods, self, attrname);
281291
}
282292

@@ -307,7 +317,10 @@ static PyMethodDef GeoIP_Class_methods[] = {
307317
DL_EXPORT(void)
308318
initGeoIP(void)
309319
{
310-
PyObject *m, *d, *tmp;
320+
PyObject *m, *d, *tmp, *ccode, *cname, *ccont, *name;
321+
int i;
322+
const int total_ccodes = sizeof (GeoIP_country_code) /
323+
sizeof (GeoIP_country_code[0]);
311324
GeoIP_GeoIPType.ob_type = &PyType_Type;
312325

313326
m = Py_InitModule("GeoIP", GeoIP_Class_methods);
@@ -316,33 +329,44 @@ initGeoIP(void)
316329
PyGeoIPError = PyErr_NewException("py_geoip.error", NULL, NULL);
317330
PyDict_SetItemString(d, "error", PyGeoIPError);
318331

319-
int total_ccodes = 253;
320-
321-
PyObject *ccode = PyTuple_New(total_ccodes);
322-
PyObject *cname = PyDict_New();
323-
PyObject *ccont = PyDict_New();
324-
PyObject *s;
332+
ccode = PyTuple_New(total_ccodes);
333+
cname = PyDict_New();
334+
ccont = PyDict_New();
325335

326-
int i = 0;
327-
for (; i<total_ccodes; i++)
336+
for (i = 0; i<total_ccodes; i++)
328337
{
329-
s = PyString_FromString(GeoIP_country_code[i]);
330-
PyTuple_SET_ITEM(ccode, i, s);
331-
Py_INCREF(s);
332-
PyDict_SetItem(cname, s, PyString_FromString(GeoIP_country_name[i]));
333-
Py_INCREF(s);
334-
PyDict_SetItem(ccont, s, PyString_FromString(GeoIP_country_continent[i]));
335-
};
338+
name = PyString_FromString(GeoIP_country_code[i]);
339+
PyTuple_SET_ITEM(ccode, i, name);
340+
341+
tmp = PyString_FromString(GeoIP_country_name[i]);
342+
PyDict_SetItem(cname, name, tmp);
343+
Py_DECREF(tmp);
344+
345+
tmp = PyString_FromString(GeoIP_country_continent[i]);
346+
PyDict_SetItem(ccont, name, tmp);
347+
Py_DECREF(tmp);
348+
}
336349

337350
PyDict_SetItemString(d, "country_codes", ccode);
351+
Py_DECREF(ccode);
338352
PyDict_SetItemString(d, "country_names", cname);
353+
Py_DECREF(cname);
339354
PyDict_SetItemString(d, "country_continents", ccont);
355+
Py_DECREF(ccont);
340356

341-
tmp = PyInt_FromLong(0);
357+
tmp = PyInt_FromLong(GEOIP_STANDARD);
342358
PyDict_SetItemString(d, "GEOIP_STANDARD", tmp);
343359
Py_DECREF(tmp);
344360

345-
tmp = PyInt_FromLong(1);
361+
tmp = PyInt_FromLong(GEOIP_MEMORY_CACHE);
346362
PyDict_SetItemString(d, "GEOIP_MEMORY_CACHE", tmp);
347363
Py_DECREF(tmp);
364+
365+
tmp = PyInt_FromLong(GEOIP_CHECK_CACHE);
366+
PyDict_SetItemString(d, "GEOIP_CHECK_CACHE", tmp);
367+
Py_DECREF(tmp);
368+
369+
tmp = PyInt_FromLong(GEOIP_INDEX_CACHE);
370+
PyDict_SetItemString(d, "GEOIP_INDEX_CACHE", tmp);
371+
Py_DECREF(tmp);
348372
}

0 commit comments

Comments
 (0)