1
1
#!/usr/bin/env python3
2
+ # /// script
3
+ # requires-python = ">=3.11"
4
+ # ///
2
5
"""Generate phrases for autokey."""
3
6
4
7
# %% Imports
35
38
LEGAL_CHARS = re .compile (r"[\x21-\x5B\x5D-\x7E]+" )
36
39
"""Regex pattern for legal characters."""
37
40
ABBREVIATION_PATTERN = re .compile (r"\x5C[\x21-\x7E]+\x20" )
38
- # ABBREVIATION_PATTERN = re.compile(r"\x5C[\x21-\x5B\x5D-\x7E]+\x20") # exclude x5C (backslash)
39
41
"""Regex pattern for abbreviations start with backslash (5C) and end in space (20)."""
42
+ SPECIAL_DIR = "custom_special"
43
+ """Directory for special characters."""
40
44
41
45
42
46
def is_char (s : object , / ) -> TypeGuard [CHAR ]:
@@ -258,6 +262,7 @@ def load_icons(fname: str | Path, /) -> list[UnprocessedSample]:
258
262
259
263
260
264
def process_icons (data : list [UnprocessedSample ], / ) -> list [UnicodeSample ]:
265
+ r"""Process the unicode samples."""
261
266
samples : list [UnicodeSample ] = []
262
267
# parsed = Counter() # ucode -> count
263
268
# all_abbreviations = defaultdict(list) # abbrv -> ucode
@@ -463,30 +468,44 @@ def generate_character(
463
468
target_dir : Path ,
464
469
default : bool = False ,
465
470
) -> None :
466
- """Generate special characters."""
467
-
471
+ r"""Generate special characters."""
468
472
text_file = target_dir / f"{ sample .ucode } .txt"
469
473
json_file = target_dir / f".{ sample .ucode } .json"
470
-
471
474
if text_file .exists () or json_file .exists ():
472
- if query_choice (
473
- f"Delete existing special character { sample .char !r} ?" , choices = [False , True ]
475
+ match query_choice (
476
+ f"Found existing special character { sample .char !r} ({ sample .ucode } )." ,
477
+ choices = ["keep" , "delete" , "overwrite" ],
474
478
):
475
- text_file .unlink ()
476
- json_file .unlink ()
477
- elif query_choice (
478
- f"Do you want to generate special character { sample .char !r} ? ({ default = } )" ,
479
- choices = [default , not default ],
480
- ):
481
- target_dir .mkdir (exist_ok = True )
482
- create_autokey_phrase (
483
- NEWLINE , template = template , path = target_dir , overwrite = True
484
- )
485
- LOGGER .info ("✅ Added %s." , sample .char )
479
+ case "keep" :
480
+ pass
481
+ case "delete" :
482
+ text_file .unlink ()
483
+ json_file .unlink ()
484
+ return
485
+ case "overwrite" :
486
+ text_file .unlink ()
487
+ json_file .unlink ()
488
+ case _:
489
+ raise ValueError ("Invalid choice." )
490
+ else :
491
+ match query_choice (
492
+ f"Install special character { sample .char !r} ?" ,
493
+ choices = [False , True ],
494
+ ):
495
+ case False :
496
+ return
497
+ case True :
498
+ pass
499
+ case _:
500
+ raise ValueError ("Invalid choice." )
501
+
502
+ target_dir .mkdir (exist_ok = True )
503
+ create_autokey_phrase (sample , template = template , path = target_dir , overwrite = True )
504
+ LOGGER .info ("✅ Added %s." , sample .char )
486
505
487
506
488
507
def generate_help (* , target_dir : Path ) -> None :
489
- """Create help script and config."""
508
+ r """Create help script and config."""
490
509
LOGGER .info ("=" * 80 )
491
510
LOGGER .info ("Creating help script in %s." , target_dir )
492
511
@@ -505,7 +524,7 @@ def generate_help(*, target_dir: Path) -> None:
505
524
506
525
507
526
def make_template (abbreviations : list [str ], description : str , sendmode : str ) -> JSON :
508
- """Create the template."""
527
+ r """Create the template."""
509
528
return TEMPLATE | {
510
529
"abbreviation" : ABBREVIATION | {"abbreviations" : abbreviations },
511
530
"description" : description ,
@@ -517,6 +536,7 @@ def make_template(abbreviations: list[str], description: str, sendmode: str) ->
517
536
518
537
519
538
def main () -> None :
539
+ r"""Install function."""
520
540
logging .basicConfig (level = logging .INFO )
521
541
522
542
detected_directory = Path .home () / ".config/autokey/data/"
@@ -586,7 +606,7 @@ def main() -> None:
586
606
generate_codes (directory , target_dir = target_dir , template = template )
587
607
588
608
# add special characters
589
- special = target_dir / "special"
609
+ special = target_dir / SPECIAL_DIR
590
610
generate_character (TABULATOR , target_dir = special , template = template , default = False )
591
611
generate_character (NEWLINE , target_dir = special , template = template , default = False )
592
612
0 commit comments