Skip to content

Commit bc649f4

Browse files
committed
docs: add docstrings to all classes and methods
1 parent 744d91f commit bc649f4

File tree

8 files changed

+32
-21
lines changed

8 files changed

+32
-21
lines changed

src/twyn/cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
@click.group()
4040
@click.version_option(__version__, "--version")
4141
def entry_point() -> None:
42+
"""Provide main CLI entry point for Twyn."""
4243
pass
4344

4445

@@ -188,13 +189,15 @@ def run( # noqa: C901
188189

189190
@entry_point.group()
190191
def allowlist() -> None:
192+
"""Manage package allowlist configuration."""
191193
pass
192194

193195

194196
@allowlist.command()
195197
@click.option("--config", type=click.STRING)
196198
@click.argument("package_name")
197199
def add(package_name: str, config: str) -> None:
200+
"""Add package to allowlist."""
198201
fh = FileHandler(config or ConfigHandler.get_default_config_file_path())
199202
ConfigHandler(fh).add_package_to_allowlist(package_name)
200203

@@ -203,12 +206,14 @@ def add(package_name: str, config: str) -> None:
203206
@click.option("--config", type=click.STRING)
204207
@click.argument("package_name")
205208
def remove(package_name: str, config: str) -> None:
209+
"""Remove package from allowlist."""
206210
fh = FileHandler(config or DEFAULT_PROJECT_TOML_FILE)
207211
ConfigHandler(fh).remove_package_from_allowlist(package_name)
208212

209213

210214
@entry_point.group()
211215
def cache() -> None:
216+
"""Manage cache operations."""
212217
pass
213218

214219

src/twyn/dependency_managers/dependency_manager.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/twyn/dependency_managers/managers/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ class BaseDependencyManager:
1818

1919
@classmethod
2020
def matches_dependency_file(cls, dependency_file: str) -> bool:
21+
"""Check if this manager can handle the given dependency file."""
2122
return Path(dependency_file).name in cls.dependency_files
2223

2324
@classmethod
2425
def matches_ecosystem_name(cls, name: str) -> bool:
26+
"""Check if this manager matches the given ecosystem name."""
2527
return cls.name == Path(name).name.lower()
2628

2729
@classmethod
2830
def get_alternative_source(cls, sources: dict[str, str]) -> Optional[str]:
31+
"""Get alternative source URL for this ecosystem from sources dict."""
2932
match = [x for x in sources if x == cls.name]
3033

3134
return sources[match[0]] if match else None

src/twyn/dependency_managers/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66

77
def get_dependency_manager_from_file(dependency_file: str) -> type[BaseDependencyManager]:
8+
"""Get dependency manager that can handle the given file."""
89
for manager in DEPENDENCY_MANAGERS:
910
if manager.matches_dependency_file(dependency_file):
1011
return manager
1112
raise NoMatchingDependencyManagerError
1213

1314

1415
def get_dependency_manager_from_name(name: str) -> type[BaseDependencyManager]:
16+
"""Get dependency manager by ecosystem name."""
1517
for manager in DEPENDENCY_MANAGERS:
1618
if manager.matches_ecosystem_name(name):
1719
return manager

src/twyn/dependency_parser/dependency_selector.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212

1313

1414
class DependencySelector:
15+
"""Select and provide parsers for dependency files."""
16+
1517
def __init__(self, dependency_files: Optional[set[str]] = None, root_path: str = ".") -> None:
1618
self.dependency_files = dependency_files or set()
1719
self.root_path = root_path
1820

1921
def auto_detect_dependency_file_parser(self) -> list[AbstractParser]:
22+
"""Automatically detect and return parsers for dependency files."""
2023
parsers: list[AbstractParser] = []
2124
root = Path(self.root_path)
2225
for path in root.rglob("*"):
@@ -37,6 +40,7 @@ def auto_detect_dependency_file_parser(self) -> list[AbstractParser]:
3740
return parsers
3841

3942
def get_dependency_file_parsers_from_file_name(self) -> list[AbstractParser]:
43+
"""Get parsers for dependency files based on their names."""
4044
parsers = []
4145
for dependency_file in self.dependency_files:
4246
for known_dependency_file_name in DEPENDENCY_FILE_MAPPING:
@@ -49,6 +53,7 @@ def get_dependency_file_parsers_from_file_name(self) -> list[AbstractParser]:
4953
return parsers
5054

5155
def get_dependency_parsers(self) -> list[AbstractParser]:
56+
"""Get appropriate dependency parsers based on configuration."""
5257
if self.dependency_files:
5358
logger.debug("Dependency file provided. Assigning a parser.")
5459
return self.get_dependency_file_parsers_from_file_name()

src/twyn/dependency_parser/parsers/abstract_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ def __init__(self, file_path: str) -> None:
2020
self.file_handler = FileHandler(file_path=self.file_path)
2121

2222
def __str__(self) -> str:
23+
"""Return string representation of parser class name."""
2324
return self.__class__.__name__
2425

2526
def file_exists(self) -> bool:
27+
"""Check if dependency file exists."""
2628
return self.file_handler.exists()
2729

2830
@abstractmethod

src/twyn/file_handler/file_handler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212

1313
class FileHandler:
14+
"""Handle file operations for reading and writing."""
15+
1416
def __init__(self, file_path: str) -> None:
1517
self.file_path = self._get_file_path(file_path)
1618

1719
def is_handler_of_file(self, name: str) -> bool:
20+
"""Check if this handler manages the specified file."""
1821
return self._get_file_path(name) == self.file_path
1922

2023
def read(self) -> str:
24+
"""Read file content as string."""
2125
self._raise_for_file_exists()
2226

2327
content = self.file_path.read_text()
@@ -27,30 +31,35 @@ def read(self) -> str:
2731

2832
@contextmanager
2933
def open(self, mode="r") -> Iterator[TextIO]:
34+
"""Open file with context manager."""
3035
self._raise_for_file_exists()
3136

3237
with self.file_path.open(mode) as fp:
3338
yield fp
3439
logger.debug("Successfully read content from local dependencies file")
3540

3641
def exists(self) -> bool:
42+
"""Check if file exists and is a valid file."""
3743
try:
3844
self._raise_for_file_exists()
3945
except (PathNotFoundError, PathIsNotFileError):
4046
return False
4147
return True
4248

4349
def _raise_for_file_exists(self) -> None:
50+
"""Raise appropriate exception if file doesn't exist or isn't a file."""
4451
if not self.file_path.exists():
4552
raise PathNotFoundError
4653

4754
if not self.file_path.is_file():
4855
raise PathIsNotFileError
4956

5057
def write(self, data: str) -> None:
58+
"""Write data to file."""
5159
self.file_path.write_text(data)
5260

5361
def delete(self, delete_parent_dir: bool = False) -> None:
62+
"""Delete file and optionally its parent directory."""
5463
if not self.exists():
5564
logger.info("File does not exist, nothing to delete")
5665
return
@@ -68,4 +77,5 @@ def delete(self, delete_parent_dir: bool = False) -> None:
6877
)
6978

7079
def _get_file_path(self, file_path: str) -> Path:
80+
"""Convert string path to absolute Path object."""
7181
return Path(os.path.abspath(os.path.join(os.getcwd(), file_path)))

src/twyn/similarity/algorithm.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212

1313
class SimilarityThreshold:
14+
"""Define threshold values for similarity comparison."""
15+
1416
LENGTH_CUTOFF = 5
1517
MIN_VALUE = 1.0
1618
MAX_FOR_SHORT_WORDS = 1.0
@@ -25,6 +27,7 @@ def __init__(self, max: float) -> None:
2527

2628
@classmethod
2729
def from_name(cls, name: str) -> SimilarityThreshold:
30+
"""Create threshold based on name length."""
2831
name_length = len(name)
2932
if name_length <= cls.LENGTH_CUTOFF:
3033
logger.debug("max length of %s selected for %s", cls.MAX_FOR_SHORT_WORDS, name)
@@ -34,6 +37,7 @@ def from_name(cls, name: str) -> SimilarityThreshold:
3437
return cls(max=cls.MAX_FOR_LONG_WORDS) # we allow more typos if the name is longer
3538

3639
def is_inside_threshold(self, value: float) -> bool:
40+
"""Check if value is within threshold bounds."""
3741
return self.min <= value <= self.max
3842

3943

@@ -60,4 +64,5 @@ class EditDistance(AbstractSimilarityAlgorithm):
6064
"""Levenshtein algorithm that computes the edit distance between words."""
6165

6266
def _run_algorithm(self, first_sequence: str, second_sequence: str) -> int:
67+
"""Compute Damerau-Levenshtein distance between sequences."""
6368
return DamerauLevenshtein.distance(s1=first_sequence, s2=second_sequence)

0 commit comments

Comments
 (0)