Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make all imports appear at top of file.
PEP8 requires this, flake8 complains, and I figured it’d be easier to
refactor it as much as possible (instead of just blindly ignoring the warning).

Comments and sanity checks welcome.

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Feb 7, 2015
1 parent 1b29533 commit 420fdf2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 78 deletions.
33 changes: 15 additions & 18 deletions nikola/nikola.py
Expand Up @@ -41,35 +41,20 @@
except ImportError:
from urllib.parse import urlparse, urlsplit, urljoin # NOQA

from blinker import signal
try:
import pyphen
except ImportError:
pyphen = None
import dateutil.tz

import dateutil.tz
import logging
from . import DEBUG

if DEBUG:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.ERROR)

import PyRSS2Gen as rss

import lxml.html
from yapsy.PluginManager import PluginManager

# Default "Read more..." link
DEFAULT_INDEX_READ_MORE_LINK = '<p class="more"><a href="{link}">{read_more}…</a></p>'
DEFAULT_RSS_READ_MORE_LINK = '<p><a href="{link}">{read_more}…</a> ({min_remaining_read})</p>'

# Default pattern for translation files' names
DEFAULT_TRANSLATIONS_PATTERN = '{path}.{lang}.{ext}'
from blinker import signal

from .post import Post
from . import utils
from . import DEBUG, utils
from .plugin_categories import (
Command,
LateTask,
Expand All @@ -83,6 +68,18 @@
ConfigPlugin,
)

if DEBUG:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.ERROR)

# Default "Read more..." link
DEFAULT_INDEX_READ_MORE_LINK = '<p class="more"><a href="{link}">{read_more}…</a></p>'
DEFAULT_RSS_READ_MORE_LINK = '<p><a href="{link}">{read_more}…</a> ({min_remaining_read})</p>'

# Default pattern for translation files' names
DEFAULT_TRANSLATIONS_PATTERN = '{path}.{lang}.{ext}'


config_changed = utils.config_changed

Expand Down
10 changes: 5 additions & 5 deletions nikola/plugin_categories.py
Expand Up @@ -28,6 +28,11 @@
import sys
import os

from yapsy.IPlugin import IPlugin
from doit.cmd_base import Command as DoitCommand

from .utils import LOGGER, first_line

__all__ = [
'Command',
'LateTask',
Expand All @@ -40,11 +45,6 @@
'SignalHandler'
]

from yapsy.IPlugin import IPlugin
from doit.cmd_base import Command as DoitCommand

from .utils import LOGGER, first_line


class BasePlugin(IPlugin):
"""Base plugin class."""
Expand Down
7 changes: 3 additions & 4 deletions nikola/plugins/compile/rest/listing.py
Expand Up @@ -37,6 +37,8 @@
except ImportError:
from urllib.parse import urlunsplit # NOQA

import docutils.parsers.rst.directives.body
import docutils.parsers.rst.directives.misc
from docutils import core
from docutils import nodes
from docutils.parsers.rst import Directive, directives
Expand All @@ -48,6 +50,7 @@
import pygments.util

from nikola import utils
from nikola.plugin_categories import RestExtension


# A sanitized version of docutils.parsers.rst.directives.body.CodeBlock.
Expand Down Expand Up @@ -115,13 +118,9 @@ def run(self):
return [node]

# Monkey-patch: replace insane docutils CodeBlock with our implementation.
import docutils.parsers.rst.directives.body
import docutils.parsers.rst.directives.misc
docutils.parsers.rst.directives.body.CodeBlock = CodeBlock
docutils.parsers.rst.directives.misc.CodeBlock = CodeBlock

from nikola.plugin_categories import RestExtension


class Plugin(RestExtension):

Expand Down
3 changes: 1 addition & 2 deletions nikola/plugins/task/galleries.py
Expand Up @@ -38,15 +38,14 @@
from urllib.parse import urljoin # NOQA

import natsort
Image = None
try:
from PIL import Image # NOQA
except ImportError:
try:
import Image as _Image
Image = _Image
except ImportError:
pass
Image = None

import PyRSS2Gen as rss

Expand Down
88 changes: 39 additions & 49 deletions nikola/utils.py
Expand Up @@ -27,7 +27,6 @@
"""Utility functions."""

from __future__ import print_function, unicode_literals, absolute_import
from collections import defaultdict, Callable
import calendar
import datetime
import dateutil.tz
Expand All @@ -42,20 +41,50 @@
import shutil
import subprocess
import sys
from zipfile import ZipFile as zipf
try:
from imp import reload
except ImportError:
pass

import dateutil.parser
import dateutil.tz
import logbook
import warnings
import PyRSS2Gen as rss
from collections import defaultdict, Callable
from logbook.more import ExceptionHandler, ColorizedStderrHandler
from pygments.formatters import HtmlFormatter
from zipfile import ZipFile as zipf
from doit import tools
from unidecode import unidecode
from pkg_resources import resource_filename
from doit.cmdparse import CmdParse

from nikola import DEBUG

__all__ = ['get_theme_path', 'get_theme_chain', 'load_messages', 'copy_tree',
'copy_file', 'slugify', 'unslugify', 'to_datetime', 'apply_filters',
'config_changed', 'get_crumbs', 'get_tzname', 'get_asset_path',
'_reload', 'unicode_str', 'bytes_str', 'unichr', 'Functionary',
'TranslatableSetting', 'TemplateHookRegistry', 'LocaleBorg',
'sys_encode', 'sys_decode', 'makedirs', 'get_parent_theme_name',
'demote_headers', 'get_translation_candidate', 'write_metadata',
'ask', 'ask_yesno', 'options2docstring', 'os_path_split',
'get_displayed_page_number', 'adjust_name_for_index_path_list',
'adjust_name_for_index_path', 'adjust_name_for_index_link',
'NikolaPygmentsHTML', 'create_redirect']

# Are you looking for 'generic_rss_renderer'?
# It's defined in nikola.nikola.Nikola (the site object).

if sys.version_info[0] == 3:
# Python 3
bytes_str = bytes
unicode_str = str
unichr = chr
raw_input = input
from imp import reload as _reload
else:
bytes_str = str
unicode_str = unicode # NOQA
_reload = reload # NOQA
unichr = unichr


class ApplicationWarning(Exception):
pass
Expand Down Expand Up @@ -100,9 +129,6 @@ def get_logger(name, handlers):
logging.basicConfig(level=logging.INFO)


import warnings


def showwarning(message, category, filename, lineno, file=None, line=None):
"""Show a warning (from the warnings subsystem) to the user."""
try:
Expand Down Expand Up @@ -159,42 +185,8 @@ def req_missing(names, purpose, python=True, optional=False):

return msg

if sys.version_info[0] == 3:
# Python 3
bytes_str = bytes
unicode_str = str
unichr = chr
raw_input = input
from imp import reload as _reload
else:
bytes_str = str
unicode_str = unicode # NOQA
_reload = reload # NOQA
unichr = unichr

from doit import tools
from unidecode import unidecode
from pkg_resources import resource_filename
from nikola import filters as task_filters

import PyRSS2Gen as rss

__all__ = ['get_theme_path', 'get_theme_chain', 'load_messages', 'copy_tree',
'copy_file', 'slugify', 'unslugify', 'to_datetime', 'apply_filters',
'config_changed', 'get_crumbs', 'get_tzname', 'get_asset_path',
'_reload', 'unicode_str', 'bytes_str', 'unichr', 'Functionary',
'TranslatableSetting', 'TemplateHookRegistry', 'LocaleBorg',
'sys_encode', 'sys_decode', 'makedirs', 'get_parent_theme_name',
'demote_headers', 'get_translation_candidate', 'write_metadata',
'ask', 'ask_yesno', 'options2docstring', 'os_path_split',
'get_displayed_page_number', 'adjust_name_for_index_path_list',
'adjust_name_for_index_path', 'adjust_name_for_index_link',
'NikolaPygmentsHTML', 'create_redirect']

# Are you looking for 'generic_rss_renderer'?
# It's defined in nikola.nikola.Nikola (the site object).


from nikola import filters as task_filters # NOQA
ENCODING = sys.getfilesystemencoding() or sys.stdin.encoding


Expand Down Expand Up @@ -1287,10 +1279,6 @@ def ask_yesno(query, default=None):
return ask_yesno(query, default)


from nikola.plugin_categories import Command
from doit.cmdparse import CmdParse


class CommandWrapper(object):
"""Converts commands into functions."""

Expand Down Expand Up @@ -1341,6 +1329,8 @@ def _run(self, cmd_args):
self.main.run(cmd_args)

def _run_with_kw(self, cmd, *a, **kw):
# cyclic import hack
from nikola.plugin_categories import Command
cmd = self.main.sub_cmds[cmd]
options, _ = CmdParse(cmd.options).parse([])
options.update(kw)
Expand Down

0 comments on commit 420fdf2

Please sign in to comment.