77# -------------------------------------------------------------------------------
88
99import re
10+ import os
1011
1112"""Preview of High-Level, object oriented Python interface to the SW360 REST API.
1213For now, this does NOT strive to be stable or complete. Feel free to use it as
@@ -19,7 +20,7 @@ class SW360Resource:
1920 release etc.
2021 """
2122
22- def __init__ (self , json = None , resource_id = None , ** kwargs ):
23+ def __init__ (self , json = None , resource_id = None , sw360 = None , ** kwargs ):
2324 self .details = {}
2425 """All resource details which are not explicitely supported by the
2526 constructor parameters of the derived objexts are stored in the
@@ -31,6 +32,9 @@ def __init__(self, json=None, resource_id=None, **kwargs):
3132 the object is yet unknown to SW360 - otherwise, it stores the SW360
3233 id (release_id, component_id, etc.)."""
3334
35+ self .sw360 = sw360
36+ """SW360 api object"""
37+
3438 for key , value in kwargs .items ():
3539 self .details [key ] = value
3640
@@ -42,7 +46,7 @@ def _parse_release_list(self, json_list, component_id=None):
4246 them to `container`."""
4347 releases = {}
4448 for release_json in json_list :
45- release = Release (component_id = component_id )
49+ release = Release (component_id = component_id , sw360 = self . sw360 )
4650 release .from_json (release_json )
4751 releases [release .id ] = release
4852 return releases
@@ -52,7 +56,7 @@ def _parse_attachment_list(self, json_list, resources=[]):
5256 them to `container`."""
5357 attachments = {}
5458 for attachment_json in json_list :
55- attachment = Attachment (resources = resources )
59+ attachment = Attachment (resources = resources , sw360 = self . sw360 )
5660 attachment .from_json (attachment_json )
5761 attachments [attachment .id ] = attachment
5862 return attachments
@@ -111,22 +115,24 @@ class Release(SW360Resource):
111115 :param version: the actual version
112116 :param downloadurl: URL the release was downloaded from
113117 :param release_id: id of the release (if exists in SW360 already)
118+ :param sw360: your SW360 instance for interacting with the API
114119 :param kwargs: additional relase details as specified in the SW360 REST API
115120 :type json: SW360 JSON object
116121 :type component_id: string
117122 :type version: string
118123 :type downloadurl: string
119124 :type release_id: string
125+ :type sw360: instance from SW360 class
120126 :type kwargs: dictionary
121127 """
122128 def __init__ (self , json = None , release_id = None , component_id = None ,
123- version = None , downloadurl = None , ** kwargs ):
129+ version = None , downloadurl = None , sw360 = None , ** kwargs ):
124130 self .attachments = {}
125131
126132 self .component_id = component_id
127133 self .version = version
128134 self .downloadurl = downloadurl
129- super ().__init__ (json , release_id , ** kwargs )
135+ super ().__init__ (json , release_id , sw360 = sw360 , ** kwargs )
130136
131137 def from_json (self , json ):
132138 """Parse release JSON object from SW360 REST API. The component it
@@ -142,6 +148,21 @@ def from_json(self, json):
142148 json ,
143149 copy_attributes = ("name" , "version" , "downloadurl" ))
144150
151+ def get (self , sw360 = None , id_ = None ):
152+ """Retrieve/update release from SW360."""
153+ if sw360 :
154+ self .sw360 = sw360
155+ if id_ :
156+ self .id = id_
157+ self .from_json (self .sw360 .get_release (self .id ))
158+ return self
159+
160+ def get_component (self , sw360 = None ):
161+ """Retrieve/update component of this release."""
162+ if sw360 :
163+ self .sw360 = sw360
164+ return Component ().get (self .sw360 , self .component_id )
165+
145166 def __repr__ (self ):
146167 """Representation string."""
147168 return "<Release %s %s id:%s>" % (self .name , self .version , self .id )
@@ -160,7 +181,7 @@ class Attachment(SW360Resource):
160181
161182 For JSON parsing, please read documentation of from_json() method.
162183
163- :param json: create release from SW360 JSON object by calling from_json()
184+ :param json: create it from SW360 JSON object by calling from_json()
164185 :param attachment_id: SW360 id of the attachment (if it exists already)
165186 :param resources: dictionary of SW360 resource objects the attachment belongs to
166187 (instances of Release(), Component() or Project() with id as key)
@@ -169,23 +190,25 @@ class Attachment(SW360Resource):
169190 :param attachment_type: one of "DOCUMENT", "SOURCE", "SOURCE_SELF"
170191 "CLEARING_REPORT", "COMPONENT_LICENSE_INFO_XML", "BINARY",
171192 "BINARY_SELF", "LICENSE_AGREEMENT", "README_OSS"
193+ :param sw360: your SW360 instance for interacting with the API
172194 :param kwargs: additional relase details as specified in the SW360 REST API
173195 :type json: SW360 JSON object
174196 :type attachment_id: string
175197 :type release_id: string
176198 :type filename: string
177199 :type sha1: string
178200 :type attachment_type: string
201+ :type sw360: instance from SW360 class
179202 :type kwargs: dictionary
180203 """
181204 def __init__ (self , json = None , attachment_id = None , resources = {},
182- filename = None , sha1 = None , attachment_type = None , ** kwargs ):
205+ filename = None , sha1 = None , attachment_type = None , sw360 = None , ** kwargs ):
183206 self .resources = resources
184207 self .filename = filename
185208 self .sha1 = sha1
186209 self .attachment_type = attachment_type
187210 self .download_link = None
188- super ().__init__ (json , attachment_id , ** kwargs )
211+ super ().__init__ (json , attachment_id , sw360 , ** kwargs )
189212
190213 def from_json (self , json ):
191214 """Parse attachment JSON object from SW360 REST API. For now, we don't
@@ -203,6 +226,38 @@ def from_json(self, json):
203226 "checkedBy" , "checkedTeam" , "checkedComment" , "checkedOn" ,
204227 "checkStatus" ))
205228
229+ def get (self , sw360 = None , id_ = None ):
230+ """Retrieve/update attachment from SW360."""
231+ if sw360 :
232+ self .sw360 = sw360
233+ if id_ :
234+ self .id = id_
235+ self .from_json (self .sw360 .get_attachment (self .id ))
236+ return self
237+
238+ def get_releases (self , sw360 = None ):
239+ """Retrieve/update releases of this attachment."""
240+ if sw360 :
241+ self .sw360 = sw360
242+ releases = [
243+ Release ().get (self .sw360 , id_ )
244+ for id_ in self .releases
245+ ]
246+ return releases
247+
248+ def download (self , target_path , filename = None ):
249+ """download an attachment to local file.
250+
251+ :param target_path: path where to store the attachment
252+ :type target_path: str
253+ :param filename: local filename. If not given, use the filename stored in SW360
254+ :type filename: str, optional
255+ """
256+ if filename is None :
257+ filename = os .path .basename (self .filename )
258+ self .sw360 .download_attachment (os .path .join (target_path , filename ),
259+ self .download_link )
260+
206261 def __repr__ (self ):
207262 """Representation string."""
208263 return "<Attachment %s id:%s>" % (self .filename , self .id )
@@ -226,25 +281,27 @@ class Component(SW360Resource):
226281 :param homepage: homepage of the component
227282 :param component_type: one of "INTERNAL", "OSS", "COTS", "FREESOFTWARE",
228283 "INNER_SOURCE", "SERVICE", "CODE_SNIPPET"
284+ :param sw360: your SW360 instance for interacting with the API
229285 :param kwargs: additional component details as specified in the SW360 REST API
230286 :type json: SW360 JSON object
231287 :type component_id: string
232288 :type name: string
233289 :type description: string
234290 :type homepage: string
235291 :type component_type: string
292+ :type sw360: instance from SW360 class
236293 :type kwargs: dictionary
237294 """
238295 def __init__ (self , json = None , component_id = None , name = None , description = None ,
239- homepage = None , component_type = None , ** kwargs ):
296+ homepage = None , component_type = None , sw360 = None , ** kwargs ):
240297 self .releases = {}
241298 self .attachments = {}
242299
243300 self .name = name
244301 self .description = description
245302 self .homepage = homepage
246303 self .component_type = component_type
247- super ().__init__ (json , component_id , ** kwargs )
304+ super ().__init__ (json , component_id , sw360 , ** kwargs )
248305
249306 def from_json (self , json ):
250307 """Parse component JSON object from SW360 REST API. Information for
@@ -263,6 +320,15 @@ def from_json(self, json):
263320 copy_attributes = ("name" , "description" , "homepage" ,
264321 "componentType" ))
265322
323+ def get (self , sw360 = None , id_ = None ):
324+ """Retrieve/update component from SW360."""
325+ if sw360 :
326+ self .sw360 = sw360
327+ if id_ :
328+ self .id = id_
329+ self .from_json (self .sw360 .get_component (self .id ))
330+ return self
331+
266332 def __repr__ (self ):
267333 """Representation string."""
268334 return "<Component %s id:%s>" % (self .name , self .id )
@@ -290,6 +356,7 @@ class Project(SW360Resource):
290356 "EVERYONE"
291357 :param project_type: one of "CUSTOMER", "INTERNAL", "PRODUCT", "SERVICE",
292358 "INNER_SOURCE"
359+ :param sw360: your SW360 instance for interacting with the API
293360 :param kwargs: additional project details as specified in the SW360 REST API
294361 :type json: SW360 JSON object
295362 :type project_id: string
@@ -298,19 +365,20 @@ class Project(SW360Resource):
298365 :type description: string
299366 :type visibility: string
300367 :type project_type: string
368+ :type sw360: instance from SW360 class
301369 :type kwargs: dictionary
302370 """
303371 def __init__ (self , json = None , project_id = None , name = None , version = None ,
304372 description = None , visibility = None , project_type = None ,
305- ** kwargs ):
373+ sw360 = None , ** kwargs ):
306374 self .releases = {}
307375
308376 self .name = name
309377 self .version = version
310378 self .description = description
311379 self .visibility = visibility
312380 self .project_type = project_type
313- super ().__init__ (json , project_id , ** kwargs )
381+ super ().__init__ (json , project_id , sw360 , ** kwargs )
314382
315383 def from_json (self , json ):
316384 """Parse project JSON object from SW360 REST API. Information for
@@ -328,6 +396,15 @@ def from_json(self, json):
328396 copy_attributes = ("name" , "description" , "version" , "visibility" ,
329397 "projectType" ))
330398
399+ def get (self , sw360 = None , id_ = None ):
400+ """Retrieve/update project from SW360."""
401+ if sw360 :
402+ self .sw360 = sw360
403+ if id_ :
404+ self .id = id_
405+ self .from_json (self .sw360 .get_project (self .id ))
406+ return self
407+
331408 def __repr__ (self ):
332409 """Representation string."""
333410 return "<Project %s id:%s>" % (self .name , self .id )
0 commit comments