Added script for generating modules.

This commit is contained in:
2025-06-19 15:40:48 +02:00
parent 34730454ae
commit 9bfbd44e34
8 changed files with 326 additions and 25 deletions

View File

@@ -4,7 +4,7 @@ import logging
import os
import sys
from pathlib import Path
from typing import Optional
from typing import Optional, Literal
sys.path.append(os.path.dirname(__file__))
@@ -16,6 +16,11 @@ _PRIVATE_PATH = Path('private')
_PUBLIC_PATH = Path('public')
_HEADER_TEMPLATE_NAME = 'header.hpp.jinja'
_SOURCE_TEMPLATE_NAME = 'source.cpp.jinja'
_HEADER_CHOICES = {
'public': _PUBLIC_PATH,
'private': _PRIVATE_PATH,
'no': None
}
_logger = logging.getLogger(__name__)
_header_path: Optional[Path] = None
@@ -30,27 +35,32 @@ def verify_tools() -> None:
if not success:
raise RuntimeError('one or more required tools could not be found')
def query_params() -> None:
def _make_header_path(header_folder: Path, base_path: Path) -> Path:
return header_folder / base_path.with_suffix('.hpp')
def _make_source_path(base_path: Path) -> Path:
return _PRIVATE_PATH / base_path.with_suffix('.cpp')
def _apply_params(header_folder: Path, do_create_source: bool, base_path: Path) -> None:
global _header_path, _source_path, _namespace
header_folder = prompt_choices('Create Header?', {
'public': _PUBLIC_PATH,
'private': _PRIVATE_PATH,
'no': None
})
_namespace = base_path.parts[0]
if header_folder is not None:
_header_path = _make_header_path(header_folder, base_path)
if do_create_source:
_source_path = _make_source_path(base_path)
def query_params() -> None:
header_folder = prompt_choices('Create Header?', _HEADER_CHOICES)
do_create_source = prompt_yesno('Create source?')
if header_folder is None and not do_create_source:
raise RuntimeError('Neither header nor source selected for creation.')
def _make_header_path(base_path: Path) -> Path:
return header_folder / base_path.with_suffix('.hpp')
def _make_source_path(base_path: Path) -> Path:
return _PRIVATE_PATH / base_path.with_suffix('.cpp')
def _validate_path(path: Path) -> bool:
result = True
if header_folder is not None:
header_path = _make_header_path(path)
header_path = _make_header_path(header_folder, path)
if header_path.exists():
print(f'Header file {header_path} already exists.')
result = False
@@ -63,12 +73,7 @@ def query_params() -> None:
input_path = prompt_path('Enter basename for the source (relative to public/private folder, no file extension).',
allow_slash=True, validator=_validate_path)
_namespace = input_path.parts[0]
if header_folder is not None:
_header_path = _make_header_path(input_path)
if do_create_source:
_source_path = _make_source_path(input_path)
_apply_params(header_folder, do_create_source, input_path)
def create_header() -> None:
@@ -94,11 +99,18 @@ def create_source() -> None:
'namespace': _namespace
})
def script_main() -> None:
verify_tools()
query_params()
def _run() -> None:
create_header()
create_source()
def run_new_source(header: Literal['private', 'public', 'no'], source: bool, path: Path) -> None:
_apply_params(_HEADER_CHOICES[header], source, path)
_run()
def script_main() -> None:
verify_tools()
query_params()
_run()
if __name__ == '__main__':
run_script(script_main)