Skip to content

Commit

Permalink
Use custom classproperty for python 3.8 compatibility
Browse files Browse the repository at this point in the history
We can use @classmethod and @Property together in python 3.9. Not so in
3.8

Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
  • Loading branch information
Hritik14 committed Feb 5, 2022
1 parent d7bc7b4 commit c62f81f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
9 changes: 9 additions & 0 deletions vulnerabilities/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,12 @@ def split_markdown_front_matter(text: str) -> Tuple[str, str]:
return frontmatter, markdown

return "", text


# TODO: Replace this with combination of @classmethod and @property after upgrading to python 3.9
class classproperty(object):
def __init__(self, fget):
self.fget = fget

def __get__(self, owner_self, owner_cls):
return self.fget(owner_cls)
16 changes: 7 additions & 9 deletions vulnerabilities/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from univers.version_range import VersionRange
from univers.versions import Version
from vulnerabilities.helpers import nearest_patched_package
from vulnerabilities.helpers import classproperty
from vulnerabilities.oval_parser import OvalParser
from vulnerabilities.severity_systems import ScoringSystem
from vulnerabilities.severity_systems import SCORING_SYSTEMS
Expand Down Expand Up @@ -218,9 +219,8 @@ class NoLicenseError(Exception):

class Importer:
"""
An Importer collects data from various upstreams and returns corresponding
AdvisoryData objects in its advisory_data method.
Subclass this class to implement an importer
An Importer collects data from various upstreams and returns corresponding AdvisoryData objects
in its advisory_data method. Subclass this class to implement an importer
"""

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

@classmethod
@property
def qualified_name(cls):
@classproperty
def qualified_name(self):
"""
Fully qualified name prefixed with the module name of the data source
used in logging.
Fully qualified name prefixed with the module name of the improver used in logging.
"""
return f"{cls.__module__}.{cls.__qualname__}"
return f"{self.__module__}.{self.__qualname__}"

def advisory_data(self) -> Iterable[AdvisoryData]:
"""
Expand Down
26 changes: 12 additions & 14 deletions vulnerabilities/improver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from vulnerabilities.importer import Reference
from vulnerabilities.importer import AdvisoryData
from vulnerabilities.helpers import classproperty

logger = logging.getLogger(__name__)

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

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

@classproperty
def qualified_name(self):
"""
Fully qualified name prefixed with the module name of the improver used in logging.
"""
return f"{self.__module__}.{self.__qualname__}"

@property
def interesting_advisories(self) -> QuerySet:
"""
Expand All @@ -92,12 +99,3 @@ def get_inferences(self, advisory_data: AdvisoryData) -> Iterable[Inference]:
Generate and return Inferences for the given advisory data
"""
raise NotImplementedError

@classmethod
@property
def qualified_name(cls):
"""
Fully qualified name prefixed with the module name of the improver
used in logging.
"""
return f"{cls.__module__}.{cls.__qualname__}"

0 comments on commit c62f81f

Please sign in to comment.