Skip to main content

A Python package for querying CWE trees

Project description

CWE Tree

Python package for querying the Common Weakness Enumeration (CWE) hierarchy as a multi-rooted forest structure.

Quick Start

from cwe_tree import query

# Get a node
node = query.get_cwe("CWE-79")  # Both "79" and "CWE-79" works

# Navigate relationships
parents = query.get_parents("CWE-79")
children = query.get_children("CWE-79")

# Get metadata
metadata = query.get_metadata("CWE-79")

# Traverse forest
roots = query.get_root_nodes()
descendants = query.descendants(node)
ancenstors = query.ancenstors(node)

# Subnode Check
query.is_ancestor("CWE-74", "CWE-77")  
query.is_descendant("CWE-77", "CWE-74") 

# Visualize structure
query.show()                    # Display entire forest
query.show("CWE-79")          # Display subtree from specific node

Install

pip install cwe-tree

# With dev tools (linters, type checker, jupyter)
pip install -e ".[dev]"
# or
uv sync --dev

API

Core Methods

Method Purpose
node(cwe_id) Get node by ID
nodes(predicate=None) Iterate all nodes with optional filter
get_parents(cwe_id) Get parent nodes
get_children(cwe_id) Get child nodes
get_metadata(cwe_id) Get complete node metadata with relationships
get_layer(cwe_id) Get layer/depth information
get_root_nodes() Get all root nodes (no parents)
show(cwe_id=None) Visualize forest structure with ASCII tree

Traversal (inherited from AbcGraphQuerier)

Method Purpose
succ(node, predicate=None) Get successor nodes
prev(node, predicate=None) Get predecessor nodes
descendants(node, max_depth=None) BFS to find all descendants
ancestors(node, max_depth=None) BFS to find all ancestors
edges(predicate=None) Iterate all edges
first_node(predicate=None) Get first matching node

Data Model

  • Forest Structure: Multiple independent trees with different roots (CWEs with no parents)
  • Edge Type: "PARENT_OF" edges flow parent → child
  • Node Properties: id, name, abstract (Class/Base/Variant), layer (depth in hierarchies)

Documentation

Development

Code Quality

All code must pass (perfect score maintained):

make quality  # Run all checks

# Or individually:
uv run isort --check-only src/
uv run black --check src/
uv run mypy src/
uv run pylint src/

Format Code

make format

Run Tests

# Coming soon - comprehensive test suite

Architecture

See docs/design.md for:

  • Problem statement and system role
  • Core concepts (nodes, trees, forest structure)
  • Project structure
  • Data contracts
  • Typical scenarios and boundary cases

License

MIT


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cwe_tree-1.1.2.tar.gz (151.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cwe_tree-1.1.2-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file cwe_tree-1.1.2.tar.gz.

File metadata

  • Download URL: cwe_tree-1.1.2.tar.gz
  • Upload date:
  • Size: 151.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for cwe_tree-1.1.2.tar.gz
Algorithm Hash digest
SHA256 5df9f93c5589df3b177e03da7ab46f671596fb8a2726d5d7408db779533d4902
MD5 acb2e6663e20fa39d328d17d1d99b743
BLAKE2b-256 696c73a98eaea5d6e4c5327d400d717195eddebe83ca308b7c259cc197c11a3a

See more details on using hashes here.

File details

Details for the file cwe_tree-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: cwe_tree-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for cwe_tree-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0e1f3f96c09cd0e53cada45dff9a1de9d855fd254adec902727b0de0af50514b
MD5 edc1aaa75f37150e7c4b14812dac33fb
BLAKE2b-256 6e830189a8fd814dfc38c361ef34cd850b3e99b69ed5215b7fc373bb095206dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page