Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix docstrings (pep257).
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jul 27, 2015
1 parent e807212 commit d5d1993
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 222 deletions.
2 changes: 2 additions & 0 deletions nikola/__init__.py
Expand Up @@ -24,6 +24,8 @@
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Nikola -- a modular, fast, simple, static website generator."""

from __future__ import absolute_import
import os

Expand Down
30 changes: 25 additions & 5 deletions nikola/__main__.py
Expand Up @@ -24,6 +24,8 @@
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""The main function of Nikola."""

from __future__ import print_function, unicode_literals
from collections import defaultdict
import os
Expand Down Expand Up @@ -63,6 +65,7 @@


def main(args=None):
"""Run Nikola."""
colorful = False
if sys.stderr.isatty() and os.name != 'nt':
colorful = True
Expand Down Expand Up @@ -172,11 +175,12 @@ def main(args=None):


class Help(DoitHelp):
"""show Nikola usage."""

"""Show Nikola usage."""

@staticmethod
def print_usage(cmds):
"""print nikola "usage" (basic help) instructions"""
"""Print nikola "usage" (basic help) instructions."""
# Remove 'run'. Nikola uses 'build', though we support 'run' for
# people used to it (eg. doit users).
# WARNING: 'run' is the vanilla doit command, without support for
Expand All @@ -195,8 +199,11 @@ def print_usage(cmds):


class Build(DoitRun):
"""expose "run" command as "build" for backward compatibility"""

"""Expose "run" command as "build" for backwards compatibility."""

def __init__(self, *args, **kw):
"""Initialize Build."""
opts = list(self.cmd_options)
opts.append(
{
Expand Down Expand Up @@ -231,9 +238,11 @@ def __init__(self, *args, **kw):


class Clean(DoitClean):
"""A clean that removes cache/"""

"""Clean site, including the cache directory."""

def clean_tasks(self, tasks, dryrun):
"""Clean tasks."""
if not dryrun and config:
cache_folder = config.get('CACHE_FOLDER', 'cache')
if os.path.exists(cache_folder):
Expand All @@ -246,12 +255,16 @@ def clean_tasks(self, tasks, dryrun):


class NikolaTaskLoader(TaskLoader):
"""custom task loader to get tasks from Nikola instead of dodo.py file"""

"""Nikola-specific task loader."""

def __init__(self, nikola, quiet=False):
"""Initialize the loader."""
self.nikola = nikola
self.quiet = quiet

def load_tasks(self, cmd, opt_values, pos_args):
"""Load Nikola tasks."""
if self.quiet:
DOIT_CONFIG = {
'verbosity': 0,
Expand All @@ -275,17 +288,22 @@ def load_tasks(self, cmd, opt_values, pos_args):


class DoitNikola(DoitMain):

"""Nikola-specific implementation of DoitMain."""

# overwite help command
DOIT_CMDS = list(DoitMain.DOIT_CMDS) + [Help, Build, Clean, DoitAuto]
TASK_LOADER = NikolaTaskLoader

def __init__(self, nikola, quiet=False):
"""Initialzie DoitNikola."""
super(DoitNikola, self).__init__()
self.nikola = nikola
nikola.doit = self
self.task_loader = self.TASK_LOADER(nikola, quiet)

def get_cmds(self):
"""Get commands."""
# core doit commands
cmds = DoitMain.get_cmds(self)
# load nikola commands
Expand All @@ -294,6 +312,7 @@ def get_cmds(self):
return cmds

def run(self, cmd_args):
"""Run Nikola."""
sub_cmds = self.get_cmds()
args = self.process_args(cmd_args)
args = [sys_decode(arg) for arg in args]
Expand Down Expand Up @@ -341,6 +360,7 @@ def run(self, cmd_args):

@staticmethod
def print_version():
"""Print Nikola version."""
print("Nikola v" + __version__)


Expand Down
37 changes: 29 additions & 8 deletions nikola/filters.py
Expand Up @@ -24,7 +24,7 @@
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Utility functions to help you run filters on files."""
"""Utility functions to help run filters on files."""

from .utils import req_missing
from functools import wraps
Expand All @@ -42,9 +42,12 @@


def apply_to_binary_file(f):
"""Take a function f that transforms a data argument, and returns
"""Apply a filter to a binary file.
Take a function f that transforms a data argument, and returns
a function that takes a filename and applies f to the contents,
in place. Reads files in binary mode."""
in place. Reads files in binary mode.
"""
@wraps(f)
def f_in_file(fname):
with open(fname, 'rb') as inf:
Expand All @@ -57,9 +60,12 @@ def f_in_file(fname):


def apply_to_text_file(f):
"""Take a function f that transforms a data argument, and returns
"""Apply a filter to a text file.
Take a function f that transforms a data argument, and returns
a function that takes a filename and applies f to the contents,
in place. Reads files in UTF-8."""
in place. Reads files in UTF-8.
"""
@wraps(f)
def f_in_file(fname):
with io.open(fname, 'r', encoding='utf-8') as inf:
Expand All @@ -72,7 +78,7 @@ def f_in_file(fname):


def list_replace(the_list, find, replacement):
"Replace all occurrences of ``find`` with ``replacement`` in ``the_list``"
"""Replace all occurrences of ``find`` with ``replacement`` in ``the_list``."""
for i, v in enumerate(the_list):
if v == find:
the_list[i] = replacement
Expand All @@ -93,7 +99,6 @@ def runinplace(command, infile):
You can also supply command as a list.
"""

if not isinstance(command, list):
command = shlex.split(command)

Expand All @@ -118,6 +123,7 @@ def runinplace(command, infile):


def yui_compressor(infile):
"""Run YUI Compressor on a file."""
yuicompressor = False
try:
subprocess.call('yui-compressor', stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
Expand All @@ -136,39 +142,48 @@ def yui_compressor(infile):


def closure_compiler(infile):
"""Run closure-compiler on a file."""
return runinplace(r'closure-compiler --warning_level QUIET --js %1 --js_output_file %2', infile)


def optipng(infile):
"""Run optipng on a file."""
return runinplace(r"optipng -preserve -o2 -quiet %1", infile)


def jpegoptim(infile):
"""Run jpegoptim on a file."""
return runinplace(r"jpegoptim -p --strip-all -q %1", infile)


def html_tidy_withconfig(infile):
"""Run HTML Tidy with tidy5.conf as config file."""
return _html_tidy_runner(infile, r"-quiet --show-info no --show-warnings no -utf8 -indent -config tidy5.conf -modify %1")


def html_tidy_nowrap(infile):
"""Run HTML Tidy without line wrapping."""
return _html_tidy_runner(infile, r"-quiet --show-info no --show-warnings no -utf8 -indent --indent-attributes no --sort-attributes alpha --wrap 0 --wrap-sections no --drop-empty-elements no --tidy-mark no -modify %1")


def html_tidy_wrap(infile):
"""Run HTML Tidy with line wrapping."""
return _html_tidy_runner(infile, r"-quiet --show-info no --show-warnings no -utf8 -indent --indent-attributes no --sort-attributes alpha --wrap 80 --wrap-sections no --drop-empty-elements no --tidy-mark no -modify %1")


def html_tidy_wrap_attr(infile):
"""Run HTML tidy with line wrapping and attribute indentation."""
return _html_tidy_runner(infile, r"-quiet --show-info no --show-warnings no -utf8 -indent --indent-attributes yes --sort-attributes alpha --wrap 80 --wrap-sections no --drop-empty-elements no --tidy-mark no -modify %1")


def html_tidy_mini(infile):
"""Run HTML tidy with minimal settings."""
return _html_tidy_runner(infile, r"-quiet --show-info no --show-warnings no -utf8 --indent-attributes no --sort-attributes alpha --wrap 0 --wrap-sections no --tidy-mark no --drop-empty-elements no -modify %1")


def _html_tidy_runner(infile, options):
""" Warnings (returncode 1) are not critical, and *everything* is a warning """
"""Run HTML Tidy."""
# Warnings (returncode 1) are not critical, and *everything* is a warning.
try:
status = runinplace(r"tidy5 " + options, infile)
except subprocess.CalledProcessError as err:
Expand All @@ -178,6 +193,7 @@ def _html_tidy_runner(infile, options):

@apply_to_text_file
def html5lib_minify(data):
"""Minify with html5lib."""
import html5lib
import html5lib.serializer
data = html5lib.serializer.serialize(html5lib.parse(data, treebuilder='lxml'),
Expand All @@ -193,6 +209,7 @@ def html5lib_minify(data):

@apply_to_text_file
def html5lib_xmllike(data):
"""Transform document to an XML-like form with html5lib."""
import html5lib
import html5lib.serializer
data = html5lib.serializer.serialize(html5lib.parse(data, treebuilder='lxml'),
Expand All @@ -207,11 +224,13 @@ def html5lib_xmllike(data):

@apply_to_text_file
def minify_lines(data):
"""Do nothing -- deprecated filter."""
return data


@apply_to_text_file
def typogrify(data):
"""Prettify text with typogrify."""
if typo is None:
req_missing(['typogrify'], 'use the typogrify filter')

Expand All @@ -226,6 +245,7 @@ def typogrify(data):

@apply_to_text_file
def typogrify_sans_widont(data):
"""Prettify text with typogrify, skipping the widont filter."""
# typogrify with widont disabled because it caused broken headline
# wrapping, see issue #1465
if typo is None:
Expand All @@ -241,6 +261,7 @@ def typogrify_sans_widont(data):

@apply_to_text_file
def php_template_injection(data):
"""Insert PHP code into Nikola templates."""
import re
template = re.search('<\!-- __NIKOLA_PHP_TEMPLATE_INJECTION source\:(.*) checksum\:(.*)__ -->', data)
if template:
Expand Down
3 changes: 3 additions & 0 deletions nikola/image_processing.py
Expand Up @@ -24,6 +24,8 @@
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Process images."""

from __future__ import unicode_literals
import datetime
import os
Expand All @@ -43,6 +45,7 @@


class ImageProcessor(object):

"""Apply image operations."""

image_ext_list_builtin = ['.jpg', '.png', '.jpeg', '.gif', '.svg', '.bmp', '.tiff']
Expand Down

0 comments on commit d5d1993

Please sign in to comment.