@@ -24,8 +24,10 @@ def __init__(self, language: str):
24
24
self ._documentation_count = 0
25
25
self ._empty_count = 0
26
26
self ._file_count = 0
27
+ self ._file_percentage = 0.0
27
28
self ._string_count = 0
28
29
self ._is_pseudo_language = _PSEUDO_LANGUAGE_REGEX .match (self .language ) is not None
30
+ self ._has_up_to_date_percentages = False
29
31
30
32
@property
31
33
def language (self ) -> str :
@@ -37,31 +39,70 @@ def code_count(self) -> int:
37
39
"""sum lines of code for this language"""
38
40
return self ._code_count
39
41
42
+ @property
43
+ def code_percentage (self ) -> float :
44
+ """percentage of lines containing code for this language across entire project"""
45
+ return _percentage_or_0 (self .code_count , self .line_count )
46
+
47
+ def _assert_has_up_to_date_percentages (self ):
48
+ assert self ._has_up_to_date_percentages , "update_percentages() must be called first"
49
+
40
50
@property
41
51
def documentation_count (self ) -> int :
42
52
"""sum lines of documentation for this language"""
43
53
return self ._documentation_count
44
54
55
+ @property
56
+ def documentation_percentage (self ) -> float :
57
+ """percentage of lines containing documentation for this language across entire project"""
58
+ return _percentage_or_0 (self .documentation_count , self .line_count )
59
+
45
60
@property
46
61
def empty_count (self ) -> int :
47
62
"""sum empty lines for this language"""
48
63
return self ._empty_count
49
64
65
+ @property
66
+ def empty_percentage (self ) -> float :
67
+ """percentage of empty lines for this language across entire project"""
68
+ return _percentage_or_0 (self .empty_count , self .line_count )
69
+
50
70
@property
51
71
def file_count (self ) -> int :
52
72
"""number of source code files for this language"""
53
73
return self ._file_count
54
74
75
+ @property
76
+ def file_percentage (self ) -> float :
77
+ """percentage of files in project"""
78
+ self ._assert_has_up_to_date_percentages ()
79
+ return self ._file_percentage
80
+
81
+ @property
82
+ def line_count (self ) -> int :
83
+ """sum count of all lines of any kind for this language"""
84
+ return self .code_count + self .documentation_count + self .empty_count + self .string_count
85
+
55
86
@property
56
87
def string_count (self ) -> int :
57
- """sum number of lines containing only strings for this language"""
88
+ """sum number of lines containing strings for this language"""
58
89
return self ._string_count
59
90
91
+ @property
92
+ def string_percentage (self ) -> float :
93
+ """percentage of lines containing strings for this language across entire project"""
94
+ return _percentage_or_0 (self .string_count , self .line_count )
95
+
60
96
@property
61
97
def source_count (self ) -> int :
62
98
"""sum number of source lines of code"""
63
99
return self .code_count + self .string_count
64
100
101
+ @property
102
+ def source_percentage (self ) -> float :
103
+ """percentage of source lines for code for this language across entire project"""
104
+ return _percentage_or_0 (self .source_count , self .line_count )
105
+
65
106
@property
66
107
def is_pseudo_language (self ) -> bool :
67
108
"""``True`` if the language is not a real programming language"""
@@ -84,13 +125,18 @@ def add(self, source_analysis: SourceAnalysis) -> None:
84
125
assert source_analysis is not None
85
126
assert source_analysis .language == self .language
86
127
128
+ self ._has_up_to_date_percentages = False
87
129
self ._file_count += 1
88
130
if source_analysis .is_countable :
89
131
self ._code_count += source_analysis .code_count
90
132
self ._documentation_count += source_analysis .documentation_count
91
133
self ._empty_count += source_analysis .empty_count
92
134
self ._string_count += source_analysis .string_count
93
135
136
+ def update_file_percentage (self , project_summary : "ProjectSummary" ):
137
+ self ._file_percentage = _percentage_or_0 (self .file_count , project_summary .total_file_count )
138
+ self ._has_up_to_date_percentages = True
139
+
94
140
def __repr__ (self ):
95
141
result = "{0}(language={1!r}, file_count={2}" .format (self .__class__ .__name__ , self .language , self .file_count )
96
142
if not self .is_pseudo_language :
@@ -101,6 +147,12 @@ def __repr__(self):
101
147
return result
102
148
103
149
150
+ def _percentage_or_0 (partial_count : int , total_count : int ) -> float :
151
+ assert partial_count >= 0
152
+ assert total_count >= 0
153
+ return 100 * partial_count / total_count if total_count != 0 else 0.0
154
+
155
+
104
156
class ProjectSummary :
105
157
"""
106
158
Summary of source code counts for several languages and files.
@@ -126,22 +178,42 @@ def language_to_language_summary_map(self) -> Dict[str, LanguageSummary]:
126
178
def total_code_count (self ) -> int :
127
179
return self ._total_code_count
128
180
181
+ @property
182
+ def total_code_percentage (self ) -> float :
183
+ return _percentage_or_0 (self .total_code_count , self .total_line_count )
184
+
129
185
@property
130
186
def total_documentation_count (self ) -> int :
131
187
return self ._total_documentation_count
132
188
189
+ @property
190
+ def total_documentation_percentage (self ) -> float :
191
+ return _percentage_or_0 (self .total_documentation_count , self .total_line_count )
192
+
133
193
@property
134
194
def total_empty_count (self ) -> int :
135
195
return self ._total_empty_count
136
196
197
+ @property
198
+ def total_empty_percentage (self ) -> float :
199
+ return _percentage_or_0 (self .total_empty_count , self .total_line_count )
200
+
137
201
@property
138
202
def total_string_count (self ) -> int :
139
203
return self ._total_string_count
140
204
205
+ @property
206
+ def total_string_percentage (self ) -> float :
207
+ return _percentage_or_0 (self .total_string_count , self .total_line_count )
208
+
141
209
@property
142
210
def total_source_count (self ) -> int :
143
211
return self .total_code_count + self .total_string_count
144
212
213
+ @property
214
+ def total_source_percentage (self ) -> float :
215
+ return _percentage_or_0 (self .total_source_count , self .total_line_count )
216
+
145
217
@property
146
218
def total_file_count (self ) -> int :
147
219
return self ._total_file_count
@@ -173,10 +245,15 @@ def add(self, source_analysis: SourceAnalysis) -> None:
173
245
)
174
246
self ._total_string_count += source_analysis .string_count
175
247
248
+ def update_file_percentages (self ) -> None :
249
+ """Update percentages for all languages part of the project."""
250
+ for language_summary in self ._language_to_language_summary_map .values ():
251
+ language_summary .update_file_percentage (self )
252
+
176
253
def __repr__ (self ):
177
- return "{0}(total_file_count={1}, total_line_count={2}, " "languages={3}" . format (
178
- self .__class__ .__name__ ,
179
- self .total_file_count ,
180
- self .total_line_count ,
181
- sorted (self .language_to_language_summary_map .keys ()),
254
+ return (
255
+ f" { self .__class__ .__name__ } ("
256
+ f"total_file_count= { self .total_file_count } , "
257
+ f"total_line_count= { self .total_line_count } , "
258
+ f"languages= { sorted (self .language_to_language_summary_map .keys ())} )"
182
259
)
0 commit comments