1
|
|
|
import abc |
2
|
|
|
import copy |
3
|
|
|
from typing import List, Optional |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class Reader(metaclass=abc.ABCMeta): |
7
|
|
|
""" |
8
|
|
|
Abstract parent class for reading (directly or indirectly) rows from the source. |
9
|
|
|
""" |
10
|
|
|
|
11
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
12
|
|
|
def __init__(self): |
13
|
|
|
""" |
14
|
|
|
Object constructor. |
15
|
|
|
""" |
16
|
|
|
|
17
|
|
|
self._fields: Optional[List[str]] = None |
18
|
|
|
""" |
19
|
|
|
The mapping from field (columns) names to the column numbers in the CSV files. |
20
|
|
|
""" |
21
|
|
|
|
22
|
|
|
self._row_number: int = -1 |
23
|
|
|
""" |
24
|
|
|
The row number for identifying the row in the source data. |
25
|
|
|
""" |
26
|
|
|
|
27
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
28
|
|
|
@property |
29
|
|
|
def fields(self) -> Optional[List[str]]: |
30
|
|
|
""" |
31
|
|
|
Getter for fields. |
32
|
|
|
""" |
33
|
|
|
return copy.copy(self._fields) |
34
|
|
|
|
35
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
36
|
|
|
@fields.setter |
37
|
|
|
def fields(self, fields: Optional[List[str]]) -> None: |
38
|
|
|
""" |
39
|
|
|
Setter for fields. If set to None this reader yields each row from the source as a list. If set to a list of |
40
|
|
|
field names this reader yields each row from the source as a dictionary using the given field names. |
41
|
|
|
|
42
|
|
|
:param fields: The fields (or columns) that must be read from the source. |
43
|
|
|
""" |
44
|
|
|
self._fields = fields |
45
|
|
|
|
46
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
47
|
|
|
@property |
48
|
|
|
def row_number(self) -> int: |
49
|
|
|
""" |
50
|
|
|
Getter for row count. |
51
|
|
|
""" |
52
|
|
|
return self._row_number |
53
|
|
|
|
54
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
55
|
|
|
@abc.abstractmethod |
56
|
|
|
def get_source_name(self) -> str: |
57
|
|
|
""" |
58
|
|
|
Returns a name for identifying the current source. |
59
|
|
|
""" |
60
|
|
|
raise NotImplementedError() |
61
|
|
|
|
62
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
63
|
|
|
@abc.abstractmethod |
64
|
|
|
def next(self): |
65
|
|
|
""" |
66
|
|
|
Yields the next row from the source. |
67
|
|
|
""" |
68
|
|
|
raise NotImplementedError() |
69
|
|
|
|
70
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
71
|
|
|
|