Skip to content

Commit

Permalink
use abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Oct 25, 2023
1 parent 01e311b commit 78dbd68
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/codemodder/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(
verbose: bool,
registry: CodemodRegistry,
repo_manager: PythonRepoManager,
):
): # pylint: disable=too-many-arguments
self.directory = directory
self.dry_run = dry_run
self.verbose = verbose
Expand Down
17 changes: 12 additions & 5 deletions src/codemodder/project_analysis/file_parsers/base_parser.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
from abc import ABC, abstractmethod

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 = ""

def _parse_dependencies(self, lines: List[str]):
@property
@abstractmethod
def file_name(self):
...

Check warning on line 16 in src/codemodder/project_analysis/file_parsers/base_parser.py

View check run for this annotation

Codecov / codecov/patch

src/codemodder/project_analysis/file_parsers/base_parser.py#L16

Added line #L16 was not covered by tests

def _parse_dependencies(self, dependencies: List[str]):
return [
Requirement(line)
for x in lines
for x in dependencies
# Skip empty lines and comments
if (line := x.strip()) and not line.startswith("#")
]

@abstractmethod
def _parse_file(self, file: Path):
raise NotImplementedError
...

Check warning on line 28 in src/codemodder/project_analysis/file_parsers/base_parser.py

View check run for this annotation

Codecov / codecov/patch

src/codemodder/project_analysis/file_parsers/base_parser.py#L28

Added line #L28 was not covered by tests

def find_file_locations(self) -> List[Path]:
return list(Path(self.parent_directory).rglob(self.file_name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from codemodder.project_analysis.file_parsers.package_store import PackageStore
from packaging.requirements import Requirement
from pathlib import Path
from typing import List
from .base_parser import BaseParser


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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@


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):
def _parse_dependencies(self, dependencies):
return [
Requirement(line)
for x in cst_dependencies
for x in dependencies
# Skip empty lines and comments
if (line := clean_simplestring(x.value)) and not line.startswith("#")
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=redefined-outer-name
import pytest
from codemodder.project_analysis.file_parsers import PyprojectTomlParser

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=redefined-outer-name
import pytest
from codemodder.project_analysis.file_parsers import SetupCfgParser

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=redefined-outer-name
import pytest
from codemodder.project_analysis.file_parsers import SetupPyParser

Expand Down

0 comments on commit 78dbd68

Please sign in to comment.