7zon-cli 0.2.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
zon7/secrets.py ADDED
@@ -0,0 +1,133 @@
1
+ """Maxfiy qiymatlar ombori: parollar, tokenlar, kodlar.
2
+
3
+ Qiymatlar operatsion tizimning parollar omborida saqlanadi (macOS Keychain, Windows Credential
4
+ Manager). Ombor bo'lmagan joyda (Android/Termux, serverlar) ~/.7zon/secrets.json fayliga yoziladi:
5
+ faqat egasi o'qiy oladi (600), gh va boshqa CLI'lar tokenlarni shunday saqlaydi.
6
+ Sozlamalar faylida faqat nomlar turadi.
7
+ AI hech qachon qiymatni ko'rmaydi: buyruqlarda u faqat $NOM ko'rinishida ishlatiladi.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import json
13
+ import os
14
+ import re
15
+ from pathlib import Path
16
+
17
+ from zon7 import config
18
+
19
+ SERVICE = "7zon-cli"
20
+ NAME_RE = re.compile(r"^[A-Z][A-Z0-9_]{1,63}$")
21
+
22
+
23
+ class SecretError(Exception):
24
+ pass
25
+
26
+
27
+ def normalize(name: str) -> str:
28
+ name = re.sub(r"[^A-Za-z0-9_]", "_", name.strip()).upper()
29
+ if not NAME_RE.match(name):
30
+ raise SecretError("Nom lotin harfidan boshlansin va faqat A-Z, 0-9, _ dan iborat bo'lsin (masalan INSTAGRAM_PASSWORD).")
31
+ return name
32
+
33
+
34
+ def _keyring():
35
+ try:
36
+ import keyring
37
+
38
+ return keyring
39
+ except ImportError: # pragma: no cover
40
+ return None
41
+
42
+
43
+ def secrets_file() -> Path:
44
+ return Path.home() / ".7zon" / "secrets.json"
45
+
46
+
47
+ def _file_read() -> dict[str, str]:
48
+ try:
49
+ return json.loads(secrets_file().read_text(encoding="utf-8"))
50
+ except (OSError, json.JSONDecodeError):
51
+ return {}
52
+
53
+
54
+ def _file_write(data: dict[str, str]) -> None:
55
+ path = secrets_file()
56
+ path.parent.mkdir(parents=True, exist_ok=True)
57
+ try:
58
+ os.chmod(path.parent, 0o700)
59
+ except OSError:
60
+ pass
61
+ # Avval faqat egasi o'qiy oladigan qilib yaratamiz, keyin yozamiz
62
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
63
+ with os.fdopen(fd, "w", encoding="utf-8") as fh:
64
+ json.dump(data, fh, ensure_ascii=False, indent=2)
65
+ os.chmod(path, 0o600)
66
+
67
+
68
+ def names() -> list[str]:
69
+ return list(config.load().get("secret_names", []))
70
+
71
+
72
+ def save(name: str, value: str) -> str:
73
+ """Qiymatni saqlaydi. Qaytaradi: "keychain", "file" yoki "" (saqlab bo'lmadi)."""
74
+ where = ""
75
+ kr = _keyring()
76
+ if kr is not None:
77
+ try:
78
+ kr.set_password(SERVICE, name, value)
79
+ where = "keychain"
80
+ except Exception:
81
+ where = ""
82
+ if not where:
83
+ try:
84
+ data = _file_read()
85
+ data[name] = value
86
+ _file_write(data)
87
+ where = "file"
88
+ except OSError:
89
+ return ""
90
+ data = config.load()
91
+ stored = list(data.get("secret_names", []))
92
+ if name not in stored:
93
+ stored.append(name)
94
+ data["secret_names"] = stored
95
+ config.save(data)
96
+ return where
97
+
98
+
99
+ def load_all() -> dict[str, str]:
100
+ kr = _keyring()
101
+ result: dict[str, str] = {k: v for k, v in _file_read().items() if k in names()}
102
+ if kr is None:
103
+ return result
104
+ for name in names():
105
+ if name in result:
106
+ continue
107
+ try:
108
+ value = kr.get_password(SERVICE, name)
109
+ except Exception:
110
+ value = None
111
+ if value:
112
+ result[name] = value
113
+ return result
114
+
115
+
116
+ def delete(name: str) -> bool:
117
+ kr = _keyring()
118
+ found = name in names()
119
+ if kr is not None:
120
+ try:
121
+ kr.delete_password(SERVICE, name)
122
+ found = True
123
+ except Exception:
124
+ pass
125
+ stored_file = _file_read()
126
+ if name in stored_file:
127
+ del stored_file[name]
128
+ _file_write(stored_file)
129
+ found = True
130
+ data = config.load()
131
+ data["secret_names"] = [n for n in data.get("secret_names", []) if n != name]
132
+ config.save(data)
133
+ return found
zon7/shell.py ADDED
@@ -0,0 +1,61 @@
1
+ """Buyruqni joriy OS uchun to'g'ri shell'da ishga tushirish argumentlari.
2
+
3
+ - macOS / Linux / Termux: foydalanuvchi shell'i (bash, zsh, sh) `-c` bilan.
4
+ - Windows: PowerShell (pwsh 7 bo'lsa o'sha, aks holda Windows PowerShell 5.1), UTF-8 chiqish bilan.
5
+ PowerShell topilmasa, cmd.exe.
6
+
7
+ `marker` berilsa, buyruqdan keyin joriy papka `<marker><yo'l>` qatori bilan chiqariladi
8
+ (chiqish kodi saqlanadi), shunda `cd` keyingi buyruqlarga ham ta'sir qiladi.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import os
14
+ import platform
15
+ import shutil
16
+
17
+ from zon7 import context
18
+
19
+
20
+ def is_windows() -> bool:
21
+ return platform.system() == "Windows"
22
+
23
+
24
+ def powershell_exe() -> str | None:
25
+ return shutil.which("pwsh") or shutil.which("powershell")
26
+
27
+
28
+ def kind() -> str:
29
+ """AI'ga aytiladigan shell turi: aynan shu shell'da bajariladi."""
30
+ if is_windows():
31
+ return "powershell" if powershell_exe() else "cmd"
32
+ return os.path.basename(context.shell_path())
33
+
34
+
35
+ def argv(command: str, marker: str | None = None, interactive: bool = False, ps: str | None = None,
36
+ windows: bool | None = None) -> list[str]:
37
+ windows = is_windows() if windows is None else windows
38
+ if windows:
39
+ ps = ps if ps is not None else powershell_exe()
40
+ if ps:
41
+ # UTF-8: o'zbek/kirill harflari to'g'ri chiqsin
42
+ # UTF-8 chiqish va rang kodlarisiz oddiy matn (PowerShell 7.2+ da $PSStyle bor)
43
+ script = ("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; "
44
+ "$OutputEncoding = [System.Text.Encoding]::UTF8; "
45
+ "if ($PSStyle) { $PSStyle.OutputRendering = 'PlainText' }\n" + command)
46
+ if marker:
47
+ script += (
48
+ "\n$__7zon_ok = $?"
49
+ "\n$__7zon_rc = if ($LASTEXITCODE) { $LASTEXITCODE } elseif ($__7zon_ok) { 0 } else { 1 }"
50
+ f"\nWrite-Output \"{marker}$((Get-Location).Path)\""
51
+ "\nexit $__7zon_rc"
52
+ )
53
+ flags = ["-NoLogo", "-NoProfile"] + ([] if interactive else ["-NonInteractive"])
54
+ return [ps, *flags, "-ExecutionPolicy", "Bypass", "-Command", script]
55
+ script = command if not marker else f"{command} & echo {marker}%CD%"
56
+ return ["cmd.exe", "/d", "/s", "/c", script]
57
+ script = command
58
+ if marker:
59
+ script = f'{command}\n__7zon_rc=$?\nprintf "{marker}%s\\n" "$PWD"\nexit $__7zon_rc'
60
+ return [context.shell_path(), "-c", script]
61
+