Skip to content

YamlFile

YamlFile

YamlFile(filespec)

Bases: object

Config file in YAML format.

Parameters:

Name Type Description Default
filespec str

The filespec of the YAML file to read

required

Returns:

Type Description
YamlFile

An instance of this object

Source code in osm_merge/yamlfile.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def __init__(
    self,
    filespec: str,
):
    """This parses a yaml file into a dictionary for easy access.

    Args:
        filespec (str): The filespec of the YAML file to read

    Returns:
        (YamlFile): An instance of this object
    """
    self.filespec = None
    # if data == str:
    self.filespec = filespec
    self.file = open(filespec, "rb").read()
    self.yaml = yaml.load(self.file, Loader=yaml.Loader)
    self.data = dict()

get

get(keyword, tag=None)

Get the values for a top level keyword Args: keyword (str): The top level keyword to get the values for tag (str): The category for the tag/values

Returns:

Type Description
dict

The values for the top level keyword

Source code in osm_merge/yamlfile.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def get(self,
        keyword: str,
        tag: str = None,
        ):
    """
    Get the values for a top level keyword\

    Args:
        keyword (str): The top level keyword to get the values for
        tag (str): The category for the tag/values

    Returns:
        (dict): The values for the top level keyword
    """
    return self.yaml[keyword][tag]

getEntries

getEntries()

Convert the list from the YAML file into a searchable data structure

Returns:

Type Description
dict

The parsed config file

Source code in osm_merge/yamlfile.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def getEntries(self):
    """
    Convert the list from the YAML file into a searchable data structure

    Returns:
        (dict): The parsed config file
    """
    columns = list()
    for entry in self.yaml:
        for key, values in entry.items():
            self.data[key] = dict()
            # values is a list of dicts which are tag/value pairs
            for item in values:
                [[k, v]] = item.items()
                if type(v) == str:
                    self.data[key][k] = v
                elif type(v) == float or type(v) == int:
                    self.data[key][k] = str(v)
                elif type(v) == list:
                    self.data[key][k] = dict()
                    for newval in v:
                        if newval is None:
                            continue
                        if type(newval) == dict:
                            [[k2, v2]] = newval.items()
                            if type(v2) == str:
                                self.data[key][k].update(newval)
                            elif type(v2) == list:
                                self.data[key][k][k2] = dict()
                                for xxx in v2:
                                    [[k3, v3]] = xxx.items()
                                    if type(v3) == list:
                                        self.data[key][k][k2][k3] = dict()
                                        for foo in v3:
                                            self.data[key][k][k2][k3].update(foo)
                                        pass
                                    else:
                                        self.data[key][k][k2].update(xxx)
                else:
                    log.error(f"{type(v)} is not suported.")

    # breakpoint()
    return self.data

dump

dump()

Dump internal data structures, for debugging purposes only.

Source code in osm_merge/yamlfile.py
111
112
113
114
115
116
117
118
119
def dump(self):
    """Dump internal data structures, for debugging purposes only."""
    if self.filespec:
        print("YAML file: %s" % self.filespec)
    for key, values in self.data.items():
        print(f"Key is: {key}")
        for k, v in values.items():
            print(f"\t{k} = {v}")
        print("------------------")

options: show_source: false heading_level: 3