Skip to content

Commit

Permalink
🚸(ozi.new): move pre/postprocess_arguments to ozi.new.validate
Browse files Browse the repository at this point in the history
Signed-off-by: rjdbcm <[email protected]>
  • Loading branch information
rjdbcm committed Jul 10, 2024
1 parent 964e9c5 commit 4bb70e2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 81 deletions.
83 changes: 2 additions & 81 deletions ozi/new/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
"""ozi-new entrypoint script."""
from __future__ import annotations

import re
import shlex
import sys
from itertools import chain
from pathlib import Path
from typing import TYPE_CHECKING

from ozi_spec import METADATA # pyright: ignore
Expand All @@ -18,16 +16,8 @@

from ozi.new.interactive import interactive_prompt
from ozi.new.parser import parser
from ozi.new.validate import valid_classifier
from ozi.new.validate import valid_contact_info
from ozi.new.validate import valid_copyright_head
from ozi.new.validate import valid_emails
from ozi.new.validate import valid_home_page
from ozi.new.validate import valid_license
from ozi.new.validate import valid_project_name
from ozi.new.validate import valid_project_url
from ozi.new.validate import valid_spdx
from ozi.new.validate import valid_summary
from ozi.new.validate import postprocess_arguments
from ozi.new.validate import preprocess_arguments
from ozi.render import RenderedContent

if TYPE_CHECKING:
Expand All @@ -38,75 +28,6 @@
Composable: TypeAlias = Callable[[Namespace], Namespace]


def _valid_project(project: Namespace) -> Namespace:
"""Validate a project namespace."""
valid_project_name(name=project.name)
valid_summary(project.summary)
project.license = valid_license(
_license=project.license,
license_expression=project.license_expression,
)
valid_home_page(home_page=project.home_page)
valid_project_url(project_url=project.project_url)
project.copyright_head = valid_copyright_head(
copyright_head=project.copyright_head,
project_name=project.name,
license_file=project.license_file,
)
valid_spdx(project.license_expression)
valid_contact_info(
author=project.author,
maintainer=project.maintainer,
author_email=project.author_email,
maintainer_email=project.maintainer_email,
)
for i in [
project.audience,
project.environment,
project.framework,
project.topic,
]:
for classifier in i:
valid_classifier(classifier)
return project


def preprocess_arguments(project: Namespace) -> Namespace:
"""Preprocess (validate) arguments for project namespace."""
if project.strict:
with TAP.strict(): # pragma: no cover
return _valid_project(project)
else:
return _valid_project(project)


def postprocess_arguments(project: Namespace) -> Namespace:
"""Postprocess (normalize) arguments for project namespace."""
project.author_email, project.maintainer_email = valid_emails(
author_email=project.author_email,
maintainer_email=project.maintainer_email,
verify=project.verify_email,
)
project.keywords = project.keywords.split(',')
project.name = re.sub(r'[-_.]+', '-', project.name)
project.target = Path(project.target)
project.topic = list(set(project.topic))
project.dist_requires = list(set(project.dist_requires))
if any(
i for i in project.target.iterdir() if i not in project.allow_file
): # defer to good-issue
TAP.not_ok('target directory not empty', 'no files will be created', skip=True)
match project.ci_provider:
case 'github': # pragma: no cover
pass
case _:
TAP.not_ok(
f'--ci-provider "{project.ci_provider}" unrecognized. ci_user will not be set.',
)
project.allow_file = set(map(Path, project.allow_file))
return project


def project(project: Namespace) -> None:
"""Create a new project in a target directory."""
project = postprocess_arguments(preprocess_arguments(project))
Expand Down
78 changes: 78 additions & 0 deletions ozi/new/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from __future__ import annotations

import re
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Sequence
from urllib.parse import urlparse
Expand All @@ -24,6 +26,13 @@
from ozi.vendor.email_validator import ValidatedEmail
from ozi.vendor.email_validator import validate_email

if TYPE_CHECKING:
from argparse import Namespace
from typing import Callable
from typing import TypeAlias

Composable: TypeAlias = Callable[[Namespace], Namespace]

_CLASSIFIERS = {i.partition(' :: ')[2].strip() for i in classifiers}


Expand Down Expand Up @@ -231,3 +240,72 @@ def valid_emails(
case None: # pragma: no cover
continue
return _author_email, _maintainer_email


def _valid_project(project: Namespace) -> Namespace:
"""Validate a project namespace."""
valid_project_name(name=project.name)
valid_summary(project.summary)
project.license = valid_license(
_license=project.license,
license_expression=project.license_expression,
)
valid_home_page(home_page=project.home_page)
valid_project_url(project_url=project.project_url)
project.copyright_head = valid_copyright_head(
copyright_head=project.copyright_head,
project_name=project.name,
license_file=project.license_file,
)
valid_spdx(project.license_expression)
valid_contact_info(
author=project.author,
maintainer=project.maintainer,
author_email=project.author_email,
maintainer_email=project.maintainer_email,
)
for i in [
project.audience,
project.environment,
project.framework,
project.topic,
]:
for classifier in i:
valid_classifier(classifier)
return project


def preprocess_arguments(project: Namespace) -> Namespace:
"""Preprocess (validate) arguments for project namespace."""
if project.strict:
with TAP.strict(): # pragma: no cover
return _valid_project(project)
else:
return _valid_project(project)


def postprocess_arguments(project: Namespace) -> Namespace:
"""Postprocess (normalize) arguments for project namespace."""
project.author_email, project.maintainer_email = valid_emails(
author_email=project.author_email,
maintainer_email=project.maintainer_email,
verify=project.verify_email,
)
project.keywords = project.keywords.split(',')
project.name = re.sub(r'[-_.]+', '-', project.name)
project.target = Path(project.target)
project.topic = list(set(project.topic))
project.dist_requires = list(set(project.dist_requires))
if any(
i for i in project.target.iterdir() if i not in project.allow_file
): # defer to good-issue
TAP.not_ok('target directory not empty', 'no files will be created', skip=True)
match project.ci_provider:
case 'github': # pragma: no cover
pass
case _:
TAP.not_ok(
f'--ci-provider "{project.ci_provider}" unrecognized. ci_user will not be set.',
)
project.allow_file = set(map(Path, project.allow_file))
return project

0 comments on commit 4bb70e2

Please sign in to comment.