Skip to content

Commit 7bb6d87

Browse files
authored
feat: --validate-output-json to output validation result as JSON (#536)
1 parent a3536ce commit 7bb6d87

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Options:
191191
--cache-page-folder TEXT Folder used to cache rendered pages.
192192
--validate-all Validate all mediawiki files and report all
193193
errors
194+
--validate-output-json Report validation result as JSON
194195
-h, --help Show this message and exit.
195196
```
196197

truewiki/__main__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import click
3+
import json
34
import logging
45
import os
56
import re
@@ -159,7 +160,8 @@ async def wait_for_storage():
159160
@click_user_microsoft
160161
@click_page
161162
@click.option("--validate-all", help="Validate all mediawiki files and report all errors", is_flag=True)
162-
def main(bind, port, storage, frontend_url, cache_time, remote_ip_header, validate_all):
163+
@click.option("--validate-output-json", help="Report validation result as JSON", is_flag=True)
164+
def main(bind, port, storage, frontend_url, cache_time, remote_ip_header, validate_all, validate_output_json):
163165
if frontend_url and frontend_url.endswith("/"):
164166
frontend_url = frontend_url[:-1]
165167
singleton.FRONTEND_URL = frontend_url
@@ -178,7 +180,13 @@ def main(bind, port, storage, frontend_url, cache_time, remote_ip_header, valida
178180
config.load()
179181

180182
if validate_all:
181-
validate.all()
183+
log.info("Validating all mediawiki files ..")
184+
185+
errors = {} if validate_output_json else None
186+
validate.all(errors)
187+
188+
if errors is not None:
189+
print(json.dumps(errors, indent=4))
182190
return
183191

184192
webapp = web.Application(client_max_size=MAX_UPLOAD_SIZE, middlewares=[remove_cookie_middleware])

truewiki/validate.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from .wiki_page import WikiPage
66

77

8-
def validate_folder(folder, ignore_index):
8+
def validate_folder(folder, ignore_index, errors):
99
for item in sorted(glob.glob(f"{folder}/*")):
1010
if os.path.isdir(item):
11-
validate_folder(item, ignore_index)
11+
validate_folder(item, ignore_index, errors)
1212
continue
1313

1414
if not item.endswith(".mediawiki"):
@@ -21,15 +21,21 @@ def validate_folder(folder, ignore_index):
2121
try:
2222
wiki_page = WikiPage(item).render()
2323
except Exception as e:
24-
print(f"{item}:")
25-
print(" - EXCEPTION: ", e)
24+
if errors is None:
25+
print(f"{item}:")
26+
print(" - EXCEPTION: ", e)
27+
else:
28+
errors[item] = {"exception": str(e)}
2629
continue
2730

2831
if wiki_page.errors:
29-
print(f"{item}:")
30-
for error in wiki_page.errors:
31-
print(f" - {error}")
32+
if errors is None:
33+
print(f"{item}:")
34+
for error in wiki_page.errors:
35+
print(f" - {error}")
36+
else:
37+
errors[item] = {"errors": wiki_page.errors}
3238

3339

34-
def all():
35-
validate_folder(singleton.STORAGE.folder, len(singleton.STORAGE.folder) + 1)
40+
def all(errors):
41+
validate_folder(singleton.STORAGE.folder, len(singleton.STORAGE.folder) + 1, errors)

0 commit comments

Comments
 (0)