Beispiel #1
0
 def __get_snippet_by_id(self, config, full_id):
     snippet_cache = spec.SpecCache(
         *spec.FileSystemLoader(config, spec.Snippet).find())
     try:
         return snippet_cache.get_spec(full_id=full_id)
     except KeyError:
         cliutils.print_error(
             'No snippet skeleton with this id: {}'.format(full_id))
         raise SystemExit()
Beispiel #2
0
 def __validate_values(self, variables, valuedict):
     errors = []
     for variable in variables.iterate_input_variables():
         value = valuedict.get(variable.name, None)
         if value is None:
             if variable.is_required():
                 errors.append(
                     'Variable {!r}: This variable is required.'.format(
                         variable.name))
         else:
             try:
                 variable.validate_value(value=value)
             except exceptions.ValueValidationError as error:
                 errors.append('Variable {!r}: {}'.format(
                     variable.name, error))
     if errors:
         for error in errors:
             cliutils.print_error(error)
         cliutils.safe_print('')
         cliutils.print_bold(
             'Try adding --help for documentation for all the variables.')
         raise SystemExit()
Beispiel #3
0
    def unregister(self, directory=None):
        """
        Unregister a skeleton directory.

        --directory
            The directory to unregister. Must be a directory
            with a ``trees`` or ``snippets`` subdirectory.
            Defaults to the current directory.
        """
        directory = directory or os.getcwd()
        self.__validate_skeleton_directory(directory=directory)
        config = self.__get_config()
        absolute_directory_path = os.path.abspath(directory)
        if config.has_spec_directory(directory):
            config.unregister_spec_directory(directory)
            config.save_to_disk()
            cliutils.print_success(
                '{!r} is no longer registered as a skeleton directory.'.format(
                    absolute_directory_path))
        else:
            cliutils.print_error(
                '{!r} is not a registered skeleton directory.'.format(
                    absolute_directory_path))
            raise SystemExit()
Beispiel #4
0
 def __validate_skeleton_directory(self, directory):
     if not self.__is_skeleton_directory(directory):
         cliutils.print_error(
             '{!r} is not a codeskeleton directory. Must have '
             'a "trees" or a "snippets" subdirectory.'.format(directory))
         raise SystemExit()
Beispiel #5
0
    def create_tree(self,
                    id,
                    out=None,
                    overwrite=False,
                    preview='short',
                    help=False,
                    pretend=False,
                    **variables):
        """
        Create a directory tree from a ``skeleton.tree.yaml`` file.

        --id <id>
            The ID of a tree skeleton spec. Use "list-trees" to list all available IDs.
            REQUIRED.
        --help
            Show help for the the tree spec. This includes information about the
            tree spec, and all the available variables.
        --out <directory>
            The output directory. Will be created if it does not exist.
            Defaults to the current directory.
        --overwrite
            If you use this, existing files in the output directory will be overwritten.
        --preview <none|filenames|short|full>
            Controls the level of verbosity for the preview of what will be created:

            - none: No preview.
            - filenames: Only list filenames.
            - short (default): List filenames and the 3 first lines of each file.
            - full: List filenames and the full content of each file.
        --pretend
            Do not change anything, just show what would be created.
        --<variable-name> <value>
            Set variables for the tree spec. Use --help to see the available variables.
            I.E.: If the tree spec takes a variable named ``project_name``, you would
            specify a value for this variable using ``--project-name "My project"``.
        """
        config = self.__get_config()
        tree = self.__get_tree_by_id(config=config, full_id=id)
        tree.validate_spec()
        if help:
            self.__print_spec_help(toplevel_spec=tree)
            return
        self.__validate_values(variables=tree.variables, valuedict=variables)
        tree.variables.set_variable_values(**variables)
        output_directory = out or os.getcwd()
        try:
            skipped_writers, writers = tree.collect_writers(
                output_directory=output_directory, overwrite=overwrite)
        except exceptions.SpecError as error:
            cliutils.print_error(error)
            raise SystemExit()
        if preview != 'none':
            cliutils.print_bold('The following will be created:')
        self.__print_create_tree_preview(writers=writers,
                                         skipped_writers=skipped_writers,
                                         previewmode=preview)
        if pretend or not writers:
            return
        if cliutils.confirm('Create the tree?'):
            for writer in writers:
                writer.write()