Skip to content

Commit 9ee0b38

Browse files
committed
Add support for license-text, license-text-diagnostics, summary, and license-clarity-score in HTML output
The HTML output now supports --license-text, --license-text-diagnostics, --summary,--license-clarity-score Signed-off-by: uttam282005 <uttam282005@gmail.com>
1 parent b2c4bd0 commit 9ee0b38

2 files changed

Lines changed: 136 additions & 7 deletions

File tree

src/formattedcode/output_html.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
TEMPLATES_DIR = join(dirname(__file__), 'templates')
4444

4545

46-
TRACE = False
46+
TRACE = True
4747

4848

4949
def logger_debug(*args):
@@ -83,12 +83,16 @@ def process_codebase(self, codebase, html, **kwargs):
8383
license_references = []
8484
if hasattr(codebase.attributes, 'license_references'):
8585
license_references = codebase.attributes.license_references
86+
summary = None
87+
if hasattr(codebase.attributes, 'summary'):
88+
summary = codebase.attributes.summary
8689
template_loc = join(TEMPLATES_DIR, 'html', 'template.html')
8790
output_file = html
8891
write_templated(
8992
output_file=output_file,
9093
results=results,
9194
license_references=license_references,
95+
summary=summary,
9296
version=version,
9397
template_loc=template_loc,
9498
)
@@ -133,18 +137,22 @@ def process_codebase(self, codebase, custom_output, custom_template, **kwargs):
133137
license_references = []
134138
if hasattr(codebase.attributes, 'license_references'):
135139
license_references = codebase.attributes.license_references
140+
summary = None
141+
if hasattr(codebase.attributes, 'summary'):
142+
summary = codebase.attributes.summary
136143
template_loc = custom_template
137144
output_file = custom_output
138145
write_templated(
139146
output_file=output_file,
140147
results=results,
141148
license_references=license_references,
149+
summary=summary,
142150
version=version,
143151
template_loc=template_loc
144152
)
145153

146154

147-
def write_templated(output_file, results, license_references, version, template_loc):
155+
def write_templated(output_file, results, license_references, summary, version, template_loc):
148156
"""
149157
Write scan output `results` to the `output_file` opened file using a template
150158
file at `template_loc`.
@@ -155,6 +163,7 @@ def write_templated(output_file, results, license_references, version, template_
155163
for template_chunk in generate_output(
156164
results=results,
157165
license_references=license_references,
166+
summary=summary,
158167
version=version,
159168
template=template,
160169
):
@@ -184,7 +193,7 @@ def get_template(location):
184193
return env.get_template(template_name)
185194

186195

187-
def generate_output(results, license_references, version, template):
196+
def generate_output(results, license_references, summary, version, template):
188197
"""
189198
Yield unicode strings from incrementally rendering `results` and `version`
190199
with the Jinja `template` object.
@@ -197,11 +206,12 @@ def generate_output(results, license_references, version, template):
197206
converted = {}
198207
converted_infos = {}
199208
converted_packages = {}
200-
licenses = {}
209+
licenses = {}
201210

202211
LICENSES = 'license_detections'
203212
COPYRIGHTS = 'copyrights'
204213
PACKAGES = 'package_data'
214+
logger_debug(f"summary: {summary}")
205215

206216
# Create a flattened data dict keyed by path
207217
for scanned_file in results:
@@ -223,12 +233,21 @@ def generate_output(results, license_references, version, template):
223233
if TRACE:
224234
logger_debug(f"match: {match}")
225235
license_expression = match['license_expression']
226-
results.append({
236+
match_data = {
227237
'start': match['start_line'],
228238
'end': match['end_line'],
229239
'what': 'license',
230240
'value': license_expression,
231-
})
241+
}
242+
243+
if 'matched_text' in match:
244+
match_data['matched_text'] = match['matched_text']
245+
246+
if 'matched_text_diagnostics' in match:
247+
logger_debug(match['matched_text_diagnostics'])
248+
match_data['matched_text_diagnostics'] = match['matched_text_diagnostics']
249+
250+
results.append(match_data)
232251

233252
if not license_references and license_expression not in licenses:
234253
license_object = get_licenses_db().get(license_expression)
@@ -261,7 +280,7 @@ def generate_output(results, license_references, version, template):
261280
'package_data': converted_packages
262281
}
263282

264-
return template.generate(files=files, license_references=license_references, version=version)
283+
return template.generate(files=files, license_references=license_references, summary=summary, version=version)
265284

266285

267286
@output_impl

src/formattedcode/templates/html/template.html

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,101 @@
4646
font-weight: normal;
4747
font-size: 12px;
4848
}
49+
details {
50+
margin: 5px 0;
51+
}
52+
summary {
53+
cursor: pointer;
54+
color: #5E81B7;
55+
font-weight: bold;
56+
}
57+
summary:hover {
58+
text-decoration: underline;
59+
}
60+
pre {
61+
background: #f5f5f5;
62+
padding: 10px;
63+
border: 1px solid #ddd;
64+
overflow-x: auto;
65+
font-family: monospace;
66+
font-size: 11px;
67+
white-space: pre-wrap;
68+
word-wrap: break-word;
69+
}
70+
.summary-section {
71+
background: #f0f8ff;
72+
padding: 15px;
73+
margin-bottom: 20px;
74+
border-left: 4px solid #5E81B7;
75+
}
76+
.summary-section h3 {
77+
margin-top: 0;
78+
color: #5E81B7;
79+
font-size: 16px;
80+
}
81+
.summary-section p {
82+
margin: 5px 0;
83+
}
4984
</style>
5085
</head>
5186
<body>
87+
{% if summary %}
88+
<div class="summary-section">
89+
<h3>Scan Summary</h3>
90+
{% if summary.declared_license_expression %}
91+
<p><strong>Declared License:</strong> {{ summary.declared_license_expression }}</p>
92+
{% endif %}
93+
{% if summary.declared_holder %}
94+
<p><strong>Declared Holder:</strong> {{ summary.declared_holder }}</p>
95+
{% endif %}
96+
{% if summary.primary_language %}
97+
<p><strong>Primary Language:</strong> {{ summary.primary_language }}</p>
98+
{% endif %}
99+
{% if summary.other_license_expressions %}
100+
<p><strong>Other License Expressions:</strong></p>
101+
<ul>
102+
{% for expr in summary.other_license_expressions %}
103+
<li>{{ expr.value }} (count: {{ expr.count }})</li>
104+
{% endfor %}
105+
</ul>
106+
{% endif %}
107+
{% if summary.other_holders %}
108+
<p><strong>Other Holders:</strong></p>
109+
<ul>
110+
{% for holder in summary.other_holders %}
111+
<li>{{ holder }}</li>
112+
{% endfor %}
113+
</ul>
114+
{% endif %}
115+
</div>
116+
{% endif %}
117+
{% if summary and summary.license_clarity_score %}
118+
<table>
119+
<caption>License Clarity Score</caption>
120+
<thead>
121+
<tr>
122+
<th>Overall Score</th>
123+
<th>Declared License</th>
124+
<th>Identification Precision</th>
125+
<th>Has License Text</th>
126+
<th>Declared Copyrights</th>
127+
<th>Ambiguous Compound</th>
128+
<th>Conflicting Categories</th>
129+
</tr>
130+
</thead>
131+
<tbody>
132+
<tr>
133+
<td><strong>{{ summary.license_clarity_score.score }}</strong></td>
134+
<td>{{ summary.license_clarity_score.declared_license }}</td>
135+
<td>{{ summary.license_clarity_score.identification_precision }}</td>
136+
<td>{{ summary.license_clarity_score.has_license_text }}</td>
137+
<td>{{ summary.license_clarity_score.declared_copyrights }}</td>
138+
<td>{{ summary.license_clarity_score.ambiguous_compound_licensing }}</td>
139+
<td>{{ summary.license_clarity_score.conflicting_license_categories }}</td>
140+
</tr>
141+
</tbody>
142+
</table>
143+
{% endif %}
52144
{% if files.license_copyright %}
53145
<table>
54146
<caption>Copyrights and Licenses Information</caption>
@@ -59,6 +151,7 @@
59151
<th>end</th>
60152
<th>what</th>
61153
<th>value</th>
154+
<th>matched text/ diagnostics</th>
62155
</tr>
63156
</thead>
64157
<tbody>
@@ -71,6 +164,23 @@
71164
<td>{{ row.what }}</td>
72165
{% if row.what == 'license' %}
73166
<td><a href="#license_{{ row.value }}">{{ row.value }}</a></td>
167+
<td>
168+
{% if row.matched_text %}
169+
<details>
170+
<summary>View Matched Text</summary>
171+
<pre>{{ row.matched_text|escape }}</pre>
172+
</details>
173+
{% endif %}
174+
{% if row.matched_text_diagnostics %}
175+
<details>
176+
<summary>View Diagnostics</summary>
177+
<pre>{{ row.matched_text_diagnostics|escape }}</pre>
178+
</details>
179+
{% endif %}
180+
{% if not row.matched_text and not row.matched_text_diagnostics %}
181+
-
182+
{% endif %}
183+
</td>
74184
{% else %}
75185
<td>{{ row.value|escape }}</td>
76186
{% endif %}

0 commit comments

Comments
 (0)