# Path: makeapp/appconfig.py
# class Config(metaclass=ConfigMeta):
#     """Base for application template configuration."""
# 
#     parent_template: List[str] = None
#     """Parent template names."""
# 
#     cleanup: List[str] = None
#     """Paths to cleanup after rollout."""
# 
#     def __init__(self, app_template: 'AppTemplate'):
#         """
# 
#         :param app_template:
# 
#         """
#         self.app_template = app_template
#         self.logger = app_template.maker.logger
# 
#         settings = {}
# 
#         for key, val in self.__class__.__dict__.items():
# 
#             if isinstance(val, ConfigSetting):
#                 settings[f'{app_template.name}_{key}'] = val
# 
#         self.settings = settings
# 
#     def hook_configure(self):
#         """Executed on rollout configuration."""
# 
#         settings_conf = self.settings
# 
#         if not settings_conf:
#             return
# 
#         app_template = self.app_template
#         settings_current = app_template.maker.settings
#         settings_gathered = {}
#         advertise_template = True
# 
#         for setting_name, settings_obj in settings_conf.items():
# 
#             current_value = settings_current.get(setting_name)
# 
#             if current_value is None:
# 
#                 if advertise_template:
#                     click.secho(
#                         f"Gathering settings for '{app_template.name}' application template ...",
#                         fg='blue'
#                     )
#                     advertise_template = False
# 
#                 current_value = click.prompt(
#                     f'{settings_obj.title} ({setting_name})',
#                     default=settings_obj.default,
#                     type=settings_obj.type,
#                 )
# 
#                 settings_gathered[setting_name] = current_value
# 
#         app_template.maker.update_settings(settings_gathered)
# 
#     def print_banner(self, text: str):
#         """Prints out a banner with the given text.
# 
#         :param text:
# 
#         """
#         max_line_len = max(map(len, text.splitlines()))
#         text = text + '\n' + '=' * max_line_len + '\n'
# 
#         self.logger.warning(text)
#         sleep(1)
# 
#     def hook_rollout_init(self):
#         """Executed on skeleton rollout initialization."""
# 
#     def hook_rollout_pre(self):
#         """Executed before application skeleton rollout."""
# 
#     def hook_rollout_post(self):
#         """Executed after application skeleton rollout."""
#         cleanup = self.cleanup or []
# 
#         for path in cleanup:
#             path = Path(path).absolute()
#             self.logger.info(f'Cleanup {path} ...')
#             rmtree(path, ignore_errors=True)
# 
# class ConfigSetting:
# 
#     name = None  # Runtime bound by metaclass.
# 
#     def __init__(self, title: str, default: Any = None, type: Any = None):
#         self.title = title
#         self.default = default
#         self.type = type
# 
#     def __get__(self, instance: 'Config', owner: Type['Config']) -> Any:
#         """Allows convenient IDE-friendly access from Config
#         heirs to settings defined in them.
# 
#         :param instance:
#         :param owner:
# 
#         """
#         app_template = instance.app_template
# 
#         return app_template.maker.settings[f'{app_template.name}_{self.name}']
# 
# Path: makeapp/utils.py
# @contextmanager
# def temp_dir() -> Generator[str, None, None]:
#     """Context manager to temporarily create a directory."""
# 
#     dir_tmp = tempfile.mkdtemp(prefix='makeapp_')
# 
#     try:
#         yield dir_tmp
# 
#     finally:
#         shutil.rmtree(dir_tmp, ignore_errors=True)
# 
# def replace_infile(filepath: str, pairs: Dict[str, str]):
#     """Replaces some term by another in file contents.
# 
#     :param filepath:
#     :param pairs: search -> replace.
# 
#     """
#     with fileinput.input(files=filepath, inplace=True) as f:
# 
#         for line in f:
# 
#             for search, replace in pairs.items():
#                 line = line.replace(search, replace)
# 
#             sys.stdout.write(line)
# 
# def run_command(command: str, *, err_msg: str = '') -> List[str]:
#     """Runs a command in a shell process.
# 
#     Returns a list of strings gathered from a command.
# 
#     :param command:
# 
#     :param err_msg: Message to show on error.
# 
#     :raises: CommandError
# 
#     """
#     prc = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True, universal_newlines=True)
# 
#     LOG.debug(f'Run command: `{command}` ...')
# 
#     data = []
# 
#     out, _ = prc.communicate()
# 
#     if isinstance(out, bytes):
#         out = out.decode('utf-8')
# 
#     has_error = prc.returncode
# 
#     for item in out.splitlines():
#         item = item.strip()
# 
#         if not item:
#             continue
# 
#         data.append(item)
# 
#     if has_error:
#         raise CommandError(err_msg or f"Command `{command}` failed: %s" % '\n'.join(data))
# 
#     return data


import os
import shutil
from functools import partial
from makeapp.appconfig import Config, ConfigSetting
from makeapp.utils import temp_dir, replace_infile, run_command


join = os.path.join  # Short alias


class WebscaffConfig(Config):

    parent_template = ['pytest']

    domain = ConfigSetting(title='Domain Name', default='')

# Path: makeapp/appconfig.py
# class Config(metaclass=ConfigMeta):
#     """Base for application template configuration."""
# 
#     parent_template: List[str] = None
#     """Parent template names."""
# 
#     cleanup: List[str] = None
#     """Paths to cleanup after rollout."""
# 
#     def __init__(self, app_template: 'AppTemplate'):
#         """
# 
#         :param app_template:
# 
#         """
#         self.app_template = app_template
#         self.logger = app_template.maker.logger
# 
#         settings = {}
# 
#         for key, val in self.__class__.__dict__.items():
# 
#             if isinstance(val, ConfigSetting):
#                 settings[f'{app_template.name}_{key}'] = val
# 
#         self.settings = settings
# 
#     def hook_configure(self):
#         """Executed on rollout configuration."""
# 
#         settings_conf = self.settings
# 
#         if not settings_conf:
#             return
# 
#         app_template = self.app_template
#         settings_current = app_template.maker.settings
#         settings_gathered = {}
#         advertise_template = True
# 
#         for setting_name, settings_obj in settings_conf.items():
# 
#             current_value = settings_current.get(setting_name)
# 
#             if current_value is None:
# 
#                 if advertise_template:
#                     click.secho(
#                         f"Gathering settings for '{app_template.name}' application template ...",
#                         fg='blue'
#                     )
#                     advertise_template = False
# 
#                 current_value = click.prompt(
#                     f'{settings_obj.title} ({setting_name})',
#                     default=settings_obj.default,
#                     type=settings_obj.type,
#                 )
# 
#                 settings_gathered[setting_name] = current_value
# 
#         app_template.maker.update_settings(settings_gathered)
# 
#     def print_banner(self, text: str):
#         """Prints out a banner with the given text.
# 
#         :param text:
# 
#         """
#         max_line_len = max(map(len, text.splitlines()))
#         text = text + '\n' + '=' * max_line_len + '\n'
# 
#         self.logger.warning(text)
#         sleep(1)
# 
#     def hook_rollout_init(self):
#         """Executed on skeleton rollout initialization."""
# 
#     def hook_rollout_pre(self):
#         """Executed before application skeleton rollout."""
# 
#     def hook_rollout_post(self):
#         """Executed after application skeleton rollout."""
#         cleanup = self.cleanup or []
# 
#         for path in cleanup:
#             path = Path(path).absolute()
#             self.logger.info(f'Cleanup {path} ...')
#             rmtree(path, ignore_errors=True)
# 
# class ConfigSetting:
# 
#     name = None  # Runtime bound by metaclass.
# 
#     def __init__(self, title: str, default: Any = None, type: Any = None):
#         self.title = title
#         self.default = default
#         self.type = type
# 
#     def __get__(self, instance: 'Config', owner: Type['Config']) -> Any:
#         """Allows convenient IDE-friendly access from Config
#         heirs to settings defined in them.
# 
#         :param instance:
#         :param owner:
# 
#         """
#         app_template = instance.app_template
# 
#         return app_template.maker.settings[f'{app_template.name}_{self.name}']
# 
# Path: makeapp/utils.py
# @contextmanager
# def temp_dir() -> Generator[str, None, None]:
#     """Context manager to temporarily create a directory."""
# 
#     dir_tmp = tempfile.mkdtemp(prefix='makeapp_')
# 
#     try:
#         yield dir_tmp
# 
#     finally:
#         shutil.rmtree(dir_tmp, ignore_errors=True)
# 
# def replace_infile(filepath: str, pairs: Dict[str, str]):
#     """Replaces some term by another in file contents.
# 
#     :param filepath:
#     :param pairs: search -> replace.
# 
#     """
#     with fileinput.input(files=filepath, inplace=True) as f:
# 
#         for line in f:
# 
#             for search, replace in pairs.items():
#                 line = line.replace(search, replace)
# 
#             sys.stdout.write(line)
# 
# def run_command(command: str, *, err_msg: str = '') -> List[str]:
#     """Runs a command in a shell process.
# 
#     Returns a list of strings gathered from a command.
# 
#     :param command:
# 
#     :param err_msg: Message to show on error.
# 
#     :raises: CommandError
# 
#     """
#     prc = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True, universal_newlines=True)
# 
#     LOG.debug(f'Run command: `{command}` ...')
# 
#     data = []
# 
#     out, _ = prc.communicate()
# 
#     if isinstance(out, bytes):
#         out = out.decode('utf-8')
# 
#     has_error = prc.returncode
# 
#     for item in out.splitlines():
#         item = item.strip()
# 
#         if not item:
#             continue
# 
#         data.append(item)
# 
#     if has_error:
#         raise CommandError(err_msg or f"Command `{command}` failed: %s" % '\n'.join(data))
# 
#     return data


# Path: makeapp/app_templates/webscaff/makeappconf.py
import os
import shutil
from functools import partial
from makeapp.appconfig import Config, ConfigSetting
from makeapp.utils import temp_dir, replace_infile, run_command



join = os.path.join  # Short alias


class WebscaffConfig(Config):

    parent_template = ['pytest']

    domain = ConfigSetting(title='Domain Name', default='')

    email = ConfigSetting(title='Admin E-mail')