Skip to content

Commit

Permalink
Use dataclasses only for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Apr 17, 2024
1 parent 8133fb2 commit 0c569a9
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 97 deletions.
6 changes: 3 additions & 3 deletions mypy_boto3_builder/cli_parser.py
Expand Up @@ -27,7 +27,7 @@ def get_absolute_path(path: str) -> Path:


@dataclass(kw_only=True, slots=True)
class Namespace:
class CLINamespace:
"""
CLI arguments namespace.
"""
Expand All @@ -44,7 +44,7 @@ class Namespace:
disable_smart_version: bool


def parse_args(args: Sequence[str]) -> Namespace:
def parse_args(args: Sequence[str]) -> CLINamespace:
"""
Parse CLI arguments.
Expand Down Expand Up @@ -122,7 +122,7 @@ def parse_args(args: Sequence[str]) -> Namespace:
)
result = parser.parse_args(args)

return Namespace(
return CLINamespace(
log_level=logging.DEBUG if result.debug else logging.INFO,
output_path=result.output_path,
service_names=result.service_names,
Expand Down
4 changes: 2 additions & 2 deletions mypy_boto3_builder/main.py
Expand Up @@ -8,7 +8,7 @@

from botocore.session import Session as BotocoreSession

from mypy_boto3_builder.cli_parser import Namespace, parse_args
from mypy_boto3_builder.cli_parser import CLINamespace, parse_args
from mypy_boto3_builder.constants import Product, ProductLibrary
from mypy_boto3_builder.generators.aioboto3_generator import AioBoto3Generator
from mypy_boto3_builder.generators.aiobotocore_generator import AioBotocoreGenerator
Expand Down Expand Up @@ -102,7 +102,7 @@ def get_generator_cls(product: Product) -> type[BaseGenerator]:

def generate_product(
product: Product,
args: Namespace,
args: CLINamespace,
service_names: Sequence[ServiceName],
master_service_names: Sequence[ServiceName],
) -> None:
Expand Down
5 changes: 1 addition & 4 deletions mypy_boto3_builder/parsers/shape_parser.py
Expand Up @@ -159,9 +159,6 @@ def resource_name(self) -> str:
def _get_operation(self, name: str) -> OperationModel:
return self.service_model.operation_model(name)

def _get_operation_names(self) -> list[str]:
return list(self.service_model.operation_names)

def _get_paginator(self, name: str) -> PaginatorShape:
if not self._paginators_shape:
raise ShapeParserError(f"Unknown paginator: {name}")
Expand Down Expand Up @@ -304,7 +301,7 @@ def get_client_method_map(self) -> dict[str, Method]:
Type.none,
),
}
for operation_name in self._get_operation_names():
for operation_name in self.service_model.operation_names:
operation_model = self._get_operation(operation_name)
arguments: list[Argument] = [Argument("self", None)]
method_name = xform_name(operation_name)
Expand Down
17 changes: 11 additions & 6 deletions mypy_boto3_builder/structures/argument.py
Expand Up @@ -3,7 +3,6 @@
"""

from collections.abc import Iterator
from dataclasses import dataclass
from typing import Literal

from typing_extensions import Self
Expand All @@ -12,7 +11,6 @@
from mypy_boto3_builder.type_annotations.type_constant import TypeConstant


@dataclass
class Argument:
"""
Method or function argument.
Expand All @@ -24,10 +22,17 @@ class Argument:
prefix -- Used for starargs.
"""

name: str
type_annotation: FakeAnnotation | None
default: TypeConstant | None = None
prefix: Literal["*", "**", ""] = ""
def __init__(
self,
name: str,
type_annotation: FakeAnnotation | None,
default: TypeConstant | None = None,
prefix: Literal["*", "**", ""] = "",
) -> None:
self.name = name
self.type_annotation = type_annotation
self.default = default
self.prefix: Literal["*", "**", ""] = prefix

def render(self) -> str:
"""
Expand Down
26 changes: 17 additions & 9 deletions mypy_boto3_builder/structures/attribute.py
Expand Up @@ -3,13 +3,11 @@
"""

from collections.abc import Iterator
from dataclasses import dataclass

from mypy_boto3_builder.type_annotations.fake_annotation import FakeAnnotation
from mypy_boto3_builder.type_annotations.type_constant import TypeConstant


@dataclass
class Attribute:
"""
Class or module attribute.
Expand All @@ -24,13 +22,23 @@ class Attribute:
is_collection -- Whether the attribute parsed from collections.
"""

name: str
type_annotation: FakeAnnotation
value: TypeConstant | None = None
type_ignore: bool = False
is_reference: bool = False
is_identifier: bool = False
is_collection: bool = False
def __init__(
self,
name: str,
type_annotation: FakeAnnotation,
value: TypeConstant | None = None,
type_ignore: bool = False,
is_reference: bool = False,
is_identifier: bool = False,
is_collection: bool = False,
) -> None:
self.name = name
self.type_annotation = type_annotation
self.value = value
self.type_ignore = type_ignore
self.is_reference = is_reference
self.is_identifier = is_identifier
self.is_collection = is_collection

def iterate_types(self) -> Iterator[FakeAnnotation]:
"""
Expand Down
18 changes: 12 additions & 6 deletions mypy_boto3_builder/utils/version.py
Expand Up @@ -5,8 +5,6 @@
import contextlib
import importlib.metadata

from boto3 import __version__ as boto3_version
from botocore import __version__ as botocore_version
from newversion import Version

from mypy_boto3_builder.constants import PACKAGE_NAME
Expand All @@ -22,11 +20,11 @@ def get_builder_version() -> str:
return "0.0.0"


def get_supported_python_versions() -> list[str]:
def get_supported_python_versions() -> tuple[str, ...]:
"""
Get supported python versions.
"""
return ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
return ("3.8", "3.9", "3.10", "3.11", "3.12", "3.13")


def get_min_build_version(version: str) -> str:
Expand All @@ -47,14 +45,22 @@ def get_botocore_version() -> str:
"""
Get botocore package version.
"""
return botocore_version
try:
from botocore import __version__ as version
except ImportError as e:
raise RuntimeError("botocore is not installed") from e
return f"{version}"


def get_boto3_version() -> str:
"""
Get boto3 package version.
"""
return boto3_version
try:
from boto3 import __version__ as version
except ImportError as e:
raise RuntimeError("boto3 is not installed") from e
return f"{version}"


def get_aiobotocore_version() -> str:
Expand Down
35 changes: 13 additions & 22 deletions mypy_boto3_builder/writers/package_writer.py
Expand Up @@ -3,7 +3,6 @@
"""

from collections.abc import Sequence
from dataclasses import dataclass
from pathlib import Path

from mypy_boto3_builder.constants import TEMPLATES_PATH
Expand All @@ -23,25 +22,19 @@
)


@dataclass
class TemplateRender:
"""
Template render target.
"""

template_path: Path
path: Path | None = None
paths: tuple[Path, ...] = ()

@property
def output_paths(self) -> tuple[Path, ...]:
"""
Get output paths as a tuple.
"""
return (
*([self.path] if self.path else []),
*self.paths,
)
def __init__(
self,
template_path: Path,
path: Path | None = None,
paths: tuple[Path, ...] = (),
) -> None:
self.template_path = template_path
self.paths = (*([path] if path else []), *paths)


class PackageWriter:
Expand Down Expand Up @@ -159,9 +152,7 @@ def _render_templates(
template_renders: Sequence[TemplateRender],
) -> None:
for template_render in template_renders:
self._render_template(
template_render.template_path, template_render.output_paths, package
)
self._render_template(template_render.template_path, template_render.paths, package)

def _write_template(self, path: Path, content: str) -> None:
if not path.parent.exists():
Expand All @@ -180,7 +171,7 @@ def _render_md_templates(
# if file_path.suffix == ".md":
# content = fix_pypi_headers(content)
# content = format_md(content)
for output_path in template_render.output_paths:
for output_path in template_render.paths:
self._write_template(output_path, content)

def _cleanup(self, valid_paths: Sequence[Path], output_path: Path) -> None:
Expand Down Expand Up @@ -216,7 +207,7 @@ def write_package(
]
self._render_templates(package, template_renders)

rendered_paths = [path for t in template_renders for path in t.output_paths]
rendered_paths = [path for t in template_renders for path in t.paths]
valid_paths = (*rendered_paths, *static_paths)
output_path = self._get_setup_path(package) if self.generate_setup else package_path
self._cleanup(valid_paths, output_path)
Expand Down Expand Up @@ -327,7 +318,7 @@ def write_service_package(self, package: ServicePackage, templates_path: Path) -
]
self._render_templates(package, template_renders)

valid_paths = [path for t in template_renders for path in t.output_paths]
valid_paths = [path for t in template_renders for path in t.paths]
output_path = (
self._get_setup_path(package)
if self.generate_setup
Expand Down Expand Up @@ -380,5 +371,5 @@ def write_service_docs(self, package: ServicePackage, templates_path: Path) -> N
)

self._render_md_templates(package, template_renders)
valid_paths = [path for t in template_renders for path in t.output_paths]
valid_paths = [path for t in template_renders for path in t.paths]
self._cleanup(valid_paths, docs_path)

0 comments on commit 0c569a9

Please sign in to comment.