示例#1
0
# determine current state
status = safefile.safeGetState (dataFile);
if status == safefile.SAFE_INTERVENE:
    print ("Inventory file requires administrator action")
    sys.exit (1)
elif status == safefile.DOES_NOT_EXIST:
    print ("Inventory file missing")
    sys.exit (1)
elif status == safefile.SAFE_RECOVERABLE:
    safefile.safeRecover (dataFile)
    print ("Inventory file auto recovered")

# load and validate file content
inventory = None;
code, inventory, message = validate (dataFile, schema, None, None)
# if invalid, print error message
if code != VALID:
    print ("Inventory file validation failed")
    print ("Error: " + message)
    sys.exit (1)

# program content goes here ...
# to show updates, increment item count by 1 for all items
for item in inventory:
    item['count'] = item['count'] + 1

# apply formatting to content before writing when
# content is intended to be user readable/editable
output = dumps (inventory, indent = 2, sort_keys = True);
 def test_invalid_with_ref (self):
     code, data, message = validate ("test/invalid2.json", "test/schema2.json", ["test/ref2.json"], None)
     assert code == VALIDATION_ERROR
 def test_invalid_with_3_level_jsdb (self):
     code, data, message = validate ("test/invalid4.json", "test/schema4.json", None, "test/jsdb4.json")
     assert code == VALIDATION_ERROR
 def test_valid_with_jsdb (self):
     code, data, message = validate ("test/valid3.json", "test/schema3.json", None, "test/jsdb3.json")
     assert code == VALID
 def test_invalid (self):
     code, data, message = validate ("test/invalid1.json", "test/schema1.json", None, None)
     assert code == VALIDATION_ERROR
 def test_valid (self):
     code, data, message = validate ("test/valid1.json", "test/schema1.json", None, None)
     assert code == VALID
 def test_missing_id (self):
     code, data, message = validate ("test/valid1.json", "test/schema1.json", ["test/valid1.json"], None)
     assert code == MISSING_ID
 def test_invalid_json_jsdb (self):
     code, data, message = validate ("test/valid1.json", "test/schema1.json", None, "test/invalid.json")
     assert code == INVALID_JSON
 def test_invalid_name_schema (self):
     code, data, message = validate ("test/valid1.json", None, None, None)
     assert code == INVALID_NAME
 def test_does_not_exist_jsdb (self):
     code, data, message = validate ("test/valid1.json", "test/schema1.json", None, "test/nofile.json")
     assert code == DOES_NOT_EXIST