pull-requests-san 0.1.0__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.
@@ -0,0 +1,69 @@
1
+ """Sanskrit/Indic GitHub pull-request single-pane word-flow and character-level reviewer."""
2
+
3
+ from pull_requests_san.pr_review import (
4
+ AlignedDiffRow,
5
+ DEFAULT_API_BASE,
6
+ DEFAULT_LOCAL_ORG_DIR,
7
+ DEFAULT_OWNER,
8
+ DiffHunk,
9
+ GitHubAPIError,
10
+ GitHubClient,
11
+ PRReviewSession,
12
+ PullRequestCommit,
13
+ PullRequestDetail,
14
+ PullRequestFileDiff,
15
+ PullRequestSummary,
16
+ ReviewComment,
17
+ ReviewThread,
18
+ VALID_DIFF_MODES,
19
+ VALID_MERGE_METHODS,
20
+ VALID_NORMALIZATIONS,
21
+ VALID_REVIEW_EVENTS,
22
+ apply_normalization,
23
+ build_full_file_hunk,
24
+ build_inline_flow_rows,
25
+ build_unified_patch_from_texts,
26
+ compute_char_diff,
27
+ compute_stream_diff,
28
+ extract_three_way_conflicts,
29
+ normalize_owner,
30
+ parse_full_diff_by_file,
31
+ parse_patch_hunks,
32
+ resolve_github_token,
33
+ strip_punctuation,
34
+ tokenize_for_diff,
35
+ )
36
+
37
+ __all__ = [
38
+ "AlignedDiffRow",
39
+ "DEFAULT_API_BASE",
40
+ "DEFAULT_LOCAL_ORG_DIR",
41
+ "DEFAULT_OWNER",
42
+ "DiffHunk",
43
+ "GitHubAPIError",
44
+ "GitHubClient",
45
+ "PRReviewSession",
46
+ "PullRequestCommit",
47
+ "PullRequestDetail",
48
+ "PullRequestFileDiff",
49
+ "PullRequestSummary",
50
+ "ReviewComment",
51
+ "ReviewThread",
52
+ "VALID_DIFF_MODES",
53
+ "VALID_MERGE_METHODS",
54
+ "VALID_NORMALIZATIONS",
55
+ "VALID_REVIEW_EVENTS",
56
+ "apply_normalization",
57
+ "build_full_file_hunk",
58
+ "build_inline_flow_rows",
59
+ "build_unified_patch_from_texts",
60
+ "compute_char_diff",
61
+ "compute_stream_diff",
62
+ "extract_three_way_conflicts",
63
+ "normalize_owner",
64
+ "parse_full_diff_by_file",
65
+ "parse_patch_hunks",
66
+ "resolve_github_token",
67
+ "strip_punctuation",
68
+ "tokenize_for_diff",
69
+ ]
@@ -0,0 +1,6 @@
1
+ """CLI entrypoint to launch the Sanskrit PR reviewer via ``python -m pull_requests_san``."""
2
+
3
+ from pull_requests_san.pr_review_gui import main
4
+
5
+ if __name__ == "__main__":
6
+ raise SystemExit(main())
@@ -0,0 +1,179 @@
1
+ """GitHub owner normalization and API token resolution helpers."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import inspect
6
+ import os
7
+ from pathlib import Path
8
+ import re
9
+ import shutil
10
+ import subprocess
11
+
12
+ try:
13
+ import tomllib
14
+ except ImportError: # Python 3.10 fallback
15
+ try:
16
+ import tomli as tomllib # type: ignore[no-redef]
17
+ except ImportError:
18
+ tomllib = None # type: ignore[assignment]
19
+
20
+
21
+ DEFAULT_OWNER = "vishvAsa"
22
+
23
+
24
+ def _default_tokens_toml_path() -> Path | None:
25
+ try:
26
+ from curation_utils import creds
27
+
28
+ default_path = inspect.signature(creds.get_toml_value).parameters["path"].default
29
+ return Path(default_path) if default_path else None
30
+ except Exception:
31
+ return None
32
+
33
+
34
+ def normalize_owner(owner: str) -> str:
35
+ """Normalize an owner string or GitHub org URL (e.g. ``https://github.com/vishvAsa/``) to its login."""
36
+ raw = (owner or "").strip()
37
+ if not raw:
38
+ return DEFAULT_OWNER
39
+ m = re.match(r"^https?://(?:www\.)?github\.com/([^/?#]+)", raw, re.IGNORECASE)
40
+ if m:
41
+ return m.group(1).strip()
42
+ return raw.strip("/").split("/")[0]
43
+
44
+
45
+ def _read_gh_hosts_token(hosts_path: str | Path | None = None) -> str | None:
46
+ path = Path(hosts_path or os.path.expanduser("~/.config/gh/hosts.yml"))
47
+ if not path.is_file():
48
+ return None
49
+ try:
50
+ text = path.read_text(encoding="utf-8")
51
+ except OSError:
52
+ return None
53
+ try:
54
+ import yaml # type: ignore
55
+
56
+ data = yaml.safe_load(text) or {}
57
+ if isinstance(data, dict):
58
+ gh_entry = data.get("github.com")
59
+ if isinstance(gh_entry, dict):
60
+ tok = gh_entry.get("oauth_token")
61
+ if isinstance(tok, str) and tok.strip():
62
+ return tok.strip()
63
+ except Exception:
64
+ pass
65
+ in_github = False
66
+ for line in text.splitlines():
67
+ if not line.strip() or line.lstrip().startswith("#"):
68
+ continue
69
+ if not line.startswith(" "):
70
+ in_github = line.strip().startswith("github.com:")
71
+ continue
72
+ if in_github:
73
+ m = re.match(r"^\s+oauth_token:\s*(.+?)\s*$", line)
74
+ if m:
75
+ val = m.group(1).strip().strip("'\"")
76
+ if val:
77
+ return val
78
+ return None
79
+
80
+
81
+ def _read_simple_github_toml(toml_path: Path) -> str | None:
82
+ """Minimal TOML reader for ``[github]`` tokens when tomllib/tomli is unavailable (Python 3.10)."""
83
+ try:
84
+ text = toml_path.read_text(encoding="utf-8")
85
+ except OSError:
86
+ return None
87
+ if tomllib is not None:
88
+ try:
89
+ data = tomllib.loads(text)
90
+ gh = data.get("github")
91
+ if isinstance(gh, dict):
92
+ for k in ("general_kv", "checkin"):
93
+ val = gh.get(k)
94
+ if isinstance(val, dict):
95
+ val = val.get("key") or val.get("token")
96
+ if isinstance(val, str) and val.strip():
97
+ return val.strip()
98
+ except Exception:
99
+ pass
100
+ return None
101
+ section = ""
102
+ found: dict[str, str] = {}
103
+ for raw_line in text.splitlines():
104
+ line = raw_line.split("#", 1)[0].strip()
105
+ if not line:
106
+ continue
107
+ if line.startswith("[") and line.endswith("]"):
108
+ section = line[1:-1].strip()
109
+ continue
110
+ if section == "github" and "=" in line:
111
+ k, v = line.split("=", 1)
112
+ key = k.strip()
113
+ val = v.strip().strip("'\"")
114
+ if key in ("general_kv", "checkin") and val:
115
+ found[key] = val
116
+ elif section in ("github.general_kv", "github.checkin") and "=" in line:
117
+ k, v = line.split("=", 1)
118
+ if k.strip() in ("key", "token"):
119
+ val = v.strip().strip("'\"")
120
+ if val:
121
+ found.setdefault(section.split(".", 1)[1], val)
122
+ for k in ("general_kv", "checkin"):
123
+ if found.get(k):
124
+ return found[k]
125
+ return None
126
+
127
+
128
+ def resolve_github_token(
129
+ token: str | None = None,
130
+ *,
131
+ cred_path: str | Path | None = None,
132
+ allow_gh_cli: bool = True,
133
+ ) -> str | None:
134
+ """Resolve a GitHub API token.
135
+
136
+ When ``token`` is explicitly provided as ``""`` (empty string), returns ``None``
137
+ to allow opting out of ambient credentials (e.g. in tests). When ``token is None``,
138
+ checks env vars, gh CLI config, ``gh auth token``, and ``curation_utils.creds``.
139
+ """
140
+ if token is not None:
141
+ return token.strip() or None
142
+ for env_name in ("GITHUB_TOKEN", "GH_TOKEN"):
143
+ val = os.environ.get(env_name)
144
+ if val and val.strip():
145
+ return val.strip()
146
+ hosts_tok = _read_gh_hosts_token()
147
+ if hosts_tok:
148
+ return hosts_tok
149
+ if allow_gh_cli and shutil.which("gh"):
150
+ try:
151
+ out = subprocess.check_output(
152
+ ["gh", "auth", "token"],
153
+ stderr=subprocess.DEVNULL,
154
+ text=True,
155
+ timeout=5,
156
+ ).strip()
157
+ if out:
158
+ return out
159
+ except (subprocess.SubprocessError, OSError):
160
+ pass
161
+ try:
162
+ from curation_utils import creds
163
+
164
+ toml_path = Path(cred_path) if cred_path else _default_tokens_toml_path()
165
+ if toml_path and toml_path.is_file():
166
+ for key in ("github.general_kv", "github.checkin"):
167
+ val = creds.get_toml_value(key=key, path=toml_path)
168
+ if isinstance(val, dict):
169
+ val = val.get("key") or val.get("token")
170
+ if isinstance(val, str) and val.strip():
171
+ return val.strip()
172
+ except Exception:
173
+ pass
174
+ toml_path = Path(cred_path) if cred_path else Path(os.path.expanduser("~/.config/curation_utils/tokens.toml"))
175
+ if toml_path.is_file():
176
+ tok = _read_simple_github_toml(toml_path)
177
+ if tok:
178
+ return tok
179
+ return None