Vérifie le schéma de traductions.json

Cela ajoute deux vérifications:

  - que les données ont le bon format (anglais, français, classe - genre est
    optionnel)
  - que les données sont triées par "anglais"
This commit is contained in:
Etienne Millon
2015-12-13 15:03:20 +01:00
parent 159cb914c7
commit ba0ca3e4fb
3 changed files with 62 additions and 0 deletions

1
besoins.txt Normal file
View File

@@ -0,0 +1 @@
jsonschema==2.5.1

37
schema.json Normal file
View File

@@ -0,0 +1,37 @@
{ "type": "object",
"properties": {
"faux mots": {
"type": "array",
"items": {"$ref": "#/definitions/mot"}
},
"vrais mots": {
"type": "array",
"items": {"$ref": "#/definitions/mot"}
}
},
"definitions": {
"mot": {
"type": "object",
"properties":
{
"anglais": {"type": "string"},
"francais": {"type": "string"},
"classe": {
"type": "string",
"enum": [
"groupe nominal",
"groupe verbal",
"proposition",
"verbe"
]
},
"genre": {
"type": "string",
"enum": ["f", "m"]
},
"pluriel": {"type": "boolean"}
},
"required": ["anglais", "francais", "classe"]
}
}
}

24
verifie.py Normal file
View File

@@ -0,0 +1,24 @@
import json
from jsonschema import validate as valide
def verifie_ordre(mots):
for (a, b) in zip(mots, mots[1:]):
mot_a = a['anglais'].lower()
mot_b = b['anglais'].lower()
msg = "%s et %s ne sont pas dans le bon ordre" % (mot_a, mot_b)
assert mot_a < mot_b, msg
def principal():
with open('traductions.json') as f:
d = json.load(f)
with open('schema.json') as f:
schema = json.load(f)
valide(d, schema)
verifie_ordre(d['faux mots'])
verifie_ordre(d['vrais mots'])
if __name__ == '__main__':
principal()