From 5a6e68909a50837b817fa34dc6343063de025cef Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 1 Jul 2018 16:58:26 +0200 Subject: [PATCH 1/2] ics con accenti (solo py3) --- plugins/talks.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/plugins/talks.py b/plugins/talks.py index 11aa077..80347c5 100644 --- a/plugins/talks.py +++ b/plugins/talks.py @@ -180,7 +180,8 @@ def get_talk_data(talkname): def overlap(interval_a, interval_b): '''how many minutes do they overlap?''' - return max(0, min(interval_a[1], interval_b[1]) - max(interval_a[0], interval_b[0])) + return max(0, min(interval_a[1], interval_b[1]) - + max(interval_a[0], interval_b[0])) def get_talk_overlaps(name): @@ -376,12 +377,18 @@ def talks_to_ics(): def talk_to_ics(talk): + def _decode(s): + if six.PY2: + return unidecode.unidecode(s) + else: + return s + if 'time' not in talk or 'duration' not in talk: return None e = ics.Event( uid="%s@%d.hackmeeting.org" % (talk['id'], get_global_data()['startdate'].year), - name=unidecode.unidecode(talk['title']), + name=_decode(talk['title']), begin=talk['time'], duration=datetime.timedelta(minutes=talk['duration']), transparent=True, @@ -390,7 +397,7 @@ def talk_to_ics(talk): # unidecode replaces letters with their most similar ASCII counterparts # (ie: accents get stripped) if 'text' in talk: - e.description = unidecode.unidecode(talk['text']) + e.description = _decode(talk['text']) e.url = pelican.settings['SCHEDULEURL'] + '#talk-' + talk['id'] if 'room' in talk: e.location = talk['room'] From a3913c75532fd3db18e956b57300e6d113ff0a04 Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 1 Jul 2018 17:03:10 +0200 Subject: [PATCH 2/2] clean some code --- plugins/talks.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/plugins/talks.py b/plugins/talks.py index 80347c5..c1217cc 100644 --- a/plugins/talks.py +++ b/plugins/talks.py @@ -11,7 +11,6 @@ import logging import re import datetime import shutil -import time from copy import copy import locale from contextlib import contextmanager @@ -37,8 +36,6 @@ import dateutil pelican = None # This will be set during register() - - def memoize(function): '''decorators to cache''' memo = {} @@ -111,7 +108,7 @@ def get_talk_data(talkname): with io.open(fname, encoding='utf8') as buf: try: data = yaml.load(buf) - except: + except Exception: logging.exception("Syntax error reading %s; skipping", fname) return None if data is None: @@ -173,7 +170,7 @@ def get_talk_data(talkname): if os.path.isdir(resdir) and os.listdir(resdir): data['resources'] = resdir return data - except: + except Exception: logging.exception("Error on talk %s", talkname) raise @@ -221,7 +218,9 @@ def check_overlaps(): @memoize def jinja_env(): env = jinja2.Environment( - loader=jinja2.FileSystemLoader(os.path.join(pelican.settings['TALKS_PATH'], '_templates')), + loader=jinja2.FileSystemLoader(os.path.join( + pelican.settings['TALKS_PATH'], + '_templates')), autoescape=True, ) env.filters['markdown'] = lambda text: jinja2.Markup(markdown(text)) @@ -312,7 +311,9 @@ class TalkGridDirective(Directive): rooms.add(r) else: rooms.add(t['room']) - rooms = list(sorted(rooms)) # TODO: ordina in base a qualcosa nel meta.yaml globale + # TODO: ordina in base a qualcosa nel meta.yaml globale + rooms = list(sorted(rooms)) + # room=* is not a real room. # Remove it unless that day only has special rooms if '*' in rooms and len(rooms) > 1: @@ -420,17 +421,19 @@ class TalksGenerator(generators.Generator): if 'resources' in self.talks[talkname]: outdir = os.path.join(self.output_path, pelican.settings['TALKS_PATH'], talkname, - pelican.settings['TALKS_ATTACHMENT_PATH']) + pelican.settings['TALKS_ATTACHMENT_PATH'] + ) if os.path.isdir(outdir): shutil.rmtree(outdir) shutil.copytree(self.talks[talkname]['resources'], outdir) if ICS_ENABLED: - with io.open(os.path.join(self.output_path, pelican.settings.get('TALKS_ICS')), - 'w', - encoding='utf8') as buf: + with io.open(os.path.join(self.output_path, + pelican.settings.get('TALKS_ICS')), + 'w', encoding='utf8') as buf: buf.write(talks_to_ics()) else: - logging.warning('module `ics` not found. ICS calendar will not be generated') + logging.warning('module `ics` not found. ' + 'ICS calendar will not be generated') def add_talks_option_defaults(pelican): @@ -448,6 +451,7 @@ def pelican_init(pelicanobj): global pelican pelican = pelicanobj + try: import yaml except ImportError: