Skip to content

Commit 420fdf2

Browse files
committedFeb 7, 2015
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>
1 parent 1b29533 commit 420fdf2

File tree

5 files changed

+63
-78
lines changed

5 files changed

+63
-78
lines changed
 

‎nikola/nikola.py

+15-18
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,20 @@
4141
except ImportError:
4242
from urllib.parse import urlparse, urlsplit, urljoin # NOQA
4343

44-
from blinker import signal
4544
try:
4645
import pyphen
4746
except ImportError:
4847
pyphen = None
49-
import dateutil.tz
5048

49+
import dateutil.tz
5150
import logging
52-
from . import DEBUG
53-
54-
if DEBUG:
55-
logging.basicConfig(level=logging.DEBUG)
56-
else:
57-
logging.basicConfig(level=logging.ERROR)
58-
5951
import PyRSS2Gen as rss
60-
6152
import lxml.html
6253
from yapsy.PluginManager import PluginManager
63-
64-
# Default "Read more..." link
65-
DEFAULT_INDEX_READ_MORE_LINK = '<p class="more"><a href="{link}">{read_more}…</a></p>'
66-
DEFAULT_RSS_READ_MORE_LINK = '<p><a href="{link}">{read_more}…</a> ({min_remaining_read})</p>'
67-
68-
# Default pattern for translation files' names
69-
DEFAULT_TRANSLATIONS_PATTERN = '{path}.{lang}.{ext}'
54+
from blinker import signal
7055

7156
from .post import Post
72-
from . import utils
57+
from . import DEBUG, utils
7358
from .plugin_categories import (
7459
Command,
7560
LateTask,
@@ -83,6 +68,18 @@
8368
ConfigPlugin,
8469
)
8570

71+
if DEBUG:
72+
logging.basicConfig(level=logging.DEBUG)
73+
else:
74+
logging.basicConfig(level=logging.ERROR)
75+
76+
# Default "Read more..." link
77+
DEFAULT_INDEX_READ_MORE_LINK = '<p class="more"><a href="{link}">{read_more}…</a></p>'
78+
DEFAULT_RSS_READ_MORE_LINK = '<p><a href="{link}">{read_more}…</a> ({min_remaining_read})</p>'
79+
80+
# Default pattern for translation files' names
81+
DEFAULT_TRANSLATIONS_PATTERN = '{path}.{lang}.{ext}'
82+
8683

8784
config_changed = utils.config_changed
8885

‎nikola/plugin_categories.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
import sys
2929
import os
3030

31+
from yapsy.IPlugin import IPlugin
32+
from doit.cmd_base import Command as DoitCommand
33+
34+
from .utils import LOGGER, first_line
35+
3136
__all__ = [
3237
'Command',
3338
'LateTask',
@@ -40,11 +45,6 @@
4045
'SignalHandler'
4146
]
4247

43-
from yapsy.IPlugin import IPlugin
44-
from doit.cmd_base import Command as DoitCommand
45-
46-
from .utils import LOGGER, first_line
47-
4848

4949
class BasePlugin(IPlugin):
5050
"""Base plugin class."""

‎nikola/plugins/compile/rest/listing.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
except ImportError:
3838
from urllib.parse import urlunsplit # NOQA
3939

40+
import docutils.parsers.rst.directives.body
41+
import docutils.parsers.rst.directives.misc
4042
from docutils import core
4143
from docutils import nodes
4244
from docutils.parsers.rst import Directive, directives
@@ -48,6 +50,7 @@
4850
import pygments.util
4951

5052
from nikola import utils
53+
from nikola.plugin_categories import RestExtension
5154

5255

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

117120
# Monkey-patch: replace insane docutils CodeBlock with our implementation.
118-
import docutils.parsers.rst.directives.body
119-
import docutils.parsers.rst.directives.misc
120121
docutils.parsers.rst.directives.body.CodeBlock = CodeBlock
121122
docutils.parsers.rst.directives.misc.CodeBlock = CodeBlock
122123

123-
from nikola.plugin_categories import RestExtension
124-
125124

126125
class Plugin(RestExtension):
127126

‎nikola/plugins/task/galleries.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@
3838
from urllib.parse import urljoin # NOQA
3939

4040
import natsort
41-
Image = None
4241
try:
4342
from PIL import Image # NOQA
4443
except ImportError:
4544
try:
4645
import Image as _Image
4746
Image = _Image
4847
except ImportError:
49-
pass
48+
Image = None
5049

5150
import PyRSS2Gen as rss
5251

‎nikola/utils.py

+39-49
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"""Utility functions."""
2828

2929
from __future__ import print_function, unicode_literals, absolute_import
30-
from collections import defaultdict, Callable
3130
import calendar
3231
import datetime
3332
import dateutil.tz
@@ -42,20 +41,50 @@
4241
import shutil
4342
import subprocess
4443
import sys
45-
from zipfile import ZipFile as zipf
46-
try:
47-
from imp import reload
48-
except ImportError:
49-
pass
50-
5144
import dateutil.parser
5245
import dateutil.tz
5346
import logbook
47+
import warnings
48+
import PyRSS2Gen as rss
49+
from collections import defaultdict, Callable
5450
from logbook.more import ExceptionHandler, ColorizedStderrHandler
5551
from pygments.formatters import HtmlFormatter
52+
from zipfile import ZipFile as zipf
53+
from doit import tools
54+
from unidecode import unidecode
55+
from pkg_resources import resource_filename
56+
from doit.cmdparse import CmdParse
5657

5758
from nikola import DEBUG
5859

60+
__all__ = ['get_theme_path', 'get_theme_chain', 'load_messages', 'copy_tree',
61+
'copy_file', 'slugify', 'unslugify', 'to_datetime', 'apply_filters',
62+
'config_changed', 'get_crumbs', 'get_tzname', 'get_asset_path',
63+
'_reload', 'unicode_str', 'bytes_str', 'unichr', 'Functionary',
64+
'TranslatableSetting', 'TemplateHookRegistry', 'LocaleBorg',
65+
'sys_encode', 'sys_decode', 'makedirs', 'get_parent_theme_name',
66+
'demote_headers', 'get_translation_candidate', 'write_metadata',
67+
'ask', 'ask_yesno', 'options2docstring', 'os_path_split',
68+
'get_displayed_page_number', 'adjust_name_for_index_path_list',
69+
'adjust_name_for_index_path', 'adjust_name_for_index_link',
70+
'NikolaPygmentsHTML', 'create_redirect']
71+
72+
# Are you looking for 'generic_rss_renderer'?
73+
# It's defined in nikola.nikola.Nikola (the site object).
74+
75+
if sys.version_info[0] == 3:
76+
# Python 3
77+
bytes_str = bytes
78+
unicode_str = str
79+
unichr = chr
80+
raw_input = input
81+
from imp import reload as _reload
82+
else:
83+
bytes_str = str
84+
unicode_str = unicode # NOQA
85+
_reload = reload # NOQA
86+
unichr = unichr
87+
5988

6089
class ApplicationWarning(Exception):
6190
pass
@@ -100,9 +129,6 @@ def get_logger(name, handlers):
100129
logging.basicConfig(level=logging.INFO)
101130

102131

103-
import warnings
104-
105-
106132
def showwarning(message, category, filename, lineno, file=None, line=None):
107133
"""Show a warning (from the warnings subsystem) to the user."""
108134
try:
@@ -159,42 +185,8 @@ def req_missing(names, purpose, python=True, optional=False):
159185

160186
return msg
161187

162-
if sys.version_info[0] == 3:
163-
# Python 3
164-
bytes_str = bytes
165-
unicode_str = str
166-
unichr = chr
167-
raw_input = input
168-
from imp import reload as _reload
169-
else:
170-
bytes_str = str
171-
unicode_str = unicode # NOQA
172-
_reload = reload # NOQA
173-
unichr = unichr
174-
175-
from doit import tools
176-
from unidecode import unidecode
177-
from pkg_resources import resource_filename
178-
from nikola import filters as task_filters
179-
180-
import PyRSS2Gen as rss
181-
182-
__all__ = ['get_theme_path', 'get_theme_chain', 'load_messages', 'copy_tree',
183-
'copy_file', 'slugify', 'unslugify', 'to_datetime', 'apply_filters',
184-
'config_changed', 'get_crumbs', 'get_tzname', 'get_asset_path',
185-
'_reload', 'unicode_str', 'bytes_str', 'unichr', 'Functionary',
186-
'TranslatableSetting', 'TemplateHookRegistry', 'LocaleBorg',
187-
'sys_encode', 'sys_decode', 'makedirs', 'get_parent_theme_name',
188-
'demote_headers', 'get_translation_candidate', 'write_metadata',
189-
'ask', 'ask_yesno', 'options2docstring', 'os_path_split',
190-
'get_displayed_page_number', 'adjust_name_for_index_path_list',
191-
'adjust_name_for_index_path', 'adjust_name_for_index_link',
192-
'NikolaPygmentsHTML', 'create_redirect']
193-
194-
# Are you looking for 'generic_rss_renderer'?
195-
# It's defined in nikola.nikola.Nikola (the site object).
196-
197188

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

200192

@@ -1287,10 +1279,6 @@ def ask_yesno(query, default=None):
12871279
return ask_yesno(query, default)
12881280

12891281

1290-
from nikola.plugin_categories import Command
1291-
from doit.cmdparse import CmdParse
1292-
1293-
12941282
class CommandWrapper(object):
12951283
"""Converts commands into functions."""
12961284

@@ -1341,6 +1329,8 @@ def _run(self, cmd_args):
13411329
self.main.run(cmd_args)
13421330

13431331
def _run_with_kw(self, cmd, *a, **kw):
1332+
# cyclic import hack
1333+
from nikola.plugin_categories import Command
13441334
cmd = self.main.sub_cmds[cmd]
13451335
options, _ = CmdParse(cmd.options).parse([])
13461336
options.update(kw)

0 commit comments

Comments
 (0)
Please sign in to comment.