xpuz.utils
¤
General/specialised utility functions.
BlockUtils
¤
Methods to help with manipulating block objects in both pages/editor.py
and pages/browser.py.
_config_selectors
classmethod
¤
Enable or disable all the radiobutton selectors in cls.blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs |
Dict[str, Any]
|
Configuration parameters. |
{}
|
Source code in src/xpuz/utils.py
378 379 380 381 382 383 384 385 386 | |
_match_block_query
staticmethod
¤
Return True if any part of block_name (split into the words that
it consists of) starts with query, or if category starts with
query. All comparisons are caseless and do not regard whitespace
(except the category, which keeps its whitespace).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query |
str
|
A query entered by the user. |
required |
block_name |
str
|
The current block name that is being queried. |
required |
category |
str
|
The category of the current block name that is being queried. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the query was found in the category or block name, or not. |
Source code in src/xpuz/utils.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
_put_block
classmethod
¤
_put_block(block: CTkFrame, side: str = 'left') -> None
Pack block in its parent container and append it to the available
blocks in cls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block |
CTkFrame
|
The block instance to be packed. |
required |
side |
str
|
The side to pack the block. |
'left'
|
Source code in src/xpuz/utils.py
336 337 338 339 340 341 342 343 344 345 346 347 | |
_remove_block
classmethod
¤
_remove_block(block: CTkFrame) -> None
Remove block from its parent container and the available blocks
in cls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block |
CTkFrame
|
The block to remove. |
required |
Source code in src/xpuz/utils.py
349 350 351 352 353 354 355 356 357 358 | |
_set_all
classmethod
¤
_set_all(func: Callable) -> None
Call func, passing each block in cls.blocks as parameters.
Most often used to put or remove all of the blocks in cls.blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func |
Callable
|
The function to perform on the block. |
required |
Source code in src/xpuz/utils.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
GUIHelper
¤
_install_translations
staticmethod
¤
_install_translations(locale: Locale) -> None
Install translations from locale.language with gettext.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
locale |
Locale
|
The current locale object. |
required |
Source code in src/xpuz/utils.py
59 60 61 62 63 64 65 66 67 68 69 70 71 | |
confirm_with_messagebox
staticmethod
¤
Provide confirmations to the user with tkinter messageboxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args |
Tuple[str]
|
Optional info messages to be included with specific messagebox calls. |
()
|
**kwargs |
Dict[str, str]
|
The specific messagebox name, selected by this method's
|
{}
|
Returns:
| Type | Description |
|---|---|
bool
|
Whether the user confirmed/pressed yes on the messagebox or not. |
Source code in src/xpuz/utils.py
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 | |
show_messagebox
staticmethod
¤
Show an error/info messagebox
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args |
Tuple[str]
|
Optional error messages/details to be included with specific messagebox calls. |
()
|
**kwargs |
Dict[str, str]
|
The specific messagebox name, selected by this method's
|
{}
|
Source code in src/xpuz/utils.py
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 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 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 | |
_check_doc_cfg_is_up_to_date
¤
_check_doc_cfg_is_up_to_date() -> bool
Check if the all the sections (and values of those sections) are present in the document config.ini file.
Returns:
| Type | Description |
|---|---|
bool
|
Whether the config.ini in the system's documents directory is up-to-date |
bool
|
or not. |
Source code in src/xpuz/utils.py
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 | |
_check_version
¤
Find latest remote GitHub release if it is higher than the local
release using the urllib module.
Returns:
| Type | Description |
|---|---|
Union[None, str]
|
The latest release, if it was found. Otherwise, returns |
Source code in src/xpuz/utils.py
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 | |
_doc_data_routine
¤
_doc_data_routine(
doc_callback: Optional[Callable] = None,
local_callback: Optional[Callable] = None,
toplevel: PathLike = DOC_PATH,
datalevel: PathLike = DOC_DATA_PATH,
sublevel: PathLike = DOC_CFG_PATH,
) -> bool
Scan through both the package and system document directories, making the required folders if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc_callback |
Optional[Callable]
|
A function to execute if possible when scanning the system's document directory to make missing files. |
None
|
local_callback |
Optional[Callable]
|
A function to execute if possible when scanning the
local data of the |
None
|
toplevel |
PathLike
|
The path of the system's document directory. |
DOC_PATH
|
datalevel |
PathLike
|
The path to the data files within |
DOC_DATA_PATH
|
sublevel |
PathLike
|
The path to |
DOC_CFG_PATH
|
Returns:
| Type | Description |
|---|---|
bool
|
Whether the document data has been successfully made or not. |
Source code in src/xpuz/utils.py
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 | |
_find_best_crossword
¤
_find_best_crossword(
crossword: Crossword, cls: Crossword
) -> Crossword
Determine the best crossword out of a amount of instantiated crosswords based on the largest amount of total intersections and smallest amount of fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crossword |
Crossword
|
The |
required |
cls |
Crossword
|
The |
required |
Returns:
| Type | Description |
|---|---|
Crossword
|
The best |
Source code in src/xpuz/utils.py
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 930 931 932 933 934 935 936 937 938 939 940 | |
_format_definitions
¤
Randomly pick definitions from a larger sample, then prune everything except the language characters from the words (the keys of the definitions).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
definitions |
Dict[str, str]
|
The crossword's definitions. |
required |
word_count |
int
|
The crossword's word count. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
The formatted crossword definitions. |
Source code in src/xpuz/utils.py
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 | |
_get_base_categories
¤
Get all the available crossword categories sorted alphabetically.
Returns:
| Type | Description |
|---|---|
Iterable[DirEntry]
|
All the directory entries to the available crossword categories. They |
Iterable[DirEntry]
|
can be sourced from both the package and the system's data. |
Source code in src/xpuz/utils.py
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 | |
_get_base_crosswords
¤
_get_base_crosswords(
category: Union[DirEntry, PathLike],
sort: bool = True,
allow_empty_defs: bool = False,
) -> Iterable[DirEntry]
Get all the available crosswords from the base crossword directory if
they have valid definitions.json files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category |
Union[DirEntry, PathLike]
|
The path/directory entry to the category. |
required |
sort |
bool
|
Whether to sort the obtained crosswords or not. |
True
|
allow_empty_defs |
bool
|
Consider a crossword with no definitions to be valid if it is user made. |
False
|
Returns:
| Type | Description |
|---|---|
Iterable[DirEntry]
|
The acquired crosswords, sorted or unsorted, based on if any errors |
Iterable[DirEntry]
|
occurred during the sorting process. |
Source code in src/xpuz/utils.py
682 683 684 685 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 | |
_get_colour_palette
¤
_get_colour_palette(
appearance_mode: Union[
Literal["Light"], Literal["Dark"], Literal["System"]
]
) -> Dict[str, str]
Create a dictionary based on constants.Colour for the web app.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
appearance_mode |
Union[Literal['Light'], Literal['Dark'], Literal['System']]
|
The current tkinter appearance mode. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
A colour code dictionary. |
Source code in src/xpuz/utils.py
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 | |
_get_english_string
¤
_get_english_string(
eng_arr: List[str],
localised_arr: List[str],
index_value: Union[str, int],
) -> str
Find the english version of index_value by finding its index in
localised_arr, then using the resulting integer to index eng_arr.
This function assumes both arrays have the same relative order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eng_arr |
List[str]
|
The default english values. |
required |
localised_arr |
List[str]
|
The localised values. |
required |
index_value |
Union[str, int]
|
The position of the localised string within |
required |
Returns:
| Type | Description |
|---|---|
str
|
The english string that was indexed. |
Source code in src/xpuz/utils.py
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
_get_language_options
¤
Gather a dictionary that maps each localised language name to its
english acronym, and a list that contains all of the localised language
names. This data is derived from LOCALES_PATH.
Returns:
| Type | Description |
|---|---|
Tuple[Dict[str, str], List[str]]
|
The localised language dictionary and the localised languages list. |
Source code in src/xpuz/utils.py
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 | |
_get_open_filename
¤
Get user input for the location to a file that is to be opened.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title |
str
|
The file explorer's titlebar text. |
required |
filetypes |
List[Tuple[str]]
|
Available filetypes to display. |
required |
Source code in src/xpuz/utils.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
_get_saveas_filename
¤
_get_saveas_filename(
title: str,
name: str,
extension: str,
filetypes: List[Tuple[str]],
) -> Union[str, PathLike]
Get user input for a file save location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title |
str
|
The file explorer's titlebar text. |
required |
name |
str
|
The default file name. |
required |
extension |
str
|
The file extension. |
required |
filetypes |
List[Tuple[str]]
|
Available filetypes to save the file as. |
required |
Source code in src/xpuz/utils.py
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
_interpret_cword_data
¤
_interpret_cword_data(
crossword: Crossword,
) -> Tuple[
List[Tuple[int]],
List[Dict[int, Tuple[str]]],
List[List[int]],
]
Gather data to help with the templated creation of the crossword web application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crossword |
Crossword
|
The |
required |
Returns:
| Type | Description |
|---|---|
List[Tuple[int]]
|
The interpreted crossword data. Please view the source code below for |
List[Dict[int, Tuple[str]]]
|
more information. |
Source code in src/xpuz/utils.py
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 | |
_load_attempts_db
¤
Load attempts_db.json, which specifies how many generation attempts
should be conducted for a crossword based on its word count. This is
integral to the crossword optimisation process, as crossword generation
time scales logarithmically with word count.
Returns:
| Type | Description |
|---|---|
Dict[str, int]
|
The attempts dictionary. |
Source code in src/xpuz/utils.py
799 800 801 802 803 804 805 806 807 808 809 810 | |
_make_category_info_json
¤
Write a new info.json to a category since it does not exist in the a category's directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp |
PathLike
|
The path to write the category's |
required |
hex_ |
str
|
A hexadecimal colour value stored in a string, which represents the colour of the category's bottom tag colour. |
None
|
Source code in src/xpuz/utils.py
784 785 786 787 788 789 790 791 792 793 794 795 796 | |
_make_cword_info_json
¤
Make an info.json file for a given crossword, since it doesn't exist. Infer any required information that is not passed by the caller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp |
PathLike
|
The path to write the info to. |
required |
cword_name |
str
|
The name of the crossword. |
required |
category |
str
|
The crossword's category. |
required |
Source code in src/xpuz/utils.py
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 | |
_make_doc_cfg
¤
_make_doc_cfg() -> None
Write the contents of template.config.ini into config.ini, located
in the system's document directory.
Source code in src/xpuz/utils.py
573 574 575 576 577 578 579 580 | |
_open_file
¤
_open_file(fp: PathLike) -> None
Open fp (directory) in the OS' default file explorer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp |
PathLike
|
The filepath to open. |
required |
Source code in src/xpuz/utils.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
_randomise_definitions
¤
Randomises the existing definitions when attempting reinsertion,
which prevents _find_best_crossword from favouring certain word
groups with intrinsically higher intersections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
definitions |
Dict[str, str]
|
The crossword's definitions. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
The randomised crossword definitions. |
Source code in src/xpuz/utils.py
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | |
_read_cfg
¤
_read_cfg(cfg: ConfigParser) -> None
Determine which config file to access (whether it is
template.config.ini in the package or config.ini in the system
documents directory), and write its contents to cfg.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg |
ConfigParser
|
The config parser instance. |
required |
Source code in src/xpuz/utils.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 | |
_sort_crosswords_by_suffix
¤
_sort_crosswords_by_suffix(
cwords: Union[
List[DirEntry], List[Tuple[DirEntry, DirEntry]]
]
) -> Union[List[DirEntry], List[Tuple[DirEntry, DirEntry]]]
Sort an iterable container with crossword directory entries based on the crossword's suffix (from -easy to -extreme, if possible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cwords |
Union[List[DirEntry], List[Tuple[DirEntry, DirEntry]]]
|
A list of directory entries of crosswords, or an array of tuples with lists of both category entries and crossword entries, if sorting all available crosswords. |
required |
Returns:
| Type | Description |
|---|---|
Union[List[DirEntry], List[Tuple[DirEntry, DirEntry]]]
|
The unsorted crosswords if any one of them is missing a suffix, or the |
Union[List[DirEntry], List[Tuple[DirEntry, DirEntry]]]
|
sorted crosswords in the same structure they were passed to the function. |
Source code in src/xpuz/utils.py
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 | |
_update_cfg
¤
_update_cfg(
cfg: ConfigParser, section: str, option: str, value: str
) -> None
Update cfg at the given section, option and value, then write it
to an available config path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
section |
str
|
The section to update. |
required |
option |
str
|
The option to update. |
required |
value |
str
|
The value to write to |
required |
Source code in src/xpuz/utils.py
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | |
_update_cword_info_word_count
¤
_update_cword_info_word_count(
fp: PathLike,
info: CrosswordInfo,
total_definitions: int,
) -> None
Update the word count in a crossword's info.json file if it is
inconsistent with the amount of key-pair values in its definitions.json
file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp |
PathLike
|
The toplevel of the crossword. |
required |
info |
CrosswordInfo
|
The existing info of the crossword. |
required |
total_definitions |
int
|
The |
required |
Source code in src/xpuz/utils.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 | |
_verify_definitions
¤
Process a dictionary of definitions through statements to raise
errors for particular edge cases in a definitions dictionary. This
function also uses _format_definitions to randomly sample a specified
amount of definitions from the definitions dictionary, then format
those definitions appropriately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
definitions |
Dict[str, str]
|
The crossword's definitions. |
required |
word_count |
int
|
The crossword's word count. |
required |
Raises:
| Type | Description |
|---|---|
DefinitionsParsingError
|
If the definitions are invalid. Please view the source code below for more information. |
Source code in src/xpuz/utils.py
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 | |