Skip to content

Commit

Permalink
feat(sw360_objects): better __repr__ and __str__
Browse files Browse the repository at this point in the history
This allows for easy copy'n'pasting of an object in interactive
sessions plus nicer print output.
  • Loading branch information
gernot-h committed Jul 30, 2023
1 parent d72c2fa commit db002cf
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 15 deletions.
41 changes: 26 additions & 15 deletions sw360/sw360_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ def from_json(self, json, copy_attributes=list(), snake_case=True):
else:
self.details[key] = value

def __repr__(self):
repr_ = []
for k, v in self.__dict__.items():
if v is None:
continue
if (k in ("name", "version", "filename", "attachment_type")
or k.endswith("_id")):
repr_.append(f'{k}={v!r}')
if k == "id":
repr_.append(f'{self.__class__.__name__.lower()}_id={v!r}')
return (f'{self.__class__.__name__}('
+ ", ".join(repr_)
+ ")")


class Release(SW360Resource):
"""A release is the SW360 abstraction for a single version of a component.
Expand Down Expand Up @@ -129,11 +143,12 @@ class Release(SW360Resource):
:type kwargs: dictionary
"""
def __init__(self, json=None, release_id=None, component_id=None,
version=None, downloadurl=None, sw360=None, **kwargs):
name=None, version=None, downloadurl=None, sw360=None, **kwargs):
self.attachments = {}

self.component_id = component_id
self.name = name
self.version = version
self.component_id = component_id
self.downloadurl = downloadurl
super().__init__(json, release_id, sw360=sw360, **kwargs)

Expand Down Expand Up @@ -166,9 +181,8 @@ def get_component(self, sw360=None):
self.sw360 = sw360
return Component().get(self.sw360, self.component_id)

def __repr__(self):
"""Representation string."""
return "<Release %s %s id:%s>" % (self.name, self.version, self.id)
def __str__(self):
return f'{self.name} {self.version} ({self.id})'


class Attachment(SW360Resource):
Expand Down Expand Up @@ -207,9 +221,9 @@ class Attachment(SW360Resource):
def __init__(self, json=None, attachment_id=None, resources={},
filename=None, sha1=None, attachment_type=None, sw360=None, **kwargs):
self.resources = resources
self.attachment_type = attachment_type
self.filename = filename
self.sha1 = sha1
self.attachment_type = attachment_type
self.download_link = None
super().__init__(json, attachment_id, sw360, **kwargs)

Expand Down Expand Up @@ -261,9 +275,8 @@ def download(self, target_path, filename=None):
self.sw360.download_attachment(os.path.join(target_path, filename),
self.download_link)

def __repr__(self):
"""Representation string."""
return "<Attachment %s id:%s>" % (self.filename, self.id)
def __str__(self):
return f'{self.filename} ({self.id})'


class Component(SW360Resource):
Expand Down Expand Up @@ -332,9 +345,8 @@ def get(self, sw360=None, id_=None):
self.from_json(self.sw360.get_component(self.id))
return self

def __repr__(self):
"""Representation string."""
return "<Component %s id:%s>" % (self.name, self.id)
def __str__(self):
return f'{self.name} ({self.id})'


class Project(SW360Resource):
Expand Down Expand Up @@ -408,6 +420,5 @@ def get(self, sw360=None, id_=None):
self.from_json(self.sw360.get_project(self.id))
return self

def __repr__(self):
"""Representation string."""
return "<Project %s id:%s>" % (self.name, self.id)
def __str__(self):
return f'{self.name} {self.version} ({self.id})'
13 changes: 13 additions & 0 deletions tests/test_sw360obj_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@


class Sw360ObjTestComponent(Sw360ObjTestBase):
def test_repr(self):
comp = Component(component_id="123", name="TestCmp", homepage="http://www",
component_type="INTERNAL")
comp = eval(repr(comp))
assert comp.id == "123"
assert comp.name == "TestCmp"
self.assertEqual(str(comp), "TestCmp (123)")

comp = Component()
comp = eval(repr(comp))
assert comp.id is None
assert comp.name is None

@responses.activate
def test_get_component(self):
responses.add(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_sw360obj_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@


class Sw360ObjTestProject(Sw360ObjTestBase):
def test_repr(self):
prj = Project(project_id="123", name="TestPrj", version="12")
prj = eval(repr(prj))
assert prj.id == "123"
assert prj.name == "TestPrj"
assert prj.version == "12"
self.assertEqual(str(prj), "TestPrj 12 (123)")

prj = Project()
prj = eval(repr(prj))
assert prj.id is None
assert prj.name is None

@responses.activate
def test_get_project(self):
responses.add(
Expand All @@ -25,6 +38,8 @@ def test_get_project(self):
self.assertEqual(len(proj.releases), 1)
self.assertIsNone(proj.releases["7c4"].component_id)

self.assertEqual(str(proj), "MyProj 11.0 (123)")


if __name__ == "__main__":
unittest.main()
44 changes: 44 additions & 0 deletions tests/test_sw360obj_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import responses
import unittest
from tests.test_sw360obj_base import Sw360ObjTestBase, SW360_BASE_URL
from sw360 import Release


class Sw360ObjTestRelease(Sw360ObjTestBase):
def test_repr(self):
r = Release(release_id="123", name="TestCmp", version="1.4",
component_id="456", downloadurl="http://www")

r = eval(repr(r))
assert r.id == "123"
assert r.name == "TestCmp"
assert r.version == "1.4"
self.assertEqual(str(r), "TestCmp 1.4 (123)")

r = Release()
r = eval(repr(r))
assert r.id is None
assert r.name is None
assert r.version is None

@responses.activate
def test_get_release(self):
responses.add(
responses.GET,
SW360_BASE_URL + "releases/123",
json={
'name': 'acl',
'version': '1.4',
'somekey': 'value',
'_links': {
'sw360:component': {
'href': SW360_BASE_URL + 'components/7b4'}}})
r = Release().get(self.lib, "123")
self.assertEqual(r.name, "acl")
self.assertEqual(r.details["somekey"], "value")
self.assertEqual(len(r.purls), 0)
self.assertEqual(r.component_id, "7b4")


if __name__ == "__main__":
unittest.main()

0 comments on commit db002cf

Please sign in to comment.