-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlanguage_statistics.py
More file actions
53 lines (41 loc) · 2.07 KB
/
language_statistics.py
File metadata and controls
53 lines (41 loc) · 2.07 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
from collections import defaultdict
from dataclasses import dataclass
from typing import Dict
from git_analytics.entities import AnalyticsCommit, AnalyticsResult
from git_analytics.interfaces import CommitAnalyzer
@dataclass
class FileExtensionChangeStats:
insertions: int = 0
deletions: int = 0
@dataclass
class Result(AnalyticsResult):
files_extensions_total: Dict[str, FileExtensionChangeStats]
files_extensions_by_author: Dict[str, Dict[str, FileExtensionChangeStats]]
def _get_file_extension(file_path: str) -> str:
filename = file_path.split("/")[-1]
filename_parts = filename.split(".")
if len(filename_parts) == 1 or filename_parts[0] == "":
return "no_extension"
return filename_parts[-1].lower().replace("}", "")
class LanguageAnalyzer(CommitAnalyzer):
name = "language_statistics"
def __init__(self) -> None:
self._total: Dict[str, FileExtensionChangeStats] = defaultdict(FileExtensionChangeStats)
self._by_author: Dict[str, Dict[str, FileExtensionChangeStats]] = defaultdict(
lambda: defaultdict(FileExtensionChangeStats)
)
def process(self, commit: AnalyticsCommit) -> None:
for changed_file in commit.files:
file_extension = _get_file_extension(changed_file)
self._total[file_extension].insertions += commit.files[changed_file].insertions
self._total[file_extension].deletions += commit.files[changed_file].deletions
self._by_author[commit.commit_author][file_extension].insertions += commit.files[changed_file].insertions
self._by_author[commit.commit_author][file_extension].deletions += commit.files[changed_file].deletions
def result(self) -> Result:
return Result(
files_extensions_total=dict(sorted(self._total.items(), key=lambda item: item[1].insertions, reverse=True)),
files_extensions_by_author={
author: dict(sorted(counter.items(), key=lambda item: item[1].insertions, reverse=True))
for author, counter in self._by_author.items()
},
)