13
13
from datetime import date , datetime
14
14
from aiofiles import open
15
15
from enum import Enum
16
+ from pathlib import Path
17
+
18
+ from .utils import str2path
16
19
17
20
# Setup logging
18
21
logger = logging .getLogger ()
@@ -50,7 +53,9 @@ def csv_headers(self) -> list[str]:
50
53
"""Provide CSV headers as list"""
51
54
return list (self .dict (exclude_unset = False , by_alias = False ).keys ())
52
55
53
- def _csv_write_fields (self , left : dict [str , Any ]) -> tuple [dict [str , Any ], dict [str , Any ]]:
56
+ def _csv_write_fields (
57
+ self , left : dict [str , Any ]
58
+ ) -> tuple [dict [str , Any ], dict [str , Any ]]:
54
59
"""Write CSV fields with custom encoders
55
60
56
61
Returns columns_done, columns_left
@@ -90,7 +95,9 @@ def csv_row(self) -> dict[str, str | int | float | bool]:
90
95
res [key ] = None
91
96
return self ._clear_None (res )
92
97
93
- def _clear_None (self , res : dict [str , str | int | float | bool | None ]) -> dict [str , str | int | float | bool ]:
98
+ def _clear_None (
99
+ self , res : dict [str , str | int | float | bool | None ]
100
+ ) -> dict [str , str | int | float | bool ]:
94
101
out : dict [str , str | int | float | bool ] = dict ()
95
102
for key , value in res .items ():
96
103
if value is None :
@@ -100,7 +107,9 @@ def _clear_None(self, res: dict[str, str | int | float | bool | None]) -> dict[s
100
107
return out
101
108
102
109
@classmethod
103
- def _csv_read_fields (cls , row : dict [str , Any ]) -> tuple [dict [str , Any ], dict [str , Any ]]:
110
+ def _csv_read_fields (
111
+ cls , row : dict [str , Any ]
112
+ ) -> tuple [dict [str , Any ], dict [str , Any ]]:
104
113
"""read CSV fields with custom encoding.
105
114
Returns read, unread fields as dict[str, Any]"""
106
115
@@ -140,7 +149,9 @@ def from_csv(cls, row: dict[str, Any]) -> Self | None:
140
149
elif field_type is bool :
141
150
res [field ] = row [field ] == "True"
142
151
elif issubclass (field_type , Enum ):
143
- res [field ] = field_type [str (row [field ])] ## Enums are stored by key, not value
152
+ res [field ] = field_type [
153
+ str (row [field ])
154
+ ] ## Enums are stored by key, not value
144
155
elif field_type is date :
145
156
res [field ] = date .fromisoformat (row [field ])
146
157
elif field_type is datetime :
@@ -162,11 +173,11 @@ def from_csv(cls, row: dict[str, Any]) -> Self | None:
162
173
return None
163
174
164
175
@classmethod
165
- async def import_csv (cls , filename : str ) -> AsyncGenerator [Self , None ]:
176
+ async def import_csv (cls , filename : Path | str ) -> AsyncGenerator [Self , None ]:
166
177
"""Import from filename, one model per line"""
167
178
try :
179
+ debug ("importing from CSV file: %s" , str (filename ))
168
180
dialect : Type [Dialect ] = excel
169
- debug ("importing from CSV file: %s" , filename )
170
181
async with open (filename , mode = "r" , newline = "" ) as f :
171
182
async for row in AsyncDictReader (f , dialect = dialect ):
172
183
# debug ("row: %s", row)
0 commit comments