-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.py
180 lines (155 loc) · 5.51 KB
/
loader.py
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
#!/usr/local/bin/python
#encoding:utf8
'''
Loader interface and some build-in classes.
'''
from __future__ import with_statement
import os
import codecs
from utility import check_required_params
class Loader(object):
'''Abstract Loader interface'''
def open(self):
'''open the source connection'''
raise NotImplementedError
def items(self):
'''generator for the items'''
raise NotImplementedError
def close(self):
'''close the connection'''
raise NotImplementedError
class LocalFileLoader(Loader):
'''loader that handles a single local file'''
def __init__(self, params):
super(LocalFileLoader, self).__init__()
check_required_params(['src'], params)
self._source = params['src']
self._encoding = 'utf8' if 'encoding' not in params else params['encoding']
self._fd = None
def open(self):
'''open the source connection'''
try:
self._fd = codecs.open(self._source, mode='r', encoding=self._encoding, errors='ignore')
except IOError:
if self._fd:
self._fd.close()
self._fd = None
def items(self):
'''generator for the items'''
# only one item
if self._fd:
yield self._source, self._fd.read() #.decode(self._encoding, 'ignore')
def close(self):
'''close the connection'''
if self._fd:
self._fd.close()
class LocalFilelistLoader(Loader):
'''loader that handles a file with all files' paths in it '''
def __init__(self, params):
super(LocalFilelistLoader, self).__init__()
check_required_params(['src'], params)
self._source = params['src']
self._encoding = 'utf8' if 'encoding' not in params else params['encoding']
self._fd = None
def open(self):
'''open the source connection'''
try:
self._fd = codecs.open(self._source, mode='r', encoding=self._encoding, errors='ignore')
except IOError:
if self._fd:
self._fd.close()
self._fd = None
def items(self):
'''generator for the items'''
if self._fd:
for line in self._fd:
line = line.strip()
if not line or not os.path.exists(line):
continue
else:
with codecs.open(line, mode='r', encoding=self._encoding, errors='ignore') as fditem:
yield line, fditem.read()
def close(self):
'''close the connection'''
if self._fd:
self._fd.close()
class LocalKVFileLoader(Loader):
'''loader that handles a file with one document per line; doc key\ttxt'''
def __init__(self, params):
super(LocalKVFileLoader, self).__init__()
check_required_params(['src'], params)
self._source = params['src']
self._encoding = 'utf8' if 'encoding' not in params else params['encoding']
self._fd = None
def open(self):
'''open the source connection'''
try:
self._fd = codecs.open(self._source, mode='r', encoding=self._encoding, errors='ignore')
except IOError:
if self._fd:
self._fd.close()
self._fd = None
def items(self):
'''generator for the items'''
if self._fd:
for line in self._fd:
line = line.strip()
parts = line.split('\t')
if not parts or len(parts) != 2:
continue
else:
yield parts[0], parts[1]
def close(self):
'''close the connection'''
if self._fd:
self._fd.close()
class WebLoader(Loader):
'''loader that handles a single web page represented by URL'''
def __init__(self, params):
super(WebLoader, self).__init__()
check_required_params(['url'], params)
self._url = params['url']
self._encoding = 'utf8' if 'encoding' not in params else params['encoding']
self._doc = None
def open(self):
from urllib import urlopen
self._doc = urlopen(self._url)
def items(self):
if self._doc:
content = self._doc.read()
ucontent = ''
try:
ucontent = content.decode('utf8')
except UnicodeError:
try:
ucontent = content.decode('gb2312')
except UnicodeError:
pass
yield self._url, ucontent
def close(self):
self._doc = None
class TextLoader(Loader):
'''loader that handles a text content'''
def __init__(self, params):
super(TextLoader, self).__init__()
check_required_params(['txt'], params)
self._text = params['txt']
self._encoding = 'utf8' if 'encoding' not in params else params['encoding']
self._content = None
def open(self):
'''open the source connection'''
self._content = self._text.decode(self._encoding, 'ignore')
def items(self):
'''generator for the items'''
# only one item
yield self._text, self._content #.decode(self._encoding, 'ignore')
def close(self):
'''close the connection'''
self._content = None
if __name__ == "__main__":
loader = WebLoader({'url':'http://tuan.aibang.com/beijing/'})
#loader = WebLoader({'url':'http://www.baidu.com/'})
loader.open()
for item in loader.items():
print item
loader.close()