Repository navigation
Expand file tree
/
Copy pathhide_file.py
More file actions
80 lines (66 loc) · 3.04 KB
/
Copy pathhide_file.py
File metadata and controls
80 lines (66 loc) · 3.04 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
"""قائمة عناصر الزر الأيمن المخفية: الملف imports\\ui-hide.nss داخل مجلد Shell.
كل سطر عنصر واحد. العنصر المخفي يُكتب أمرًا عاديًا، والعنصر المُظهَر يُكتب
نفس الأمر معلّقًا بـ // حتى يبقى في القائمة ويمكن إخفاؤه مرة أخرى.
© 2026 Ali Alhumedi - MIT License
"""
import glob
import os
import re
import shutil
import time
import theme_file
from theme_file import AppError
HIDE_FILE = os.path.join(theme_file.shell_dir(), "imports", "ui-hide.nss")
IMPORT_LINE = "import 'imports/ui-hide.nss'"
HEADER = "// Generated by Nilesoft Shell UI: hidden menu items (lines starting with // are shown)"
LINE_RE = re.compile(
r'^(?P<off>//\s*)?modify\((?:find="(?P<find>[^"]+)"|where=this\.id==id\.(?P<id>[a-z_]+))\s+vis=vis\.remove\)\s*$')
def is_linked():
try:
with open(os.path.join(theme_file.shell_dir(), "shell.nss"), encoding="utf-8-sig") as f:
return IMPORT_LINE in f.read()
except OSError:
return False
def load():
"""يرجع [{"find" أو "id", "hidden": bool}]."""
if not os.path.exists(HIDE_FILE):
return []
items = []
with open(HIDE_FILE, encoding="utf-8-sig") as f:
for line in f:
m = LINE_RE.match(line.strip())
if m:
item = {"find": m["find"]} if m["find"] else {"id": m["id"]}
item["hidden"] = not m["off"]
items.append(item)
return items
def build(items):
lines = [HEADER]
for it in items:
if it.get("id"):
if not re.fullmatch(r"[a-z_]+", it["id"]):
raise AppError(f"معرّف غير صالح: {it['id']}", f"Invalid id: {it['id']}")
cmd = f'modify(where=this.id==id.{it["id"]} vis=vis.remove)'
else:
pat = str(it.get("find", "")).strip()
if not pat or any(c in pat for c in '"\r\n'):
raise AppError(f"نص غير صالح: {pat}", f"Invalid text: {pat}")
cmd = f'modify(find="{pat}" vis=vis.remove)'
lines.append(cmd if it.get("hidden", True) else "// " + cmd)
return "\n".join(lines) + "\n"
def save(items):
text = build(items)
os.makedirs(theme_file.BACKUP_DIR, exist_ok=True)
if os.path.exists(HIDE_FILE):
stamp = time.strftime("%Y%m%d-%H%M%S")
shutil.copy2(HIDE_FILE, os.path.join(theme_file.BACKUP_DIR, f"ui-hide-{stamp}.nss"))
for p in sorted(glob.glob(os.path.join(theme_file.BACKUP_DIR, "ui-hide-*.nss")))[:-theme_file.KEEP_BACKUPS]:
os.remove(p)
try:
# الكتابة في نفس الملف: الصلاحية على الملف وحده
with open(HIDE_FILE, "w", encoding="utf-8-sig", newline="\r\n") as f:
f.write(text)
except PermissionError:
raise AppError("لا توجد صلاحية كتابة على ملف العناصر المخفية: " + HIDE_FILE,
"No write permission on the hidden-items file: " + HIDE_FILE)
return load()