Skip to content

dataherb.parse.model_json

parse.model_json¤

dataherb.parse.model_json is the json based metadata engine.

MetaData ¤

JSON metadata object

Parameters:

Name Type Description Default
folder Path

path to the dataherb folder

required
Source code in dataherb/parse/model_json.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class MetaData:
    """
    JSON metadata object

    :param folder: path to the dataherb folder
    """

    def __init__(self, folder: Path):
        self.dataherb_folder = folder
        self.metadata_file = "dataherb.json"
        self.metadata: dict = {}

    def load(self) -> dict:
        """load the existing datapackage file"""
        metadata_full_path = self.dataherb_folder / self.metadata_file
        logger.debug(f"Load metadata from file: {metadata_full_path}")
        with open(metadata_full_path, "r") as fp:
            self.metadata = json.load(fp)

        logger.debug(f"Loaded metadata: {self.metadata}")

        return self.metadata

    def create(self, overwrite: bool = False) -> None:
        """creates .dataherb folder"""

        try:
            self.dataherb_folder.mkdir(parents=True, exist_ok=False)
            logger.info("Created ", self.dataherb_folder)
        except FileExistsError:
            logger.warning(
                f"{self.dataherb_folder} already exists! Will use the folder."
                "Pass the flag `overwrite=True` to recreate it, if one desires."
            )

        metadata_full_path = self.dataherb_folder / self.metadata_file

        if metadata_full_path.is_file():
            if not overwrite:
                raise FileExistsError(f"File {metadata_full_path} already exists!")
            else:
                logger.warning(f"Will overwrite {metadata_full_path}")

        with open(metadata_full_path, "w") as fp:
            json.dump(self.metadata, fp, indent=4)

        logger.debug(f"written to {metadata_full_path}")

    def validate(self) -> None:
        """validate the existing metadata file"""

        metadata_full_path = self.dataherb_folder / self.metadata_file

        self._validate_paths()

        with open(metadata_full_path, "r") as fp:
            metadata = json.load(fp)
        logger.info("loaded metadata ", self.dataherb_folder)
        logger.debug(f"loaded metadata {metadata}")

    def _validate_paths(self) -> None:
        """Check if the metadata path exists"""

        metadata_full_path = self.dataherb_folder / self.metadata_file

        if not self.dataherb_folder.exists():
            raise Exception(f"Path {self.dataherb_folder} doesn't exist!")
        else:
            logger.info(f"Path {self.dataherb_folder} exists.")

        if not metadata_full_path.is_file():
            raise FileNotFoundError(f"File {metadata_full_path} doesn't exist!")
        else:
            logger.info(f"File {metadata_full_path} exists!")

_validate_paths() ¤

Check if the metadata path exists

Source code in dataherb/parse/model_json.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def _validate_paths(self) -> None:
    """Check if the metadata path exists"""

    metadata_full_path = self.dataherb_folder / self.metadata_file

    if not self.dataherb_folder.exists():
        raise Exception(f"Path {self.dataherb_folder} doesn't exist!")
    else:
        logger.info(f"Path {self.dataherb_folder} exists.")

    if not metadata_full_path.is_file():
        raise FileNotFoundError(f"File {metadata_full_path} doesn't exist!")
    else:
        logger.info(f"File {metadata_full_path} exists!")

create(overwrite=False) ¤

creates .dataherb folder

Source code in dataherb/parse/model_json.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def create(self, overwrite: bool = False) -> None:
    """creates .dataherb folder"""

    try:
        self.dataherb_folder.mkdir(parents=True, exist_ok=False)
        logger.info("Created ", self.dataherb_folder)
    except FileExistsError:
        logger.warning(
            f"{self.dataherb_folder} already exists! Will use the folder."
            "Pass the flag `overwrite=True` to recreate it, if one desires."
        )

    metadata_full_path = self.dataherb_folder / self.metadata_file

    if metadata_full_path.is_file():
        if not overwrite:
            raise FileExistsError(f"File {metadata_full_path} already exists!")
        else:
            logger.warning(f"Will overwrite {metadata_full_path}")

    with open(metadata_full_path, "w") as fp:
        json.dump(self.metadata, fp, indent=4)

    logger.debug(f"written to {metadata_full_path}")

load() ¤

load the existing datapackage file

Source code in dataherb/parse/model_json.py
23
24
25
26
27
28
29
30
31
32
def load(self) -> dict:
    """load the existing datapackage file"""
    metadata_full_path = self.dataherb_folder / self.metadata_file
    logger.debug(f"Load metadata from file: {metadata_full_path}")
    with open(metadata_full_path, "r") as fp:
        self.metadata = json.load(fp)

    logger.debug(f"Loaded metadata: {self.metadata}")

    return self.metadata

validate() ¤

validate the existing metadata file

Source code in dataherb/parse/model_json.py
59
60
61
62
63
64
65
66
67
68
69
def validate(self) -> None:
    """validate the existing metadata file"""

    metadata_full_path = self.dataherb_folder / self.metadata_file

    self._validate_paths()

    with open(metadata_full_path, "r") as fp:
        metadata = json.load(fp)
    logger.info("loaded metadata ", self.dataherb_folder)
    logger.debug(f"loaded metadata {metadata}")