codeyear 0.2.14__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.
codeyear/i18n.py ADDED
@@ -0,0 +1,345 @@
1
+ """Tiny two-language message table for the CLI (no gettext dependency).
2
+
3
+ Chinese is the default; ``--lang en``, ``CODEYEAR_LANG=en`` or an English
4
+ system locale switches the CLI help and selected runtime messages to English.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import argparse
10
+ import locale
11
+ import os
12
+
13
+ _LANG = "zh"
14
+
15
+ EN = {
16
+ # top-level parser
17
+ "界面与 Profile 卡片语言 / UI and profile-card language":
18
+ "UI and profile-card language",
19
+ # GUI language-switch label stays in Chinese
20
+ "中文": "中文",
21
+ "扫描本地 git 仓库,生成一份年度代码报告(HTML + PNG 海报)。":
22
+ "Scan local git repositories and generate an annual code report "
23
+ "(HTML + poster).",
24
+ # command help
25
+ "图形界面:点选目录、看进度、选输出位置":
26
+ "Desktop GUI: pick folders, watch progress, choose output",
27
+ "终端交互向导:选目录、看进度、选输出位置":
28
+ "Terminal wizard: pick folders, watch progress, choose output",
29
+ "检查或安装最新版本": "Check for or install the latest release",
30
+ "只检查是否有新版本,不安装":
31
+ "Only check whether an update is available",
32
+ "从公开 GitHub 账号生成(联网,仅公开仓库)":
33
+ "Generate from a public GitHub account (network, public repos only)",
34
+ "启动公开 GitHub 卡片 HTTP 服务":
35
+ "Run the public GitHub card HTTP service",
36
+ "扫描一个文件夹下的所有 git 仓库":
37
+ "Scan all git repositories under a folder",
38
+ # github
39
+ "GitHub 用户名": "GitHub username",
40
+ "统计年份": "Year to report",
41
+ "输出目录": "Output directory",
42
+ "GitHub token;也可用 GITHUB_TOKEN":
43
+ "GitHub token; GITHUB_TOKEN also works",
44
+ "最多拉取多少条提交(无 token 时自动限制为 30)":
45
+ "Maximum commits to fetch (capped at 30 without a token)",
46
+ "包含 fork 仓库": "Include fork repositories",
47
+ "生成后打开报告": "Open the report when done",
48
+ "不打印扫描进度": "Suppress scan progress",
49
+ "卡片主题:paper / mono / forest / ocean":
50
+ "Card theme: paper / mono / forest / ocean",
51
+ "只生成主页卡片和 embed 片段,不生成完整报告":
52
+ "Only generate the profile card and embed snippet",
53
+ "本地 GitHub Profile 仓库路径;自动更新 README 里的卡片":
54
+ "Local GitHub profile repository; updates the card in its README",
55
+ "更新后提交并推送 Profile 仓库(联网);需同时指定 --profile-repo":
56
+ "Commit and push the profile repo after updating (network); "
57
+ "requires --profile-repo",
58
+ "缓存公开 commit 详情到该目录,重复运行更快":
59
+ "Cache public commit details in this directory for faster reruns",
60
+ "禁用公开 commit 详情本地缓存":
61
+ "Disable local caching of public commit details",
62
+ "当年没有提交时回退到上一年,适合跨年自动更新":
63
+ "Fall back to the previous year when the requested year has no commits",
64
+ "时区(如 UTC+8 或 Asia/Shanghai;默认本机时区)":
65
+ "Timezone (e.g. UTC+8 or Asia/Shanghai; default local timezone)",
66
+ # serve
67
+ "监听地址,默认 127.0.0.1": "Listen address, default 127.0.0.1",
68
+ "监听端口,默认 8000": "Listen port, default 8000",
69
+ "每张卡片最多拉取多少条提交": "Maximum commits fetched per card",
70
+ # scan
71
+ "要扫描的文件夹(可以多个,默认当前目录)":
72
+ "Folders to scan (multiple allowed; default current directory)",
73
+ "统计年份,默认今年": "Year to report, default current year",
74
+ "统计过去 365 天(滚动年度),而非自然年":
75
+ "Count the past 365 days (rolling year) instead of a calendar year",
76
+ "排除指定仓库名或通配符(可重复或逗号分隔)":
77
+ "Exclude repositories by name or glob (repeatable or comma-separated)",
78
+ "输出目录,默认 ./codeyear-output":
79
+ "Output directory, default ./codeyear-output",
80
+ "只统计匹配的 git author(--author 参数)":
81
+ "Only count the matching git author (--author)",
82
+ "同时扫描去年,在报告里生成同比数据":
83
+ "Also scan last year and add year-over-year data",
84
+ "额外作者邮箱或名字,可重复;用于旧提交":
85
+ "Extra author email/name, repeatable; for old commits",
86
+ "忽略仓库里的 .mailmap(默认自动使用)":
87
+ "Ignore the repository's .mailmap (used automatically by default)",
88
+ "统计所有本地分支/tag 的提交(默认只看当前分支)":
89
+ "Include every local branch/tag (default: current branch only)",
90
+ "统计所有作者的提交(默认只统计你本人,按 git user.name / user.email 识别)":
91
+ "Count every author (default: only you, by git user.name / user.email)",
92
+ "向下查找仓库的最大文件夹层数,默认 6":
93
+ "Maximum folder depth to search for repositories, default 6",
94
+ "单个仓库的扫描超时秒数": "Per-repository timeout in seconds",
95
+ "并行扫描的仓库数,默认 1": "Repositories to scan in parallel, default 1",
96
+ "不生成 PNG 海报": "Skip the PNG poster",
97
+ "不生成完整版长图": "Skip the full-length image",
98
+ "不生成 GitHub 主页卡片": "Skip the GitHub profile card",
99
+ "不生成 HTML 报告": "Skip the HTML report",
100
+ "本地 GitHub Profile 仓库路径;只写入本地,不联网":
101
+ "Local GitHub profile repository; writes locally only",
102
+ "生成后自动打开报告": "Open the report when done",
103
+ # runtime: scan
104
+ "请改用 codeyear tui 或 codeyear scan。":
105
+ "Use `codeyear tui` or `codeyear scan` instead.",
106
+ "无法自动打开文件: {exc}": "Could not open the file: {exc}",
107
+ "无法加载图形界面(Tkinter): {exc}":
108
+ "Could not load the GUI (Tkinter): {exc}",
109
+ "错误: {exc}": "Error: {exc}",
110
+ "--push-profile 需要同时指定 --profile-repo":
111
+ "--push-profile requires --profile-repo",
112
+ "在 {where} 下没有找到 git 仓库。":
113
+ "No git repositories found under {where}.",
114
+ "CodeYear {year} · 在 {where} 下找到 {count} 个仓库":
115
+ "CodeYear {year} · found {count} repositories under {where}",
116
+ "! 未检测到 git user.name / user.email,将统计所有作者的提交":
117
+ "! No git user.name / user.email detected; counting all authors",
118
+ "{year} 年没有找到提交。可以换 --year,或检查 --author 过滤条件。":
119
+ "No commits found in {year}. Try another --year or check --author.",
120
+ "! 指定了 --profile-repo,但 --no-card 关闭了卡片生成":
121
+ "! --profile-repo was set, but --no-card disabled card generation",
122
+ "Profile 已提交并推送": "Profile committed and pushed",
123
+ "Profile 仅写入本地": "Profile written locally",
124
+ "Profile 卡片已是最新": "Profile card is up to date",
125
+ "已提交并推送": "committed and pushed",
126
+ "仅写入本地": "written locally",
127
+ "Profile {state} · {repo}": "Profile {state} · {repo}",
128
+ "Profile 卡片已是最新 · {repo}": "Profile card is up to date · {repo}",
129
+ "! 更新 Profile 失败: {exc}": "! Profile update failed: {exc}",
130
+ "活跃 {days} 天({pct})· 最长连续 {streak} 天":
131
+ "Active {days} days ({pct}) · longest streak {streak} days",
132
+ "语言 {count} 种": "Languages {count}",
133
+ "最忙一天 {day} · {count} 次提交":
134
+ "Busiest day {day} · {count} commits",
135
+ "活跃 {days} 天 · 涉及 {count} 个文件":
136
+ "Active {days} days · across {count} files",
137
+ "警告:": "Warnings:",
138
+ "提示:": "Notes:",
139
+ "仓库": "Repos",
140
+ "身份": "Identity",
141
+ "已过滤": "Filtered",
142
+ "次他人提交": "commits by others",
143
+ "提交": "Commits",
144
+ "代码行": "Lines",
145
+ "活跃": "Active",
146
+ "最长连续": "Longest streak",
147
+ "语言": "Languages",
148
+ "最忙一天": "Busiest day",
149
+ "警告": "Warnings",
150
+ "身份 --author={author}": "Identity --author={author}",
151
+ "身份 全部作者": "Identity all authors",
152
+ "全部作者": "all authors",
153
+ "CodeYear {year} · {title}": "CodeYear {year}",
154
+ # runtime: public github
155
+ "未设置 GITHUB_TOKEN,最多拉取 30 条提交。":
156
+ "GITHUB_TOKEN not set; limiting to 30 commits.",
157
+ " {repo} · {count} 次提交": " {repo} · {count} commits",
158
+ "账号": "Account",
159
+ "输出": "Output",
160
+ "提示": "Notes",
161
+ "找不到 GitHub 用户: {username}": "GitHub user not found: {username}",
162
+ "GitHub API 错误: {exc}": "GitHub API error: {exc}",
163
+ "{login} 在 {year} 年没有找到公开提交。":
164
+ "No public commits found for {login} in {year}.",
165
+ "已达到 --max-commits 上限({limit})":
166
+ "Reached the --max-commits limit ({limit})",
167
+ "GitHub API 额度不足,请使用更高额度的 token(如 CODEYEAR_TOKEN)后重试。":
168
+ "GitHub API quota exhausted; use a higher-limit token "
169
+ "(e.g. CODEYEAR_TOKEN) and retry.",
170
+ # wizard
171
+ "选择要扫描的目录。默认是当前目录,可以继续添加。":
172
+ "Choose folders to scan. The current directory is selected by default.",
173
+ "已选择:": "Selected:",
174
+ "(空)": "(empty)",
175
+ "[Enter] 开始 [a] 添加目录 [r] 删除 [c] 清空":
176
+ "[Enter] start [a] add folder [r] remove [c] clear",
177
+ "至少选择一个目录。": "Choose at least one folder.",
178
+ "目录路径": "Folder path",
179
+ "这不是一个文件夹。": "That is not a folder.",
180
+ "这个目录已经在列表里。": "That folder is already in the list.",
181
+ "删除第几个": "Remove number",
182
+ "编号不对。": "Invalid number.",
183
+ "未知选项。": "Unknown option.",
184
+ "无法自动打开报告: {exc}": "Could not open the report: {exc}",
185
+ "向下扫描层级(文件夹层数)": "Folder depth to scan",
186
+ "正在查找 git 仓库…": "Looking for git repositories…",
187
+ "找到 {count} 个 git 仓库。": "Found {count} git repositories.",
188
+ "没有找到仓库,换个目录试试。": "No repositories found; try another folder.",
189
+ "只统计你本人({label})?": "Count only you ({label})?",
190
+ "旧邮箱/名字(逗号分隔,可空)":
191
+ "Old email/name (comma-separated, optional)",
192
+ "未检测到 git 身份,将统计所有作者。":
193
+ "No git identity detected; counting all authors.",
194
+ "同时统计去年,生成同比?": "Also scan last year and add year-over-year data?",
195
+ "统计所有分支/tag?": "Include all branches/tags?",
196
+ "并行扫描数": "Parallel scans",
197
+ "完成后打开报告?": "Open the report when done?",
198
+ "{year} 年没有找到提交。": "No commits found in {year}.",
199
+ "已取消。": "Cancelled.",
200
+ # GUI
201
+ "CodeYear · 代码手账": "CodeYear",
202
+ "代码手账": "CodeYear",
203
+ "选择目录,生成一份本地年度代码报告。":
204
+ "Choose folders and generate a local annual code report.",
205
+ "扫描目录": "Scan folders",
206
+ "添加目录": "Add folder",
207
+ "删除选中": "Remove selected",
208
+ "清空": "Clear",
209
+ "检查仓库": "Check repos",
210
+ "可添加多个目录": "Multiple folders allowed",
211
+ "年份": "Year",
212
+ "扫描层级": "Depth",
213
+ "卡片主题": "Card theme",
214
+ "保存默认": "Save defaults",
215
+ "已保存默认设置。": "Default settings saved.",
216
+ "只统计我本人({who})": "Count only me ({who})",
217
+ "未检测到 git 身份,将统计所有作者":
218
+ "No git identity detected; counting all authors",
219
+ "扫描层级 = 向下查找仓库的文件夹层数,默认 6。":
220
+ "Depth = folder levels to search for repositories, default 6.",
221
+ "并行数": "Jobs",
222
+ "同比去年": "Compare last year",
223
+ "所有分支": "All branches",
224
+ "旧邮箱/名字": "Old emails/names",
225
+ "选择…": "Choose…",
226
+ "开始生成": "Generate",
227
+ "打开报告": "Open report",
228
+ "打开输出目录": "Open output folder",
229
+ "选择目录后点击「开始生成」。":
230
+ "Choose folders, then click Generate.",
231
+ "结果": "Result",
232
+ "选择要扫描的目录": "Choose folders to scan",
233
+ "已添加,开始生成时会统计仓库数量":
234
+ "Added; repository count is checked when generating",
235
+ "没有目录": "No folders",
236
+ "请先添加至少一个目录。": "Add at least one folder first.",
237
+ "正在检查…": "Checking…",
238
+ "选择输出目录": "Choose output folder",
239
+ "请先添加至少一个要扫描的目录。":
240
+ "Add at least one folder to scan first.",
241
+ "参数不对": "Invalid values",
242
+ "年份、扫描层级和并行数必须是数字。":
243
+ "Year, depth and jobs must be numbers.",
244
+ "没有找到 git 仓库。": "No git repositories found.",
245
+ "找到 {count} 个 git 仓库": "Found {count} git repositories",
246
+ "找到 {count} 个仓库,开始扫描…":
247
+ "Found {count} repositories; scanning…",
248
+ "仓库 {repos} · 提交 {commits} · 活跃 {days} 天 · 涉及 {files} 个文件":
249
+ "Repos {repos} · Commits {commits} · Active {days} days · "
250
+ "{files} files",
251
+ "输出目录:{path}": "Output folder: {path}",
252
+ "警告:": "Warnings:",
253
+ "完成。": "Done.",
254
+ "失败:{message}": "Failed: {message}",
255
+ "生成失败": "Generation failed",
256
+ "无法打开": "Cannot open",
257
+ "无法保存": "Could not save",
258
+ "无法启动图形界面: {exc}": "Could not start the GUI: {exc}",
259
+ "检查更新": "Check update",
260
+ "发现新版 v{version}": "Update v{version}",
261
+ "正在检查更新…": "Checking for updates…",
262
+ "正在下载并更新…": "Downloading and installing update…",
263
+ "发现新版本": "Update available",
264
+ "发现新版本 v{latest}(当前 v{current})。\n是否立即在软件内自动更新?":
265
+ "Version v{latest} is available (current v{current}).\n"
266
+ "Update in-app now?",
267
+ "更新完成": "Update complete",
268
+ "近 365 天": "Last 365d",
269
+ "排除仓库": "Exclude",
270
+ "GitHub 账号(留空扫本地)": "GitHub user (optional)",
271
+ "Profile 仓库": "Profile repo",
272
+ "推送": "Push",
273
+ "选择 Profile 仓库目录": "Choose profile repository folder",
274
+ "正在拉取公开 GitHub 数据…": "Fetching public GitHub data…",
275
+ "无法连接更新服务器,请检查网络。":
276
+ "Could not reach the update server; check your connection.",
277
+ "当前已是最新版本(v{version})。":
278
+ "Already on the latest version (v{version}).",
279
+ "发现新版本 v{latest}(当前 v{current}): {url}":
280
+ "New version v{latest} available (current v{current}): {url}",
281
+ "安装包 SHA256 校验未通过,已取消更新。":
282
+ "Update package failed SHA256 verification; cancelled.",
283
+ "已更新到 v{version},重新打开软件即可生效。":
284
+ "Updated to v{version}. Restart CodeYear to use the new version.",
285
+ "已更新到 v{version}。": "Updated to v{version}.",
286
+ "更新失败: {exc}": "Update failed: {exc}",
287
+ }
288
+
289
+
290
+ def _normalize(value: str | None) -> str:
291
+ return "en" if (value or "").lower().startswith("en") else "zh"
292
+
293
+
294
+ def set_lang(value: str | None) -> None:
295
+ global _LANG
296
+ _LANG = _normalize(value)
297
+
298
+
299
+ def get_lang() -> str:
300
+ return _LANG
301
+
302
+
303
+ def detect(argv: list[str] | None = None) -> str:
304
+ args = list(argv or [])
305
+ for index, arg in enumerate(args):
306
+ if arg == "--lang" and index + 1 < len(args):
307
+ return _normalize(args[index + 1])
308
+ if arg.startswith("--lang="):
309
+ return _normalize(arg.split("=", 1)[1])
310
+ env = os.environ.get("CODEYEAR_LANG")
311
+ if env:
312
+ return _normalize(env)
313
+ loc = os.environ.get("LC_ALL") or os.environ.get("LANG") or ""
314
+ if not loc:
315
+ try:
316
+ loc = locale.getlocale()[0] or ""
317
+ except (ValueError, OSError):
318
+ loc = ""
319
+ loc = loc.lower()
320
+ if "zh" in loc or "chinese" in loc:
321
+ return "zh"
322
+ return "en" if loc else "zh"
323
+
324
+
325
+ def tr(text: str, **kwargs) -> str:
326
+ if _LANG == "en":
327
+ text = EN.get(text, text)
328
+ return text.format(**kwargs) if kwargs else text
329
+
330
+
331
+ def localize_parser(parser: argparse.ArgumentParser) -> None:
332
+ """Translate argparse help strings in place (recursing into subparsers)."""
333
+ if _LANG != "en":
334
+ return
335
+ if parser.description:
336
+ parser.description = tr(parser.description)
337
+ for action in parser._actions:
338
+ if action.help:
339
+ action.help = tr(action.help)
340
+ if isinstance(action, argparse._SubParsersAction):
341
+ for choice in action._choices_actions:
342
+ if choice.help:
343
+ choice.help = tr(choice.help)
344
+ for subparser in action.choices.values():
345
+ localize_parser(subparser)
codeyear/keywords.py ADDED
@@ -0,0 +1,98 @@
1
+ """Lightweight keyword extraction for commit subjects (no third-party deps).
2
+
3
+ English words are extracted with a regex. Chinese has no whitespace, so we
4
+ count 2-character n-grams and drop a stopword list of high-frequency commit
5
+ vocabulary. It is deliberately simple: the goal is a fun word cloud, not NLP.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import re
11
+ from collections import Counter
12
+
13
+ EN_STOP: set[str] = {
14
+ # conventional commit noise
15
+ "fix", "fixes", "fixed", "feat", "feature", "features", "chore", "docs",
16
+ "doc", "refactor", "style", "test", "tests", "build", "ci", "release",
17
+ "version", "bump", "merge", "branch", "revert", "update", "updates",
18
+ "updated", "add", "adds", "added", "remove", "removes", "removed",
19
+ "delete", "deleted", "change", "changes", "changed", "improve",
20
+ "improves", "improved", "support", "supports", "initial", "init",
21
+ "commit", "wip", "minor", "major", "patch", "new", "use",
22
+ "using", "used", "make", "makes", "made", "move", "moves", "moved",
23
+ "rename", "renamed", "cleanup", "clean", "misc", "small",
24
+ "readme", "license", "gitignore",
25
+ # generic English
26
+ "the", "and", "for", "with", "from", "this", "that", "these", "those",
27
+ "into", "onto", "over", "under", "when", "where", "which", "while",
28
+ "then", "than", "also", "only", "just", "more", "less", "some", "any",
29
+ "all", "not", "but", "can", "could", "should", "would", "will", "may",
30
+ "have", "has", "had", "was", "were", "are", "is", "be", "been", "being",
31
+ "you", "your", "our", "their", "its", "it", "we", "they", "them",
32
+ "via", "per", "after", "before", "during", "without", "within",
33
+ "file", "files", "code", "project", "work", "stuff", "thing", "things",
34
+ "todo", "note", "notes", "error", "errors", "issue", "issues",
35
+ "type", "types", "line", "lines", "page", "pages",
36
+ }
37
+
38
+ # High-frequency Chinese 2-grams that carry little meaning in a word cloud.
39
+ ZH_STOP_2: set[str] = {
40
+ "修复", "更新", "新增", "添加", "删除", "修改", "优化", "重构", "调整",
41
+ "支持", "实现", "使用", "增加", "解决", "完善", "测试", "文档", "版本",
42
+ "功能", "问题", "代码", "项目", "文件", "部分", "相关", "进行", "一个",
43
+ "可以", "已经", "没有", "这个", "那个", "以及", "但是", "因为", "所以",
44
+ "如果", "或者", "并且", "同时", "目前", "现在", "还是", "就是", "不是",
45
+ "不能", "需要", "可能", "我们", "你们", "他们", "自己", "一下", "一些",
46
+ "这些", "那些", "所有", "主要", "默认", "临时", "合并", "分支", "提交",
47
+ "首次", "初始", "补充", "改进", "处理", "逻辑", "页面", "接口", "数据",
48
+ "用户", "系统", "模块", "配置", "参数", "样式", "组件", "服务", "环境",
49
+ "本地", "线上", "生产", "开发", "调试", "编译", "部署", "发布", "回滚",
50
+ "兼容", "性能", "安全", "错误", "异常", "警告", "日志", "缓存", "网络",
51
+ "请求", "响应", "数据库", "字段", "表格", "列表", "详情", "编辑", "创建",
52
+ "初始化", "加载", "显示", "隐藏", "点击", "输入", "输出", "上传", "下载",
53
+ "导入", "导出", "重新", "再次", "直接", "通过", "根据", "对于",
54
+ "关于", "用于", "位于", "包括", "包含", "提供", "获取", "设置", "打开",
55
+ "关闭", "开始", "结束", "完成", "失败", "成功", "正常", "手动",
56
+ "自动", "简单", "复杂", "小型", "大型", "新的", "旧的", "当前", "之前",
57
+ "之后", "今年", "今天", "昨天", "明天", "最后", "第一", "第二",
58
+ "一直", "一起", "一样", "一定", "非常", "比较", "更加", "最好",
59
+ }
60
+
61
+ _CJK_RE = re.compile(r"[\u4e00-\u9fff]{2,}")
62
+ _EN_RE = re.compile(r"[A-Za-z][A-Za-z0-9_+#.\-]{2,}")
63
+ _PREFIX_RE = re.compile(r"^\s*(?:[A-Za-z]+(?:\([^)]*\))?!?:|[^\s::]{1,8}[::])\s*")
64
+
65
+ # Chinese 2-grams containing these characters are usually grammatical glue,
66
+ # not meaningful keywords ("个原", "的功", ...).
67
+ _ZH_SKIP_CHARS = set(
68
+ "个的了在是和与及把被对为以于之其此该每各等即并或而则因所由从到向给让使"
69
+ "得地着过这那有没就都也很还再又最更太我你他她它们一二三四五六七八九十"
70
+ )
71
+
72
+
73
+ def _strip_prefix(text: str) -> str:
74
+ return _PREFIX_RE.sub("", text, count=1)
75
+
76
+
77
+ def extract_keywords(subjects: list[str], top: int = 14) -> list[tuple[str, int]]:
78
+ counts: Counter[str] = Counter()
79
+
80
+ for raw in subjects:
81
+ text = _strip_prefix(raw)
82
+ for word in _EN_RE.findall(text):
83
+ word = word.lower().strip(".-_")
84
+ if len(word) >= 3 and word not in EN_STOP:
85
+ counts[word] += 1
86
+ for seq in _CJK_RE.findall(text):
87
+ for i in range(len(seq) - 1):
88
+ gram = seq[i:i + 2]
89
+ if gram[0] in _ZH_SKIP_CHARS or gram[1] in _ZH_SKIP_CHARS:
90
+ continue
91
+ if gram not in ZH_STOP_2:
92
+ counts[gram] += 1
93
+
94
+ # Single occurrences are usually noise; keep them only when we have little data.
95
+ min_count = 2 if sum(counts.values()) >= 40 else 1
96
+ items = [(k, v) for k, v in counts.items() if v >= min_count]
97
+ items.sort(key=lambda kv: (-kv[1], kv[0]))
98
+ return items[:top]
codeyear/languages.py ADDED
@@ -0,0 +1,132 @@
1
+ """File path -> programming language mapping and noise filtering."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ # Extensions we count as "code" in the language chart.
8
+ EXT_LANG: dict[str, str] = {
9
+ # Python
10
+ ".py": "Python", ".pyw": "Python", ".pyi": "Python",
11
+ # Web
12
+ ".js": "JavaScript", ".mjs": "JavaScript", ".cjs": "JavaScript", ".jsx": "JavaScript",
13
+ ".ts": "TypeScript", ".tsx": "TypeScript", ".mts": "TypeScript",
14
+ ".vue": "Vue", ".svelte": "Svelte", ".astro": "Astro",
15
+ ".html": "HTML", ".htm": "HTML",
16
+ ".css": "CSS", ".scss": "SCSS", ".sass": "SCSS", ".less": "LESS",
17
+ # JVM / .NET
18
+ ".java": "Java", ".kt": "Kotlin", ".kts": "Kotlin", ".groovy": "Groovy",
19
+ ".cs": "C#", ".fs": "F#", ".vb": "Visual Basic",
20
+ # Native
21
+ ".c": "C", ".h": "C/C++", ".cc": "C++", ".cpp": "C++", ".cxx": "C++",
22
+ ".hpp": "C++", ".hh": "C++", ".hxx": "C++", ".m": "Objective-C",
23
+ ".mm": "Objective-C++", ".swift": "Swift", ".go": "Go", ".rs": "Rust",
24
+ ".zig": "Zig", ".nim": "Nim", ".d": "D", ".v": "V",
25
+ # Scripts
26
+ ".rb": "Ruby", ".php": "PHP", ".lua": "Lua", ".pl": "Perl", ".r": "R",
27
+ ".jl": "Julia", ".dart": "Dart", ".ex": "Elixir", ".exs": "Elixir",
28
+ ".erl": "Erlang", ".hs": "Haskell", ".scala": "Scala", ".clj": "Clojure",
29
+ ".rkt": "Racket", ".lisp": "Lisp", ".el": "Emacs Lisp",
30
+ # Shell
31
+ ".sh": "Shell", ".bash": "Shell", ".zsh": "Shell", ".fish": "Shell",
32
+ ".ps1": "PowerShell", ".psm1": "PowerShell", ".bat": "Batch", ".cmd": "Batch",
33
+ # Data / query
34
+ ".sql": "SQL", ".graphql": "GraphQL", ".gql": "GraphQL", ".proto": "Protobuf",
35
+ # Docs / config
36
+ ".md": "Markdown", ".mdx": "MDX", ".rst": "reStructuredText", ".tex": "TeX",
37
+ ".json": "JSON", ".jsonc": "JSON", ".yaml": "YAML", ".yml": "YAML",
38
+ ".toml": "TOML", ".ini": "INI", ".cfg": "Config", ".conf": "Config",
39
+ # Notebooks / shaders / embedded
40
+ ".ipynb": "Jupyter", ".glsl": "Shader", ".hlsl": "Shader", ".wgsl": "Shader",
41
+ ".metal": "Shader", ".ino": "Arduino", ".pde": "Processing",
42
+ ".asm": "Assembly", ".s": "Assembly", ".sol": "Solidity",
43
+ }
44
+
45
+ NAME_LANG: dict[str, str] = {
46
+ "dockerfile": "Docker",
47
+ "makefile": "Makefile",
48
+ "cmakelists.txt": "CMake",
49
+ "gemfile": "Ruby",
50
+ "rakefile": "Ruby",
51
+ "procfile": "Config",
52
+ "justfile": "Makefile",
53
+ ".gitignore": "Config",
54
+ ".gitattributes": "Config",
55
+ ".editorconfig": "Config",
56
+ }
57
+
58
+ # Binary / generated / data files that must never count as source lines.
59
+ SKIP_EXTS: set[str] = {
60
+ ".png", ".jpg", ".jpeg", ".gif", ".webp", ".ico", ".bmp", ".tif", ".tiff",
61
+ ".psd", ".ai", ".eps", ".pdf", ".svg",
62
+ ".zip", ".gz", ".tgz", ".bz2", ".xz", ".tar", ".7z", ".rar",
63
+ ".exe", ".dll", ".so", ".dylib", ".a", ".lib", ".o", ".obj", ".class",
64
+ ".jar", ".war", ".apk", ".ipa", ".wasm", ".pyc", ".pyo", ".pyd",
65
+ ".woff", ".woff2", ".ttf", ".otf", ".eot",
66
+ ".mp3", ".mp4", ".mov", ".avi", ".mkv", ".webm", ".wav", ".flac", ".ogg",
67
+ ".bin", ".dat", ".db", ".sqlite", ".sqlite3",
68
+ ".pt", ".pth", ".onnx", ".ckpt", ".safetensors", ".npy", ".npz", ".pkl",
69
+ ".parquet", ".feather", ".arrow",
70
+ ".csv", ".tsv", ".xlsx", ".xls", ".docx", ".doc", ".pptx", ".ppt",
71
+ }
72
+
73
+ SKIP_NAMES: set[str] = {
74
+ "package-lock.json", "pnpm-lock.yaml", "yarn.lock", "bun.lockb",
75
+ "cargo.lock", "poetry.lock", "pipfile.lock", "composer.lock",
76
+ "go.sum", "gemfile.lock", "flake.lock", "npm-shrinkwrap.json",
77
+ ".ds_store", "thumbs.db", "desktop.ini",
78
+ }
79
+
80
+ SKIP_PARTS: set[str] = {
81
+ "node_modules", "bower_components", "dist", "build", "out", "output",
82
+ "target", "vendor", "third_party", "third-party", "coverage", "htmlcov",
83
+ ".next", ".nuxt", ".svelte-kit", ".parcel-cache", ".turbo",
84
+ "__pycache__", ".venv", "venv", "env", "site-packages",
85
+ "pods", "deriveddata", ".gradle", ".m2", "obj", "bin",
86
+ "codeyear-output",
87
+ }
88
+
89
+ # Languages that are technically text but not interesting for "most edited files".
90
+ NON_CODE_LANGS: set[str] = {
91
+ "Markdown", "MDX", "reStructuredText", "TeX", "JSON", "YAML", "TOML",
92
+ "INI", "Config", "Jupyter",
93
+ }
94
+
95
+
96
+ def normalize_path(path: str) -> str:
97
+ return path.replace("\\", "/")
98
+
99
+
100
+ def is_skipped(path: str) -> bool:
101
+ """Return True for binary, generated, vendored or lock files."""
102
+ p = normalize_path(path).strip()
103
+ if not p:
104
+ return True
105
+ parts = [x.lower() for x in p.split("/") if x]
106
+ if not parts:
107
+ return True
108
+ name = parts[-1]
109
+ if name in SKIP_NAMES:
110
+ return True
111
+ if name.endswith((".min.js", ".min.css", ".map", ".lock", ".sum")):
112
+ return True
113
+ if any(part in SKIP_PARTS for part in parts[:-1]):
114
+ return True
115
+ suffix = Path(name).suffix.lower()
116
+ return suffix in SKIP_EXTS
117
+
118
+
119
+ def language_for(path: str) -> str | None:
120
+ """Map a repo-relative path to a language, or None if it should be ignored."""
121
+ if is_skipped(path):
122
+ return None
123
+ p = normalize_path(path)
124
+ name = p.rsplit("/", 1)[-1].lower()
125
+ if name in NAME_LANG:
126
+ return NAME_LANG[name]
127
+ suffix = Path(name).suffix.lower()
128
+ return EXT_LANG.get(suffix)
129
+
130
+
131
+ def is_code_language(lang: str | None) -> bool:
132
+ return bool(lang) and lang not in NON_CODE_LANGS