Readers
MemoryReader
¶
Bases: Reader
Reader for a list of dicts already in memory.
Source code in ckanext/versioned_datastore/lib/importing/readers.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
__init__(source)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
List[dict]
|
a list of dicts |
required |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
360 361 362 363 364 | |
get_fields()
¶
Return the fields present in the source dicts. The fields will be returned in the order they are found in the source from first dict to last. This ensures the ordering is representative of the source.
Returns:
| Type | Description |
|---|---|
List[str]
|
a list of field names |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
NoCandidateFileFoundInZip
¶
Bases: Exception
Raised when there is no file found in the zip which we can ingest.
Source code in ckanext/versioned_datastore/lib/importing/readers.py
391 392 393 394 395 396 397 | |
Reader
¶
Bases: ABC
Abstract base class for reader implementations.
Source code in ckanext/versioned_datastore/lib/importing/readers.py
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 | |
get_count()
abstractmethod
¶
Returns the number of rows in the source.
Returns:
| Type | Description |
|---|---|
int
|
an integer representing the number of rows |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
176 177 178 179 180 181 182 183 | |
get_fields()
abstractmethod
¶
Returns the list of the field names found in the source.
Returns:
| Type | Description |
|---|---|
List[str]
|
the fields in the source this Reader is reading |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
158 159 160 161 162 163 164 165 | |
get_name()
abstractmethod
¶
Returns:
| Type | Description |
|---|---|
str
|
a name for the Reader instance (for logging) |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
151 152 153 154 155 156 | |
read()
abstractmethod
¶
Actually reads the data from the source and yields dicts for each row found.
Returns:
| Type | Description |
|---|---|
Iterable[dict]
|
yields dicts |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
167 168 169 170 171 172 173 174 | |
ReaderNotFound
¶
Bases: Exception
Exception indicating that no reader could be found for the provided file format.
Source code in ckanext/versioned_datastore/lib/importing/readers.py
21 22 23 24 25 26 27 28 | |
SVReader
¶
Bases: Reader
Class for reading csv and tsv files.
Source code in ckanext/versioned_datastore/lib/importing/readers.py
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 244 245 246 247 248 249 250 | |
__init__(source)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
the source file path |
required |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
191 192 193 194 195 196 197 198 199 200 201 | |
get_fields()
¶
Reads the first line of the source file and returns it as a list.
Returns:
| Type | Description |
|---|---|
List[str]
|
the field names |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
225 226 227 228 229 230 231 232 | |
get_name()
¶
Creates a name for this SVReader instance which represents the dialect and character encoding found (useful for debugging why someone's data wasn't ingested as expected).
Returns:
| Type | Description |
|---|---|
str
|
the name |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
read()
¶
Reads each line of the source file and yields each row as a dict.
Returns:
| Type | Description |
|---|---|
Iterable[dict]
|
yields each row as a dict |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
234 235 236 237 238 239 240 241 | |
UnidentifiedEncoding
¶
Bases: Exception
Exception indicating that the encoding could not be identified.
Source code in ckanext/versioned_datastore/lib/importing/readers.py
132 133 134 135 136 137 138 139 140 141 142 143 | |
XLSReader
¶
Bases: Reader
Reader for XLS files (i.e. old Excel spreadsheets).
Source code in ckanext/versioned_datastore/lib/importing/readers.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
__init__(source)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
the file path to the source XLS file |
required |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
258 259 260 261 262 263 264 265 266 267 268 269 | |
read()
¶
Yields a dict for each row in the file's first sheet. We do some basic handling of types to ensure text, numbers, and booleans are converted correctly. Dates are not handled because the way dates are handled in XLS files is extremely complicated, and it's easier just to tell people not to do it, or use a string representation that Splitgill can parse.
Empty field names, empty values, and values we can't convert are ignored.
Returns:
| Type | Description |
|---|---|
Iterable[dict]
|
yields a dict per row |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
XLSXReader
¶
Bases: Reader
Reader for new style Excel spreadsheets.
Source code in ckanext/versioned_datastore/lib/importing/readers.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
__init__(source)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
the path to the XLSX file |
required |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
316 317 318 319 320 321 322 323 324 325 326 327 | |
read()
¶
Yields the rows from the spreadsheet's first sheet as dicts. All type conversions are handled by openpyxl. Empty field names or empty cell values are ignored.
Returns:
| Type | Description |
|---|---|
Iterable[dict]
|
a dict per row |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
ZipReader
¶
Bases: Reader
A reader for zip files.
This reader looks in the zip and inspects the files inside in alphabetical order until it finds a file it can read, at which point it creates a reader for the file and this reader is used to fulfill the abstract base class's requirements.
Source code in ckanext/versioned_datastore/lib/importing/readers.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
__init__(source)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
the path to the zip file |
required |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
choose_reader(resource_format, source)
¶
Chooses an appropriate reader for the provided source. If the source is a file, this uses the resource_format to choose the reader, if the source is a list of dicts, then the MemoryReader is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_format
|
str
|
the resource format |
required |
source
|
Union[Path, List[dict]]
|
the source data, either a path to a file, or a list of dicts |
required |
Returns:
| Type | Description |
|---|---|
Reader
|
a Reader instance |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
choose_reader_for_resource(resource, source)
¶
Selects a reader for the given resource using the format primarily, and resource url secondarily.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource
|
dict
|
the resource dict |
required |
source
|
Union[Path, List[dict]]
|
the source data, either a path to a file, or a list of dicts |
required |
Returns:
| Type | Description |
|---|---|
Reader
|
a Reader instance |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
detect_encoding(source, test_encoding=True)
¶
Given a file, attempt to detect the character encoding it uses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
the path to the file |
required |
test_encoding
|
bool
|
whether to test the detected encoding - and some common alternatives if necessary (default True) |
True
|
Returns:
| Type | Description |
|---|---|
str
|
the character encoding |
Source code in ckanext/versioned_datastore/lib/importing/readers.py
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 | |