Skip to content

common

trestle.core.commands.author.common ¤

AuthorCommonCommands - reusable utilities to increase code base abstraction for author command.

logger ¤

Classes¤

AuthorCommonCommand (CommandPlusDocs) ¤

Extension for the subset of commands that operate using the common mode structure.

Source code in trestle/core/commands/author/common.py
class AuthorCommonCommand(CommandPlusDocs):
    """Extension for the subset of commands that operate using the common mode structure."""

    trestle_root: pathlib.Path

    task_name: str

    def _initialize(self, args: argparse.Namespace) -> int:
        log.set_log_level_from_args(args)
        # Externalize
        self.trestle_root = args.trestle_root
        self.task_name = args.task_name

        try:
            self.global_ = args.__getattribute__('global')
        except AttributeError:
            self.global_ = None

        if self.task_name:
            self.task_path = self.trestle_root / self.task_name
            if not file_utils.is_directory_name_allowed(self.task_name):
                logger.error(
                    f'Task name {self.task_name} is invalid as it interferes with OSCAL and trestle reserved names.'
                )
                return CmdReturnCodes.COMMAND_ERROR.value

        rc = self._setup_template_dir(args)

        return rc

    def rel_dir(self, path: pathlib.Path) -> str:
        """Stringify a directory relative to trestle root."""
        return str(path.relative_to(self.trestle_root))

    def _setup_template_dir(self, args: argparse.Namespace) -> int:
        """Set template directory and update to new format."""
        if not self.global_ and self.task_name is None:
            logger.error('At least a global flag or a task name should be provided.')
            return CmdReturnCodes.INCORRECT_ARGS.value
        if self.global_:
            old_template_dir = self.trestle_root / TRESTLE_CONFIG_DIR / 'author' / '__global__'
            self._set_template_version_to_latest(args, old_template_dir)
            self.template_dir = old_template_dir / args.template_version
        elif self.task_name and not self.global_:
            old_template_dir = self.trestle_root / TRESTLE_CONFIG_DIR / 'author' / self.task_name
            self._set_template_version_to_latest(args, old_template_dir)
            self.template_dir = old_template_dir / args.template_version

        if old_template_dir.exists():
            TemplateVersioning.update_template_folder_structure(old_template_dir)

        return CmdReturnCodes.SUCCESS.value

    def _set_template_version_to_latest(self, args: argparse.Namespace, template_dir: pathlib.Path):
        """Set template version argument to the latest version if none was given."""
        if not TemplateVersioning.is_valid_version(args.template_version):
            raise TrestleError(f'Version {args.template_version} is invalid, version format should be: 0.0.1')
        if args.template_version is None and args.mode == ARG_VALIDATE:
            # in validate mode no version will validate instances based on header version
            args.template_version = ''
        if args.template_version is None:
            args.template_version = START_TEMPLATE_VERSION
            if template_dir.exists():
                all_versions = TemplateVersioning.get_all_versions_for_task(template_dir)
                if all_versions:
                    args.template_version = max(all_versions)
        if args.template_version == '':
            logger.info('Instances will be validated against template version specified in their headers.')
        else:
            logger.info(f'Set template version to {args.template_version}.')
Methods¤
rel_dir(self, path) ¤

Stringify a directory relative to trestle root.

Source code in trestle/core/commands/author/common.py
def rel_dir(self, path: pathlib.Path) -> str:
    """Stringify a directory relative to trestle root."""
    return str(path.relative_to(self.trestle_root))

handler: python