ScriptCollection 4.4.18__py3-none-any.whl → 4.4.20__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.
- ScriptCollection/ArbTranslationsOrganizer.py +172 -0
- ScriptCollection/GeneralUtilities.py +1 -0
- ScriptCollection/RepositoryDiagramGenerator.py +125 -0
- ScriptCollection/RepositoryHistoryAnalyzer.py +121 -0
- ScriptCollection/ScriptCollectionCore.py +149 -14
- ScriptCollection/TFCPS/CPP/TFCPS_CodeUnitSpecific_CPP.py +243 -0
- ScriptCollection/TFCPS/CPP/__init__.py +0 -0
- ScriptCollection/TFCPS/Flutter/TFCPS_CodeUnitSpecific_Flutter.py +494 -12
- ScriptCollection/TFCPS/NodeJS/TFCPS_CodeUnitSpecific_NodeJS.py +9 -2
- ScriptCollection/TFCPS/SCTaskRunnerServer.py +69 -9
- ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnits.py +2 -12
- ScriptCollection/TFCPS/TFCPS_RemoteBuild.py +2 -0
- ScriptCollection/TFCPS/TFCPS_Tools_General.py +38 -0
- ScriptCollection/UnassignedVariablesCheck.py +12 -31
- {scriptcollection-4.4.18.dist-info → scriptcollection-4.4.20.dist-info}/METADATA +2 -1
- {scriptcollection-4.4.18.dist-info → scriptcollection-4.4.20.dist-info}/RECORD +19 -14
- {scriptcollection-4.4.18.dist-info → scriptcollection-4.4.20.dist-info}/WHEEL +0 -0
- {scriptcollection-4.4.18.dist-info → scriptcollection-4.4.20.dist-info}/entry_points.txt +0 -0
- {scriptcollection-4.4.18.dist-info → scriptcollection-4.4.20.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import xml.etree.ElementTree as ET
|
|
4
|
+
from .GeneralUtilities import GeneralUtilities
|
|
5
|
+
from .ScriptCollectionCore import ScriptCollectionCore
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ARBTranslationHelper:
|
|
9
|
+
"""Reusable helper-functions for interacting with a Flutter-codeunit's ARB translation-files
|
|
10
|
+
(lib/l10n/app_<locale>.arb) and with the XLIFF 2.0 files (Other/Resources/Translations/messages(.language).xlf)
|
|
11
|
+
used to synchronize them. See ArbTranslationsOrganizer for the algorithm these are used by.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
_XLIFF2_NAMESPACE = "urn:oasis:names:tc:xliff:document:2.0"
|
|
15
|
+
_FILE_ID = "flutterl10n"
|
|
16
|
+
|
|
17
|
+
@staticmethod
|
|
18
|
+
def arb_file_path(arb_folder: str, language: str) -> str:
|
|
19
|
+
# ARB-files use Dart's/Flutter's own locale-notation (underscore, e.g. "de_CH"), unlike the XLIFF-files below
|
|
20
|
+
# (hyphen, e.g. "de-CH"), matching how ConSurvFrontend's messages.<language>.xlf are already named.
|
|
21
|
+
return os.path.join(arb_folder, f"app_{language.replace('-', '_')}.arb")
|
|
22
|
+
|
|
23
|
+
@staticmethod
|
|
24
|
+
def base_xlf_file_path(xlf_folder: str) -> str:
|
|
25
|
+
return os.path.join(xlf_folder, "messages.xlf")
|
|
26
|
+
|
|
27
|
+
@staticmethod
|
|
28
|
+
def xlf_file_path(xlf_folder: str, language: str) -> str:
|
|
29
|
+
return os.path.join(xlf_folder, f"messages.{language}.xlf")
|
|
30
|
+
|
|
31
|
+
@staticmethod
|
|
32
|
+
def read_arb_messages(arb_file: str) -> dict[str, str]:
|
|
33
|
+
"""The message key/value pairs of an arb-file, skipping the "@@locale"-entry and any "@key"-metadata-entries.
|
|
34
|
+
Returns an empty dict if the file does not exist."""
|
|
35
|
+
if not os.path.isfile(arb_file):
|
|
36
|
+
return {}
|
|
37
|
+
with open(arb_file, "r", encoding="utf-8") as f:
|
|
38
|
+
content: dict = json.load(f)
|
|
39
|
+
return {key: value for key, value in content.items() if not key.startswith("@")}
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def write_arb_messages(arb_file: str, language: str, template_keys: list[str], messages: dict[str, str]) -> None:
|
|
43
|
+
"""Writes an arb-file containing "@@locale" plus one entry per key of template_keys which has a value in
|
|
44
|
+
messages (in that same order, so the file stays diffable against the template arb-file, and so a message-key
|
|
45
|
+
which is not part of template_keys is dropped)."""
|
|
46
|
+
content = {"@@locale": language.replace("-", "_")}
|
|
47
|
+
for key in template_keys:
|
|
48
|
+
if key in messages:
|
|
49
|
+
content[key] = messages[key]
|
|
50
|
+
with open(arb_file, "w", encoding="utf-8", newline="\n") as f:#newline="\n": arb-files use LF line-endings; without this, Python would translate "\n" to the OS default (CRLF on Windows).
|
|
51
|
+
json.dump(content, f, ensure_ascii=False, indent=2)
|
|
52
|
+
f.write("\n")
|
|
53
|
+
|
|
54
|
+
@classmethod
|
|
55
|
+
def read_effective_messages(cls, xlf_file: str) -> dict[str, str]:
|
|
56
|
+
"""For every <unit> of an XLIFF-file: its <target>-text if its segment has one, otherwise its <source>-text
|
|
57
|
+
(i.e. the value this message currently effectively has, translated or not). Returns an empty dict if the file
|
|
58
|
+
does not exist yet."""
|
|
59
|
+
if not os.path.isfile(xlf_file):
|
|
60
|
+
return {}
|
|
61
|
+
ns = {"x": cls._XLIFF2_NAMESPACE}
|
|
62
|
+
messages: dict[str, str] = {}
|
|
63
|
+
for unit in ET.parse(xlf_file).getroot().findall(".//x:unit", ns):
|
|
64
|
+
segment = unit.find("x:segment", ns)
|
|
65
|
+
if segment is None:
|
|
66
|
+
continue
|
|
67
|
+
target = segment.find("x:target", ns)
|
|
68
|
+
source = segment.find("x:source", ns)
|
|
69
|
+
text = target.text if target is not None and target.text else (source.text if source is not None else None)
|
|
70
|
+
if text is not None:
|
|
71
|
+
messages[unit.get("id")] = text
|
|
72
|
+
return messages
|
|
73
|
+
|
|
74
|
+
@classmethod
|
|
75
|
+
def build_unit(cls, unit_id: str, source: str, target: str | None) -> ET.Element:
|
|
76
|
+
"""An XLIFF <unit> with the given id and source-text; state="translated" with the given <target> if target is
|
|
77
|
+
given, otherwise state="initial" without a <target>."""
|
|
78
|
+
unit = ET.Element(f"{{{cls._XLIFF2_NAMESPACE}}}unit", {"id": unit_id})
|
|
79
|
+
segment = ET.SubElement(unit, f"{{{cls._XLIFF2_NAMESPACE}}}segment")
|
|
80
|
+
ET.SubElement(segment, f"{{{cls._XLIFF2_NAMESPACE}}}source").text = source
|
|
81
|
+
if target is None:
|
|
82
|
+
segment.set("state", "initial")
|
|
83
|
+
else:
|
|
84
|
+
segment.set("state", "translated")
|
|
85
|
+
ET.SubElement(segment, f"{{{cls._XLIFF2_NAMESPACE}}}target").text = target
|
|
86
|
+
return unit
|
|
87
|
+
|
|
88
|
+
@classmethod
|
|
89
|
+
def write_xliff2_file(cls, file: str, src_lang: str, trg_lang: str | None, units: list[ET.Element]) -> None:
|
|
90
|
+
"""(Over)writes an XLIFF 2.0 file at "file" from scratch, containing exactly the given units."""
|
|
91
|
+
ET.register_namespace("", cls._XLIFF2_NAMESPACE)
|
|
92
|
+
attributes = {"version": "2.0", "srcLang": src_lang}
|
|
93
|
+
if trg_lang is not None:
|
|
94
|
+
attributes["trgLang"] = trg_lang
|
|
95
|
+
root = ET.Element(f"{{{cls._XLIFF2_NAMESPACE}}}xliff", attributes)
|
|
96
|
+
file_element = ET.SubElement(root, f"{{{cls._XLIFF2_NAMESPACE}}}file", {"id": cls._FILE_ID, "original": "app_en.arb"})
|
|
97
|
+
for unit in units:
|
|
98
|
+
file_element.append(unit)
|
|
99
|
+
tree = ET.ElementTree(root)
|
|
100
|
+
ET.indent(tree, space=" ")
|
|
101
|
+
with open(file, "wb") as f:#binary mode (as opposed to passing the filename directly to tree.write): guarantees LF line-endings regardless of the OS' default line-ending, since binary mode never translates "\n" to "\r\n".
|
|
102
|
+
tree.write(f, encoding="utf-8", xml_declaration=True)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class ArbTranslationsOrganizer:
|
|
106
|
+
"""Synchronizes a codeunit's ARB translation-files (lib/l10n/app_<locale>.arb) with XLIFF 2.0 files
|
|
107
|
+
(messages(.language).xlf), so both stay consistent. This type makes no assumption about which kind of codeunit
|
|
108
|
+
calls it or where its files live within that codeunit - it only operates on the given, already-resolved
|
|
109
|
+
"arb_folder"/"xlf_folder" paths and the given ScriptCollectionCore instance (used for the actual xlf-sync, see
|
|
110
|
+
ScriptCollectionCore.sync_xlf2_files); a caller like TFCPS_CodeUnitSpecific_Flutter_Functions.organize_translations()
|
|
111
|
+
resolves those paths from its own codeunit-folder-conventions first.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
BaseLanguage="en"#"en" means en-US; this is also why "en-GB" is allowed to be contained in "languages" further down.
|
|
115
|
+
|
|
116
|
+
def organize_translations(self,sc:ScriptCollectionCore,arb_folder:str,xlf_folder:str,languages:list[str])->dict[str,dict[int,float]]:#languages look like ["fr","de","es","de-CH","en-GB"]; "en" is not contained because en is always the default language.
|
|
117
|
+
#after this functions all texts in the arb files should be in the xlf files, and all texts in the xlf files should be in the arb files. see algorithm below
|
|
118
|
+
#in general: arb_en.arb is the source of the truth regarding to which texts exist and the source of truh for the english texts.
|
|
119
|
+
#returns the translation-state-statistics of the xlf-files (not of the arb-files), see ScriptCollectionCore.sync_xlf2_files.
|
|
120
|
+
GeneralUtilities.assert_folder_exists(arb_folder)
|
|
121
|
+
GeneralUtilities.ensure_directory_exists(xlf_folder)
|
|
122
|
+
|
|
123
|
+
self.__ensure_arb_files_exist(arb_folder,languages)
|
|
124
|
+
self.__ensure_xlf_files_exist(arb_folder,xlf_folder,languages)
|
|
125
|
+
self.__clean_up_arb_files(arb_folder,languages)
|
|
126
|
+
self.__write_arb_entries_to_xlf_files(arb_folder,xlf_folder)
|
|
127
|
+
statistics=self.__sync_xlf_files(sc,xlf_folder,languages)
|
|
128
|
+
self.__sync_xlf_files_to_arb_files(arb_folder,xlf_folder,languages)
|
|
129
|
+
return statistics
|
|
130
|
+
|
|
131
|
+
def __ensure_arb_files_exist(self,arb_folder:str,languages:list[str]):
|
|
132
|
+
#after this function in arb_folder should be a arb-file for each language in languages. create the file if not already exist.
|
|
133
|
+
for language in languages:
|
|
134
|
+
arb_file=ARBTranslationHelper.arb_file_path(arb_folder,language)
|
|
135
|
+
if not os.path.isfile(arb_file):
|
|
136
|
+
ARBTranslationHelper.write_arb_messages(arb_file,language,[],{})
|
|
137
|
+
|
|
138
|
+
def __ensure_xlf_files_exist(self,arb_folder:str,xlf_folder:str,languages:list[str]):
|
|
139
|
+
#after this function in xlf_folder should be a xlf-file for each language in languages. create the file if not already exist.
|
|
140
|
+
#a newly created file is bootstrapped from the language's current arb-translations (not left empty/untranslated), so translation-work already done directly in the arb-file (as it is right now for de/fr/es) is not discarded once that language starts taking part in the xlf-side of the sync.
|
|
141
|
+
english_messages=ARBTranslationHelper.read_arb_messages(ARBTranslationHelper.arb_file_path(arb_folder,self.BaseLanguage))
|
|
142
|
+
for language in languages:
|
|
143
|
+
xlf_file=ARBTranslationHelper.xlf_file_path(xlf_folder,language)
|
|
144
|
+
if os.path.isfile(xlf_file):
|
|
145
|
+
continue
|
|
146
|
+
arb_messages=ARBTranslationHelper.read_arb_messages(ARBTranslationHelper.arb_file_path(arb_folder,language))
|
|
147
|
+
units=[ARBTranslationHelper.build_unit(key,value,arb_messages.get(key)) for key,value in english_messages.items()]
|
|
148
|
+
ARBTranslationHelper.write_xliff2_file(xlf_file,self.BaseLanguage,language,units)
|
|
149
|
+
|
|
150
|
+
def __clean_up_arb_files(self,arb_folder:str,languages:list[str]):
|
|
151
|
+
#after this function all arb files should only contain message-keys which are contained in app_en.arb. (texts which are not in app_en.arb anymore should be removed in the other arb files)
|
|
152
|
+
template_keys=list(ARBTranslationHelper.read_arb_messages(ARBTranslationHelper.arb_file_path(arb_folder,self.BaseLanguage)).keys())
|
|
153
|
+
for language in languages:
|
|
154
|
+
arb_file=ARBTranslationHelper.arb_file_path(arb_folder,language)
|
|
155
|
+
ARBTranslationHelper.write_arb_messages(arb_file,language,template_keys,ARBTranslationHelper.read_arb_messages(arb_file))
|
|
156
|
+
|
|
157
|
+
def __write_arb_entries_to_xlf_files(self,arb_folder:str,xlf_folder:str):
|
|
158
|
+
#after this function all texts from arb_en.arb should be in messages.xlf (do not touch other files than this both in the other steps) replace the entire message content of all entries in xlf. arb_en.arb defines all "true" entries and this is the only source of truth. so messages.xlf should contain these and only these entries. simple algorithm: remove the entire content from messages.xlf, then for each entry in arb_en.arb create a new entry in messages.xlf with the same message-key and the same message-value.
|
|
159
|
+
english_messages=ARBTranslationHelper.read_arb_messages(ARBTranslationHelper.arb_file_path(arb_folder,self.BaseLanguage))
|
|
160
|
+
units=[ARBTranslationHelper.build_unit(key,value,None) for key,value in english_messages.items()]
|
|
161
|
+
ARBTranslationHelper.write_xliff2_file(ARBTranslationHelper.base_xlf_file_path(xlf_folder),self.BaseLanguage,None,units)
|
|
162
|
+
|
|
163
|
+
def __sync_xlf_files(self,sc:ScriptCollectionCore,xlf_folder:str,languages:list[str])->dict[str,dict[int,float]]:
|
|
164
|
+
#sync xlf-files. after this function the xlf files should be synchronized in that way that all xlf-files do have all values, does not matter if translated or untranslated (even if the xlf file for other languages than english should state it if a text is not translated in the usual way it is stated in xlf files). also ensure that the messages.<language>.xlf files do not have message-keys which are not contained in messages.xlf
|
|
165
|
+
return sc.sync_xlf2_files("messages",languages,xlf_folder)
|
|
166
|
+
|
|
167
|
+
def __sync_xlf_files_to_arb_files(self,arb_folder:str,xlf_folder:str,languages:list[str]):
|
|
168
|
+
#for each "messages.<language>.xlf"-xlf-file (means: for all xlf files other than messages.xlf): for each message-key: write the message-value to the corresponding arb-file in arb_folder. overwrite any existing value in the corresponding arb-file.
|
|
169
|
+
template_keys=list(ARBTranslationHelper.read_arb_messages(ARBTranslationHelper.arb_file_path(arb_folder,self.BaseLanguage)).keys())
|
|
170
|
+
for language in languages:
|
|
171
|
+
effective_messages=ARBTranslationHelper.read_effective_messages(ARBTranslationHelper.xlf_file_path(xlf_folder,language))
|
|
172
|
+
ARBTranslationHelper.write_arb_messages(ARBTranslationHelper.arb_file_path(arb_folder,language),language,template_keys,effective_messages)
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from .GeneralUtilities import GeneralUtilities
|
|
3
|
+
from .RepositoryHistoryAnalyzer import CommitterActivityData
|
|
4
|
+
|
|
5
|
+
_VEGA_LITE_SCHEMA = "https://vega.github.io/schema/vega-lite/v5.json"
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class RepositoryDiagramGenerator:
|
|
9
|
+
"""Generates Vega-Lite diagram-specifications from committer-activity-data produced by
|
|
10
|
+
RepositoryHistoryAnalyzer. This class only produces the diagram-specification (as json); rendering it into an
|
|
11
|
+
image (for example svg or png) is out of scope of this class."""
|
|
12
|
+
|
|
13
|
+
@GeneralUtilities.check_arguments
|
|
14
|
+
def generate_committer_timeline(self, data: CommitterActivityData, target_file: str) -> None:
|
|
15
|
+
"""Generates a line-diagram-specification with one line per committer, showing that committer's
|
|
16
|
+
commit-count over the calendar periods contained in "data" (see RepositoryHistoryAnalyzer.get_git_history_data)."""
|
|
17
|
+
spec = self.__build_activity_line_spec(data, "Period", "Calendar period", "Commits per committer over time")
|
|
18
|
+
self.__write_spec(spec, target_file)
|
|
19
|
+
|
|
20
|
+
@GeneralUtilities.check_arguments
|
|
21
|
+
def generate_commits_per_weekday_chart(self, data: CommitterActivityData, target_file: str) -> None:
|
|
22
|
+
"""Generates a line-diagram-specification with one line per committer, showing that committer's
|
|
23
|
+
commit-count per weekday (see RepositoryHistoryAnalyzer.get_commits_per_weekday_data)."""
|
|
24
|
+
weekday_order = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
|
25
|
+
spec = self.__build_activity_line_spec(data, "Weekday", "Weekday", "Commits per committer and weekday", weekday_order)
|
|
26
|
+
self.__write_spec(spec, target_file)
|
|
27
|
+
|
|
28
|
+
@GeneralUtilities.check_arguments
|
|
29
|
+
def generate_commits_per_hour_chart(self, data: CommitterActivityData, target_file: str) -> None:
|
|
30
|
+
"""Generates a line-diagram-specification with one line per committer, showing that committer's
|
|
31
|
+
commit-count per hour of day (see RepositoryHistoryAnalyzer.get_commits_per_hour_data)."""
|
|
32
|
+
hour_order = [f"{hour:02d}" for hour in range(24)]
|
|
33
|
+
spec = self.__build_activity_line_spec(data, "Hour", "Hour of day", "Commits per committer and hour of day", hour_order)
|
|
34
|
+
self.__write_spec(spec, target_file)
|
|
35
|
+
|
|
36
|
+
@GeneralUtilities.check_arguments
|
|
37
|
+
def generate_committer_pie_chart(self, data: CommitterActivityData, target_file: str) -> None:
|
|
38
|
+
"""Generates a pie-chart-specification showing each committer's share of the total commit-count."""
|
|
39
|
+
totals = {}
|
|
40
|
+
for data_point in data.data_points:
|
|
41
|
+
totals[data_point.committer] = totals.get(data_point.committer, 0) + data_point.commit_count
|
|
42
|
+
values = [{"Committer": committer, "Commits": commit_count} for committer, commit_count in totals.items()]
|
|
43
|
+
spec = {
|
|
44
|
+
"$schema": _VEGA_LITE_SCHEMA,
|
|
45
|
+
"description": "Share of commits per committer",
|
|
46
|
+
"width": 400,
|
|
47
|
+
"height": 400,
|
|
48
|
+
"datasets": {"committerEmails": self.__committer_emails_dataset(data)},
|
|
49
|
+
"data": {"values": values},
|
|
50
|
+
"transform": [
|
|
51
|
+
{"joinaggregate": [{"op": "sum", "field": "Commits", "as": "TotalCommits"}]},
|
|
52
|
+
{"calculate": "datum.Commits / datum.TotalCommits", "as": "Share"},
|
|
53
|
+
],
|
|
54
|
+
"mark": {"type": "arc", "tooltip": True},
|
|
55
|
+
"encoding": {
|
|
56
|
+
"theta": {"field": "Commits", "type": "quantitative", "stack": True},
|
|
57
|
+
"color": {"field": "Committer", "type": "nominal", "title": "Committer", "legend": self.__legend_with_email_tooltip()},
|
|
58
|
+
"tooltip": [
|
|
59
|
+
{"field": "Committer", "type": "nominal"},
|
|
60
|
+
{"field": "Commits", "type": "quantitative"},
|
|
61
|
+
{"field": "Share", "type": "quantitative", "format": ".1%", "title": "Share of commits"},
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
self.__write_spec(spec, target_file)
|
|
66
|
+
|
|
67
|
+
@GeneralUtilities.check_arguments
|
|
68
|
+
def __build_activity_line_spec(self, data: CommitterActivityData, bucket_field_name: str, bucket_title: str, description: str, sort_order: list = None) -> dict:
|
|
69
|
+
values = [{"Committer": data_point.committer, bucket_field_name: data_point.bucket, "Commits": data_point.commit_count} for data_point in data.data_points]
|
|
70
|
+
x_encoding = {
|
|
71
|
+
"field": bucket_field_name,
|
|
72
|
+
"type": "ordinal",
|
|
73
|
+
"title": bucket_title,
|
|
74
|
+
}
|
|
75
|
+
if sort_order is not None:
|
|
76
|
+
x_encoding["sort"] = sort_order
|
|
77
|
+
return {
|
|
78
|
+
"$schema": _VEGA_LITE_SCHEMA,
|
|
79
|
+
"description": description,
|
|
80
|
+
"width": 800,
|
|
81
|
+
"height": 400,
|
|
82
|
+
"datasets": {"committerEmails": self.__committer_emails_dataset(data)},
|
|
83
|
+
"data": {"values": values},
|
|
84
|
+
"mark": {"type": "line", "point": True},
|
|
85
|
+
"encoding": {
|
|
86
|
+
"x": x_encoding,
|
|
87
|
+
"y": {"field": "Commits", "type": "quantitative", "title": "Number of commits"},
|
|
88
|
+
"color": {"field": "Committer", "type": "nominal", "title": "Committer", "legend": self.__legend_with_email_tooltip()},
|
|
89
|
+
"tooltip": [
|
|
90
|
+
{"field": "Committer", "type": "nominal"},
|
|
91
|
+
{"field": bucket_field_name, "type": "ordinal", "title": bucket_title},
|
|
92
|
+
{"field": "Commits", "type": "quantitative"},
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@GeneralUtilities.check_arguments
|
|
98
|
+
def __committer_emails_dataset(self, data: CommitterActivityData) -> list:
|
|
99
|
+
return [{"Committer": committer, "Email": data.committer_emails.get(committer, GeneralUtilities.empty_string)} for committer in data.committers]
|
|
100
|
+
|
|
101
|
+
@GeneralUtilities.check_arguments
|
|
102
|
+
def __legend_with_email_tooltip(self) -> dict:
|
|
103
|
+
# Vega-Lite's own tooltip-encoding only applies to marks, not to legend-entries. Showing the hovered
|
|
104
|
+
# committer's email-address therefore needs this "legend.encode"-passthrough (a documented Vega-Lite
|
|
105
|
+
# mechanism to customize legend-marks with raw Vega mark-encodings): "datum.value" is the hovered
|
|
106
|
+
# legend-label (the committer-name), and "pluck"/"indexof" look its matching email up in the
|
|
107
|
+
# "committerEmails"-dataset. Vega's expression-language has no lambda/".find()", so this
|
|
108
|
+
# lambda-free array/index-lookup is used instead of a "filter"-callback.
|
|
109
|
+
return {
|
|
110
|
+
"encode": {
|
|
111
|
+
"labels": {
|
|
112
|
+
"update": {
|
|
113
|
+
"tooltip": {
|
|
114
|
+
"signal": "pluck(data('committerEmails'), 'Email')[indexof(pluck(data('committerEmails'), 'Committer'), datum.value)]"
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@GeneralUtilities.check_arguments
|
|
122
|
+
def __write_spec(self, spec: dict, target_file: str) -> None:
|
|
123
|
+
GeneralUtilities.ensure_file_exists(target_file)
|
|
124
|
+
with open(target_file, "w", encoding="utf-8") as file_handle:
|
|
125
|
+
json.dump(spec, file_handle, indent=2, sort_keys=False, ensure_ascii=False)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from .GeneralUtilities import GeneralUtilities
|
|
6
|
+
from .ScriptCollectionCore import ScriptCollectionCore
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class GitHistoryScale(Enum):
|
|
10
|
+
Day = 0
|
|
11
|
+
Week = 1
|
|
12
|
+
Month = 2
|
|
13
|
+
Year = 3
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class CommitterActivityDataPoint:
|
|
18
|
+
committer: str
|
|
19
|
+
bucket: str
|
|
20
|
+
commit_count: int
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True)
|
|
24
|
+
class CommitterActivityData:
|
|
25
|
+
committers: list = field(default_factory=list)
|
|
26
|
+
committer_emails: dict = field(default_factory=dict)
|
|
27
|
+
data_points: list = field(default_factory=list)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class RepositoryHistoryAnalyzer:
|
|
31
|
+
"""Analyzes the git history of a repository into structured committer-activity-data.
|
|
32
|
+
A "committer" is, throughout this class, exactly an identity as "git shortlog -n -s" reports it."""
|
|
33
|
+
|
|
34
|
+
__weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
|
35
|
+
__committer_and_email_pattern = re.compile(r"^(.*?)\s*<([^<>]*)>")
|
|
36
|
+
|
|
37
|
+
def __init__(self):
|
|
38
|
+
self.sc = ScriptCollectionCore()
|
|
39
|
+
|
|
40
|
+
@GeneralUtilities.check_arguments
|
|
41
|
+
def get_committers(self, repository_path: str) -> list:
|
|
42
|
+
"""Returns the committer-names, in the same order as "git shortlog -n -s" lists them."""
|
|
43
|
+
result = self.sc.run_program_argsasarray("git", ["shortlog", "-n", "-s", "HEAD"], repository_path)
|
|
44
|
+
committers = []
|
|
45
|
+
for line in result[1].splitlines():
|
|
46
|
+
if GeneralUtilities.string_has_content(line):
|
|
47
|
+
committers.append(line.split("\t", 1)[1].strip())
|
|
48
|
+
return committers
|
|
49
|
+
|
|
50
|
+
@GeneralUtilities.check_arguments
|
|
51
|
+
def get_committer_emails(self, repository_path: str) -> dict:
|
|
52
|
+
"""Returns a mapping of committer-name to email-address, based on "git shortlog -n -s -e"."""
|
|
53
|
+
result = self.sc.run_program_argsasarray("git", ["shortlog", "-n", "-s", "-e", "HEAD"], repository_path)
|
|
54
|
+
committer_emails = {}
|
|
55
|
+
for line in result[1].splitlines():
|
|
56
|
+
if not GeneralUtilities.string_has_content(line):
|
|
57
|
+
continue
|
|
58
|
+
name_and_email = line.split("\t", 1)[1]
|
|
59
|
+
match = RepositoryHistoryAnalyzer.__committer_and_email_pattern.match(name_and_email)
|
|
60
|
+
if match is not None:
|
|
61
|
+
committer_emails[match.group(1)] = match.group(2)
|
|
62
|
+
return committer_emails
|
|
63
|
+
|
|
64
|
+
@GeneralUtilities.check_arguments
|
|
65
|
+
def __get_commits(self, repository_path: str) -> list:
|
|
66
|
+
"""Returns a list of (committer-name, committer-date)-tuples for all commits, in the committer-date's
|
|
67
|
+
own recorded utc-offset."""
|
|
68
|
+
result = self.sc.run_program_argsasarray("git", ["log", "--pretty=format:%cI%x09%cn"], repository_path)
|
|
69
|
+
commits = []
|
|
70
|
+
for line in result[1].splitlines():
|
|
71
|
+
if not GeneralUtilities.string_has_content(line):
|
|
72
|
+
continue
|
|
73
|
+
date_as_string, committer = line.split("\t", 1)
|
|
74
|
+
commits.append((committer, datetime.fromisoformat(date_as_string)))
|
|
75
|
+
return commits
|
|
76
|
+
|
|
77
|
+
@GeneralUtilities.check_arguments
|
|
78
|
+
def __to_bucket(self, moment: datetime, scale: GitHistoryScale) -> str:
|
|
79
|
+
if scale == GitHistoryScale.Day:
|
|
80
|
+
return moment.strftime("%Y-%m-%d")
|
|
81
|
+
if scale == GitHistoryScale.Week:
|
|
82
|
+
iso_calendar = moment.isocalendar()
|
|
83
|
+
return f"{iso_calendar[0]}-W{iso_calendar[1]:02d}"
|
|
84
|
+
if scale == GitHistoryScale.Month:
|
|
85
|
+
return moment.strftime("%Y-%m")
|
|
86
|
+
if scale == GitHistoryScale.Year:
|
|
87
|
+
return moment.strftime("%Y")
|
|
88
|
+
raise ValueError(f"Unknown GitHistoryScale: {scale}")
|
|
89
|
+
|
|
90
|
+
@GeneralUtilities.check_arguments
|
|
91
|
+
def __aggregate(self, repository_path: str, bucket_selector) -> CommitterActivityData:
|
|
92
|
+
committers = self.get_committers(repository_path)
|
|
93
|
+
committer_emails = self.get_committer_emails(repository_path)
|
|
94
|
+
counts = {}
|
|
95
|
+
for committer, moment in self.__get_commits(repository_path):
|
|
96
|
+
bucket = bucket_selector(moment)
|
|
97
|
+
key = (committer, bucket)
|
|
98
|
+
counts[key] = counts.get(key, 0) + 1
|
|
99
|
+
data_points = [CommitterActivityDataPoint(committer, bucket, commit_count) for (committer, bucket), commit_count in counts.items()]
|
|
100
|
+
return CommitterActivityData(committers, committer_emails, data_points)
|
|
101
|
+
|
|
102
|
+
@GeneralUtilities.check_arguments
|
|
103
|
+
def get_git_history_data(self, repository_path: str, scale: GitHistoryScale) -> CommitterActivityData:
|
|
104
|
+
"""Returns, per committer and per calendar period (day/week/year, depending on "scale"), the amount of
|
|
105
|
+
commits that committer made in that calendar period. Only calendar periods with at least one commit are
|
|
106
|
+
contained in the result."""
|
|
107
|
+
return self.__aggregate(repository_path, lambda moment: self.__to_bucket(moment, scale))
|
|
108
|
+
|
|
109
|
+
@GeneralUtilities.check_arguments
|
|
110
|
+
def get_commits_per_weekday_data(self, repository_path: str) -> CommitterActivityData:
|
|
111
|
+
"""Returns, per committer and per weekday (Monday..Sunday), the amount of commits that committer made on
|
|
112
|
+
that weekday, summed across the whole history. Only weekdays with at least one commit are contained in
|
|
113
|
+
the result."""
|
|
114
|
+
return self.__aggregate(repository_path, lambda moment: RepositoryHistoryAnalyzer.__weekdays[moment.weekday()])
|
|
115
|
+
|
|
116
|
+
@GeneralUtilities.check_arguments
|
|
117
|
+
def get_commits_per_hour_data(self, repository_path: str) -> CommitterActivityData:
|
|
118
|
+
"""Returns, per committer and per hour of day ("00"..."23"), the amount of commits that committer made in
|
|
119
|
+
that hour, summed across the whole history. Only hours with at least one commit are contained in the
|
|
120
|
+
result."""
|
|
121
|
+
return self.__aggregate(repository_path, lambda moment: f"{moment.hour:02d}")
|