Readers API
src.readers
¶
Reader initialization and registration.
__all__ = ['BaseReader', 'SQLiteReader', 'CSVReader', 'XMLReader']
module-attribute
¶
BaseReader
¶
Bases: ABC
Abstract base class for all file format readers.
This class provides core functionality for reading diabetes device data files and automatic reader selection based on file types. It handles timestamp processing, data validation, and resource management.
Source code in src/readers/base.py
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
_readers: Dict[FileType, Type[BaseReader]] = {}
class-attribute
instance-attribute
¶
file_path = path
instance-attribute
¶
file_config = file_config
instance-attribute
¶
register(file_type: FileType) -> Callable[[Type[T]], Type[T]]
classmethod
¶
Register a reader class for a specific file type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_type | FileType | FileType enum value to associate with the reader | required |
Returns:
Name | Type | Description |
---|---|---|
Callable | Callable[[Type[T]], Type[T]] | Decorator function that registers the reader class |
Source code in src/readers/base.py
get_reader_for_format(fmt: DeviceFormat, file_path: Path) -> BaseReader
classmethod
¶
Get appropriate reader instance for the detected format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fmt | DeviceFormat | Detected device format specification | required |
file_path | Path | Path to the data file | required |
Returns:
Type | Description |
---|---|
BaseReader | Instance of appropriate reader class |
Raises:
Type | Description |
---|---|
ReaderError | If no reader is registered for the file type |
Source code in src/readers/base.py
__init__(path: Path, file_config: FileConfig)
¶
Initialize reader with file path and configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | Path | Path to the data file | required |
file_config | FileConfig | Configuration for the file format | required |
Raises:
Type | Description |
---|---|
ValueError | If file does not exist |
Source code in src/readers/base.py
__enter__()
¶
__exit__(exc_type, exc_val, exc_tb)
¶
_cleanup()
¶
read_table(table_structure: TableStructure) -> Optional[TableData]
abstractmethod
¶
Read and process a single table according to its structure.
This method must be implemented by each specific reader.
read_all_tables() -> Dict[str, TableData]
¶
Read and process all tables defined in the file configuration.
Source code in src/readers/base.py
detect_timestamp_format(series: pd.Series) -> TimestampType
¶
Detect the format of timestamp data, assuming chronological order.
Source code in src/readers/base.py
_convert_timestamp_to_utc(df: pd.DataFrame, timestamp_column: str) -> Tuple[pd.DataFrame, TimestampType]
¶
Convert timestamp column to UTC datetime and set as index.
Source code in src/readers/base.py
_validate_required_data(df: pd.DataFrame, columns: List[ColumnMapping]) -> List[str]
¶
Check for missing data in required columns.
Source code in src/readers/base.py
_validate_identifier(identifier: str) -> bool
staticmethod
¶
Validate that an identifier only contains safe characters.
CSVReader
¶
Bases: BaseReader
Reads and processes CSV files according to the provided format configuration.
Source code in src/readers/csv.py
_data = None
instance-attribute
¶
__init__(path: Path, file_config: FileConfig)
¶
_cleanup()
¶
read_table(table_structure: TableStructure) -> Optional[TableData]
¶
Read and process a single table according to its structure.
For CSV files, we treat each file as a single table, reading all data at once and caching it for subsequent operations if needed.
Source code in src/readers/csv.py
SQLiteReader
¶
Bases: BaseReader
Reads and processes SQLite files according to the provided format configuration.
Source code in src/readers/sqlite.py
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 |
|
_engine = None
instance-attribute
¶
engine
property
¶
Lazy initialization of database engine.
__init__(path: Path, file_config: FileConfig)
¶
_cleanup()
¶
read_table(table_structure: TableStructure) -> Optional[TableData]
¶
Read and process a single table according to its structure.
Source code in src/readers/sqlite.py
XMLReader
¶
Bases: BaseReader
Reads and processes XML files according to the provided format configuration.
Source code in src/readers/xml.py
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 |
|
_tree = None
instance-attribute
¶
_root = None
instance-attribute
¶
__init__(path: Path, file_config: FileConfig)
¶
_cleanup()
¶
_init_xml()
¶
Initialize XML parsing if not already done.
Source code in src/readers/xml.py
_extract_value(element: ET.Element, column: str) -> str
staticmethod
¶
Extract value from XML element, checking both attributes and text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element | Element | XML element to extract from | required |
column | str | Column name to look for | required |
Returns:
Type | Description |
---|---|
str | Value from attribute or element text |
Source code in src/readers/xml.py
read_table(table_structure: TableStructure) -> Optional[TableData]
¶
Read and process a single table according to its structure.
For XML files, each table is expected to be contained within elements matching the table name or a configured xpath.