Exemple #1
0
 def __print_create_tree_preview(self,
                                 writers,
                                 skipped_writers,
                                 previewmode='short'):
     if previewmode == 'none':
         return
     if writers:
         for writer in writers:
             cliutils.print_success(writer.get_absolute_output_path())
             if previewmode == 'filenames':
                 continue
             content = writer.get_content().strip()
             if not content:
                 content = '--- EMPTY FILE ---'
             if previewmode != 'full':
                 contentlines = content.split('\n')
                 if len(contentlines) > 3:
                     contentlines = contentlines[:3]
                     contentlines.append('...')
                 content = '\n'.join(contentlines)
                 # if len(contentlines) > 3:
                 #     cliutils.print_blue('...')
             cliutils.safe_print(textwrap.indent(content, prefix='    '))
             cliutils.safe_print('')
     if skipped_writers:
         cliutils.print_warning(
             'Will skip the following files - they already exist:')
         for writer in skipped_writers:
             cliutils.safe_print('- {}'.format(
                 writer.get_absolute_output_path()))
         cliutils.print_bold('Use --overwrite to overwrite these files.')
     if not writers:
         cliutils.print_warning('All files already exist.')
Exemple #2
0
    def __print_spec_help(self, toplevel_spec):
        cliutils.safe_print('')
        cliutils.print_bold('ABOUT:')
        cliutils.safe_print('id: {}'.format(toplevel_spec.full_id))
        if toplevel_spec.title:
            cliutils.safe_print(toplevel_spec.title)
        if toplevel_spec.description:
            cliutils.safe_print('')
            cliutils.safe_print(toplevel_spec.description)
        cliutils.safe_print('')

        if toplevel_spec.variables:
            cliutils.print_bold('VARIABLES:')
            self.__print_variables_help(variables=toplevel_spec.variables)
Exemple #3
0
 def __list_specs(self, spec_class, create_command_name, quiet=False):
     config = self.__get_config()
     specs = spec.FileSystemLoader(config, spec_class).find()
     for context, spec_objects in self.__group_specs_by_context(
             specs).items():
         if not quiet:
             cliutils.safe_print('')
             cliutils.print_bold('{}:'.format(context))
         for spec_object in spec_objects:
             output = spec_object.full_id
             if not quiet and spec_object.title:
                 output = '{} ({})'.format(output, spec_object.title)
             cliutils.safe_print('- {}'.format(output))
     if not quiet:
         cliutils.safe_print('')
         cliutils.print_bold('Use:')
         cliutils.safe_print(
             '{prefix} {scriptname} {create_command_name} <id> --help'.
             format(prefix=colorize.colored_text('$ ',
                                                 color=colorize.COLOR_GREY,
                                                 bold=True),
                    scriptname=os.path.basename(sys.argv[0]),
                    create_command_name=create_command_name))
         cliutils.print_bold('to show usage help for a {}.'.format(
             spec_class.__name__.lower()))
Exemple #4
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()
Exemple #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()