Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to slsutil.banner #53499

Merged
merged 7 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions salt/modules/slsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,11 @@ def banner(width=72, commentchar='#', borderchar='#', blockstart=None, blockend=
:param newline: Boolean value to indicate whether the comment block should
end with a newline. Default is ``False``.
This banner can be injected into any templated file, for example:
**Example 1 - the default banner:**
.. code-block:: jinja
{{ salt['slsutil.banner'](width=120, commentchar='//') }}
The default banner:
{{ salt['slsutil.banner']() }}
.. code-block:: none
Expand All @@ -296,6 +294,42 @@ def banner(width=72, commentchar='#', borderchar='#', blockstart=None, blockend=
# The contents of this file are managed by Salt. Any changes to this #
# file may be overwritten automatically and without warning. #
########################################################################
**Example 2 - a Javadoc-style banner:**
.. code-block:: jinja
{{ salt['slsutil.banner'](commentchar=' *', borderchar='*', blockstart='/**', blockend=' */') }}
.. code-block:: none
/**
***********************************************************************
* *
* THIS FILE IS MANAGED BY SALT - DO NOT EDIT *
* *
* The contents of this file are managed by Salt. Any changes to this *
* file may be overwritten automatically and without warning. *
***********************************************************************
*/
**Example 3 - custom text:**
.. code-block:: jinja
{{ set copyright='This file may not be copied or distributed without permission of SaltStack, Inc.' }}
{{ salt['slsutil.banner'](title='Copyright 2019 SaltStack, Inc.', text=copyright, width=60) }}
.. code-block:: none
############################################################
# #
# Copyright 2019 SaltStack, Inc. #
# #
# This file may not be copied or distributed without #
# permission of SaltStack, Inc. #
############################################################
'''

if title is None:
Expand All @@ -304,18 +338,26 @@ def banner(width=72, commentchar='#', borderchar='#', blockstart=None, blockend=
if text is None:
text = ('The contents of this file are managed by Salt. '
'Any changes to this file may be overwritten '
'automatically and without warning')
'automatically and without warning.')

# Set up some typesetting variables
lgutter = commentchar.strip() + ' '
rgutter = ' ' + commentchar.strip()
ledge = commentchar.rstrip()
redge = commentchar.strip()
lgutter = ledge + ' '
rgutter = ' ' + redge
textwidth = width - len(lgutter) - len(rgutter)
border_line = commentchar + borderchar[:1] * (width - len(commentchar) * 2) + commentchar

# Check the width
if textwidth <= 0:
raise salt.exceptions.ArgumentValueError('Width is too small to render banner')

# Define the static elements
border_line = commentchar + borderchar[:1] * (width - len(ledge) - len(redge)) + redge
spacer_line = commentchar + ' ' * (width - len(commentchar) * 2) + commentchar
wrapper = textwrap.TextWrapper(width=(width - len(lgutter) - len(rgutter)))
block = list()

# Create the banner
wrapper = textwrap.TextWrapper(width=textwidth)
block = list()
if blockstart is not None:
block.append(blockstart)
block.append(border_line)
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/modules/test_slsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_banner(self):
self.check_banner(width=20)
self.check_banner(commentchar='//', borderchar='-')
self.check_banner(title='title here', text='text here')
self.check_banner(commentchar=' *')

def check_banner(self, width=72, commentchar='#', borderchar='#', blockstart=None, blockend=None,
title=None, text=None, newline=True):
Expand All @@ -42,7 +43,7 @@ def check_banner(self, width=72, commentchar='#', borderchar='#', blockstart=Non
for line in result:
self.assertEqual(len(line), width)
self.assertTrue(line.startswith(commentchar))
self.assertTrue(line.endswith(commentchar))
self.assertTrue(line.endswith(commentchar.strip()))

def test_boolstr(self):
'''
Expand Down