Skip to content

Commit

Permalink
fixup! refactor: Use _unpickle rather than hasattr() in sources
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealFalcon committed Mar 4, 2024
1 parent f138d81 commit 7b1cf50
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
3 changes: 0 additions & 3 deletions cloudinit/sources/DataSourceEc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,6 @@ def launch_index(self):

@property
def platform(self):
# Handle upgrade path of pickled ds
if not hasattr(self, "_platform_type"):
self._platform_type = DataSourceEc2.dsname.lower()
if not self._platform_type:
self._platform_type = DataSourceEc2.dsname.lower()
return self._platform_type
Expand Down
3 changes: 0 additions & 3 deletions cloudinit/sources/DataSourceNoCloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@ def _pp2d_callback(mp, data):

@property
def platform_type(self):
# Handle upgrade path of pickled ds
if not hasattr(self, "_platform_type"):
self._platform_type = None
if not self._platform_type:
self._platform_type = "lxd" if util.is_lxd() else "nocloud"
return self._platform_type
Expand Down
37 changes: 17 additions & 20 deletions cloudinit/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,22 @@ def __init__(self, sys_cfg, distro: Distro, paths: Paths, ud_proc=None):

def _unpickle(self, ci_pkl_version: int) -> None:
"""Perform deserialization fixes for Paths."""
if not hasattr(self, "vendordata2"):
self.vendordata2 = None
if not hasattr(self, "vendordata2_raw"):
self.vendordata2_raw = None
if not hasattr(self, "skip_hotplug_detect"):
self.skip_hotplug_detect = False
expected_attrs = {
"_crawled_metadata": None,
"_platform_type": None,
"_subplatform": None,
"ec2_metadata": UNSET,
"extra_hotplug_udev_rules": None,
"metadata_address": None,
"network_json": UNSET,
"skip_hotplug_detect": False,
"vendordata2": None,
"vendordata2_raw": None,
}
for key, value in expected_attrs.items():
if not hasattr(self, key):
setattr(self, key, value)

if hasattr(self, "userdata") and self.userdata is not None:
# If userdata stores MIME data, on < python3.6 it will be
# missing the 'policy' attribute that exists on >=python3.6.
Expand All @@ -352,20 +362,7 @@ def _unpickle(self, ci_pkl_version: int) -> None:
e,
)
raise DatasourceUnpickleUserDataError() from e
if not hasattr(self, "extra_hotplug_udev_rules"):
self.extra_hotplug_udev_rules = None
if not hasattr(self, "metadata_address"):
self.metadata_address = None
if not hasattr(self, "_platform_type"):
self._platform_type = None
if not hasattr(self, "_subplatform"):
self._subplatform = None
if not hasattr(self, "_crawled_metadata"):
self._crawled_metadata = None
if not hasattr(self, "network_json"):
self.network_json = UNSET
if not hasattr(self, "ec2_metadata"):
self.ec2_metadata = UNSET


def __str__(self):
return type_utils.obj_name(self)
Expand Down

0 comments on commit 7b1cf50

Please sign in to comment.