-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
1483 lines (1281 loc) · 54.8 KB
/
Copy pathcore.py
File metadata and controls
1483 lines (1281 loc) · 54.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
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
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
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
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
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
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
930
931
932
933
934
935
936
937
938
939
940
941
942
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
#!/usr/bin/env python3
"""
PyDoc · core
====================
All the pure (GUI-free) logic so it can be unit-tested headless and reused by
the pywebview host:
* single-folder shortcut scanning + custom shortcuts.json
* unified metadata store (pin / group / alias / per-item hotkey / usage)
* fuzzy search with usage (frequency + recency) ranking
* inline calculator (safe AST eval)
* inline web-search / open-URL suggestions
* Windows auto-start (HKCU ...\\Run)
* launching targets cross-platform
"""
import os
import re
import ast
import sys
import json
import time
import math
import operator
import subprocess
# --------------------------------------------------------------------------- #
# App metadata
# --------------------------------------------------------------------------- #
APP_NAME = "PyDoc"
APP_VERSION = "1.0.0"
APP_CHANNEL = "Stable"
APP_TITLE = f"{APP_NAME} v{APP_VERSION} {APP_CHANNEL}" # PyDoc v1.0.0 Stable
# --------------------------------------------------------------------------- #
# Paths
# When frozen by PyInstaller, read-only bundled assets (web/, themes.json)
# live in sys._MEIPASS, while user data (config, shortcuts, profiles, …) must
# live next to the executable so it persists across runs.
# --------------------------------------------------------------------------- #
if getattr(sys, "frozen", False):
DATA_DIR = os.path.dirname(sys.executable) # writable, next to .exe
BUNDLE_DIR = getattr(sys, "_MEIPASS", DATA_DIR) # read-only bundle
else:
DATA_DIR = os.path.dirname(os.path.abspath(__file__))
BUNDLE_DIR = DATA_DIR
APP_DIR = DATA_DIR
SHORTCUT_DIR = os.path.join(DATA_DIR, "Shortcuts")
WEB_DIR = os.path.join(BUNDLE_DIR, "web") # bundled front-end
ICON_DIR = os.path.join(DATA_DIR, "Icons") # downloaded/custom icons
SHOT_DIR = os.path.join(DATA_DIR, "Screenshots") # saved screen captures
CONFIG_PATH = os.path.join(DATA_DIR, "config.json")
# prefer a user themes.json; fall back to the bundled default
THEMES_PATH = os.path.join(DATA_DIR, "themes.json")
if not os.path.exists(THEMES_PATH) and os.path.exists(os.path.join(BUNDLE_DIR, "themes.json")):
THEMES_PATH = os.path.join(BUNDLE_DIR, "themes.json")
PROFILES_DIR = os.path.join(DATA_DIR, "Profiles") # per-password data lives here
# These four are the ACTIVE profile's data paths. They start on the default
# profile and are reassigned by activate_profile() when an alternate or secret
# password is used to unlock. core functions read them as globals at call time.
CUSTOM_PATH = os.path.join(APP_DIR, "shortcuts.json")
STORE_PATH = os.path.join(APP_DIR, "store.json")
CMDHIST_PATH = os.path.join(APP_DIR, "cmd_history.json") # pinned cmds persist here
# remember the default-profile paths so we can switch back
_DEFAULT_SHORTCUT_DIR = SHORTCUT_DIR
_DEFAULT_CUSTOM_PATH = CUSTOM_PATH
_DEFAULT_STORE_PATH = STORE_PATH
_DEFAULT_CMDHIST_PATH = CMDHIST_PATH
ACTIVE_PROFILE = "default"
QLLINK_EXT = ".qllink" # tiny JSON link file we drop into Shortcuts/
MAX_PROFILES = 10 # alternate password-protected profiles
# --------------------------------------------------------------------------- #
# Defaults
# --------------------------------------------------------------------------- #
DEFAULT_THEMES = {
"Windows 11 Dark": {"mode": "dark", "primary": "#4cc2ff"},
"Windows 11 Light": {"mode": "light", "primary": "#0067c0"},
"Graphite": {"mode": "dark", "primary": "#9aa0a6"},
"Nord": {"mode": "dark", "primary": "#88c0d0"},
"Mint": {"mode": "dark", "primary": "#34d399"},
"Sunset": {"mode": "dark", "primary": "#fb923c"},
}
DEFAULT_CONFIG = {
"theme": "Windows 11 Dark",
"hotkey": "ctrl+windows+n", # open / toggle the launcher (Win+Ctrl+N)
"hotkey_add": "ctrl+windows+a", # open the "add shortcut" form
"show_path_subtext": True,
"autostart": True, # run as a background tray app by default
"show_on_launch": False, # start hidden in the tray
"hide_on_blur": True, # close/hide the window when it loses focus
"max_results": 9,
"enable_calculator": True,
"enable_websearch": True,
"enable_screenshot": True, # show screenshot actions in results
"hotkey_screenshot": "ctrl+windows+s", # global region-capture hotkey
"screenshot_copy": True, # also copy captures to the clipboard
"enable_runcmd": True, # show the "Run command" action
"hotkey_runcmd": "ctrl+windows+r", # global hotkey to open the command runner
"cmd_autocorrect": True, # suggest fixes for typo'd commands
"cmd_shell": "", # "" = auto (cmd/powershell/sh); or a shell path
"web_search_url": "https://www.google.com/search?q={q}",
"active_group": "All",
"lock_enabled": False, # require a password to search/launch
"lock_hash": "", # sha256(salt + main password) (default profile)
"lock_salt": "",
"master_hash": "", # sha256(salt + master password) — recovery
"master_salt": "",
"profiles": [], # list of alternate profiles (see add_profile)
"secret": {}, # the single hidden "secret" profile
"clear_on_hide": True, # clear the search box each time it hides
"max_recent": 6, # recent/frequent items shown on empty search
}
DEFAULT_CUSTOM = {
"_comment": "Base definition for custom shortcuts. Extra metadata "
"(pinned/group/alias/hotkey/usage) lives in store.json and is "
"editable from the in-app Settings window.",
"shortcuts": [
{"name": "Google", "target": "https://www.google.com", "image": "ms:language"},
{"name": "Calculator", "target": "calc", "image": "ms:calculate"},
{"name": "Open Shortcuts Folder", "target": "@open_shortcut_folder", "image": "ms:folder"},
],
}
DEFAULT_STORE = {"usage": {}, "meta": {}}
# Default icons: Google Material Symbol ligature names (rendered with the
# bundled font, prefixed "ms:" in image strings). No emojis anywhere.
ICONS_BY_EXT = {
".exe": "rocket_launch", ".lnk": "link", ".url": "language", ".bat": "terminal",
".cmd": "terminal", ".ps1": "terminal", ".sh": "terminal", ".py": "code",
".js": "code", ".html": "code", ".css": "code", ".json": "data_object",
".txt": "description", ".md": "article", ".pdf": "picture_as_pdf",
".doc": "description", ".docx": "description", ".xls": "table_chart",
".xlsx": "table_chart", ".ppt": "slideshow", ".pptx": "slideshow",
".png": "image", ".jpg": "image", ".jpeg": "image", ".gif": "image",
".svg": "image", ".webp": "image", ".ico": "image",
".mp3": "music_note", ".wav": "music_note", ".flac": "music_note",
".mp4": "movie", ".mkv": "movie", ".avi": "movie", ".mov": "movie",
".zip": "folder_zip", ".rar": "folder_zip", ".7z": "folder_zip",
}
DEFAULT_FILE_ICON = "draft"
FOLDER_ICON = "folder"
URL_ICON = "language"
# --------------------------------------------------------------------------- #
# JSON helpers
# --------------------------------------------------------------------------- #
def load_json(path, default):
if not os.path.exists(path):
save_json(path, default)
return json.loads(json.dumps(default))
try:
with open(path, "r", encoding="utf-8") as fh:
return json.load(fh)
except Exception:
return json.loads(json.dumps(default))
def save_json(path, data):
try:
with open(path, "w", encoding="utf-8") as fh:
json.dump(data, fh, indent=2, ensure_ascii=False)
return True
except Exception as exc:
print(f"[PyDoc] save error ({path}): {exc}")
return False
# --------------------------------------------------------------------------- #
# Auto-start on Windows login
# --------------------------------------------------------------------------- #
RUN_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run"
RUN_VALUE = "PyDoc"
def _autostart_command():
# When frozen, autostart the .exe directly; otherwise pythonw launcher.py
if getattr(sys, "frozen", False):
return f'"{sys.executable}"'
script = os.path.join(APP_DIR, "launcher.py")
pyw = os.path.join(os.path.dirname(sys.executable), "pythonw.exe")
exe = pyw if os.path.exists(pyw) else sys.executable
return f'"{exe}" "{script}"'
def is_autostart_enabled():
if not sys.platform.startswith("win"):
return False
try:
import winreg # type: ignore
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY) as key:
val, _ = winreg.QueryValueEx(key, RUN_VALUE)
return bool(val)
except Exception:
return False
def set_autostart(enable):
if not sys.platform.startswith("win"):
return False
try:
import winreg # type: ignore
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY, 0,
winreg.KEY_SET_VALUE) as key:
if enable:
winreg.SetValueEx(key, RUN_VALUE, 0, winreg.REG_SZ,
_autostart_command())
else:
try:
winreg.DeleteValue(key, RUN_VALUE)
except FileNotFoundError:
pass
return True
except Exception as exc:
print(f"[PyDoc] autostart error: {exc}")
return False
# --------------------------------------------------------------------------- #
# Password lock + profiles (sha256 with per-entry salt; stored in config.json)
#
# Profiles let one password reveal one set of shortcuts and another password
# reveal a completely different set:
# * default profile -> Shortcuts/ , shortcuts.json , store.json
# * alternate profile -> Profiles/<id>/ (up to MAX_PROFILES)
# * secret profile -> Profiles/__secret__/ (hidden; for sensitive links)
# Each profile has its own folder, custom-shortcuts file, store + cmd history.
# --------------------------------------------------------------------------- #
def _hash_password(password, salt):
import hashlib
return hashlib.sha256((salt + (password or "")).encode("utf-8")).hexdigest()
def _new_salt():
import secrets
return secrets.token_hex(16)
def _profile_paths(pid):
"""Return the data paths for a profile id ('default' or a profile id)."""
if pid == "default":
return {"dir": _DEFAULT_SHORTCUT_DIR, "custom": _DEFAULT_CUSTOM_PATH,
"store": _DEFAULT_STORE_PATH, "cmd": _DEFAULT_CMDHIST_PATH}
base = os.path.join(PROFILES_DIR, pid)
return {"dir": os.path.join(base, "Shortcuts"),
"custom": os.path.join(base, "shortcuts.json"),
"store": os.path.join(base, "store.json"),
"cmd": os.path.join(base, "cmd_history.json")}
def activate_profile(pid):
"""Point the active data globals at the given profile and create its dirs."""
global SHORTCUT_DIR, CUSTOM_PATH, STORE_PATH, CMDHIST_PATH, ACTIVE_PROFILE
p = _profile_paths(pid)
os.makedirs(p["dir"], exist_ok=True)
SHORTCUT_DIR = p["dir"]
CUSTOM_PATH = p["custom"]
STORE_PATH = p["store"]
CMDHIST_PATH = p["cmd"]
ACTIVE_PROFILE = pid
invalidate_cache()
clear_session_history()
return pid
# ---- main (default) password ---------------------------------------------
PIN_LENGTH = 6 # all passwords are exactly 6 digits
def _valid_pin(password):
return bool(re.fullmatch(r"\d{%d}" % PIN_LENGTH, password or ""))
def password_in_use(password, exclude=None):
"""Return True if this exact 6-digit code already guards another space.
`exclude` is an id/kind to skip: 'default', 'master', or a profile id."""
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
import hmac
def matches(salt, h):
return bool(h) and hmac.compare_digest(_hash_password(password, salt), h)
if exclude != "default" and matches(cfg.get("lock_salt", ""), cfg.get("lock_hash", "")):
return True
if exclude != "master" and matches(cfg.get("master_salt", ""), cfg.get("master_hash", "")):
return True
for p in cfg.get("profiles", []):
if p.get("id") == exclude:
continue
if matches(p.get("salt", ""), p.get("hash", "")):
return True
sec = cfg.get("secret", {})
if exclude != "__secret__" and matches(sec.get("salt", ""), sec.get("hash", "")):
return True
return False
def set_lock_password(password):
"""Set the main 6-digit code. Returns {'ok':bool, 'error':str}."""
if not _valid_pin(password):
return {"ok": False, "error": "Password must be exactly 6 digits."}
if password_in_use(password, exclude="default"):
return {"ok": False, "error": "That code is already used by another space."}
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
salt = _new_salt()
cfg["lock_salt"] = salt
cfg["lock_hash"] = _hash_password(password, salt)
cfg["lock_enabled"] = True
save_json(CONFIG_PATH, cfg)
return {"ok": True}
def disable_lock(password):
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
if not cfg.get("lock_enabled"):
return True
if not (verify_password(password) or verify_master(password)):
return False
cfg["lock_enabled"] = False
cfg["lock_hash"] = ""
cfg["lock_salt"] = ""
save_json(CONFIG_PATH, cfg)
return True
def verify_password(password):
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
if not cfg.get("lock_enabled"):
return True
expected = cfg.get("lock_hash", "")
if not expected:
return True
import hmac
return hmac.compare_digest(_hash_password(password, cfg.get("lock_salt", "")),
expected)
def is_locked():
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
return bool(cfg.get("lock_enabled") and cfg.get("lock_hash"))
# ---- master (recovery) password ------------------------------------------
def set_master_password(password):
if not _valid_pin(password):
return {"ok": False, "error": "Master code must be exactly 6 digits."}
if password_in_use(password, exclude="master"):
return {"ok": False, "error": "That code is already used by another space."}
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
salt = _new_salt()
cfg["master_salt"] = salt
cfg["master_hash"] = _hash_password(password, salt)
save_json(CONFIG_PATH, cfg)
return {"ok": True}
def has_master():
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
return bool(cfg.get("master_hash"))
def verify_master(password):
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
expected = cfg.get("master_hash", "")
if not expected:
return False
import hmac
return hmac.compare_digest(_hash_password(password, cfg.get("master_salt", "")),
expected)
# ---- alternate + secret profiles -----------------------------------------
def list_profiles():
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
return cfg.get("profiles", [])
def add_profile(name, password, secret=False):
"""Create an alternate (or the secret) profile guarded by its own 6-digit
code. Returns {'ok':bool, 'id':str, 'error':str}."""
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
if not name:
return {"ok": False, "error": "Name is required."}
if not _valid_pin(password):
return {"ok": False, "error": "Code must be exactly 6 digits."}
if password_in_use(password):
return {"ok": False, "error": "That code is already used by another space."}
salt = _new_salt()
rec = {"name": name, "salt": salt, "hash": _hash_password(password, salt)}
if secret:
rec["id"] = "__secret__"
cfg["secret"] = rec
else:
profiles = cfg.get("profiles", [])
if len(profiles) >= MAX_PROFILES:
return {"ok": False, "error": f"Limit reached (max {MAX_PROFILES})."}
# unique id
existing = {p["id"] for p in profiles}
i = 1
while f"profile{i}" in existing:
i += 1
rec["id"] = f"profile{i}"
profiles.append(rec)
cfg["profiles"] = profiles
save_json(CONFIG_PATH, cfg)
os.makedirs(_profile_paths(rec["id"])["dir"], exist_ok=True)
return {"ok": True, "id": rec["id"]}
def delete_profile(pid):
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
if pid == "__secret__":
cfg["secret"] = {}
else:
cfg["profiles"] = [p for p in cfg.get("profiles", []) if p["id"] != pid]
save_json(CONFIG_PATH, cfg)
return True
def resolve_password(password):
"""Figure out which profile a typed password unlocks.
Returns (kind, profile_id):
kind in {'default','profile','secret','master', None}
None -> password did not match anything."""
cfg = load_json(CONFIG_PATH, DEFAULT_CONFIG)
import hmac
# default / main
if cfg.get("lock_hash") and hmac.compare_digest(
_hash_password(password, cfg.get("lock_salt", "")), cfg["lock_hash"]):
return ("default", "default")
# alternate profiles
for p in cfg.get("profiles", []):
if hmac.compare_digest(_hash_password(password, p.get("salt", "")),
p.get("hash", "")):
return ("profile", p["id"])
# secret
sec = cfg.get("secret", {})
if sec.get("hash") and hmac.compare_digest(
_hash_password(password, sec.get("salt", "")), sec["hash"]):
return ("secret", "__secret__")
# master (recovery) — unlocks the default profile
if cfg.get("master_hash") and hmac.compare_digest(
_hash_password(password, cfg.get("master_salt", "")), cfg["master_hash"]):
return ("master", "default")
return (None, None)
# --------------------------------------------------------------------------- #
# Safe inline calculator
# --------------------------------------------------------------------------- #
_ALLOWED_BIN = {
ast.Add: operator.add, ast.Sub: operator.sub, ast.Mult: operator.mul,
ast.Div: operator.truediv, ast.FloorDiv: operator.floordiv,
ast.Mod: operator.mod, ast.Pow: operator.pow,
}
_ALLOWED_UNARY = {ast.UAdd: operator.pos, ast.USub: operator.neg}
_ALLOWED_FUNCS = {
k: getattr(math, k) for k in (
"sqrt", "sin", "cos", "tan", "asin", "acos", "atan", "log", "log2",
"log10", "exp", "floor", "ceil", "factorial", "degrees", "radians",
)
}
_ALLOWED_FUNCS.update({"abs": abs, "round": round, "min": min, "max": max})
_ALLOWED_NAMES = {"pi": math.pi, "e": math.e, "tau": math.tau}
def _eval_node(node):
if isinstance(node, ast.Expression):
return _eval_node(node.body)
if isinstance(node, ast.Constant):
if isinstance(node.value, (int, float)):
return node.value
raise ValueError("bad constant")
if isinstance(node, ast.BinOp) and type(node.op) in _ALLOWED_BIN:
return _ALLOWED_BIN[type(node.op)](_eval_node(node.left),
_eval_node(node.right))
if isinstance(node, ast.UnaryOp) and type(node.op) in _ALLOWED_UNARY:
return _ALLOWED_UNARY[type(node.op)](_eval_node(node.operand))
if isinstance(node, ast.Name) and node.id in _ALLOWED_NAMES:
return _ALLOWED_NAMES[node.id]
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) \
and node.func.id in _ALLOWED_FUNCS:
args = [_eval_node(a) for a in node.args]
return _ALLOWED_FUNCS[node.func.id](*args)
raise ValueError("unsupported expression")
def try_calculate(text):
"""Return a formatted result string, or None if not a math expression."""
s = text.strip()
if not s:
return None
# must contain a digit and only calculator-safe characters
if not re.search(r"\d", s):
return None
if not re.fullmatch(r"[0-9a-z.,_%\s+\-*/().^]+", s, re.IGNORECASE):
return None
expr = s.replace("^", "**").replace(",", "")
# turn "50%" into "(50/100)"
expr = re.sub(r"(\d+(?:\.\d+)?)\s*%", r"(\1/100)", expr)
try:
tree = ast.parse(expr, mode="eval")
val = _eval_node(tree)
except Exception:
return None
if isinstance(val, float):
if val.is_integer():
return str(int(val))
return f"{val:.10g}"
return str(val)
# --------------------------------------------------------------------------- #
# Fuzzy scoring
# --------------------------------------------------------------------------- #
def fuzzy_score(query, text):
q = (query or "").lower()
t = (text or "").lower()
if not q:
return 0
if t == q:
return 1000
if t.startswith(q):
return 800 - len(t)
if q in t:
return 600 - t.index(q)
qi, score, last = 0, 0, -1
for i, ch in enumerate(t):
if qi < len(q) and ch == q[qi]:
score += 10 - min(9, i - last - 1)
last = i
qi += 1
if qi == len(q):
return 100 + score
return None
# --------------------------------------------------------------------------- #
# Shortcut model + collection
# --------------------------------------------------------------------------- #
def _key_for_file(path):
return "file:" + os.path.abspath(path)
def _key_for_custom(name):
return "custom:" + name
class Shortcut:
def __init__(self, key, name, target, icon="•", subtext="", source="file",
image="", removable=False, file_path=""):
self.key = key
self.name = name
self.target = target
self.icon = icon or "" # legacy text fallback (unused for icons now)
self.image = image or "" # ms:<name> | file:<name> | url | path
self.subtext = subtext
self.source = source # "file" | "custom" | "qllink" | "child"
self.removable = removable # can the launcher delete it directly?
self.file_path = file_path # backing file (for qllink / file items)
self.parent = "" # parent key for child (folder content) items
# metadata (filled from store)
self.pinned = False
self.group = ""
self.alias = ""
self.hotkey = ""
self.count = 0
self.last = 0
def to_dict(self):
return {
"key": self.key, "name": self.name, "target": self.target,
"icon": self.icon, "image": resolve_image(self.image),
"image_raw": self.image, "subtext": self.subtext,
"source": self.source, "removable": self.removable,
"pinned": self.pinned, "group": self.group, "alias": self.alias,
"hotkey": self.hotkey, "count": self.count, "last": self.last,
}
_SCAN_CACHE = {"items": None, "time": 0.0}
_SCAN_TTL = 1.5 # seconds; keeps typing snappy by not re-scanning disk per keystroke
def invalidate_cache():
_SCAN_CACHE["items"] = None
_SCAN_CACHE["time"] = 0.0
def collect_shortcuts(force=False):
"""Cached wrapper around the disk scan so rapid searches stay snappy."""
now = time.time()
if (not force and _SCAN_CACHE["items"] is not None
and (now - _SCAN_CACHE["time"]) < _SCAN_TTL):
return _SCAN_CACHE["items"]
items = _scan_shortcuts()
_SCAN_CACHE["items"] = items
_SCAN_CACHE["time"] = now
return items
def _scan_shortcuts():
"""Scan ONLY the Shortcuts folder + shortcuts.json, merge metadata.
If a shortcut points at a FOLDER, the files directly inside that folder
are also surfaced as child entries (so a single folder shortcut exposes
its contents in the launcher).
"""
store = load_json(STORE_PATH, DEFAULT_STORE)
usage = store.get("usage", {})
meta = store.get("meta", {})
items = []
os.makedirs(SHORTCUT_DIR, exist_ok=True)
# 1) files in the single folder
try:
for entry in sorted(os.listdir(SHORTCUT_DIR)):
if entry.lower() in ("readme.txt", "desktop.ini", ".gitkeep"):
continue
full = os.path.join(SHORTCUT_DIR, entry)
name, ext = os.path.splitext(entry)
ext = ext.lower()
if ext == QLLINK_EXT:
# tiny JSON link file we created (drag-and-drop or "add")
data = load_json(full, {})
disp = data.get("name") or name
target = data.get("target", "")
image = data.get("image", "") or _guess_image(target)
items.append(Shortcut(_key_for_file(full), disp, target, "",
subtext=target, source="qllink",
image=image, removable=True,
file_path=full))
# expand folder targets -> show files inside
if os.path.isdir(target):
items.extend(_expand_folder(target, disp, _key_for_file(full)))
continue
if os.path.isdir(full):
image, disp = "ms:" + FOLDER_ICON, entry
items.append(Shortcut(_key_for_file(full), disp, full, "",
subtext=full, source="file",
image=image, removable=True, file_path=full))
items.extend(_expand_folder(full, disp, _key_for_file(full)))
else:
image = "ms:" + ICONS_BY_EXT.get(ext, DEFAULT_FILE_ICON)
disp = name if ext else entry
items.append(Shortcut(_key_for_file(full), disp, full, "",
subtext=full, source="file",
image=image, removable=True, file_path=full))
except Exception as exc:
print(f"[PyDoc] scan error: {exc}")
# 2) custom entries
custom = load_json(CUSTOM_PATH, DEFAULT_CUSTOM)
for sc in custom.get("shortcuts", []):
if not isinstance(sc, dict):
continue
name, target = sc.get("name"), sc.get("target")
if not name or not target:
continue
sub = SHORTCUT_DIR if target == "@open_shortcut_folder" else target
image = sc.get("image", "") or _guess_image(target)
items.append(Shortcut(_key_for_custom(name), name, target,
"", subtext=sub, source="custom",
image=image, removable=True))
if target not in ("@open_shortcut_folder",) and os.path.isdir(target):
items.extend(_expand_folder(target, name, _key_for_custom(name)))
# 3) merge metadata + usage (store can override icon/image too)
for it in items:
m = meta.get(it.key, {})
it.pinned = bool(m.get("pinned", False))
it.group = m.get("group", "") or ""
it.alias = m.get("alias", "") or ""
it.hotkey = m.get("hotkey", "") or ""
if m.get("image"):
it.image = m["image"]
u = usage.get(it.key, {})
it.count = int(u.get("count", 0))
it.last = float(u.get("last", 0))
return items
def _expand_folder(folder, parent_name, parent_key, limit=200):
"""Return child Shortcut entries for files directly inside a folder."""
out = []
try:
for child in sorted(os.listdir(folder))[:limit]:
cfull = os.path.join(folder, child)
cname, cext = os.path.splitext(child)
cext = cext.lower()
if os.path.isdir(cfull):
image = "ms:" + FOLDER_ICON
disp = child
else:
image = "ms:" + ICONS_BY_EXT.get(cext, DEFAULT_FILE_ICON)
disp = cname if cext else child
sc = Shortcut("child:" + os.path.abspath(cfull), disp, cfull, "",
subtext=f"{parent_name} › {child}", source="child",
image=image, removable=False, file_path=cfull)
sc.parent = parent_key
out.append(sc)
except Exception as exc:
print(f"[PyDoc] folder expand error: {exc}")
return out
def _guess_image(target):
"""Pick a Material-symbol image string for a target path/URL."""
if not target:
return "ms:" + DEFAULT_FILE_ICON
if target == "@open_shortcut_folder":
return "ms:" + FOLDER_ICON
if target.startswith(("http://", "https://")):
return "ms:" + URL_ICON
if os.path.isdir(target):
return "ms:" + FOLDER_ICON
ext = os.path.splitext(target)[1].lower()
return "ms:" + ICONS_BY_EXT.get(ext, DEFAULT_FILE_ICON)
def _guess_icon(target):
"""Back-compat: return a Material symbol name for a target."""
return _guess_image(target)[3:]
def all_groups(items):
groups = sorted({it.group for it in items if it.group})
return ["All"] + groups
def search_items(query, items, config):
"""Return (results, special) where results are dicts and special holds
calculator / web-search suggestions."""
q = (query or "").strip()
now = time.time()
def rank(it, base):
# frequency + recency boost
freq = math.log1p(it.count) * 30
rec = 0
if it.last:
age_h = (now - it.last) / 3600.0
rec = max(0, 40 - age_h) if age_h < 40 else 0
pin = 5000 if it.pinned else 0
child_pen = -25 if it.source == "child" else 0 # folder contents rank below top-level
return base + freq + rec + pin + child_pen
if not q:
# don't flood the empty view with every folder's children
scored = [(rank(it, 0), it) for it in items if it.source != "child"]
else:
scored = []
for it in items:
base = fuzzy_score(q, it.name)
if it.alias:
a = fuzzy_score(q, it.alias)
if a is not None:
a += 200 # alias matches are strong
base = a if base is None else max(base, a)
if base is None:
b2 = fuzzy_score(q, os.path.basename(it.subtext))
if b2 is None:
continue
base = b2 - 50
scored.append((rank(it, base), it))
scored.sort(key=lambda x: (-x[0], x[1].name.lower()))
results = [it.to_dict() for _, it in scored]
special = []
# calculator
if config.get("enable_calculator", True):
res = try_calculate(q)
if res is not None and res != q:
special.append({
"key": "calc", "name": f"{q} = {res}", "target": res,
"image": "ms:calculate", "subtext": "Calculator · Enter to copy",
"kind": "calc", "value": res,
})
# screenshot actions (typed)
if config.get("enable_screenshot", True) and q:
ql = q.lower()
if any(ql.startswith(w[:len(ql)]) and len(ql) >= 3
for w in ("screenshot", "snip", "capture", "screen grab")):
special.append({"key": "shot:region", "name": "Screenshot — select a region",
"target": "region", "image": "ms:screenshot_region", "kind": "shot",
"subtext": "Snip an area (saved + copied)"})
special.append({"key": "shot:full", "name": "Screenshot — whole screen",
"target": "full", "image": "ms:fullscreen", "kind": "shot",
"subtext": "Capture all monitors"})
special.append({"key": "shot:window", "name": "Screenshot — active window",
"target": "window", "image": "ms:web_asset", "kind": "shot",
"subtext": "Capture the focused window"})
# command runner (typed: "cmd", "run", "terminal", or "> ...")
if config.get("enable_runcmd", True) and q:
ql = q.lower()
run_prefix = q.startswith((">", "$"))
if run_prefix or any(ql.startswith(w[:len(ql)]) and len(ql) >= 2
for w in ("cmd", "run", "terminal", "command", "shell")):
special.append({
"key": "runcmd", "name": "Run a command…",
"target": q.lstrip(">$ ").strip(), "image": "ms:terminal",
"subtext": "Open the command runner (Enter)", "kind": "runcmd",
})
# open URL / web search
if q and config.get("enable_websearch", True):
if looks_like_url(q):
url = q if "://" in q else "https://" + q
special.append({
"key": "openurl", "name": f"Open {q}", "target": url,
"image": "ms:link", "subtext": "Open in browser", "kind": "url",
})
else:
tmpl = config.get("web_search_url", DEFAULT_CONFIG["web_search_url"])
url = tmpl.replace("{q}", quote(q))
special.append({
"key": "websearch", "name": f"Search the web for “{q}”",
"target": url, "image": "ms:travel_explore",
"subtext": "Web search", "kind": "url",
})
return results, special
def looks_like_url(s):
s = s.strip()
if " " in s:
return False
if s.startswith(("http://", "https://")):
return True
return bool(re.fullmatch(r"[\w-]+(\.[\w-]+)+(/\S*)?", s))
def quote(s):
from urllib.parse import quote_plus
return quote_plus(s)
# --------------------------------------------------------------------------- #
# Mutations (used by the Settings UI / API)
# --------------------------------------------------------------------------- #
def record_usage(key):
store = load_json(STORE_PATH, DEFAULT_STORE)
u = store.setdefault("usage", {})
rec = u.setdefault(key, {"count": 0, "last": 0})
rec["count"] = int(rec.get("count", 0)) + 1
rec["last"] = time.time()
save_json(STORE_PATH, store)
invalidate_cache()
def set_meta(key, **fields):
store = load_json(STORE_PATH, DEFAULT_STORE)
meta = store.setdefault("meta", {})
rec = meta.setdefault(key, {})
for k, v in fields.items():
if v in (None, ""):
rec.pop(k, None)
else:
rec[k] = v
if not rec:
meta.pop(key, None)
save_json(STORE_PATH, store)
invalidate_cache()
def toggle_pin(key):
store = load_json(STORE_PATH, DEFAULT_STORE)
rec = store.setdefault("meta", {}).setdefault(key, {})
rec["pinned"] = not rec.get("pinned", False)
if not rec["pinned"]:
rec.pop("pinned", None)
save_json(STORE_PATH, store)
invalidate_cache()
return rec.get("pinned", False)
def add_custom(name, target, icon="•", image=""):
data = load_json(CUSTOM_PATH, DEFAULT_CUSTOM)
entry = {"name": name, "target": target, "icon": icon or "•"}
if image:
entry["image"] = image
data.setdefault("shortcuts", []).append(entry)
ok = save_json(CUSTOM_PATH, data); invalidate_cache(); return ok
def update_custom(index, name, target, icon="•", image=""):
data = load_json(CUSTOM_PATH, DEFAULT_CUSTOM)
lst = data.setdefault("shortcuts", [])
if 0 <= index < len(lst):
entry = {"name": name, "target": target, "icon": icon or "•"}
if image:
entry["image"] = image
lst[index] = entry
ok = save_json(CUSTOM_PATH, data); invalidate_cache(); return ok
return False
def delete_custom(index):
data = load_json(CUSTOM_PATH, DEFAULT_CUSTOM)
lst = data.setdefault("shortcuts", [])
if 0 <= index < len(lst):
del lst[index]
ok = save_json(CUSTOM_PATH, data); invalidate_cache(); return ok
return False
# --------------------------------------------------------------------------- #
# .qllink shortcut files (drag-and-drop / "Add shortcut")
# --------------------------------------------------------------------------- #
def _safe_filename(name):
keep = "-_.() "
cleaned = "".join(c for c in name if c.isalnum() or c in keep).strip()
return cleaned or "shortcut"
def create_qllink(name, target, icon="", image=""):
"""Write a .qllink file into the Shortcuts folder. Returns the path."""
os.makedirs(SHORTCUT_DIR, exist_ok=True)
base = _safe_filename(name)
path = os.path.join(SHORTCUT_DIR, base + QLLINK_EXT)
i = 2
while os.path.exists(path):
path = os.path.join(SHORTCUT_DIR, f"{base} ({i}){QLLINK_EXT}")
i += 1
payload = {"name": name, "target": target}
if icon:
payload["icon"] = icon
if image:
payload["image"] = image
save_json(path, payload)
invalidate_cache()
return path
def add_dropped_path(path):
"""Drag-and-drop handler: turn a dropped file/folder into a .qllink."""
path = path.strip().strip('"')
if not path:
return None
if path.startswith("file://"):
from urllib.parse import unquote, urlparse
path = unquote(urlparse(path).path)
if sys.platform.startswith("win") and path.startswith("/"):
path = path[1:]
name = os.path.splitext(os.path.basename(path.rstrip("/\\")))[0] or path
created = create_qllink(name, path, image=_guess_image(path))
return os.path.basename(created)
def add_dropped_url(url, name=""):
url = url.strip()
if not url:
return None
if not name:
from urllib.parse import urlparse
name = urlparse(url).netloc or url
return os.path.basename(create_qllink(name, url, image="ms:" + URL_ICON))
def delete_shortcut(key):
"""Remove a shortcut by key. Handles file/qllink (delete file),
custom (remove from json). Returns True on success."""
items = {it.key: it for it in collect_shortcuts(force=True)}
it = items.get(key)
if not it:
return False
# also drop its stored metadata/usage
_purge_store(key)
invalidate_cache()
if it.source == "custom":
data = load_json(CUSTOM_PATH, DEFAULT_CUSTOM)
lst = data.get("shortcuts", [])
for i, sc in enumerate(lst):
if isinstance(sc, dict) and sc.get("name") == it.name:
del lst[i]
ok = save_json(CUSTOM_PATH, data); invalidate_cache(); return ok
return False
# file or qllink -> delete the backing file
if it.file_path and os.path.exists(it.file_path):
try:
if os.path.isdir(it.file_path):
import shutil
shutil.rmtree(it.file_path)
else:
os.remove(it.file_path)
return True
except Exception as exc: