Skip to content

Commit 3e6ba90

Browse files
authored
Merge pull request #47 from fossology/feat/support-1.1.1
Feat/support 1.1.1
2 parents 9383384 + 348f88b commit 3e6ba90

File tree

17 files changed

+282
-67
lines changed

17 files changed

+282
-67
lines changed

.github/workflows/staticchecks.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jobs:
1111
strategy:
1212
matrix:
1313
container:
14-
- 'python:3.6-slim'
1514
- 'python:3.7-slim'
1615
- 'python:3.8-slim'
1716
- 'python:3.9-slim'

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
.. |PyPI Version| image:: https://badge.fury.io/py/fossology.svg
77
:target: https://pypi.org/project/fossology
88

9-
.. |Python Version| image:: https://img.shields.io/badge/python-3.6%2C3.7%2C3.8-blue?logo=python
9+
.. |Python Version| image:: https://img.shields.io/badge/python-3.7%2C3.8%2C3.9-blue?logo=python
1010
:target: https://www.python.org/doc/versions/
1111

1212
.. |Static Checks| image:: https://github.com/deveaud-m/fossology-python/workflows/Static%20Checks/badge.svg
@@ -22,7 +22,7 @@ A simple wrapper for the Fossology REST API.
2222

2323
See `the OpenAPI specification <https://raw.githubusercontent.com/fossology/fossology/master/src/www/ui/api/documentation/openapi.yaml>`_ used to implement this library.
2424

25-
Compatible with API version 1.1.0
25+
Compatible with API version 1.1.1
2626

2727
Documentation
2828
=============
@@ -91,7 +91,7 @@ Develop
9191
`docstrings <https://realpython.com/documenting-python-code/>`__ to
9292
document functions and classes
9393

94-
- Don't forget to extend the minimal `testsuite <test.py>`_ with the
94+
- Extend the testsuite `poetry run pytest`_ with the
9595
new functions/classes
9696

9797
- The **documentation website** can automatically be generated by the `Sphinx autodoc

docs-source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
# -- Project information -----------------------------------------------------
2121

2222
project = "fossology"
23-
copyright = "2020, Siemens AG"
23+
copyright = "2021, Siemens AG"
2424

2525
# The full version, including major/minor/patch tags
26-
release = "0.2.0"
26+
release = "1.0.0"
2727

2828

2929
# -- General configuration ---------------------------------------------------

fossology/__init__.py

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
# Copyright 2019-2020 Siemens AG
1+
# Copyright 2019-2021 Siemens AG
22
# SPDX-License-Identifier: MIT
33

44
import re
55
import logging
66
import requests
77
from datetime import date, timedelta
88

9-
from typing import List
10-
from fossology.obj import Agents, User, File, TokenScope, SearchTypes, get_options
9+
from typing import Dict, List
10+
from fossology.obj import (
11+
Agents,
12+
Upload,
13+
User,
14+
File,
15+
TokenScope,
16+
SearchTypes,
17+
get_options,
18+
)
1119
from fossology.folders import Folders
1220
from fossology.uploads import Uploads
1321
from fossology.jobs import Jobs
@@ -22,6 +30,37 @@
2230
logger.setLevel(logging.DEBUG)
2331

2432

33+
def search_headers(
34+
searchType: SearchTypes = SearchTypes.ALLFILES,
35+
upload: Upload = None,
36+
filename: str = None,
37+
tag: str = None,
38+
filesizemin: int = None,
39+
filesizemax: int = None,
40+
license: str = None,
41+
copyright: str = None,
42+
group: str = None,
43+
) -> Dict:
44+
headers = {"searchType": searchType.value}
45+
if upload:
46+
headers["uploadId"] = str(upload.id)
47+
if filename:
48+
headers["filename"] = filename
49+
if tag:
50+
headers["tag"] = tag
51+
if filesizemin:
52+
headers["filesizemin"] = filesizemin
53+
if filesizemax:
54+
headers["filesizemax"] = filesizemax
55+
if license:
56+
headers["license"] = license
57+
if copyright:
58+
headers["copyright"] = copyright
59+
if group:
60+
headers["groupName"] = group
61+
return headers
62+
63+
2564
def fossology_token(
2665
url, username, password, token_name, token_scope=TokenScope.READ, token_expire=None
2766
):
@@ -50,6 +89,8 @@ def fossology_token(
5089
:type expire: string, e.g. 2019-12-25
5190
:return: the new token
5291
:rtype: string
92+
:raises AuthenticationError: if the username or password is incorrect
93+
:raises FossologyApiError: if another error occurs
5394
"""
5495
data = {
5596
"username": username,
@@ -67,6 +108,9 @@ def fossology_token(
67108
if response.status_code == 201:
68109
token = response.json()["Authorization"]
69110
return re.sub("Bearer ", "", token)
111+
elif response.status_code == 404:
112+
description = "Authentication error"
113+
raise AuthenticationError(description, response)
70114
else:
71115
description = "Error while generating new token"
72116
raise FossologyApiError(description, response)
@@ -131,8 +175,8 @@ def _auth(self):
131175
if user.name == self.name:
132176
self.user = user
133177
return self.user
134-
logger.error(f"User {self.name} was not found")
135-
raise AuthenticationError(self.host)
178+
description = f"User {self.name} was not found on {self.host}"
179+
raise AuthenticationError(description)
136180

137181
def close(self):
138182
self.session.close()
@@ -222,28 +266,31 @@ def delete_user(self, user):
222266

223267
def search(
224268
self,
225-
searchType=SearchTypes.ALLFILES,
226-
filename=None,
227-
tag=None,
228-
filesizemin=None,
229-
filesizemax=None,
230-
license=None,
231-
copyright=None,
232-
group=None,
269+
searchType: SearchTypes = SearchTypes.ALLFILES,
270+
upload: Upload = None,
271+
filename: str = None,
272+
tag: str = None,
273+
filesizemin: int = None,
274+
filesizemax: int = None,
275+
license: str = None,
276+
copyright: str = None,
277+
group: str = None,
233278
):
234279
"""Search for a specific file
235280
236281
API Endpoint: GET /search
237282
238283
:param searchType: Limit search to: directory, allfiles (default), containers
284+
:param upload: Limit search to a specific upload
239285
:param filename: Filename to find, can contain % as wild-card
240286
:param tag: tag to find
241287
:param filesizemin: Min filesize in bytes
242288
:param filesizemax: Max filesize in bytes
243289
:param license: License search filter
244290
:param copyright: Copyright search filter
245291
:param group: the group name to choose while performing search (default: None)
246-
:type searchType: SearchType Enum
292+
:type searchType: one of SearchTypes Enum
293+
:type upload: Upload
247294
:type filename: string
248295
:type tag: string
249296
:type filesizemin: int
@@ -256,22 +303,17 @@ def search(
256303
:raises FossologyApiError: if the REST call failed
257304
:raises AuthorizationError: if the user can't access the group
258305
"""
259-
headers = {"searchType": searchType.value}
260-
if filename:
261-
headers["filename"] = filename
262-
if tag:
263-
headers["tag"] = tag
264-
if filesizemin:
265-
headers["filesizemin"] = filesizemin
266-
if filesizemax:
267-
headers["filesizemax"] = filesizemax
268-
if license:
269-
headers["license"] = license
270-
if copyright:
271-
headers["copyright"] = copyright
272-
if group:
273-
headers["groupName"] = group
274-
306+
headers = search_headers(
307+
searchType,
308+
upload,
309+
filename,
310+
tag,
311+
filesizemin,
312+
filesizemax,
313+
license,
314+
copyright,
315+
group,
316+
)
275317
response = self.session.get(f"{self.api}/search", headers=headers)
276318

277319
if response.status_code == 200:
@@ -294,7 +336,7 @@ def filesearch(
294336
295337
The response does not generate Python objects yet, the plain JSON data is simply returned.
296338
297-
:param filelist: the list of files (or containers) to search for (default: [])
339+
:param filelist: the list of files (or containers) hashes to search for (default: [])
298340
:param group: the group name to choose while performing search (default: None)
299341
:type filelist: list
300342
:return: list of items corresponding to the search criteria

fossology/exceptions.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020 Siemens AG
1+
# Copyright 2019-2021 Siemens AG
22
# SPDX-License-Identifier: MIT
33

44
from json.decoder import JSONDecodeError
@@ -13,12 +13,15 @@ class Error(Exception):
1313
class AuthenticationError(Error):
1414
"""Authentication error"""
1515

16-
def __init__(self, url):
17-
self.url = url
18-
self.message = (
19-
f"An error occurred during authentication against {self.url}\n"
20-
f"Check your API Token and try again"
21-
)
16+
def __init__(self, description, response=None):
17+
if response:
18+
try:
19+
message = response.json().get("message")
20+
except JSONDecodeError:
21+
message = response.text
22+
self.message = f"{description}: {message} ({response.status_code})"
23+
else:
24+
self.message = description
2225

2326

2427
class AuthorizationError(Error):

fossology/folders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020 Siemens AG
1+
# Copyright 2019-2021 Siemens AG
22
# SPDX-License-Identifier: MIT
33

44
import logging

fossology/jobs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020 Siemens AG
1+
# Copyright 2019-2021 Siemens AG
22
# SPDX-License-Identifier: MIT
33

44
import json

fossology/obj.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020 Siemens AG
1+
# Copyright 2019-2021 Siemens AG
22
# SPDX-License-Identifier: MIT
33

44
import json
@@ -334,7 +334,7 @@ def __init__(
334334
self.additional_info = kwargs
335335

336336
def __str__(self):
337-
return f"File SHA1: {self.sha1} MD5 {self.md5} SH256 {self.sha256} Size {self.filesize}B"
337+
return f"File SHA1: {self.sha1} MD5 {self.md5} SH256 {self.sha256} Size {self.size}B"
338338

339339
@classmethod
340340
def from_json(cls, json_dict):

fossology/report.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020 Siemens AG
1+
# Copyright 2019-2021 Siemens AG
22
# SPDX-License-Identifier: MIT
33

44
import re
@@ -104,7 +104,7 @@ def download_report(self, report_id: int, group: str = None) -> Tuple[str, str]:
104104
return response.text, report_name
105105
elif response.status_code == 403:
106106
description = (
107-
f"Getting report {report_id} {get_options(group)} not authorized"
107+
f"Getting report {report_id} {get_options(group)}not authorized"
108108
)
109109
raise AuthorizationError(description, response)
110110
elif response.status_code == 503:

fossology/uploads.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 Siemens AG
1+
# Copyright 2019-2021 Siemens AG
22
# SPDX-License-Identifier: MIT
33

44
import json
@@ -193,7 +193,7 @@ def upload_file( # noqa: C901
193193
f"{self.api}/uploads", data=data, headers=headers
194194
)
195195
else:
196-
logger.debug(
196+
logger.info(
197197
"Neither VCS, or Url or filename option given, not uploading anything"
198198
)
199199
return

0 commit comments

Comments
 (0)