stepup.core.nglob¶
Glob with named back-reference support.
Named glob (NGlob) patterns are an advanced form of pattern matching that supports back referencing of previously matched substrings.
It has the following use cases:
-
Single named wildcard: By default, the wildcard
${*name}
is a placeholder for any string. One may also specify a pattern for${*name}
through optional arguments. For example:Unlike ordinary wildcards, named wildcards never match an empty string.
-
Consistency within one pattern: If a pattern uses the same named globs multiple times, the matching substring must also be consistent. For example:
ngs = NGlobSingle("archive_${*idx}/feedback_${*idx}.md", idx="[0-9][0-9][0-9]") ngs.glob() print(ngs.results)
These would match:
archive_042/feedback_042.md
archive_777/feedback_777.md
This won’t match:
archive_042/feedback_777.md
-
Consistency across multiple patterns: One can define multiple patterns and enforce consistency between their matches. For example:
ngm = NGlobMulti("feedback_${*idx}.md", "report_${*idx}.pdf", idx="[0-9][0-9][0-9]") ngm.glob() print(ngm.results)
This will produce pairs of matches (provided the files are present). For example, the following would match:
feedback_001.md
withreport_001.pdf
feedback_123.md
withreport_123.pdf
The following won’t be in the results, despite the fact that the files exist:
feedback_001.md
withreport_123.pdf
-
Conventional (recursive) glob wildcards are also allowed and are called “anonymous wildcards” to clarify the distinction from named wildcards.
NGlobMatch
¶
A set of matches corresponding sharing consistent values for named wildcards.
The matching files can be accessed by integer indexing or through the files
attribute:
The substring matching the named wildcards can be accessed as attributes.
For example, the substring matching a named wildcard foo
is accessed as follows:
When you expect only a single matching file, then the single
attribute can be used.
It will raise an exception when there are zero or multiple matches:
In the unfortunate case that your named wildcards are named single
, files
or mapping
,
you can access their values through the mapping
attribute:
Source code in stepup/core/nglob.py
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 |
|
files
property
¶
Matching files, all having consistent substrings matching the named wildcards.
Each item corresponds to a pattern in NGlobMulti.patterns
.
If a pattern has anonymous wildcards,
the item itself is a list of all files matching the pattern,
If the pattern contains no anonymous wildcards,
the corresponding item in the returned list is a single path.
mapping
property
¶
Dictionary with (wildcard_name, substring)
items.
single
property
¶
A single path if there is exactly one match, raises an error otherwise.
NGlobMulti
¶
A sequence of Named Glob patterns for which consistent matches are collected.
Source code in stepup/core/nglob.py
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 380 381 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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
|
has_wildcards
property
¶
True if any named or anonymous wildcards are present in the patterns.
nglob_singles
property
¶
The list of NGlobSingle instances, one for each pattern.
These instances collect (partial) matches before any consistency is imposed between the substrings matching the same name in different patterns.
patterns
property
¶
The list of Named Glob patterns.
results
property
¶
A dictionary with all matches collected so far.
A key in this dictionary is a tuple of substrings named wildcards,
using the same order as the used_names
attribute.
A value is a list of sets of paths.
Each item in the list is a set of matching filenames for the corresponding
pattern from the patterns
attribute, whose named wildcards match the substrings
of the key.
The results can be extended with the extend
and glob
methods.
Conversely, results can be removed with the reduce
method.
subs
property
¶
User-defined glob patterns for the named wildcards.
When a name is not present, *
is used.
used_names
property
¶
The names used across all the named glob patterns.
__bool__()
¶
__iter__()
¶
Iterates over self.matches
if there are named wildcards, else over self.files
.
_extend_consistent(i, values)
¶
Extend the results of this instance, given an added combination of matching substrings.
Parameters:
-
i
(int
) –The integer index of the pattern in the
patterns
attribute being processed. -
values
(tuple[str, ...]
) –A new set of substrings matching the named wildcards.
Source code in stepup/core/nglob.py
_iter_consistent(criteria, full_paths)
¶
Iterate over (partial) matching substrings and corresponding paths.
Parameters:
-
criteria
(dict[str, str]
) –A dictionary mapping named wildcards to matching substrings.
-
full_paths
(list | int
) –If this is a list, it contains lists of paths matching the patterns in of the
patterns
attribute with substrings consistent with those in the criteria argument. Note that this is a recursive iterator, so full_paths may contain fewer items than there are patterns when the recursion has not reached it full depth yet. If this is an integer, it is in index referring to the item in thepatterns
to identify the current pattern being processed.
Source code in stepup/core/nglob.py
_reduce_consistent(i, values)
¶
Return the results of this instance, given a removed combination of matching substrings.
Parameters:
-
i
(int
) –The integer index of the pattern in the
patterns
attribute being processed. -
values
(tuple[str, ...]
) –A new set of substrings matching the named wildcards.
Source code in stepup/core/nglob.py
deepcopy()
¶
equals(other)
¶
extend(paths)
¶
Try to extend the results by searching for matches in the given list of paths.
Source code in stepup/core/nglob.py
files()
¶
Return a tuple of sorted files that match the individual patterns.
No constraints between multiple patterns are imposed and files may belong to partial and inconsistent full matches.
Source code in stepup/core/nglob.py
from_patterns(patterns, subs=None)
classmethod
¶
Create a new instance for given patterns without any results.
Parameters:
-
patterns
(Iterable[str]
) –Named Glob patterns. Results will be constrained to have consistently matching substrings for the named wildcards appearing in all the patterns.
-
subs
(dict[str, str] | None
, default:None
) –Optional anonymous glob patterns for the named patterns. When a name is not present, the wildcard
*
is used for this name.
Source code in stepup/core/nglob.py
glob()
¶
Extend the results with paths found by the built-in glob function.
matches()
¶
Iterate over combinations of files that consistently match all patterns.
This offers a more convenient interface of the results
attribute.
Yields:
-
nglob_match
–An instance of NGlobMatch, which contains the substrings matching the named wildcards and the corresponding lists of paths.
Source code in stepup/core/nglob.py
may_change(deleted, added)
¶
Determine whether the results may change (later) after deleting or adding files.
Parameters:
-
deleted
(set[str]
) –Set of files to be deleted.
-
added
(set[str]
) –Set of files to be added.
Returns:
-
may_change
–True if the NGlobMulti results may change. (It may require additional additions and deletions to get any effect, but cannot be excluded that the provided deletions and updates play a role in it.)
Source code in stepup/core/nglob.py
may_match(path)
¶
Return True if the path matches one of the NGlobSingle instances.
This means that it may be a path contributing to a consistent match of NGlobMulti.
When added, it will show up in the result of the files
method,
and it may affect the outcome of the matches
method.
Source code in stepup/core/nglob.py
reduce(paths)
¶
Drop results by eliminating the provided paths.
Source code in stepup/core/nglob.py
single()
¶
Return the single matching path.
Raises:
-
ValueError
–If there is not exactly one match.
Source code in stepup/core/nglob.py
will_change(deleted, added)
¶
Determine whether the results will change after deleting or adding files.
Parameters:
-
deleted
(Collection[str]
) –Set of files to be deleted.
-
added
(Collection[str]
) –Set of files to be added.
Returns:
-
evolved
–a new modified copy with the changes if any. None otherwise.
Source code in stepup/core/nglob.py
NGlobSingle
¶
Named glob with a single pattern.
Source code in stepup/core/nglob.py
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 251 252 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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
glob_pattern
property
¶
The conversion of the named glob to a (more general) conventional glob pattern.
pattern
property
¶
The Named Glob pattern used to match filenames.
regex
property
¶
The conversion of the named glob to a regular expression.
results
property
¶
All matching files, grouped by substrings matching the named wildcards.
The keys of the results
dictionary are tuples with the substrings,
matching the respective named wildcards in the used_names
tuple.
The values are sets with matching paths.
subs
property
¶
User-defined glob patterns for the named wildcards.
When a name is not present, *
is used.
used_names
property
¶
A tuple of named wildcards present in the pattern.
_loop_matches(paths)
¶
Low-level iterator used by the extend
and reduce
methods.
The paths are tested one by one against the regular expression. In case of a hit, it yields a tuple with the following three items:
values
: the substrings matching the named wildcards.path_set
: the current set of paths associated with the combination of substrings.path
: aPath
instance of the matching path.
Source code in stepup/core/nglob.py
extend(paths)
¶
Add matching paths from the given list paths.
Yields:
-
values
–A tuple with substring matching the named wildcards, only this combination of names was not present yet.
Source code in stepup/core/nglob.py
glob()
¶
Extend the results with paths obtained through Python’s built-in glob module.
Yields:
-
values
–A tuple with substring matching the named wildcards, only this combination of names was not present yet.
Source code in stepup/core/nglob.py
reduce(paths)
¶
Remove matching paths from given list paths.
Yields:
-
values
–A tuple with deleted substring matching the named wildcards, only if the last matching paths were removed.
Source code in stepup/core/nglob.py
convert_nglob_to_glob(pattern, subs=None)
¶
Convert nglob wildcards to ordinary ones, compatible with builtin glob and fnmatch modules.
Parameters:
-
pattern
(str
) –A string with named wildcards.
-
subs
(dict[str, str] | None
, default:None
) –A dictionary mapping names to glob patterns. If a name is not present,
*
is used as default.
Returns:
-
pattern
–A conventional wildcard string, without the constraint that named wildcards must correspond. Where possible, neighboring wildcards are merged into one.
Source code in stepup/core/nglob.py
convert_nglob_to_regex(pattern, subs=None, allow_names=True)
¶
Convert a named glob pattern to a regular expressions.
Parameters:
-
pattern
(str
) –A string with named wildcards.
-
subs
(dict[str, str] | None
, default:None
) –A dictionary mapping names to glob patterns. If a name is not present,
*
is used as default. -
allow_names
(bool
, default:True
) –When set to
False
, named wildcards are not allowed.
Returns:
-
regex
–A regular expression string to test if a string matches the pattern. It also contains symbolic groups to extract values corresponding to named wildcards and to impose consistency when the same name appears multiple times.
Source code in stepup/core/nglob.py
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
|
has_anonymous_wildcards(pattern)
¶
Test if a glob pattern has anonymous wildcards.
has_wildcards(pattern)
¶
iter_wildcard_names(pattern)
¶
Iterate over the names of the named wildcards in a Named Glob pattern.