From 1cb511872c28be282f393e47bf618ba305d5dbfb Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Wed, 25 Oct 2023 08:39:02 -0300 Subject: [PATCH] use abstract --- .../project_analysis/file_parsers/base_parser.py | 13 ++++++++++--- .../file_parsers/pyproject_toml_file_parser.py | 6 +++--- .../file_parsers/requirements_txt_file_parser.py | 6 +++--- .../file_parsers/setup_cfg_file_parser.py | 6 +++--- .../file_parsers/setup_py_file_parser.py | 6 +++--- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/codemodder/project_analysis/file_parsers/base_parser.py b/src/codemodder/project_analysis/file_parsers/base_parser.py index 4dff3da95..79e67fbe2 100644 --- a/src/codemodder/project_analysis/file_parsers/base_parser.py +++ b/src/codemodder/project_analysis/file_parsers/base_parser.py @@ -1,13 +1,19 @@ +from abc import ABC, abstractmethod, abstractproperty + from pathlib import Path from typing import List from .package_store import PackageStore from packaging.requirements import Requirement -class BaseParser: +class BaseParser(ABC): def __init__(self, parent_directory: Path): self.parent_directory = parent_directory - self.file_name: str = "" + + @property + @abstractmethod + def file_name(self): + ... def _parse_dependencies(self, lines: List[str]): return [ @@ -17,8 +23,9 @@ def _parse_dependencies(self, lines: List[str]): if (line := x.strip()) and not line.startswith("#") ] + @abstractmethod def _parse_file(self, file: Path): - raise NotImplementedError + ... def find_file_locations(self) -> List[Path]: return list(Path(self.parent_directory).rglob(self.file_name)) diff --git a/src/codemodder/project_analysis/file_parsers/pyproject_toml_file_parser.py b/src/codemodder/project_analysis/file_parsers/pyproject_toml_file_parser.py index 8b67c1ef5..8cb5c60ef 100644 --- a/src/codemodder/project_analysis/file_parsers/pyproject_toml_file_parser.py +++ b/src/codemodder/project_analysis/file_parsers/pyproject_toml_file_parser.py @@ -6,9 +6,9 @@ class PyprojectTomlParser(BaseParser): - def __init__(self, parent_directory: Path): - super().__init__(parent_directory) - self.file_name = "pyproject.toml" + @property + def file_name(self): + return "pyproject.toml" def _parse_dependencies_from_toml(self, toml_data: dict): # todo: handle cases for diff --git a/src/codemodder/project_analysis/file_parsers/requirements_txt_file_parser.py b/src/codemodder/project_analysis/file_parsers/requirements_txt_file_parser.py index 25d85fac7..aca0ae170 100644 --- a/src/codemodder/project_analysis/file_parsers/requirements_txt_file_parser.py +++ b/src/codemodder/project_analysis/file_parsers/requirements_txt_file_parser.py @@ -6,9 +6,9 @@ class RequirementsTxtParser(BaseParser): - def __init__(self, parent_directory: Path): - super().__init__(parent_directory) - self.file_name = "requirements.txt" + @property + def file_name(self): + return "requirements.txt" def _parse_file(self, file: Path): with open(file, "r", encoding="utf-8") as f: diff --git a/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py b/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py index 1c94dbb80..5a6cb3f87 100644 --- a/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py +++ b/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py @@ -6,9 +6,9 @@ class SetupCfgParser(BaseParser): - def __init__(self, parent_directory: Path): - super().__init__(parent_directory) - self.file_name = "setup.cfg" + @property + def file_name(self): + return "setup.cfg" def _parse_dependencies_from_toml(self, config: configparser.ConfigParser): # todo: handle cases for diff --git a/src/codemodder/project_analysis/file_parsers/setup_py_file_parser.py b/src/codemodder/project_analysis/file_parsers/setup_py_file_parser.py index ae444ffe2..bce23f41a 100644 --- a/src/codemodder/project_analysis/file_parsers/setup_py_file_parser.py +++ b/src/codemodder/project_analysis/file_parsers/setup_py_file_parser.py @@ -9,9 +9,9 @@ class SetupPyParser(BaseParser): - def __init__(self, parent_directory: Path): - super().__init__(parent_directory) - self.file_name = "setup.py" + @property + def file_name(self): + return "setup.py" def _parse_dependencies(self, cst_dependencies): return [