• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2025 Northeastern University
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from dataclasses import dataclass, field
20from typing import List, Tuple
21
22
23@dataclass(frozen=True)
24class OpenSource:
25    name: str
26    license: str
27    license_file: str = field(metadata={"source_key": "License File"})
28    version_number: str
29    owner: str
30    upstream_url: str = field(metadata={"source_key": "Upstream URL"})
31    description: str
32    dependencies: Tuple[str, ...] = field(default_factory=tuple)
33
34    @classmethod
35    def from_dict(cls, data: dict):
36        mapping = {
37            'Name': 'name',
38            'License': 'license',
39            'License File': 'license_file',
40            'Version Number': 'version_number',
41            'Owner': 'owner',
42            'Upstream URL': 'upstream_url',
43            'Description': 'description',
44            'Dependencies': 'dependencies'
45        }
46        init_data = {}
47        for json_key, attr_name in mapping.items():
48            value = data.get(json_key)
49            if isinstance(value, str):
50                value = value.strip()
51            elif value is None:
52                value = ""
53            if attr_name == "dependencies":
54                if isinstance(value, str):
55                    value = tuple(v.strip() for v in value.split(';') if v.strip())
56                elif isinstance(value, list):
57                    value = tuple(value)
58                else:
59                    value = ()
60            init_data[attr_name] = value
61        return cls(**init_data)
62
63    def get_licenses(self) -> List[str]:
64        return [lic.strip() for lic in self.license.split(';') if lic.strip()]
65
66    def get_license_files(self) -> List[str]:
67        return [f.strip() for f in self.license_file.split(';') if f.strip()]
68
69    def to_dict(self) -> dict:
70        return {
71            "name": self.name,
72            "license": self.license,
73            "license_file": self.license_file,
74            "version_number": self.version_number,
75            "owner": self.owner,
76            "upstream_url": self.upstream_url,
77            "description": self.description,
78            "dependencies": list(self.dependencies),
79            "licenses": list(self.get_licenses()),
80            "license_files": list(self.get_license_files())
81        }
82