spp_template/tools/new_source.py

105 lines
3.1 KiB
Python

import logging
import os
import sys
from pathlib import Path
from typing import Optional
sys.path.append(os.path.dirname(__file__))
from common import prompt_choices, prompt_path, prompt_yesno, run_script
from common.jinja import is_jinja_installed, generate_file
_PRIVATE_PATH = Path('private')
_PUBLIC_PATH = Path('public')
_HEADER_TEMPLATE_NAME = 'header.hpp.jinja'
_SOURCE_TEMPLATE_NAME = 'source.cpp.jinja'
_logger = logging.getLogger(__name__)
_header_path: Optional[Path] = None
_source_path: Optional[Path] = None
_namespace: str
def verify_tools() -> None:
success = True
if not is_jinja_installed():
_logger.error('Python module Jinja2 is not installed.')
success = False
if not success:
raise RuntimeError('one or more required tools could not be found')
def query_params() -> None:
global _header_path, _source_path, _namespace
header_folder = prompt_choices('Create Header?', {
'public': _PUBLIC_PATH,
'private': _PRIVATE_PATH,
'no': None
})
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)
if header_path.exists():
print(f'Header file {header_path} already exists.')
result = False
if do_create_source:
source_path = _make_source_path(path)
if source_path.exists():
print(f'Source file {source_path} already exists.')
result = False
return result
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)
def create_header() -> None:
if _header_path is None:
return
_logger.info('Generating header at %s.', str(_header_path))
guard = '_'.join(_header_path.with_suffix('').parts[1:]).upper()
generate_file(_HEADER_TEMPLATE_NAME, _header_path, {
'guard': guard,
'namespace': _namespace
})
def create_source() -> None:
if _source_path is None:
return
_logger.info('Generating source at %s.', str(_source_path))
generate_file(_SOURCE_TEMPLATE_NAME, _source_path, {
'header_path': '/'.join(_header_path.parts[1:]) if _header_path else None,
'namespace': _namespace
})
def script_main() -> None:
verify_tools()
query_params()
create_header()
create_source()
if __name__ == '__main__':
run_script(script_main)