Skip to content

Commit

Permalink
[Refactor]: Use dataclasses for models (#457)
Browse files Browse the repository at this point in the history
* Refactor Repository class

* Refactor test result models

* Refactor config models

* Remove Optional type from attributes that cannot be None

* black

* Improve type hints

Co-authored-by: Eric Arellano <[email protected]>

* Use enum

Co-authored-by: Eric Arellano <[email protected]>

* Update ecosystem/models/test_results.py

Co-authored-by: Eric Arellano <[email protected]>

* Incorporate feedback

---------

Co-authored-by: Eric Arellano <[email protected]>
  • Loading branch information
frankharkins and Eric-Arellano authored Jul 26, 2023
1 parent 3c1d487 commit 47fe054
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 258 deletions.
107 changes: 46 additions & 61 deletions ecosystem/models/configuration.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
"""Configuration for ecosystem repository."""
from __future__ import annotations

import json
import pprint
from typing import Optional, List
from dataclasses import dataclass, field

from jinja2 import Environment, PackageLoader, select_autoescape
from jinja2 import Environment, PackageLoader, select_autoescape, Template

from ecosystem.exception import QiskitEcosystemException
from .utils import JsonSerializable
from .utils import JsonSerializable, new_list


class Languages:
"""Supported configuration languages."""

PYTHON: str = "python"

def all(self) -> List[str]:
def all(self) -> list[str]:
"""Return all supported languages."""
return [self.PYTHON]

Expand All @@ -25,7 +27,7 @@ def __repr__(self):
class LanguageConfiguration(JsonSerializable):
"""Language configuration class."""

def __init__(self, name: str, versions: List[str]):
def __init__(self, name: str, versions: list[str]):
"""Language configuration.
Args:
Expand All @@ -36,56 +38,48 @@ def __init__(self, name: str, versions: List[str]):
self.versions = versions

@classmethod
def default_version(cls) -> List[str]:
def default_version(cls) -> list[str]:
"""Default language versions."""
return []


class PythonLanguageConfiguration(LanguageConfiguration):
"""Python language config."""

def __init__(self, versions: Optional[List[str]] = None):
def __init__(self, versions: list[str] | None = None):
versions = versions or ["3.6", "3.7", "3.8", "3.9"]
super().__init__(name=Languages.PYTHON, versions=versions)

@classmethod
def default_version(cls) -> List[str]:
def default_version(cls) -> list[str]:
return ["3.6", "3.7", "3.8", "3.9"]


@dataclass
class RepositoryConfiguration(JsonSerializable):
"""Configuration for ecosystem repository."""

def __init__(
self,
language: Optional[LanguageConfiguration] = None,
dependencies_files: Optional[List[str]] = None,
extra_dependencies: Optional[List[str]] = None,
tests_command: Optional[List[str]] = None,
styles_check_command: Optional[List[str]] = None,
coverages_check_command: Optional[List[str]] = None,
):
"""Configuration for ecosystem repository.
Args:
language: repository language configuration
dependencies_files: list of dependencies files paths relative to root of repo
ex: for python `requirements.txt`
extra_dependencies: list of extra dependencies for project to install during tests run
ex: for python it might be `qiskit==0.19`
tests_command: list of commands to run tests
ex: for python `python -m unittest -v`
styles_check_command: list of commands to run style checks
ex: for python `pylint -rn ecosystem tests`
coverages_check_command: list of commands to run coverage checks
ex: for python `coverage3 -m unittest -v && coverage report`
"""
self.language = language or PythonLanguageConfiguration()
self.dependencies_files = dependencies_files or []
self.extra_dependencies = extra_dependencies or []
self.tests_command = tests_command or []
self.styles_check_command = styles_check_command or []
self.coverages_check_command = coverages_check_command or []
"""
Configuration for ecosystem repository.
Attributes:
language: repository language configuration
dependencies_files: list of dependencies files paths relative to root of repo
ex: for python `requirements.txt`
extra_dependencies: list of extra dependencies for project to install during tests run
ex: for python it might be `qiskit==0.19`
tests_command: list of commands to run tests
ex: for python `python -m unittest -v`
styles_check_command: list of commands to run style checks
ex: for python `pylint -rn ecosystem tests`
coverages_check_command: list of commands to run coverage checks
ex: for python `coverage3 -m unittest -v && coverage report`
"""

language: LanguageConfiguration = field(default_factory=PythonLanguageConfiguration)
dependencies_files: list[str] = new_list()
extra_dependencies: list[str] = new_list()
tests_command: list[str] = new_list()
styles_check_command: list[str] = new_list()
coverages_check_command: list[str] = new_list()

def save(self, path: str):
"""Saves configuration as json file."""
Expand Down Expand Up @@ -145,27 +139,18 @@ def __repr__(self):
return pprint.pformat(self.to_dict(), indent=4)


@dataclass
class PythonRepositoryConfiguration(RepositoryConfiguration):
"""Repository configuration for python based projects."""
"""
Repository configuration for python based projects.
"""

def __init__(
self,
language: Optional[LanguageConfiguration] = None,
dependencies_files: Optional[List[str]] = None,
extra_dependencies: Optional[List[str]] = None,
tests_command: Optional[List[str]] = None,
styles_check_command: Optional[List[str]] = None,
coverages_check_command: Optional[List[str]] = None,
):
language = language or PythonLanguageConfiguration()
super().__init__(
language=language,
dependencies_files=dependencies_files,
extra_dependencies=extra_dependencies,
tests_command=tests_command,
styles_check_command=styles_check_command,
coverages_check_command=coverages_check_command,
)
_tox_template: Template | None = None
_lint_template: Template | None = None
_cov_template: Template | None = None
_setup_template: Template | None = None

def __post_init__(self):
env = Environment(
loader=PackageLoader("ecosystem"), autoescape=select_autoescape()
)
Expand All @@ -191,8 +176,8 @@ def default(cls) -> "PythonRepositoryConfiguration":

def render_tox_file(
self,
ecosystem_deps: List[str] = None,
ecosystem_additional_commands: List[str] = None,
ecosystem_deps: list[str] = None,
ecosystem_additional_commands: list[str] = None,
):
"""Renders tox template from configuration."""
ecosystem_deps = ecosystem_deps or []
Expand Down
16 changes: 8 additions & 8 deletions ecosystem/models/execution_summary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Execution summary model."""
from typing import List, Optional
from __future__ import annotations


class CommandExecutionSummary:
Expand All @@ -8,9 +8,9 @@ class CommandExecutionSummary:
def __init__(
self,
code: int,
logs: List[str],
summary: Optional[str] = None,
name: Optional[str] = None,
logs: list[str],
summary: str | None = None,
name: str | None = None,
):
"""CommandExecutionSummary class."""
self.name = name or ""
Expand All @@ -23,11 +23,11 @@ def __init__(
else:
self.summary = summary

def get_warning_logs(self) -> List[str]:
def get_warning_logs(self) -> list[str]:
"""Return warning messages."""
return [log for log in self.logs if "warn" in log.lower()]

def get_qiskit_depreciation_logs(self) -> List[str]:
def get_qiskit_depreciation_logs(self) -> list[str]:
"""Return qiskit depreciation messages."""
return [
log
Expand All @@ -37,11 +37,11 @@ def get_qiskit_depreciation_logs(self) -> List[str]:
and "qiskit.aqua" not in log.lower()
]

def get_error_logs(self) -> List[str]:
def get_error_logs(self) -> list[str]:
"""Return error messages."""
return [log for log in self.logs if "error" in log.lower()]

def get_fail_logs(self) -> List[str]:
def get_fail_logs(self) -> list[str]:
"""Return fail messages."""
return [log for log in self.logs if "failed" in log.lower()]

Expand Down
Loading

0 comments on commit 47fe054

Please sign in to comment.