Skip to content

Commit

Permalink
Add a reasonable self.logger to all plugins (#2977)
Browse files Browse the repository at this point in the history
* Add a reasonable self.logger to all plugins

* fix tests

* flake8

* Avoid double calls to get_logger

* flake8
  • Loading branch information
ralsina committed Mar 13, 2018
1 parent 8b2034f commit 5a2d2f4
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 43 deletions.
14 changes: 10 additions & 4 deletions nikola/plugin_categories.py
Expand Up @@ -26,14 +26,15 @@

"""Nikola plugin categories."""

import sys
import os
import io
import os
import sys

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

from .utils import LOGGER, first_line, req_missing
from .utils import LOGGER, first_line, get_logger, req_missing

try:
import typing # NOQA
Expand Down Expand Up @@ -63,10 +64,15 @@
class BasePlugin(IPlugin):
"""Base plugin class."""

logger = None

def set_site(self, site):
"""Set site, which is a Nikola instance."""
self.site = site
self.inject_templates()
self.logger = get_logger(self.name)
if not site.debug:
self.logger.level = logbook.base.WARNING

def inject_templates(self):
"""Inject 'templates/<engine>' (if exists) very early in the theme chain."""
Expand Down
4 changes: 1 addition & 3 deletions nikola/plugins/command/auto/__init__.py
Expand Up @@ -55,7 +55,7 @@
import pkg_resources

from nikola.plugin_categories import Command
from nikola.utils import dns_sd, req_missing, get_logger, get_theme_path
from nikola.utils import dns_sd, req_missing, get_theme_path
LRJS_PATH = os.path.join(os.path.dirname(__file__), 'livereload.js')

if sys.platform == 'win32':
Expand All @@ -66,7 +66,6 @@ class CommandAuto(Command):
"""Automatic rebuilds for Nikola."""

name = "auto"
logger = None
has_server = True
doc_purpose = "builds and serves a site; automatically detects site changes, rebuilds, and optionally refreshes a browser"
dns_sd = None
Expand Down Expand Up @@ -116,7 +115,6 @@ class CommandAuto(Command):

def _execute(self, options, args):
"""Start the watcher."""
self.logger = get_logger('auto')
self.sockets = []
self.rebuild_queue = asyncio.Queue()
self.last_rebuild = datetime.datetime.now()
Expand Down
4 changes: 0 additions & 4 deletions nikola/plugins/command/check.py
Expand Up @@ -43,7 +43,6 @@
import requests

from nikola.plugin_categories import Command
from nikola.utils import get_logger


def _call_nikola_list(site, cache=None):
Expand Down Expand Up @@ -103,7 +102,6 @@ class CommandCheck(Command):
"""Check the generated site."""

name = "check"
logger = None

doc_usage = "[-v] (-l [--find-sources] [-r] | -f [--clean-files])"
doc_purpose = "check links and files in the generated site"
Expand Down Expand Up @@ -158,8 +156,6 @@ class CommandCheck(Command):

def _execute(self, options, args):
"""Check the generated site."""
self.logger = get_logger('check')

if not options['links'] and not options['files'] and not options['clean']:
print(self.help())
return False
Expand Down
4 changes: 1 addition & 3 deletions nikola/plugins/command/deploy.py
Expand Up @@ -37,7 +37,7 @@
from blinker import signal

from nikola.plugin_categories import Command
from nikola.utils import get_logger, clean_before_deployment
from nikola.utils import clean_before_deployment


class CommandDeploy(Command):
Expand All @@ -48,11 +48,9 @@ class CommandDeploy(Command):
doc_usage = "[preset [preset...]]"
doc_purpose = "deploy the site"
doc_description = "Deploy the site by executing deploy commands from the presets listed on the command line. If no presets are specified, `default` is executed."
logger = None

def _execute(self, command, args):
"""Execute the deploy command."""
self.logger = get_logger('deploy')
# Get last successful deploy date
timestamp_path = os.path.join(self.site.config['CACHE_FOLDER'], 'lastdeploy')

Expand Down
5 changes: 1 addition & 4 deletions nikola/plugins/command/github_deploy.py
Expand Up @@ -32,7 +32,7 @@

from nikola.plugin_categories import Command
from nikola.plugins.command.check import real_scan_files
from nikola.utils import get_logger, req_missing, clean_before_deployment
from nikola.utils import req_missing, clean_before_deployment
from nikola.__main__ import main
from nikola import __version__

Expand Down Expand Up @@ -76,12 +76,9 @@ class CommandGitHubDeploy(Command):
'help': 'Commit message (default: Nikola auto commit.)',
},
]
logger = None

def _execute(self, options, args):
"""Run the deployment."""
self.logger = get_logger(CommandGitHubDeploy.name)

# Check if ghp-import is installed
check_ghp_import_installed()

Expand Down
4 changes: 1 addition & 3 deletions nikola/plugins/command/serve.py
Expand Up @@ -46,7 +46,7 @@


from nikola.plugin_categories import Command
from nikola.utils import dns_sd, get_logger
from nikola.utils import dns_sd


class IPv6Server(HTTPServer):
Expand All @@ -61,7 +61,6 @@ class CommandServe(Command):
name = "serve"
doc_usage = "[options]"
doc_purpose = "start the test webserver"
logger = None
dns_sd = None

cmd_options = (
Expand Down Expand Up @@ -120,7 +119,6 @@ def shutdown(self, signum=None, _frame=None):

def _execute(self, options, args):
"""Start test server."""
self.logger = get_logger('serve')
out_dir = self.site.config['OUTPUT_FOLDER']
if not os.path.isdir(out_dir):
self.logger.error("Missing '{0}' folder?".format(out_dir))
Expand Down
7 changes: 1 addition & 6 deletions nikola/plugins/compile/ipynb.py
Expand Up @@ -42,7 +42,7 @@

from nikola import shortcodes as sc
from nikola.plugin_categories import PageCompiler
from nikola.utils import makedirs, req_missing, get_logger, LocaleBorg
from nikola.utils import makedirs, req_missing, LocaleBorg


class CompileIPynb(PageCompiler):
Expand All @@ -54,11 +54,6 @@ class CompileIPynb(PageCompiler):
default_kernel = 'python3'
supports_metadata = True

def set_site(self, site):
"""Set Nikola site."""
self.logger = get_logger('compile_ipynb')
super(CompileIPynb, self).set_site(site)

def _compile_string(self, nb_json):
"""Export notebooks as HTML strings."""
self._req_missing_ipynb()
Expand Down
5 changes: 0 additions & 5 deletions nikola/plugins/compile/rest/__init__.py
Expand Up @@ -45,7 +45,6 @@
from nikola.plugin_categories import PageCompiler
from nikola.utils import (
unicode_str,
get_logger,
makedirs,
write_metadata,
LocaleBorg,
Expand Down Expand Up @@ -185,10 +184,6 @@ def set_site(self, site):
self.config_dependencies.append(plugin_info.name)
plugin_info.plugin_object.short_help = plugin_info.description

self.logger = get_logger('compile_rest')
if not site.debug:
self.logger.level = logbook.base.WARNING


def get_observer(settings):
"""Return an observer for the docutils Reporter."""
Expand Down
3 changes: 1 addition & 2 deletions nikola/plugins/task/bundles.py
Expand Up @@ -45,13 +45,12 @@ class BuildBundles(LateTask):

def set_site(self, site):
"""Set Nikola site."""
self.logger = utils.get_logger('bundles')
super(BuildBundles, self).set_site(site)
if webassets is None and site.config['USE_BUNDLES']:
utils.req_missing(['webassets'], 'USE_BUNDLES', optional=True)
self.logger.warn('Setting USE_BUNDLES to False.')
site.config['USE_BUNDLES'] = False
site._GLOBAL_CONTEXT['use_bundles'] = False
super(BuildBundles, self).set_site(site)

def gen_tasks(self):
"""Bundle assets using WebAssets."""
Expand Down
5 changes: 1 addition & 4 deletions nikola/plugins/task/galleries.py
Expand Up @@ -62,12 +62,11 @@ class Galleries(Task, ImageProcessor):

def set_site(self, site):
"""Set Nikola site."""
super(Galleries, self).set_site(site)
site.register_path_handler('gallery', self.gallery_path)
site.register_path_handler('gallery_global', self.gallery_global_path)
site.register_path_handler('gallery_rss', self.gallery_rss_path)

self.logger = utils.get_logger('render_galleries')

self.kw = {
'thumbnail_size': site.config['THUMBNAIL_SIZE'],
'max_image_size': site.config['MAX_IMAGE_SIZE'],
Expand Down Expand Up @@ -104,8 +103,6 @@ def set_site(self, site):
# Create self.gallery_links
self.create_galleries_paths()

return super(Galleries, self).set_site(site)

def _find_gallery_path(self, name):
# The system using self.proper_gallery_links and self.improper_gallery_links
# is similar as in listings.py.
Expand Down
5 changes: 0 additions & 5 deletions nikola/plugins/task/scale_images.py
Expand Up @@ -38,11 +38,6 @@ class ScaleImage(Task, ImageProcessor):

name = "scale_images"

def set_site(self, site):
"""Set Nikola site."""
self.logger = utils.get_logger('scale_images')
return super(ScaleImage, self).set_site(site)

def process_tree(self, src, dst):
"""Process all images in a src tree and put the (possibly) rescaled images in the dst folder."""
thumb_fmt = self.kw['image_thumbnail_format']
Expand Down
1 change: 1 addition & 0 deletions tests/base.py
Expand Up @@ -167,6 +167,7 @@ class FakeSite(object):
def __init__(self):
self.template_system = self
self.invariant = False
self.debug = True
self.config = {
'DISABLED_PLUGINS': [],
'EXTRA_PLUGINS': [],
Expand Down

0 comments on commit 5a2d2f4

Please sign in to comment.