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
1diff --git a/reviewtools/containers/base_container.py b/reviewtools/containers/base_container.py
2index a9e0f41..173036a 100644
3--- a/reviewtools/containers/base_container.py
4+++ b/reviewtools/containers/base_container.py
5@@ -42,8 +42,9 @@ class BaseContainer:
6 "application/x-pie-executable",
7 ]
8
9- files = None
10- bin_files = None
11+ _unpack_dir = None
12+ _files = None
13+ _bin_files = None
14
15 def __init__(self, fn):
16 self.tmp_dir = mkdtemp(prefix=MKDTEMP_PREFIX, dir=MKDTEMP_DIR)
17@@ -58,16 +59,42 @@ class BaseContainer:
18 else:
19 raise ContainerException("Could not find '%s'" % fn)
20
21- self.unpack_dir = ".".join(self.filename.split(".")[:-1])
22- unpack_pkg(fn, dest=self.unpack_dir)
23+ @property
24+ def unpack_dir(self) -> str:
25+ """
26+ Absolute path to unpack directory temporary location. If it is not defined,
27+ then unpack_pkg() will be called and the path of the newly created directory
28+ will be returned.
29+ """
30+ if self._unpack_dir is None:
31+ self._unpack_dir = ".".join(self.filename.split(".")[:-1])
32+ unpack_pkg(self.filename, dest=self._unpack_dir)
33+ return self._unpack_dir
34
35- # Get a list of all unpacked files
36- if self.files is None:
37- self.files = self.get_files_list()
38+ @property
39+ def files(self) -> list:
40+ """
41+ List of files present in the container. List elements will be the absolute
42+ path of the file taking unpack_dir as root directory. If it is not defined
43+ (i.e. the list has not been populated yet), then get_file_list() will be
44+ called and the newly created list will be returned.
45+ """
46+ if self._files is None:
47+ self._files = self.get_files_list()
48+ return self._files
49
50- # Setup what is needed to get a list of all unpacked compiled binaries
51- if self.bin_files is None:
52- self.bin_files = self.get_compiled_binaries_list()
53+ @property
54+ def bin_files(self) -> list:
55+ """
56+ List of compiled binaries present in the container. List elements will be
57+ the absolute path of the file taking unpack_dir as root directory. If it is
58+ not defined (i.e. the list has not been populated yet), then
59+ get_compiled_binaries_list() will be called and the newly created list will
60+ be returned.
61+ """
62+ if self._bin_files is None:
63+ self._bin_files = self.get_compiled_binaries_list()
64+ return self._bin_files
65
66 def __del__(self):
67 """
68diff --git a/reviewtools/sr_tests.py b/reviewtools/sr_tests.py
69index 54c2a6f..6caae54 100644
70--- a/reviewtools/sr_tests.py
71+++ b/reviewtools/sr_tests.py
72@@ -45,9 +45,9 @@ def _mock_func(self):
73 class _BaseContainer:
74 def __init__(self, fn):
75 self.filename = fn
76- self.unpack_dir = ".".join(self.filename.split(".")[:-1])
77- self.files = []
78- self.bin_files = []
79+ self._unpack_dir = ".".join(self.filename.split(".")[:-1])
80+ self._files = []
81+ self._bin_files = []
82 self.tmp_dir = "/faketmpdir"
83
84

Subscribers

People subscribed via source and target branches