forked from openvenues/pypostal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyparser.c
212 lines (167 loc) · 5.07 KB
/
pyparser.c
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
#include <Python.h>
#include <libpostal/libpostal.h>
#include "pyutils.h"
#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif
struct module_state {
PyObject *error;
};
#ifdef IS_PY3K
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif
static PyObject *py_parse_address(PyObject *self, PyObject *args, PyObject *keywords) {
PyObject *arg_input;
PyObject *arg_language = Py_None;
PyObject *arg_country = Py_None;
PyObject *result = NULL;
static char *kwlist[] = {"address",
"language",
"country",
NULL
};
if (!PyArg_ParseTupleAndKeywords(args, keywords,
"O|OO:pyparser", kwlist,
&arg_input, &arg_language,
&arg_country
)) {
return 0;
}
char *input = PyObject_to_string(arg_input);
if (input == NULL) {
return NULL;
}
char *language = NULL;
if (arg_language != Py_None) {
language = PyObject_to_string(arg_language);
if (language == NULL) {
goto exit_free_input;
}
}
char *country = NULL;
if (arg_country != Py_None) {
country = PyObject_to_string(arg_language);
if (country == NULL) {
goto exit_free_language;
}
}
libpostal_address_parser_options_t options = libpostal_get_address_parser_default_options();
options.language = language;
options.country = country;
libpostal_address_parser_response_t *parsed = libpostal_parse_address(input, options);
if (parsed == NULL) {
goto exit_free_country;
}
result = PyList_New((Py_ssize_t)parsed->num_components);
if (!result) {
goto exit_destroy_response;
}
for (int i = 0; i < parsed->num_components; i++) {
char *component = parsed->components[i];
char *label = parsed->labels[i];
PyObject *component_unicode = PyUnicode_DecodeUTF8((const char *)component, strlen(component), "strict");
if (component_unicode == NULL) {
Py_DECREF(result);
goto exit_destroy_response;
}
PyObject *label_unicode = PyUnicode_DecodeUTF8((const char *)label, strlen(label), "strict");
if (label_unicode == NULL) {
Py_DECREF(component_unicode);
Py_DECREF(result);
goto exit_destroy_response;
}
PyObject *tuple = Py_BuildValue("(OO)", component_unicode, label_unicode);
if (tuple == NULL) {
Py_DECREF(component_unicode);
Py_DECREF(label_unicode);
goto exit_destroy_response;
}
// Note: PyList_SetItem steals a reference, so don't worry about DECREF
PyList_SetItem(result, (Py_ssize_t)i, tuple);
Py_DECREF(component_unicode);
Py_DECREF(label_unicode);
}
exit_destroy_response:
libpostal_address_parser_response_destroy(parsed);
exit_free_country:
if (country != NULL) {
free(country);
}
exit_free_language:
if (language != NULL) {
free(language);
}
exit_free_input:
if (input != NULL) {
free(input);
}
return result;
}
static PyMethodDef parser_methods[] = {
{"parse_address", (PyCFunction)py_parse_address, METH_VARARGS | METH_KEYWORDS, "parse_address(text, language, country)"},
{NULL, NULL},
};
#ifdef IS_PY3K
static int parser_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(GETSTATE(m)->error);
return 0;
}
static int parser_clear(PyObject *m) {
Py_CLEAR(GETSTATE(m)->error);
libpostal_teardown();
libpostal_teardown_parser();
return 0;
}
static struct PyModuleDef module_def = {
PyModuleDef_HEAD_INIT,
"_parser",
NULL,
sizeof(struct module_state),
parser_methods,
NULL,
parser_traverse,
parser_clear,
NULL
};
#define INITERROR return NULL
PyObject *
PyInit__parser(void) {
#else
#define INITERROR return
void cleanup_libpostal(void) {
libpostal_teardown();
libpostal_teardown_parser();
}
void
init_parser(void) {
#endif
#ifdef IS_PY3K
PyObject *module = PyModule_Create(&module_def);
#else
PyObject *module = Py_InitModule("_parser", parser_methods);
#endif
if (module == NULL) {
INITERROR;
}
struct module_state *st = GETSTATE(module);
st->error = PyErr_NewException("_parser.Error", NULL, NULL);
if (st->error == NULL) {
Py_DECREF(module);
INITERROR;
}
char* datadir = getenv("LIBPOSTAL_DATA_DIR");
if ((datadir!=NULL) && (!libpostal_setup_datadir(datadir) || !libpostal_setup_parser_datadir(datadir)) ||
(!libpostal_setup() || !libpostal_setup_parser())) {
PyErr_SetString(PyExc_TypeError,
"Error loading libpostal data");
}
#ifndef IS_PY3K
Py_AtExit(&cleanup_libpostal);
#endif
#ifdef IS_PY3K
return module;
#endif
}