|
27 | 27 | from functools import partial |
28 | 28 |
|
29 | 29 | from . import mwtab |
30 | | -from . import validator |
31 | | -from . import mwschema |
32 | 30 | from . import mwrest |
33 | 31 |
|
34 | 32 | from urllib.request import urlopen |
@@ -195,123 +193,27 @@ def read_with_class(sources: str|list[str], read_class: type, class_kwds: dict, |
195 | 193 | read_files = partial(read_with_class, read_class = mwtab.MWTabFile, class_kwds = {"duplicate_keys": True}) |
196 | 194 | read_mwrest = partial(read_with_class, read_class = mwrest.MWRESTFile, class_kwds = {}) |
197 | 195 |
|
198 | | - |
199 | | -# TODO delete this functions after testing. |
200 | | -# def read_files(sources, return_exceptions=False): |
201 | | -# """Construct a generator that yields file instances. |
202 | | - |
203 | | -# :param sources: One or more strings representing path to file(s). |
204 | | -# :param bool return_exceptions: Whether to yield a tuple with file instance and exception or just the file instance. |
205 | | -# """ |
206 | | -# try: |
207 | | -# filenames = _generate_filenames(sources, True) |
208 | | -# filehandles = _generate_handles(filenames, True) |
209 | | -# except Exception as e: |
210 | | -# yield _return_correct_yield(None, |
211 | | -# exception=e, |
212 | | -# return_exceptions=return_exceptions) |
213 | | -# for fh, source, exc in filehandles: |
214 | | -# if exc is not None: |
215 | | -# yield _return_correct_yield(source, |
216 | | -# exception=exc, |
217 | | -# return_exceptions=return_exceptions) |
218 | | -# continue |
219 | | -# try: |
220 | | -# f = mwtab.MWTabFile(source, duplicate_keys=True) |
221 | | -# f.read(fh) |
222 | | -# fh.close() |
223 | | - |
224 | | -# if VERBOSE: |
225 | | -# print("Processed file: {}".format(os.path.abspath(source))) |
226 | | - |
227 | | -# yield _return_correct_yield(f, |
228 | | -# exception=None, |
229 | | -# return_exceptions=return_exceptions) |
230 | | - |
231 | | -# except Exception as e: |
232 | | -# fh.close() |
233 | | -# if VERBOSE: |
234 | | -# print("Error processing file: ", os.path.abspath(source), "\nReason:", e) |
235 | | -# yield _return_correct_yield(source, |
236 | | -# exception=e, |
237 | | -# return_exceptions=return_exceptions) |
238 | | - |
239 | | - |
240 | | -# def read_mwrest(*sources, return_exceptions=False): |
241 | | -# """Construct a generator that yields file instances. |
242 | | - |
243 | | -# :param sources: One or more strings representing path to file(s). |
244 | | -# :param bool return_exceptions: Whether to yield a tuple with file instance and exception or just the file instance. |
245 | | -# """ |
246 | | -# try: |
247 | | -# filenames = _generate_filenames(sources, True) |
248 | | -# filehandles = _generate_handles(filenames, True) |
249 | | -# except Exception as e: |
250 | | -# yield _return_correct_yield(None, |
251 | | -# exception=e, |
252 | | -# return_exceptions=return_exceptions) |
253 | | -# for fh, source, exc in filehandles: |
254 | | -# try: |
255 | | -# f = mwrest.MWRESTFile(source) |
256 | | -# f.read(fh) |
257 | | -# fh.close() |
258 | | - |
259 | | -# if VERBOSE: |
260 | | -# print("Processed url: {}".format(source)) |
261 | | - |
262 | | -# yield _return_correct_yield(f, |
263 | | -# exception=None, |
264 | | -# return_exceptions=return_exceptions) |
265 | | - |
266 | | -# except Exception as e: |
267 | | -# fh.close() |
268 | | -# if VERBOSE: |
269 | | -# print("Error processing url: ", source, "\nReason:", e) |
270 | | -# yield _return_correct_yield(None, |
271 | | -# exception=e, |
272 | | -# return_exceptions=return_exceptions) |
273 | | - |
274 | | -# Unused function, leaving here for now. |
275 | | -# def read_lines(*sources, return_exceptions=False): |
276 | | -# """Construct a generator that yields file instances. |
277 | | - |
278 | | -# :param sources: One or more strings representing path to file(s). |
279 | | -# :param bool return_exceptions: Whether to yield a tuple with file instance and exception or just the file instance. |
280 | | -# """ |
281 | | -# try: |
282 | | -# filenames = _generate_filenames(sources, True) |
283 | | -# filehandles = _generate_handles(filenames, True) |
284 | | -# except Exception as e: |
285 | | -# yield _return_correct_yield(None, |
286 | | -# exception=e, |
287 | | -# return_exceptions=return_exceptions) |
288 | | -# for fh, source, exc in filehandles: |
289 | | -# try: |
290 | | -# string = fh.read() |
291 | | -# fh.close() |
292 | | -# if isinstance(string, str): |
293 | | -# lines = string.replace("\r", "\n").split("\n") |
294 | | -# elif isinstance(string, bytes): |
295 | | -# lines = string.decode("utf-8").replace("\r", "\n").split("\n") |
296 | | -# else: |
297 | | -# raise TypeError("Expecting <class 'str'> or <class 'bytes'>, but {} was passed".format(type(string))) |
298 | | - |
299 | | -# lines = [line for line in lines if line] |
300 | | - |
301 | | -# if VERBOSE: |
302 | | -# print("Processed file: {}".format(os.path.abspath(source))) |
303 | | - |
304 | | -# yield _return_correct_yield((lines, source), |
305 | | -# exception=None, |
306 | | -# return_exceptions=return_exceptions) |
| 196 | +class ReadLines(): |
| 197 | + def __init__(self, source, *args, **kwargs): |
| 198 | + self.source = source |
| 199 | + self.lines = [] |
| 200 | + |
| 201 | + def read(self, filehandle): |
| 202 | + """ |
| 203 | + """ |
| 204 | + string = filehandle.read() |
| 205 | + filehandle.close() |
| 206 | + if isinstance(string, str): |
| 207 | + lines = string.replace("\r", "\n").split("\n") |
| 208 | + elif isinstance(string, bytes): |
| 209 | + lines = string.decode("utf-8").replace("\r", "\n").split("\n") |
| 210 | + else: |
| 211 | + raise TypeError("Expecting <class 'str'> or <class 'bytes'>, but {} was passed".format(type(string))) |
307 | 212 |
|
308 | | -# except Exception as e: |
309 | | -# fh.close() |
310 | | -# if VERBOSE: |
311 | | -# print("Error processing file: ", source, "\nReason:", e) |
312 | | -# yield _return_correct_yield(source, |
313 | | -# exception=e, |
314 | | -# return_exceptions=return_exceptions) |
| 213 | + self.lines = [line for line in lines if line] |
| 214 | + |
| 215 | + |
| 216 | +read_lines = partial(read_with_class, read_class = ReadLines, class_kwds = {}) |
315 | 217 |
|
316 | 218 |
|
317 | 219 | class GenericFilePath(object): |
|
0 commit comments