Shared
codesectools.shared
Modules:
| Name | Description |
|---|---|
cloc |
Provide a wrapper for counting lines of code using the cloc tool. |
cwe |
Handle Common Weakness Enumeration (CWE) data. |
cloc
Provide a wrapper for counting lines of code using the cloc tool.
This module contains the Cloc class, a wrapper around the cloc tool,
to calculate the number of physical lines of source code for a specific
language within a directory.
Cloc
A wrapper for the 'cloc' (Count Lines of Code) tool.
Find the 'cloc' executable or download and use the Perl script if the executable is not available but Perl is. Provide a method to count lines of code for a specific language.
Attributes:
| Name | Type | Description |
|---|---|---|
version |
str
|
The version of the cloc Perl script to download. |
cloc_names |
dict
|
A mapping from internal language names to the names used by cloc. |
dir |
Path
|
The directory to run cloc in. |
lang |
str
|
The programming language to count, mapped to the cloc name. |
base_command |
list[str]
|
The command list to execute cloc. |
Initialize the Cloc wrapper.
Check for the 'cloc' binary. If not found, check for 'perl' and download the 'cloc.pl' script if it doesn't exist locally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
The directory to run cloc in. |
required |
|
str
|
The programming language to count. |
required |
Methods:
| Name | Description |
|---|---|
get_loc |
Get the lines of code for the specified language. |
get_loc
get_loc() -> int
Get the lines of code for the specified language.
Execute the cloc command, parse the JSON output, and return the number of source code lines.
Returns:
| Type | Description |
|---|---|
int
|
The number of lines of code, or 0 if the language is not found |
int
|
in the output. |
Raises:
| Type | Description |
|---|---|
NonZeroExit
|
If the cloc command fails. |
cwe
Handle Common Weakness Enumeration (CWE) data.
This module downloads the CWE list from cwe.mitre.org if not already cached, and provides classes to access and manage CWE data.
Attributes:
| Name | Type | Description |
|---|---|---|
CWEs |
|
CWE
CWE(
id: int,
name: str,
description: str,
parent: Self | None = None,
children: set[Self] | None = None,
)
Represent a single Common Weakness Enumeration.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
The CWE identifier. |
name |
str
|
The name of the weakness. |
description |
str
|
A description of the weakness. |
parent |
CWE | None
|
The parent CWE weakness, if any. |
children |
set[CWE]
|
A set of child CWE weaknesses. |
Initialize a CWE instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
The CWE identifier. |
required |
|
str
|
The name of the weakness. |
required |
|
str
|
A description of the weakness. |
required |
|
Self | None
|
The parent CWE weakness, if any. |
None
|
|
set[Self] | None
|
A set of child CWE weaknesses, if any. |
None
|
Methods:
| Name | Description |
|---|---|
__eq__ |
Compare this CWE with another object for equality. |
__hash__ |
Return the hash of the CWE instance, based on its ID. |
__repr__ |
Return a developer-friendly string representation of the CWE. |
extend |
Retrieve the set of related CWEs within a specified distance in the hierarchy. |
__eq__
__repr__
__repr__() -> str
Return a developer-friendly string representation of the CWE.
Returns:
| Type | Description |
|---|---|
str
|
A string showing the class name and CWE ID. |
extend
Retrieve the set of related CWEs within a specified distance in the hierarchy.
Recursively finds parent and child CWEs up to the given distance level. Includes the current CWE in the returned set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
The number of levels to traverse up (parents) and down (children). Defaults to 1. |
1
|
Returns:
| Type | Description |
|---|---|
set[Self]
|
A set of CWE objects including the self and related weaknesses. |
CWEsCollection
Manage the collection of all CWEs.
Downloads and loads the official CWE list from a CSV file.
Attributes:
| Name | Type | Description |
|---|---|---|
cwes_data |
dict
|
A mapping of CWE categories to their CSV filenames. |
directory |
Path
|
The path to the cached CWE data directory. |
cwes |
list[CWE]
|
A list of all loaded CWE objects. |
Initialize the CWEs collection.
Download the CWE data from cwe.mitre.org if it's not present in the user cache.
Methods:
| Name | Description |
|---|---|
download |
Download CWE data from the official MITRE website. |
load |
Load and parse CWE data from cached CSV files. |
from_string |
Get a CWE from a string like 'CWE-79'. |
from_id |
Get a CWE by its identifier. |
cwes_data
instance-attribute
cwes_data = {
"Software Development": "699.csv",
"Hardware Design": "1194.csv",
"Research Concepts": "1000.csv",
}
load
from_string
from_string(cwe_string: str) -> CWE