From 4d75f51df3de5578e500f13c2887c87ea7100685 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Wed, 5 Feb 2025 01:16:41 +0100 Subject: [PATCH] factor out read_license_file --- src/backend/InvenTree/InvenTree/api.py | 87 +++++++++++++------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/api.py b/src/backend/InvenTree/InvenTree/api.py index 9a5740a196..c12f6480c4 100644 --- a/src/backend/InvenTree/InvenTree/api.py +++ b/src/backend/InvenTree/InvenTree/api.py @@ -35,6 +35,48 @@ from .version import inventreeApiText logger = structlog.get_logger('inventree') +def read_license_file(path: Path) -> list: + """Extract license information from the provided file. + + Arguments: + path: Path to the license file + + Returns: A list of items containing the license information + """ + # Check if the file exists + if not path.exists(): + logger.error("License file not found at '%s'", path) + return [] + + try: + data = json.loads(path.read_text()) + except json.JSONDecodeError as e: + logger.exception("Failed to parse license file '%s': %s", path, e) + return [] + except Exception as e: + logger.exception("Exception while reading license file '%s': %s", path, e) + return [] + + output = [] + names = set() + + # Ensure we do not have any duplicate 'name' values in the list + for entry in data: + name = None + for key in entry: + if key.lower() == 'name': + name = entry[key] + break + + if name is None or name in names: + continue + + names.add(name) + output.append({key.lower(): value for key, value in entry.items()}) + + return output + + class LicenseViewSerializer(serializers.Serializer): """Serializer for license information.""" @@ -49,47 +91,6 @@ class LicenseView(APIView): permission_classes = [permissions.IsAuthenticated] - def read_license_file(self, path: Path) -> list: - """Extract license information from the provided file. - - Arguments: - path: Path to the license file - - Returns: A list of items containing the license information - """ - # Check if the file exists - if not path.exists(): - logger.error("License file not found at '%s'", path) - return [] - - try: - data = json.loads(path.read_text()) - except json.JSONDecodeError as e: - logger.exception("Failed to parse license file '%s': %s", path, e) - return [] - except Exception as e: - logger.exception("Exception while reading license file '%s': %s", path, e) - return [] - - output = [] - names = set() - - # Ensure we do not have any duplicate 'name' values in the list - for entry in data: - name = None - for key in entry: - if key.lower() == 'name': - name = entry[key] - break - - if name is None or name in names: - continue - - names.add(name) - output.append({key.lower(): value for key, value in entry.items()}) - - return output - @extend_schema(responses={200: OpenApiResponse(response=LicenseViewSerializer)}) def get(self, request, *args, **kwargs): """Return information about the InvenTree server.""" @@ -98,8 +99,8 @@ class LicenseView(APIView): 'web/static/web/.vite/dependencies.json' ) return JsonResponse({ - 'backend': self.read_license_file(backend), - 'frontend': self.read_license_file(frontend), + 'backend': read_license_file(backend), + 'frontend': read_license_file(frontend), })