parser
fixtures
fixtures(
filelist: list = [],
model: str = "",
translate: dict = {},
rename: dict = {},
uniq_keys: list = [],
) -> Generator[FixtureDict, None, None]
Generates fixtures for a specified model using a list of files.
This function takes a list of files and generates fixtures for a specified model. The fixtures can be used to populate a database or perform other data-related operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filelist |
list
|
A list of files to process and generate fixtures from. |
[]
|
model |
str
|
The name of the model for which fixtures are generated. translate: A nested dictionary representing the translation mapping for fields. The structure of the translator follows the format: The translated fields will be used as keys, and their corresponding primary keys (obtained from the provided files) will be used as values in the generated fixtures. |
''
|
rename |
dict
|
{}
|
|
uniq_keys |
list
|
A list of fields that need to be considered for uniqueness in the fixtures. If specified, the fixtures will yield only unique items based on the combination of these fields. |
[]
|
Yields:
Type | Description |
---|---|
FixtureDict
|
|
Returns:
Type | Description |
---|---|
Generator[FixtureDict, None, None]
|
This function generates fixtures but does not return any value. |
Source code in alto2txt2fixture/parser.py
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 |
|
get_fields
get_fields(
file: Union[Path, str, dict],
translate: dict = {},
rename: dict = {},
allow_null: bool = False,
) -> dict
Retrieves fields from a file and performs modifications and checks.
This function takes a file (in various formats: Path
, str
, or dict
)
and processes its fields. It retrieves the fields from the file and
performs modifications, translations, and checks on the fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
Union[Path, str, dict]
|
The file from which the fields are retrieved. |
required |
translate |
dict
|
{}
|
|
rename |
dict
|
{}
|
|
allow_null |
bool
|
Determines whether to allow |
False
|
Returns:
Type | Description |
---|---|
dict
|
A dictionary representing the retrieved fields from the file, with modifications and checks applied. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the file type is unsupported or if an error occurs during field retrieval or processing. |
Source code in alto2txt2fixture/parser.py
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 309 310 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 353 354 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 |
|
get_key_from
Retrieves a specific key from a file and returns its value.
This function reads a file and extracts the value of a specified key. If the key is not found or an error occurs while processing the file, a warning is printed, and an empty string is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Path
|
The file from which the key is extracted. |
required |
x |
str
|
The key to be retrieved from the file. |
required |
Returns:
Type | Description |
---|---|
str
|
The value of the specified key from the file. |
Source code in alto2txt2fixture/parser.py
get_translator
get_translator(
fields: list[TranslatorTuple] = [TranslatorTuple("", "", [])]
) -> dict
Converts a list of fields into a nested dictionary representing a translator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fields |
list[TranslatorTuple]
|
A list of tuples representing fields to be translated. |
[TranslatorTuple('', '', [])]
|
Returns:
Type | Description |
---|---|
dict
|
Example
Source code in alto2txt2fixture/parser.py
parse
Parses files from collections and generates fixtures for various models.
This function processes files from the specified collections and generates
fixtures for different models, such as newspapers.dataprovider
,
newspapers.ingest
, newspapers.digitisation
, newspapers.newspaper
,
newspapers.issue
, and newspapers.item
.
It performs various steps, such as file listing, fixture generation, translation mapping, renaming fields, and saving fixtures to files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collections |
list
|
A list of collections from which files are processed and fixtures are generated. |
required |
cache_home |
str
|
The directory path where the collections are located. |
required |
output |
str
|
The directory path where the fixtures will be saved. |
required |
max_elements_per_file |
int
|
The maximum number of elements per file when saving fixtures. |
required |
Returns:
Type | Description |
---|---|
None
|
This function generates fixtures but does not return any value. |
Source code in alto2txt2fixture/parser.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
|
reset_fixture_dir
Resets the fixture directory by removing all JSON files inside it.
This function takes a directory path (output
) as input and removes all
JSON files within the directory.
Prior to removal, it prompts the user for confirmation to proceed. If the user confirms, the function clears the fixture directory by deleting the JSON files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output |
str | Path
|
The directory path of the fixture directory to be reset. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the |
Source code in alto2txt2fixture/parser.py
uniq
Generates unique items from a list of files based on specified keys.
This function takes a list of files and yields unique items based on a
combination of keys. The keys are extracted from each file using the
get_key_from
function, and duplicate items are ignored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filelist |
list
|
A list of files from which unique items are generated. |
required |
keys |
list
|
A list of keys used for uniqueness. Each key specifies a field to be used for uniqueness checking in the generated items. |
[]
|
Yields:
Type | Description |
---|---|
Any
|
A unique item from |