Skip to content

Commit

Permalink
Prevent double entries in the corresponding dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
SchoolGuy committed Jun 22, 2021
1 parent a543dbc commit 05e8f59
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion libcobblersignatures/models/osbreed.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class OsBreed:
described in the JSON specification.
"""

def __init__(self, name):
def __init__(self, name: str):
if not isinstance(name, str):
raise TypeError("The name of an OsBreed must be of type str!")
self._name = name
self._osversions = collections.OrderedDict()

Expand Down Expand Up @@ -112,6 +114,8 @@ def osversion_add(self, name: str, version: Osversion):
:raises ValueError: If the Name is not a ``str`` and/or the Version is not an ``Osversion``.
"""
if isinstance(name, str) and isinstance(version, Osversion):
if name in self.osversions:
raise ValueError("The name \"%s\" already exists for the OsBreed \"%s\"!" % (name, self.name))
self.osversions[name] = version
else:
raise ValueError("Name must be str and Version must be Osversion.")
Expand Down
2 changes: 2 additions & 0 deletions libcobblersignatures/signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ def addosbreed(self, name: str):
:param name: The name of the new breed. Must not exist in the currently loaded models.
"""
if name in [x.name for x in self.osbreeds]:
raise ValueError("Breed \"%s\" already in the list of breeds!" % name)
self.osbreeds.append(OsBreed(name))

def addosversion(self, breedindex: int, versionname: str, versiondata):
Expand Down

0 comments on commit 05e8f59

Please sign in to comment.