# Path: augur/__version__.py
# def is_augur_version_compatible(version):
# 
# Path: augur/__version__.py
# def is_augur_version_compatible(version):
#     """
#     Checks if the provided **version** is the same major version
#     as the currently running version of augur.
# 
#     Parameters
#     ----------
#     version : str
#         version to check against the current version
# 
#     Returns
#     -------
#     Bool
# 
#     """
#     import packaging.version
# 
#     current_version = packaging.version.parse(__version__)
#     this_version = packaging.version.parse(version)
#     return this_version.release[0] == current_version.release[0]
# 
# Path: augur/validate.py
# def validate_json(jsonToValidate, schema, filename):
#     print("Validating schema of {!r}...".format(filename))
#     try:
#         schema.validate(jsonToValidate) # https://python-jsonschema.readthedocs.io/en/latest/validate/
#     except jsonschema.exceptions.ValidationError:
#         for error in sorted(schema.iter_errors(jsonToValidate), key=str):
#             trace = list(error.schema_path) # this may be really long for nested tree structures
#             if len(trace) > 6:
#                 trace = ["..."] + trace[-5:]
#             trace = [str(x) for x in trace]
#             print("\tERROR: {}. Trace: {}".format(error.message, " - ".join(trace)), file=sys.stderr)
#         raise ValidateError("Validation of {!r} failed.".format(filename))
# 
# class ValidateError(Exception):
#     pass
# 
# def load_json_schema(path):
#     '''
#     Load a JSON schema from the augur included set of schemas
#     (located in augur/data)
#     '''
#     try:
#         schema = json.loads(resource_string(__package__, os.path.join("data", path)))
#     except json.JSONDecodeError as err:
#         raise ValidateError("Schema {} is not a valid JSON file. Error: {}".format(path, err))
#     # check loaded schema is itself valid -- see http://python-jsonschema.readthedocs.io/en/latest/errors/
#     Validator = jsonschema.validators.validator_for(schema)
#     try:
#         Validator.check_schema(schema)
#     except jsonschema.exceptions.SchemaError as err:
#         raise ValidateError(f"Schema {path} is not a valid JSON Schema ({Validator.META_SCHEMA['$schema']}). Error: {err}")
#     return Validator(schema)


import json
from augur.__version__ import __version__
from augur.__version__ import is_augur_version_compatible
from augur.validate import validate_json, ValidateError, load_json_schema


class NodeDataFile:
    def __init__(self, fname):
        self.fname = fname

        with open(fname, encoding="utf-8") as jfile:
            self.attrs = json.load(jfile)

        self.validate()

    @property
    def annotations(self):
        return self.attrs.get("annotations")

    @property
    def nodes(self):
        return self.attrs.get("nodes")

    @property
    def generated_by(self):
        return self.attrs.get("generated_by")

    @property
    def is_generated_by_incompatible_augur(self):
        if not isinstance(self.generated_by, dict):
            # If it's not a dict created by augur, we can't reliably classify it as incompatible
            return False

        generated_by_augur = self.generated_by.get("program") == "augur"

# Path: augur/__version__.py
# def is_augur_version_compatible(version):
# 
# Path: augur/__version__.py
# def is_augur_version_compatible(version):
#     """
#     Checks if the provided **version** is the same major version
#     as the currently running version of augur.
# 
#     Parameters
#     ----------
#     version : str
#         version to check against the current version
# 
#     Returns
#     -------
#     Bool
# 
#     """
#     import packaging.version
# 
#     current_version = packaging.version.parse(__version__)
#     this_version = packaging.version.parse(version)
#     return this_version.release[0] == current_version.release[0]
# 
# Path: augur/validate.py
# def validate_json(jsonToValidate, schema, filename):
#     print("Validating schema of {!r}...".format(filename))
#     try:
#         schema.validate(jsonToValidate) # https://python-jsonschema.readthedocs.io/en/latest/validate/
#     except jsonschema.exceptions.ValidationError:
#         for error in sorted(schema.iter_errors(jsonToValidate), key=str):
#             trace = list(error.schema_path) # this may be really long for nested tree structures
#             if len(trace) > 6:
#                 trace = ["..."] + trace[-5:]
#             trace = [str(x) for x in trace]
#             print("\tERROR: {}. Trace: {}".format(error.message, " - ".join(trace)), file=sys.stderr)
#         raise ValidateError("Validation of {!r} failed.".format(filename))
# 
# class ValidateError(Exception):
#     pass
# 
# def load_json_schema(path):
#     '''
#     Load a JSON schema from the augur included set of schemas
#     (located in augur/data)
#     '''
#     try:
#         schema = json.loads(resource_string(__package__, os.path.join("data", path)))
#     except json.JSONDecodeError as err:
#         raise ValidateError("Schema {} is not a valid JSON file. Error: {}".format(path, err))
#     # check loaded schema is itself valid -- see http://python-jsonschema.readthedocs.io/en/latest/errors/
#     Validator = jsonschema.validators.validator_for(schema)
#     try:
#         Validator.check_schema(schema)
#     except jsonschema.exceptions.SchemaError as err:
#         raise ValidateError(f"Schema {path} is not a valid JSON Schema ({Validator.META_SCHEMA['$schema']}). Error: {err}")
#     return Validator(schema)


# Path: augur/util_support/node_data_file.py
import json
from augur.__version__ import __version__
from augur.__version__ import is_augur_version_compatible
from augur.validate import validate_json, ValidateError, load_json_schema



class NodeDataFile:
    def __init__(self, fname):
        self.fname = fname

        with open(fname, encoding="utf-8") as jfile:
            self.attrs = json.load(jfile)

        self.validate()

    @property
    def annotations(self):
        return self.attrs.get("annotations")

    @property
    def nodes(self):
        return self.attrs.get("nodes")

    @property
    def generated_by(self):
        return self.attrs.get("generated_by")

    @property
    def is_generated_by_incompatible_augur(self):
        if not isinstance(self.generated_by, dict):
            # If it's not a dict created by augur, we can't reliably classify it as incompatible
            return False

        generated_by_augur = self.generated_by.get("program") == "augur"

        compatible_version = is_augur_version_compatible(