-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphinx_xmlpipe2.py
More file actions
185 lines (131 loc) · 4.25 KB
/
sphinx_xmlpipe2.py
File metadata and controls
185 lines (131 loc) · 4.25 KB
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
'''
Sphinx xmlpipe2 datasource stream generator
License: MIT
'''
import sys
import json
from lxml import etree
SPHINX_NAMESPACE = 'http://sphinxsearch.com/'
_ns = '{' + SPHINX_NAMESPACE + '}'
class BaseSlot(object):
def __init__(self, name):
self.name = name
def convert(self, value):
return unicode(value)
class Field(BaseSlot):
def write_schema(self, xf):
with xf.element(_ns + 'field', name=self.name):
pass
class BaseAttr(BaseSlot):
type = None
def __init__(self, name, default=None):
BaseSlot.__init__(self, name)
self.default = default
def _schema_attrib(self):
attrs = {'name': self.name, 'type': self.type}
if self.default is not None:
attrs['default'] = self.convert(self.default)
return attrs
def write_schema(self, xf):
with xf.element(_ns + 'attr', attrib=self._schema_attrib()):
pass
class Int(BaseAttr):
type = 'int'
bits = None
def __init__(self, name, bits=32, default=None):
BaseAttr.__init__(self, name, default=default)
assert bits in xrange(1, 33)
self.bits = int(bits)
def _schema_attrib(self):
attrs = BaseAttr._schema_attrib(self)
attrs['bits'] = str(self.bits)
return attrs
class BigInt(BaseAttr):
'''Unsigned 64-bit integer'''
type = 'bigint'
class Bool(BaseAttr):
type = 'bool'
def convert(self, value):
return '1' if value else '0'
class Float(BaseAttr):
type = 'float'
class Multi(BaseAttr):
'''List of int32'''
type = 'multi'
def convert(self, value):
return ' '.join(str(item) for item in value)
class Timestamp(BaseAttr):
type = 'timestamp'
def convert(self, value):
return value.strftime('%s')
class Json(BaseAttr):
type = 'json'
def __init__(self, name, encoder=None, default=None):
BaseAttr.__init__(self, name, default=default)
self.encoder = encoder
def convert(self, value):
return json.dumps(value, cls=self.encoder)
class Pipe(object):
def __init__(self, fields, attrs=[], fp=sys.stdout):
self.slots = {s.name: s for s in [Field(n) for n in fields] + attrs}
self._xf_cm = etree.xmlfile(fp, encoding='utf-8')
def __enter__(self):
self._xf = xf = self._xf_cm.__enter__()
xf.write_declaration()
self._root = xf.element(_ns + 'docset',
nsmap={'sphinx': SPHINX_NAMESPACE})
self._root.__enter__()
self.write_schema()
return self
def __exit__(self, exc_type, exc_value, exc_tb):
self._root.__exit__(exc_type, exc_value, exc_tb)
self._xf_cm.__exit__(exc_type, exc_value, exc_tb)
def write_schema(self):
with self._xf.element(_ns + 'schema'):
for slot in self.slots.values():
slot.write_schema(self._xf)
def document(self, id, **data):
with self._xf.element(_ns + 'document', id=str(id)):
for name, value in data.items():
slot = self.slots[name]
value = slot.convert(value)
with self._xf.element(name):
self._xf.write(value)
def killlist(self, ids):
with self._xf.element(_ns + 'killlist'):
for id in ids:
with self._xf.element('id'):
self._xf.write(str(id))
if __name__=='__main__':
from datetime import datetime
pipe = Pipe(
fields = [
'title',
'body',
],
attrs = [
Int('length'),
BigInt('size'),
Bool('publish'),
Float('weight'),
Multi('sections'),
Timestamp('date'),
Json('data'),
],
)
with pipe:
for i in range(100):
pipe.document(
id = i,
title = u'title{}'.format(i),
body = u'body{}'.format(i),
length = i,
size = 2 ** (i % 50),
publish = i % 2,
weight = float(i) / (i+1),
sections = range(i, i+3),
date = datetime.now(),
data = {'i': i},
)
pipe.killlist(range(100, 200))
print