diff options
| author | 2021-02-03 19:17:50 -0500 | |
|---|---|---|
| committer | 2021-02-03 19:17:50 -0500 | |
| commit | 475d074fd74425efbe783fad08f97f2df0c4909f (patch) | |
| tree | 2acdae53999b3c74b716efa4edb5b40311fa356a /nikola/plugins/command/theme.py | |
| parent | cd502d52787f666fff3254d7d7e7578930c813c2 (diff) | |
| parent | 3a0d66f07b112b6d2bdc2b57bbf717a89a351ce6 (diff) | |
Update upstream source from tag 'upstream/8.1.2'
Update to upstream version '8.1.2'
with Debian dir e5e966a9e6010ef70618dc9a61558fa4db35aceb
Diffstat (limited to 'nikola/plugins/command/theme.py')
| -rw-r--r-- | nikola/plugins/command/theme.py | 102 |
1 files changed, 65 insertions, 37 deletions
diff --git a/nikola/plugins/command/theme.py b/nikola/plugins/command/theme.py index 7513491..6f4339a 100644 --- a/nikola/plugins/command/theme.py +++ b/nikola/plugins/command/theme.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright © 2012-2016 Roberto Alsina, Chris Warrick and others. +# Copyright © 2012-2020 Roberto Alsina, Chris Warrick and others. # Permission is hereby granted, free of charge, to any # person obtaining a copy of this software and associated @@ -26,13 +26,15 @@ """Manage themes.""" -from __future__ import print_function -import os +import configparser import io +import json.decoder +import os import shutil +import sys import time -import requests +import requests import pygments from pygments.lexers import PythonLexer from pygments.formatters import TerminalFormatter @@ -41,7 +43,7 @@ from pkg_resources import resource_filename from nikola.plugin_categories import Command from nikola import utils -LOGGER = utils.get_logger('theme', utils.STDERR_HANDLER) +LOGGER = utils.get_logger('theme') class CommandTheme(Command): @@ -89,9 +91,8 @@ class CommandTheme(Command): 'short': 'u', 'long': 'url', 'type': str, - 'help': "URL for the theme repository (default: " - "https://themes.getnikola.com/v7/themes.json)", - 'default': 'https://themes.getnikola.com/v7/themes.json' + 'help': "URL for the theme repository", + 'default': 'https://themes.getnikola.com/v8/themes.json' }, { 'name': 'getpath', @@ -122,14 +123,21 @@ class CommandTheme(Command): 'long': 'engine', 'type': str, 'default': 'mako', - 'help': 'Engine to use for new theme (mako or jinja -- default: mako)', + 'help': 'Engine to use for new theme (mako or jinja)', }, { 'name': 'new_parent', 'long': 'parent', 'type': str, 'default': 'base', - 'help': 'Parent to use for new theme (default: base)', + 'help': 'Parent to use for new theme', + }, + { + 'name': 'new_legacy_meta', + 'long': 'legacy-meta', + 'type': bool, + 'default': False, + 'help': 'Create legacy meta files for new theme', }, ] @@ -147,6 +155,7 @@ class CommandTheme(Command): new = options.get('new') new_engine = options.get('new_engine') new_parent = options.get('new_parent') + new_legacy_meta = options.get('new_legacy_meta') command_count = [bool(x) for x in ( install, uninstall, @@ -172,7 +181,7 @@ class CommandTheme(Command): elif copy_template: return self.copy_template(copy_template) elif new: - return self.new_theme(new, new_engine, new_parent) + return self.new_theme(new, new_engine, new_parent, new_legacy_meta) def do_install_deps(self, url, name): """Install themes and their dependencies.""" @@ -188,11 +197,11 @@ class CommandTheme(Command): try: utils.get_theme_path_real(parent_name, self.site.themes_dirs) break - except: # Not available + except Exception: # Not available self.do_install(parent_name, data) name = parent_name if installstatus: - LOGGER.notice('Remember to set THEME="{0}" in conf.py to use this theme.'.format(origname)) + LOGGER.info('Remember to set THEME="{0}" in conf.py to use this theme.'.format(origname)) def do_install(self, name, data): """Download and install a theme.""" @@ -225,15 +234,13 @@ class CommandTheme(Command): confpypath = os.path.join(dest_path, 'conf.py.sample') if os.path.exists(confpypath): - LOGGER.notice('This theme has a sample config file. Integrate it with yours in order to make this theme work!') + LOGGER.warning('This theme has a sample config file. Integrate it with yours in order to make this theme work!') print('Contents of the conf.py.sample file:\n') - with io.open(confpypath, 'r', encoding='utf-8') as fh: + with io.open(confpypath, 'r', encoding='utf-8-sig') as fh: if self.site.colorful: - print(utils.indent(pygments.highlight( - fh.read(), PythonLexer(), TerminalFormatter()), - 4 * ' ')) + print(pygments.highlight(fh.read(), PythonLexer(), TerminalFormatter())) else: - print(utils.indent(fh.read(), 4 * ' ')) + print(fh.read()) return True def do_uninstall(self, name): @@ -282,7 +289,9 @@ class CommandTheme(Command): themes = [] themes_dirs = self.site.themes_dirs + [resource_filename('nikola', os.path.join('data', 'themes'))] for tdir in themes_dirs: - themes += [(i, os.path.join(tdir, i)) for i in os.listdir(tdir)] + if os.path.isdir(tdir): + themes += [(i, os.path.join(tdir, i)) for i in os.listdir(tdir)] + for tname, tpath in sorted(set(themes)): if os.path.isdir(tpath): print("{0} at {1}".format(tname, tpath)) @@ -316,7 +325,7 @@ class CommandTheme(Command): LOGGER.error("This file already exists in your templates directory ({0}).".format(base)) return 3 - def new_theme(self, name, engine, parent): + def new_theme(self, name, engine, parent, create_legacy_meta=False): """Create a new theme.""" base = 'themes' themedir = os.path.join(base, name) @@ -326,9 +335,7 @@ class CommandTheme(Command): LOGGER.info("Created directory {0}".format(base)) # Check if engine and parent match - engine_file = utils.get_asset_path('engine', utils.get_theme_chain(parent, self.site.themes_dirs)) - with io.open(engine_file, 'r', encoding='utf-8') as fh: - parent_engine = fh.read().strip() + parent_engine = utils.get_template_engine(utils.get_theme_chain(parent, self.site.themes_dirs)) if parent_engine != engine: LOGGER.error("Cannot use engine {0} because parent theme '{1}' uses {2}".format(engine, parent, parent_engine)) @@ -342,24 +349,45 @@ class CommandTheme(Command): LOGGER.error("Theme already exists") return 2 - with io.open(os.path.join(themedir, 'parent'), 'w', encoding='utf-8') as fh: - fh.write(parent + '\n') - LOGGER.info("Created file {0}".format(os.path.join(themedir, 'parent'))) - with io.open(os.path.join(themedir, 'engine'), 'w', encoding='utf-8') as fh: - fh.write(engine + '\n') - LOGGER.info("Created file {0}".format(os.path.join(themedir, 'engine'))) + cp = configparser.ConfigParser() + cp['Theme'] = { + 'engine': engine, + 'parent': parent + } + + theme_meta_path = os.path.join(themedir, name + '.theme') + with io.open(theme_meta_path, 'w', encoding='utf-8') as fh: + cp.write(fh) + LOGGER.info("Created file {0}".format(theme_meta_path)) + + if create_legacy_meta: + with io.open(os.path.join(themedir, 'parent'), 'w', encoding='utf-8') as fh: + fh.write(parent + '\n') + LOGGER.info("Created file {0}".format(os.path.join(themedir, 'parent'))) + with io.open(os.path.join(themedir, 'engine'), 'w', encoding='utf-8') as fh: + fh.write(engine + '\n') + LOGGER.info("Created file {0}".format(os.path.join(themedir, 'engine'))) LOGGER.info("Theme {0} created successfully.".format(themedir)) - LOGGER.notice('Remember to set THEME="{0}" in conf.py to use this theme.'.format(name)) + LOGGER.info('Remember to set THEME="{0}" in conf.py to use this theme.'.format(name)) def get_json(self, url): """Download the JSON file with all plugins.""" if self.json is None: try: - self.json = requests.get(url).json() - except requests.exceptions.SSLError: - LOGGER.warning("SSL error, using http instead of https (press ^C to abort)") - time.sleep(1) - url = url.replace('https', 'http', 1) - self.json = requests.get(url).json() + try: + self.json = requests.get(url).json() + except requests.exceptions.SSLError: + LOGGER.warning("SSL error, using http instead of https (press ^C to abort)") + time.sleep(1) + url = url.replace('https', 'http', 1) + self.json = requests.get(url).json() + except json.decoder.JSONDecodeError as e: + LOGGER.error("Failed to decode JSON data in response from server.") + LOGGER.error("JSON error encountered:" + str(e)) + LOGGER.error("This issue might be caused by server-side issues, or by to unusual activity in your " + "network (as determined by CloudFlare). Please visit https://themes.getnikola.com/ in " + "a browser.") + sys.exit(2) + return self.json |
