diff options
Diffstat (limited to 'nikola/utils.py')
| -rw-r--r-- | nikola/utils.py | 81 |
1 files changed, 30 insertions, 51 deletions
diff --git a/nikola/utils.py b/nikola/utils.py index 0c3b4c0..46e159e 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -35,7 +35,6 @@ import locale import logging import os import re -import codecs import json import shutil import subprocess @@ -47,7 +46,7 @@ except ImportError: pass import logbook -from logbook.more import ExceptionHandler +from logbook.more import ExceptionHandler, ColorizedStderrHandler import pytz from . import DEBUG @@ -57,6 +56,15 @@ class ApplicationWarning(Exception): pass +class ColorfulStderrHandler(ColorizedStderrHandler): + """Stream handler with colors.""" + _colorful = False + + def should_colorize(self, record): + """Inform about colorization using the value obtained from Nikola.""" + return self._colorful + + def get_logger(name, handlers): """Get a logger with handlers attached.""" l = logbook.Logger(name) @@ -68,7 +76,7 @@ def get_logger(name, handlers): return l -STDERR_HANDLER = [logbook.StderrHandler( +STDERR_HANDLER = [ColorfulStderrHandler( level=logbook.NOTICE if not DEBUG else logbook.DEBUG, format_string=u'[{record.time:%Y-%m-%dT%H:%M:%SZ}] {record.level_name}: {record.channel}: {record.message}' )] @@ -126,11 +134,11 @@ from unidecode import unidecode import PyRSS2Gen as rss __all__ = ['get_theme_path', 'get_theme_chain', 'load_messages', 'copy_tree', - 'generic_rss_renderer', 'copy_file', 'slugify', 'unslugify', - 'to_datetime', 'apply_filters', 'config_changed', 'get_crumbs', - 'get_tzname', 'get_asset_path', '_reload', 'unicode_str', 'bytes_str', - 'unichr', 'Functionary', 'LocaleBorg', 'sys_encode', 'sys_decode', - 'makedirs', 'get_parent_theme_name', 'ExtendedRSS2', 'demote_headers', + 'copy_file', 'slugify', 'unslugify', 'to_datetime', 'apply_filters', + 'config_changed', 'get_crumbs', 'get_tzname', 'get_asset_path', + '_reload', 'unicode_str', 'bytes_str', 'unichr', 'Functionary', + 'LocaleBorg', 'sys_encode', 'sys_decode', 'makedirs', + 'get_parent_theme_name', 'ExtendedRSS2', 'demote_headers', 'get_translation_candidate'] @@ -313,10 +321,14 @@ def copy_tree(src, dst, link_cutoff=None): dst_dir = os.path.join(dst, *root_parts[base_len:]) makedirs(dst_dir) for src_name in files: - if src_name == '.DS_Store': + if src_name in ('.DS_Store', 'Thumbs.db'): continue dst_file = os.path.join(dst_dir, src_name) src_file = os.path.join(root, src_name) + if sys.version_info[0] == 2: + # Python2 prefers encoded str here + dst_file = sys_encode(dst_file) + src_file = sys_encode(src_file) yield { 'name': str(dst_file), 'file_dep': [src_file], @@ -326,45 +338,6 @@ def copy_tree(src, dst, link_cutoff=None): } -def generic_rss_renderer(lang, title, link, description, timeline, output_path, - rss_teasers, feed_length=10, feed_url=None): - """Takes all necessary data, and renders a RSS feed in output_path.""" - items = [] - for post in timeline[:feed_length]: - args = { - 'title': post.title(lang), - 'link': post.permalink(lang, absolute=True), - 'description': post.text(lang, teaser_only=rss_teasers, really_absolute=True), - 'guid': post.permalink(lang, absolute=True), - # PyRSS2Gen's pubDate is GMT time. - 'pubDate': (post.date if post.date.tzinfo is None else - post.date.astimezone(pytz.timezone('UTC'))), - 'categories': post._tags.get(lang, []), - 'author': post.meta('author'), - } - - items.append(ExtendedItem(**args)) - rss_obj = ExtendedRSS2( - title=title, - link=link, - description=description, - lastBuildDate=datetime.datetime.now(), - items=items, - generator='Nikola <http://getnikola.com/>', - language=lang - ) - rss_obj.self_url = feed_url - rss_obj.rss_attrs["xmlns:atom"] = "http://www.w3.org/2005/Atom" - rss_obj.rss_attrs["xmlns:dc"] = "http://purl.org/dc/elements/1.1/" - dst_dir = os.path.dirname(output_path) - makedirs(dst_dir) - with codecs.open(output_path, "wb+", "utf-8") as rss_file: - data = rss_obj.to_xml(encoding='utf-8') - if isinstance(data, bytes_str): - data = data.decode('utf-8') - rss_file.write(data) - - def copy_file(source, dest, cutoff=None): dst_dir = os.path.dirname(dest) makedirs(dst_dir) @@ -518,9 +491,11 @@ def get_tzname(dt): def current_time(tzinfo=None): - dt = datetime.datetime.now() + dt = datetime.datetime.utcnow() if tzinfo is not None: - dt = tzinfo.localize(dt) + dt = tzinfo.fromutc(dt) + else: + dt = pytz.UTC.localize(dt) return dt @@ -749,7 +724,11 @@ class LocaleBorg(object): else: # Python 2 with calendar.TimeEncoding(self.locales[lang]): s = calendar.month_name[month_no] - s = s.decode(self.encodings[lang]) + enc = self.encodings[lang] + if not enc: + enc = 'UTF-8' + + s = s.decode(enc) # paranoid about calendar ending in the wrong locale (windows) self.set_locale(self.current_lang) return s |
