Merge ~jslarraz/review-tools:move-basecontainer-attributes-to-properties into review-tools:master

Proposed by Jorge Sancho Larraz
Status: Merged
Merged at revision: d888d63d9a84bd6746c4abaddf678baa3a783eab
Proposed branch: ~jslarraz/review-tools:move-basecontainer-attributes-to-properties
Merge into: review-tools:master
Diff against target: 82 lines (+40/-13)
2 files modified
reviewtools/containers/base_container.py (+37/-10)
reviewtools/sr_tests.py (+3/-3)
Reviewer Review Type Date Requested Status
Alex Murray Approve
Review via email: mp+466375@code.launchpad.net

Commit message

move basecontainer attributes to properties

Description of the change

Using properties bring some benefits:

- Related functionality does not need to be called during the initialization but only when it is needed. It simplify unittest avoiding the need to mock related functions under certain circunstances
- Properties may not have a setter. That avoid some error during the development as the property cannot be unexpectedly modified (it needs to be done explicitly via the related _attribute)

To post a comment you must log in.
Revision history for this message
Alex Murray (alexmurray) wrote :

Nice improvement - LGTM! I wonder if we should have a HACKING.md or similar which describes the preferred coding conventions going forward?

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/reviewtools/containers/base_container.py b/reviewtools/containers/base_container.py
index a9e0f41..173036a 100644
--- a/reviewtools/containers/base_container.py
+++ b/reviewtools/containers/base_container.py
@@ -42,8 +42,9 @@ class BaseContainer:
42 "application/x-pie-executable",42 "application/x-pie-executable",
43 ]43 ]
4444
45 files = None45 _unpack_dir = None
46 bin_files = None46 _files = None
47 _bin_files = None
4748
48 def __init__(self, fn):49 def __init__(self, fn):
49 self.tmp_dir = mkdtemp(prefix=MKDTEMP_PREFIX, dir=MKDTEMP_DIR)50 self.tmp_dir = mkdtemp(prefix=MKDTEMP_PREFIX, dir=MKDTEMP_DIR)
@@ -58,16 +59,42 @@ class BaseContainer:
58 else:59 else:
59 raise ContainerException("Could not find '%s'" % fn)60 raise ContainerException("Could not find '%s'" % fn)
6061
61 self.unpack_dir = ".".join(self.filename.split(".")[:-1])62 @property
62 unpack_pkg(fn, dest=self.unpack_dir)63 def unpack_dir(self) -> str:
64 """
65 Absolute path to unpack directory temporary location. If it is not defined,
66 then unpack_pkg() will be called and the path of the newly created directory
67 will be returned.
68 """
69 if self._unpack_dir is None:
70 self._unpack_dir = ".".join(self.filename.split(".")[:-1])
71 unpack_pkg(self.filename, dest=self._unpack_dir)
72 return self._unpack_dir
6373
64 # Get a list of all unpacked files74 @property
65 if self.files is None:75 def files(self) -> list:
66 self.files = self.get_files_list()76 """
77 List of files present in the container. List elements will be the absolute
78 path of the file taking unpack_dir as root directory. If it is not defined
79 (i.e. the list has not been populated yet), then get_file_list() will be
80 called and the newly created list will be returned.
81 """
82 if self._files is None:
83 self._files = self.get_files_list()
84 return self._files
6785
68 # Setup what is needed to get a list of all unpacked compiled binaries86 @property
69 if self.bin_files is None:87 def bin_files(self) -> list:
70 self.bin_files = self.get_compiled_binaries_list()88 """
89 List of compiled binaries present in the container. List elements will be
90 the absolute path of the file taking unpack_dir as root directory. If it is
91 not defined (i.e. the list has not been populated yet), then
92 get_compiled_binaries_list() will be called and the newly created list will
93 be returned.
94 """
95 if self._bin_files is None:
96 self._bin_files = self.get_compiled_binaries_list()
97 return self._bin_files
7198
72 def __del__(self):99 def __del__(self):
73 """100 """
diff --git a/reviewtools/sr_tests.py b/reviewtools/sr_tests.py
index 54c2a6f..6caae54 100644
--- a/reviewtools/sr_tests.py
+++ b/reviewtools/sr_tests.py
@@ -45,9 +45,9 @@ def _mock_func(self):
45class _BaseContainer:45class _BaseContainer:
46 def __init__(self, fn):46 def __init__(self, fn):
47 self.filename = fn47 self.filename = fn
48 self.unpack_dir = ".".join(self.filename.split(".")[:-1])48 self._unpack_dir = ".".join(self.filename.split(".")[:-1])
49 self.files = []49 self._files = []
50 self.bin_files = []50 self._bin_files = []
51 self.tmp_dir = "/faketmpdir"51 self.tmp_dir = "/faketmpdir"
5252
5353

Subscribers

People subscribed via source and target branches