utils¤
General functions.
GitHead
dataclass
¤
GitHead(hash, timestamp)
checksum ¤
checksum(
file,
*,
algorithm="sha256",
chunk_size=65536,
shake_length=256,
)
Get the checksum of a file.
A checksum is a sequence of numbers and letters that act as a fingerprint for a file against which later comparisons can be made to detect errors or changes in the file. It can be used to verify the integrity of the file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
PathLike | FileLikeRead[bytes]
|
A file to get the checksum of. |
required |
algorithm
|
str
|
The hash algorithm to use to compute the checksum. See hashlib for more details. |
'sha256'
|
chunk_size
|
int
|
The number of bytes to read at a time from the file. It is useful to tweak this parameter when reading a large file to improve performance. |
65536
|
shake_length
|
int
|
The digest length to use for the |
256
|
Returns:
Type | Description |
---|---|
str
|
The checksum value (which only contains hexadecimal digits). |
Source code in src/msl/io/utils.py
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 |
|
copy ¤
copy(
source,
destination,
*,
overwrite=False,
include_metadata=True,
follow_symlinks=True,
)
Copy a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
PathLike
|
The path to a file to copy. |
required |
destination
|
PathLike
|
A directory to copy the file to or a full path (i.e., includes the basename). If the directory does not exist then it, and all intermediate directories, will be created. |
required |
overwrite
|
bool
|
Whether to overwrite the |
False
|
include_metadata
|
bool
|
Whether to also copy information such as the file permissions, the latest access time and latest modification time with the file. |
True
|
follow_symlinks
|
bool
|
Whether to follow symbolic links. |
True
|
Returns:
Type | Description |
---|---|
Path
|
The path to where the file was copied. |
Source code in src/msl/io/utils.py
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 |
|
get_basename ¤
get_basename(obj)
Get the basename (the final path component) of a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
PathLike | ReadLike | WriteLike
|
The object to get the basename of. If |
required |
Returns:
Type | Description |
---|---|
str
|
The basename of |
Source code in src/msl/io/utils.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
|
get_bytes ¤
get_bytes(file, *positions)
Return bytes from a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
FileLikeRead[bytes] | PathLike
|
The file to read bytes from. |
required |
positions
|
int | None
|
The position(s) in the file to retrieve bytes from. |
()
|
Examples:
get_bytes(file) # returns all bytes
get_bytes(file, 5) # returns the first 5 bytes
get_bytes(file, -5) # returns the last 5 bytes
get_bytes(file, 5, 10) # returns bytes 5 through 10 (inclusive)
get_bytes(file, 3, -1) # skips the first 2 bytes and returns the rest
get_bytes(file, -8, -4) # returns the eighth- through fourth-last bytes (inclusive)
get_bytes(file, 1, -1, 2) # returns every other byte
Returns:
Type | Description |
---|---|
bytes
|
The bytes from the file. |
Source code in src/msl/io/utils.py
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 |
|
get_extension ¤
get_extension(file)
Return the extension of the file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
PathLike | ReadLike | WriteLike
|
The file to get the extension of. |
required |
Returns:
Type | Description |
---|---|
str
|
The extension (including the |
Source code in src/msl/io/utils.py
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
|
get_lines ¤
get_lines(
file: FileLikeRead[str] | PathLike,
*lines: int | None,
remove_empty_lines: bool = False,
encoding: str | None = "utf-8",
errors: Literal["strict", "ignore"] | None = "strict",
) -> list[str]
get_lines(
file: FileLikeRead[bytes],
*lines: int | None,
remove_empty_lines: bool = False,
encoding: str | None = None,
errors: Literal["strict", "ignore"] | None = None,
) -> list[bytes]
get_lines(
file,
*lines,
remove_empty_lines=False,
encoding="utf-8",
errors="strict",
)
Return lines from a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
PathLike | ReadLike
|
The file to read lines from. |
required |
lines
|
int | None
|
The line(s) in the file to get. |
()
|
remove_empty_lines
|
bool
|
Whether to remove all empty lines. |
False
|
encoding
|
str | None
|
The name of the encoding to use to decode the file. |
'utf-8'
|
errors
|
Literal['strict', 'ignore'] | None
|
How encoding errors are to be handled. |
'strict'
|
Examples:
get_lines(file) # returns all lines
get_lines(file, 5) # returns the first 5 lines
get_lines(file, -5) # returns the last 5 lines
get_lines(file, 2, 4) # returns lines 2, 3 and 4
get_lines(file, 2, -2) # skips the first and last lines and returns the rest
get_lines(file, -4, -2) # returns the fourth-, third- and second-last lines
get_lines(file, 1, -1, 6) # returns every sixth line in the file
Returns:
Type | Description |
---|---|
list[bytes] | list[str]
|
Source code in src/msl/io/utils.py
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 |
|
git_head ¤
git_head(directory)
Get information about the HEAD of a repository.
This function requires that git is installed and
that it is available on the PATH
environment variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
PathLike
|
A directory that is under version control. |
required |
Returns:
Type | Description |
---|---|
GitHead | None
|
Information about the most recent commit on the current branch.
If |
Source code in src/msl/io/utils.py
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
|
is_admin ¤
is_admin()
Check if the current process is being run as an administrator.
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/msl/io/utils.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
is_dir_accessible ¤
is_dir_accessible(path, *, strict=False)
Check if a directory exists and is accessible.
An accessible directory is one that the user has permission to access.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathLike
|
The directory to check. |
required |
strict
|
bool
|
Whether to raise an exception if the directory is not accessible. |
False
|
Returns:
Type | Description |
---|---|
bool
|
Whether the directory exists and is accessible. |
Source code in src/msl/io/utils.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
is_file_readable ¤
is_file_readable(file, *, strict=False)
Check if a file exists and is readable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
PathLike
|
The file to check. |
required |
strict
|
bool
|
Whether to raise an exception if the file does not exist or is not readable. |
False
|
Returns:
Type | Description |
---|---|
bool
|
Whether the file exists and is readable. |
Source code in src/msl/io/utils.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
remove_write_permissions ¤
remove_write_permissions(path)
Remove all write permissions of a file.
On Windows, this function will set the file attribute to be read only.
On Linux and macOS, write permission is removed for the User, Group and Others. The read and execute permissions are preserved.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
PathLike
|
The path to remove the write permissions of. |
required |
Source code in src/msl/io/utils.py
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 |
|
run_as_admin ¤
run_as_admin(
args=None,
*,
executable=None,
cwd=None,
capture_stderr=False,
blocking=True,
show=False,
**kwargs,
)
Run a process as an administrator and return its output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
PathLike | Sequence[PathLike] | None
|
A sequence of program arguments or else a command string. Providing a sequence of arguments is generally preferred, as it allows the subprocess to take care of any required escaping and quoting of arguments (e.g., to permit spaces in file names). |
None
|
executable
|
PathLike | None
|
The executable to pass the |
None
|
cwd
|
PathLike | None
|
The working directory to use for the elevated process. |
None
|
capture_stderr
|
bool
|
Whether to send the stderr stream to stdout. |
False
|
blocking
|
bool
|
Whether to wait for the process to finish before returning to the calling program. |
True
|
show
|
bool
|
Whether to show the elevated console (Windows only). If |
False
|
kwargs
|
Any
|
If the current process already has admin privileges or if the operating system is
not Windows then all additional keyword arguments are passed to subprocess.check_output.
Otherwise, only a |
{}
|
Returns:
Type | Description |
---|---|
int | bytes | Popen[Any]
|
The returned object depends on whether the process is executed in blocking or non-blocking mode and whether Python is already running with admin privileges. If blocking, bytes are returned (the stdout stream of the process). If non-blocking, the returned object will either be the subprocess.Popen instance that is running the process (POSIX) or an int which is the process ID (Windows). |
Source code in src/msl/io/utils.py
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 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 |
|
search ¤
search(
directory,
*,
depth=0,
include=None,
exclude=None,
flags=0,
ignore_os_error=True,
ignore_hidden_folders=True,
follow_symlinks=True,
)
Search for files starting from a root directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
PathLike
|
The root directory to begin searching for files. |
required |
depth
|
int | None
|
The number of sub-directories to recursively search for files.
If |
0
|
include
|
str | Pattern[str] | None
|
A regular-expression pattern to use to include files. If
|
None
|
exclude
|
str | Pattern[str] | None
|
A regular-expression pattern to use to exclude files. The
|
None
|
flags
|
int
|
The flags to use to compile the regular-expression pattern (if it is a str type). |
0
|
ignore_os_error
|
bool
|
Whether to ignore an OSError, if one occurs, while iterating through a directory. This type of error can occur if a directory does not have the appropriate read permission. |
True
|
ignore_hidden_folders
|
bool
|
Whether to ignore a hidden directory from the search. A hidden directory
starts with a |
True
|
follow_symlinks
|
bool
|
Whether to search for files by following symbolic links. |
True
|
Yields:
Type | Description |
---|---|
Generator[Path]
|
The path to a file. |
Source code in src/msl/io/utils.py
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
send_email ¤
send_email(
config, recipients, sender=None, subject=None, body=None
)
Send an email.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
PathLike | SupportsRead[AnyStr]
|
An INI-style configuration file that contains information on how to send an email. There are two ways to send an email — Gmail API or SMTP server. An example INI file to use the Gmail API is the following (see
GMail for more details). Although all
key-value pairs are optional, a
An example INI file for an SMTP server is the following. Only the
Warning Since this information is specified in plain text in the configuration file, you should set the file permissions provided by your operating system to ensure that your authentication credentials are safe. |
required |
recipients
|
str | list[str]
|
The email address(es) of the recipient(s). Can omit the |
required |
sender
|
str | None
|
The email address of the sender. Can omit the |
None
|
subject
|
str | None
|
The text to include in the subject field. |
None
|
body
|
str | None
|
The text to include in the body of the email. The text can be enclosed
in |
None
|
Source code in src/msl/io/utils.py
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 |
|