debian/control: Add Build-Depends for `dh-python'.
[mLib-python] / url.pyx
1 ### -*-pyrex-*-
2 ###
3 ### Form-urlencoding functions
4 ###
5 ### (c) 2006 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the Python interface to mLib.
11 ###
12 ### mLib/Python is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
16 ###
17 ### mLib/Python is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with mLib/Python; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 cdef class URLEncode:
27 """URLEncode([strictp = False], [laxp = False], [semip = False])"""
28 cdef url_ectx ctx
29 cdef dstr d
30
31 def __cinit__(me, *hunoz, **hukairz):
32 url_initenc(&me.ctx)
33 DCREATE(&me.d)
34 def __init__(me, strictp = False, laxp = False, semip = False):
35 cdef unsigned f
36 f = 0
37 if strictp:
38 f = f | URLF_STRICT
39 if laxp:
40 f = f | URLF_LAX
41 if semip:
42 f = f | URLF_SEMI
43 me.ctx.f = f
44 def encode(me, char *name, char *value):
45 """UE.encode(NAME, VALUE): encode a key/value pair"""
46 url_enc(&me.ctx, &me.d, name, value)
47 return me
48 property result:
49 """UE.result -> STR: the encoded string"""
50 def __get__(me):
51 return PyString_FromStringAndSize(me.d.buf, me.d.len)
52 property strictp:
53 """UE.strictp -> BOOL: strictly escape non-alphanumerics?"""
54 def __get__(me):
55 return _tobool(me.ctx.f & URLF_STRICT)
56 def __set__(me, val):
57 if val:
58 me.ctx.f = me.ctx.f | URLF_STRICT
59 else:
60 me.ctx.f = me.ctx.f & ~URLF_STRICT
61 property laxp:
62 """UE.laxp -> BOOL: only escape obviously unsafe characters?"""
63 def __get__(me):
64 return _tobool(me.ctx.f & URLF_LAX)
65 def __set__(me, val):
66 if val:
67 me.ctx.f = me.ctx.f | URLF_LAX
68 else:
69 me.ctx.f = me.ctx.f & ~URLF_LAX
70 property semip:
71 """UE.semip -> BOOL: separate key/value pairs with semicolons?"""
72 def __get__(me):
73 return _tobool(me.ctx.f & URLF_SEMI)
74 def __set__(me, val):
75 if val:
76 me.ctx.f = me.ctx.f | URLF_SEMI
77 else:
78 me.ctx.f = me.ctx.f & ~URLF_SEMI
79 def __del__(me):
80 dstr_destroy(&me.d)
81
82 cdef class URLDecode:
83 """URLDecode(STR, [semip = False]): iterator over (KEY, VALUE) pairs"""
84 cdef url_dctx ctx
85 cdef char *p
86
87 def __cinit__(me, *hunoz, **hukairz):
88 me.p = xstrdup('')
89 url_initdec(&me.ctx, me.p)
90 def __init__(me, char *string, semip = False):
91 cdef unsigned f
92 f = 0
93 if semip:
94 f = f | URLF_SEMI
95 xfree(me.p)
96 me.p = xstrdup(string)
97 me.ctx.p = me.p
98 me.ctx.f = f
99 def __iter__(me):
100 return me
101 def __next__(me):
102 cdef dstr n
103 cdef dstr v
104 cdef object nn
105 cdef object vv
106 cdef int anyp
107 DCREATE(&n)
108 DCREATE(&v)
109 anyp = url_dec(&me.ctx, &n, &v)
110 if anyp:
111 nn = PyString_FromStringAndSize(n.buf, n.len)
112 vv = PyString_FromStringAndSize(v.buf, v.len)
113 dstr_destroy(&n)
114 dstr_destroy(&v)
115 if not anyp:
116 raise StopIteration
117 return nn, vv
118 property semip:
119 """UD.semip -> BOOL: key/value pairs separated with semicolons?"""
120 def __get__(me):
121 return _tobool(me.ctx.f & URLF_SEMI)
122 def __set__(me, val):
123 if val:
124 me.ctx.f = me.ctx.f | URLF_SEMI
125 else:
126 me.ctx.f = me.ctx.f & ~URLF_SEMI
127 def __del__(me):
128 xfree(me.p)
129
130 ###----- That's all, folks --------------------------------------------------