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()))
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.')
def snippet(self, id, help=False, no_clipboard=False, no_stdout=False, **variables): """ Build a snippet from a ``skeleton.snippet.yaml`` file. --id <id> The ID of a snippet spec. Use "list-snippets" to list all available IDs. REQUIRED. --help Show help for the snippet spec. This includes information about the snippet spec, and all the available variables. --no-clipboard Do not copy results to clipboard. --no-stdout Do not write results to stdout. --<variable-name> <value> Set variables for the snippet spec. Use --help to see the available variables. I.E.: If the tree snippet takes a variable named ``project_name``, you would specify a value for this variable using ``--project-name "My project"``. """ config = self.__get_config() snippet = self.__get_snippet_by_id(config=config, full_id=id) snippet.validate_spec() if help: self.__print_spec_help(toplevel_spec=snippet) return self.__validate_values(variables=snippet.variables, valuedict=variables) snippet.variables.set_variable_values(**variables) if not no_stdout: cliutils.safe_print(snippet.render()) if not no_clipboard: snippet.render_to_clipboard() cliutils.safe_print('') cliutils.print_success('Copied to clipboard')
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()
def __print_variables_help(self, variables): for variable in variables.iterate_input_variables(): suffix = '' if variable.is_required(): suffix = colorize.colored_text(' [REQUIRED]', color=colorize.COLOR_YELLOW, bold=True) cliutils.safe_print('--{} <value>{}'.format( variable.name.replace('_', '-'), suffix)) indent = ' ' if variable.help_text: cliutils.safe_print( textwrap.indent(textwrap.fill(variable.help_text), indent)) if variable.default: if variable.default.variable: message = 'Defaults to the value of the {!r} variable.'.format( variable.default.variable) else: message = 'Defaults to {!r}.'.format( variable.default.value) cliutils.safe_print(textwrap.indent(message, indent))
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)
def list_directories(self): config = self.__get_config() for spec_directory in sorted(config.spec_directories): cliutils.safe_print('- {}'.format(spec_directory))