8000 Added convenience function to extract SPDX elements by their Id by HarshvMahawar · Pull Request #531 · spdx/tools-python · GitHub
[go: up one dir, main page]

Skip to content

Added convenience function to extract SPDX elements by their Id #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/spdx/document_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List
from typing import Union

from spdx.model.document import Document
from spdx.model.snippet import Snippet
from spdx.model.package import Package
from spdx.model.file import File


def get_contained_spdx_element_ids(document: Document) -> List[str]:
element_ids = [file.spdx_id for file in document.files]
element_ids.extend([package.spdx_id for package in document.packages])
element_ids.extend([snippet.spdx_id for snippet in document.snippets])
return element_ids


def get_element_from_spdx_id(document: Document, spdx_id: str) -> Union[Package, File, Snippet, None]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 requires two blank lines between methods.
Which IDE are you using for your python development? There might be a plugin for your IDE to detect PEP8 violations.

elements = [file_ for file_ in document.files]
elements.extend([package_ for package_ in document.packages])
elements.extend([snippet_ for snippet_ in document.snippets])
for element in elements:
if element.spdx_id == spdx_id:
return element
0