Skip to content

Commit

Permalink
Move our copy of textwrap.indent to utils
Browse files Browse the repository at this point in the history
It was used in both install_theme and plugin, this puts it in one place for
cleaner code.

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jun 21, 2015
1 parent 287b306 commit 0ed7211
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
23 changes: 2 additions & 21 deletions nikola/plugins/command/install_theme.py
Expand Up @@ -40,25 +40,6 @@
LOGGER = utils.get_logger('install_theme', utils.STDERR_HANDLER)


# Stolen from textwrap in Python 3.3.2.
def indent(text, prefix, predicate=None): # NOQA
"""Adds 'prefix' to the beginning of selected lines in 'text'.
If 'predicate' is provided, 'prefix' will only be added to the lines
where 'predicate(line)' is True. If 'predicate' is not provided,
it will default to adding 'prefix' to all non-empty lines that do not
consist solely of whitespace characters.
"""
if predicate is None:
def predicate(line):
return line.strip()

def prefixed_lines():
for line in text.splitlines(True):
yield (prefix + line if predicate(line) else line)
return ''.join(prefixed_lines())


class CommandInstallTheme(Command):
"""Install a theme."""

Expand Down Expand Up @@ -165,9 +146,9 @@ def do_install(self, name, data):
print('Contents of the conf.py.sample file:\n')
with io.open(confpypath, 'r', encoding='utf-8') as fh:
if self.site.colorful:
print(indent(pygments.highlight(
print(utils.indent(pygments.highlight(
fh.read(), PythonLexer(), TerminalFormatter()),
4 * ' '))
else:
print(indent(fh.read(), 4 * ' '))
print(utils.indent(fh.read(), 4 * ' '))
return True
29 changes: 5 additions & 24 deletions nikola/plugins/command/plugin.py
Expand Up @@ -42,25 +42,6 @@
LOGGER = utils.get_logger('plugin', utils.STDERR_HANDLER)


# Stolen from textwrap in Python 3.3.2.
def indent(text, prefix, predicate=None): # NOQA
"""Adds 'prefix' to the beginning of selected lines in 'text'.
If 'predicate' is provided, 'prefix' will only be added to the lines
where 'predicate(line)' is True. If 'predicate' is not provided,
it will default to adding 'prefix' to all non-empty lines that do not
consist solely of whitespace characters.
"""
if predicate is None:
def predicate(line):
return line.strip()

def prefixed_lines():
for line in text.splitlines(True):
yield (prefix + line if predicate(line) else line)
return ''.join(prefixed_lines())


class CommandPlugin(Command):
"""Manage plugins."""

Expand Down Expand Up @@ -254,7 +235,7 @@ def do_install(self, url, name):
LOGGER.error('Could not install the dependencies.')
print('Contents of the requirements.txt file:\n')
with io.open(reqpath, 'r', encoding='utf-8') as fh:
print(indent(fh.read(), 4 * ' '))
print(utils.indent(fh.read(), 4 * ' '))
print('You have to install those yourself or through a '
'package manager.')
else:
Expand All @@ -268,8 +249,8 @@ def do_install(self, url, name):
with io.open(reqnpypath, 'r', encoding='utf-8') as fh:
for l in fh.readlines():
i, j = l.split('::')
print(indent(i.strip(), 4 * ' '))
print(indent(j.strip(), 8 * ' '))
print(utils.indent(i.strip(), 4 * ' '))
print(utils.indent(j.strip(), 8 * ' '))
print()

print('You have to install those yourself or through a package '
Expand All @@ -280,11 +261,11 @@ def do_install(self, url, name):
print('Contents of the conf.py.sample file:\n')
with io.open(confpypath, 'r', encoding='utf-8') as fh:
if self.site.colorful:
print(indent(pygments.highlight(
print(utils.indent(pygments.highlight(
fh.read(), PythonLexer(), TerminalFormatter()),
4 * ' '))
else:
print(indent(fh.read(), 4 * ' '))
print(utils.indent(fh.read(), 4 * ' '))
return True

def do_uninstall(self, name):
Expand Down
21 changes: 20 additions & 1 deletion nikola/utils.py
Expand Up @@ -69,7 +69,7 @@
'adjust_name_for_index_path', 'adjust_name_for_index_link',
'NikolaPygmentsHTML', 'create_redirect', 'TreeNode',
'flatten_tree_structure', 'parse_escaped_hierarchical_category_name',
'join_hierarchical_category_path']
'join_hierarchical_category_path', 'indent']

# Are you looking for 'generic_rss_renderer'?
# It's defined in nikola.nikola.Nikola (the site object).
Expand Down Expand Up @@ -1617,3 +1617,22 @@ def escape(s):
return s.replace('\\', '\\\\').replace('/', '\\/')

return '/'.join([escape(p) for p in category_path])


# Stolen from textwrap in Python 3.4.3.
def indent(text, prefix, predicate=None):
"""Adds 'prefix' to the beginning of selected lines in 'text'.
If 'predicate' is provided, 'prefix' will only be added to the lines
where 'predicate(line)' is True. If 'predicate' is not provided,
it will default to adding 'prefix' to all non-empty lines that do not
consist solely of whitespace characters.
"""
if predicate is None:
def predicate(line):
return line.strip()

def prefixed_lines():
for line in text.splitlines(True):
yield (prefix + line if predicate(line) else line)
return ''.join(prefixed_lines())

0 comments on commit 0ed7211

Please sign in to comment.