Skip to content

Commit 89cdfee

Browse files
committed
Use custom classproperty for python 3.8 compatibility
We can use @classmethod and @Property together in python 3.9. Not so in 3.8 Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent 5a48262 commit 89cdfee

3 files changed

Lines changed: 28 additions & 23 deletions

File tree

vulnerabilities/helpers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,12 @@ def split_markdown_front_matter(text: str) -> Tuple[str, str]:
167167
return frontmatter, markdown
168168

169169
return "", text
170+
171+
172+
# TODO: Replace this with combination of @classmethod and @property after upgrading to python 3.9
173+
class classproperty(object):
174+
def __init__(self, fget):
175+
self.fget = fget
176+
177+
def __get__(self, owner_self, owner_cls):
178+
return self.fget(owner_cls)

vulnerabilities/importer.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from univers.version_range import VersionRange
4444
from univers.versions import Version
4545
from vulnerabilities.helpers import nearest_patched_package
46+
from vulnerabilities.helpers import classproperty
4647
from vulnerabilities.oval_parser import OvalParser
4748
from vulnerabilities.severity_systems import ScoringSystem
4849
from vulnerabilities.severity_systems import SCORING_SYSTEMS
@@ -218,9 +219,8 @@ class NoLicenseError(Exception):
218219

219220
class Importer:
220221
"""
221-
An Importer collects data from various upstreams and returns corresponding
222-
AdvisoryData objects in its advisory_data method.
223-
Subclass this class to implement an importer
222+
An Importer collects data from various upstreams and returns corresponding AdvisoryData objects
223+
in its advisory_data method. Subclass this class to implement an importer
224224
"""
225225

226226
spdx_license_expression = ""
@@ -229,14 +229,12 @@ def __init__(self):
229229
if not self.spdx_license_expression:
230230
raise Exception(f"Cannot run importer {self!r} without a license")
231231

232-
@classmethod
233-
@property
234-
def qualified_name(cls):
232+
@classproperty
233+
def qualified_name(self):
235234
"""
236-
Fully qualified name prefixed with the module name of the data source
237-
used in logging.
235+
Fully qualified name prefixed with the module name of the improver used in logging.
238236
"""
239-
return f"{cls.__module__}.{cls.__qualname__}"
237+
return f"{self.__module__}.{self.__qualname__}"
240238

241239
def advisory_data(self) -> Iterable[AdvisoryData]:
242240
"""

vulnerabilities/improver.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from vulnerabilities.importer import Reference
1212
from vulnerabilities.importer import AdvisoryData
13+
from vulnerabilities.helpers import classproperty
1314

1415
logger = logging.getLogger(__name__)
1516

@@ -73,13 +74,19 @@ def from_advisory_data(cls, advisory_data, confidence, affected_purls, fixed_pur
7374

7475
class Improver:
7576
"""
76-
Improvers are responsible to improve already imported data by an importer.
77-
An improver is required to override the ``interesting_advisories`` property method to return a
78-
QuerySet of ``Advisory`` objects. These advisories are then passed to ``get_inferences`` method
79-
which is responsible for returning an iterable of ``Inferences`` for that particular
80-
``Advisory``
77+
Improvers are responsible to improve already imported data by an importer. An improver is
78+
required to override the ``interesting_advisories`` property method to return a QuerySet of
79+
``Advisory`` objects. These advisories are then passed to ``get_inferences`` method which is
80+
responsible for returning an iterable of ``Inferences`` for that particular ``Advisory``
8181
"""
8282

83+
@classproperty
84+
def qualified_name(self):
85+
"""
86+
Fully qualified name prefixed with the module name of the improver used in logging.
87+
"""
88+
return f"{self.__module__}.{self.__qualname__}"
89+
8390
@property
8491
def interesting_advisories(self) -> QuerySet:
8592
"""
@@ -92,12 +99,3 @@ def get_inferences(self, advisory_data: AdvisoryData) -> Iterable[Inference]:
9299
Generate and return Inferences for the given advisory data
93100
"""
94101
raise NotImplementedError
95-
96-
@classmethod
97-
@property
98-
def qualified_name(cls):
99-
"""
100-
Fully qualified name prefixed with the module name of the improver
101-
used in logging.
102-
"""
103-
return f"{cls.__module__}.{cls.__qualname__}"

0 commit comments

Comments
 (0)