from pathlib import Path
import plecos
import json
print(plecos.__version__)
#%%
path_to_json_local = Path("~/ocn/plecos/plecos/samples/sample_metadata_local.json").expanduser()
path_to_json_remote = Path("~/ocn/plecos/plecos/samples/sample_metadata_remote.json").expanduser()
path_to_broken_json = Path("~/ocn/plecos/plecos/samples/metadata_local_broken.json").expanduser()
path_to_schema_local = Path("~/ocn/plecos/plecos/schemas/metadata_local_v0_3.json").expanduser()
path_to_schema_remote = Path("~/ocn/plecos/plecos/schemas/metadata_remote_v0_3.json").expanduser()

# Select remote or local metadata
LOCAL=True

if LOCAL:
    path_json_file = path_to_json_local
    path_schema_file = path_to_schema_local

    with open(path_to_json_local) as f:
        json_dict = json.load(f)

else:
    path_json_file = path_to_json_remote
    path_schema_file = path_to_schema_remote

    with open(path_to_json_remote) as f:
        json_dict = json.load(f)

print("Json file:", path_json_file)
print("Schema file:", path_schema_file)


#%%


del json_dict['base']['files'][0]['index']
# del json_dict['base']['files'][0]['url']


# json_dict['base']['extra'] = 1
plecos.is_valid_dict(json_dict)

# json_dict['base']['files'][0]['url']

# json_dict['base']['EXTRA ATTRIB!'] = 0
# json_dict['base']['files'][0]['EXTRA_ATTR'] = "????"
# json_dict['base']['price'] = "A string is not allowed!"
errors = plecos.list_errors(json_dict, path_schema_file)

if errors:
    print("ERRORS:")
    for e in errors:
        print(e)
else:
    print("No errors")

raise
#%%


json_dict = {
  "base": {
    "name": "10 Monkey Species Small",
    "author": "Mario",
    "license": "CC0: Public Domain",
    "contentType": "jpg/txt",
    "price": 5,
    "categories": [
      "image"
    ],
    "tags": [
      "image data",
      " animals"
    ],
    "type": "dataset",
    "description": "Example description",
    "copyrightHolder": "",
    "encoding": "",
    "compression": "",
    "workExample": "",
    "inLanguage": "en",
    "files": [
      {
        "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/training.zip"
      },
      {
        "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/monkey_labels.txt"
      },
      {
        "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/validation.zip"
      }
    ],
    "links": [
      {
        "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/links/sample/sample.zip",
        "name": "sample.zip",
        "type": "sample"
      },
      {
        "url": "https://github.com/slothkong/CNN_classification_10_monkey_species",
        "name": "example code",
        "type": "example code"
      },
      {
        "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/links/discovery/n5151.jpg",
        "name": "n5151.jpg",
        "type": "discovery"
      }
    ],
    "checksum": "0",
  },

}

#%%
path_to_schema_local = Path("~/ocn/Plecos/plecos/schemas/metadata_local_190305.json").expanduser()
errors = plecos.list_errors(json_dict, path_to_schema_local)

if errors:
    print("ERRORS:")
    for e in errors:
        print(e)
else:
    print("No errors")
[((41, 0, 41, 31), 'plecos.is_valid_dict', 'plecos.is_valid_dict', ({(41, 21, 41, 30): 'json_dict'}, {}), '(json_dict)', False, 'import plecos\n'), ((48, 9, 48, 56), 'plecos.list_errors', 'plecos.list_errors', ({(48, 28, 48, 37): 'json_dict', (48, 39, 48, 55): 'path_schema_file'}, {}), '(json_dict, path_schema_file)', False, 'import plecos\n'), ((117, 9, 117, 60), 'plecos.list_errors', 'plecos.list_errors', ({(117, 28, 117, 37): 'json_dict', (117, 39, 117, 59): 'path_to_schema_local'}, {}), '(json_dict, path_to_schema_local)', False, 'import plecos\n'), ((6, 21, 6, 83), 'pathlib.Path', 'Path', ({(6, 26, 6, 82): '"""~/ocn/plecos/plecos/samples/sample_metadata_local.json"""'}, {}), "('~/ocn/plecos/plecos/samples/sample_metadata_local.json')", False, 'from pathlib import Path\n'), ((7, 22, 7, 85), 'pathlib.Path', 'Path', ({(7, 27, 7, 84): '"""~/ocn/plecos/plecos/samples/sample_metadata_remote.json"""'}, {}), "('~/ocn/plecos/plecos/samples/sample_metadata_remote.json')", False, 'from pathlib import Path\n'), ((8, 22, 8, 84), 'pathlib.Path', 'Path', ({(8, 27, 8, 83): '"""~/ocn/plecos/plecos/samples/metadata_local_broken.json"""'}, {}), "('~/ocn/plecos/plecos/samples/metadata_local_broken.json')", False, 'from pathlib import Path\n'), ((9, 23, 9, 83), 'pathlib.Path', 'Path', ({(9, 28, 9, 82): '"""~/ocn/plecos/plecos/schemas/metadata_local_v0_3.json"""'}, {}), "('~/ocn/plecos/plecos/schemas/metadata_local_v0_3.json')", False, 'from pathlib import Path\n'), ((10, 24, 10, 85), 'pathlib.Path', 'Path', ({(10, 29, 10, 84): '"""~/ocn/plecos/plecos/schemas/metadata_remote_v0_3.json"""'}, {}), "('~/ocn/plecos/plecos/schemas/metadata_remote_v0_3.json')", False, 'from pathlib import Path\n'), ((20, 20, 20, 32), 'json.load', 'json.load', ({(20, 30, 20, 31): 'f'}, {}), '(f)', False, 'import json\n'), ((27, 20, 27, 32), 'json.load', 'json.load', ({(27, 30, 27, 31): 'f'}, {}), '(f)', False, 'import json\n'), ((116, 23, 116, 85), 'pathlib.Path', 'Path', ({(116, 28, 116, 84): '"""~/ocn/Plecos/plecos/schemas/metadata_local_190305.json"""'}, {}), "('~/ocn/Plecos/plecos/schemas/metadata_local_190305.json')", False, 'from pathlib import Path\n')]