Commit 3db0b5ca authored by Matthieu Boileau's avatar Matthieu Boileau
Browse files

Merge branch 'dev-fix-indico' into 'master'

Fix indico plugin

Closes #236

See merge request !646
parents de6af4bb de5b6aab
Pipeline #9843 canceled with stages
......@@ -6,3 +6,4 @@ __pycache__
.vscode/
venv/
*.pyc
.env/
import locale
from docutils.parsers.rst import directives, Directive
from docutils.parsers.rst.directives.parts import Contents
from docutils.transforms import frontmatter
......@@ -11,6 +12,9 @@ import re
from jinja2 import Template, Environment, FileSystemLoader
from textwrap import dedent
import os
import logging
logger = logging.getLogger(__name__)
# global variables needed to link the event and display the day
# just one time
......@@ -18,9 +22,9 @@ num_day = 0
num_event = 0
# set locale in order to have french date for output string
import locale
locale.setlocale(locale.LC_TIME, "fr_FR.UTF-8")
def get_indico_event_as_schedule(url, event):
"""
Parse an indico event and return an rst like describing the schedule.
......@@ -45,12 +49,16 @@ def get_indico_event_as_schedule(url, event):
import collections
from urllib.request import urlopen
import json
import ssl
context = ssl.SSLContext()
try:
print(url + '/export/timetable/' + str(event) + ".json")
response = urlopen(url + '/export/timetable/' + str(event) + ".json").read()
except:
print("WARNING: indico content could not be fetched, no programme will be built. Offline ?")
response = urlopen(url + '/export/timetable/' + str(event) + ".json",
context=context).read()
except Exception as e:
logger.warning(
"indico content could not be fetched because of : {}".format(e))
return None
data = json.loads(response.decode('utf-8'))
......@@ -61,10 +69,14 @@ def get_indico_event_as_schedule(url, event):
day = schedule.get(date, {})
for k, v in day_v.items():
start_time_str = v['startDate']['date'] + 'T' + v['startDate']['time'] + 'Z'
end_time_str = v['endDate']['date'] + 'T' + v['endDate']['time'] + 'Z'
start_time = maya.parse(start_time_str).datetime(to_timezone='Europe/Paris', naive=False)
end_time = maya.parse(end_time_str).datetime(to_timezone='Europe/Paris', naive=False)
start_time_str = v['startDate']['date'] + \
'T' + v['startDate']['time'] + 'Z'
end_time_str = v['endDate']['date'] + \
'T' + v['endDate']['time'] + 'Z'
start_time = maya.parse(start_time_str).datetime(
to_timezone='Europe/Paris', naive=False)
end_time = maya.parse(end_time_str).datetime(
to_timezone='Europe/Paris', naive=False)
day[start_time] = {
'start': start_time.strftime("%H:%M"),
......@@ -79,7 +91,8 @@ def get_indico_event_as_schedule(url, event):
speakers.append("Orateur à confirmer")
elif v['presenters'] != []:
for person in v['presenters']:
speakers.append(person['firstName'].capitalize() + ' ' + person['familyName'].capitalize())
speakers.append(person['firstName'].capitalize(
) + ' ' + person['familyName'].capitalize())
else:
speakers.append("Orateur à confirmer")
......@@ -99,6 +112,7 @@ def get_indico_event_as_schedule(url, event):
return get_indico_event_as_schedule.template.render(schedule=schedule)
# Pre-loading Jinja2 template as static variable
get_indico_event_as_schedule.template = Template(dedent("""
{%- for key, value in schedule.items() -%}
......@@ -130,25 +144,34 @@ get_indico_event_as_schedule.template = Template(dedent("""
"""))
# define new nodes
class schedule(nodes.General, nodes.Element):
""" schedule node """
class day(nodes.General, nodes.Element):
""" day node """
class event(nodes.General, nodes.Element):
""" event node """
class event_content(nodes.General, nodes.Element):
""" event content node """
class break_event(nodes.General, nodes.Element):
""" break event node """
class button(nodes.General, nodes.Element):
""" button node """
#define new directive
# define new directive
class Section(Directive):
"""
A section create
......@@ -188,7 +211,8 @@ class Section(Directive):
[('[^\\w\\s-]', ''), ('(?u)\\A\\s*', ''), ('(?u)\\s*\\Z', ''), ('[-\\s]+', '-')])])
section['class'] = self.options.get('class', [])
section += nodes.title(self.arguments[0], self.arguments[0], classes=['toc'], refid=section['ids'][0])
section += nodes.title(self.arguments[0], self.arguments[0],
classes=['toc'], refid=section['ids'][0])
blockquote = nodes.block_quote(classes=['content'])
section += blockquote
self.state.nested_parse(self.content, self.content_offset, blockquote)
......@@ -225,16 +249,19 @@ class Schedule(Directive):
source = self.options['indico_url']
event = self.options['indico_event']
content = get_indico_event_as_schedule(source, event)
if content != None: #we are online
if content: # we are online
content = content.split('\n')
self.state.nested_parse(StringList(content), len(content), sche)
else: # we are offline and nothing is fetched from indico
self.state.nested_parse(self.content, self.content_offset, sche)
self.state.nested_parse(
StringList(content), len(content), sche)
else: # we are offline and nothing is fetched from indico
self.state.nested_parse(
self.content, self.content_offset, sche)
else:
self.assert_has_content()
return [sche]
class Day(Directive):
required_arguments = 1
optional_arguments = 0
......@@ -246,7 +273,7 @@ class Day(Directive):
global num_day
# Raise an error if the directive does not have contents.
self.assert_has_content()
d = day(classes=['day-color2' if num_day&1 else 'day-color1'])
d = day(classes=['day-color2' if num_day & 1 else 'day-color1'])
d['num_day'] = num_day
d['day'] = dateutil.parser.parse(self.arguments[0], dayfirst=True)
d['show'] = True
......@@ -254,18 +281,21 @@ class Day(Directive):
num_day += 1
return [d]
def support_converter(s):
pattern = re.compile("\[(.*?)\]\((.*?)\)")
supports = list()
for line in s.splitlines():
matches = pattern.match(line)
if matches:
supports.append( {'name': matches.group(1), 'url': directives.uri(matches.group(2))} )
supports.append({'name': matches.group(
1), 'url': directives.uri(matches.group(2))})
else:
supports.append( {'name': None, 'url': directives.uri(line)} )
supports.append({'name': None, 'url': directives.uri(line)})
return supports
class Event(Directive):
required_arguments = 1
optional_arguments = 0
......@@ -304,6 +334,7 @@ class Event(Directive):
num_event += 1
return [node]
class Break(Directive):
required_arguments = 1
optional_arguments = 0
......@@ -325,6 +356,7 @@ class Break(Directive):
num_event += 1
return [node]
class Button(Directive):
required_arguments = 1
optional_arguments = 0
......@@ -341,6 +373,7 @@ class Button(Directive):
node['blank'] = self.options.get('blank', False)
return [node]
class ContentsHook(Contents):
""" Custom table of contents options """
......@@ -363,7 +396,6 @@ class myHTMLTranslator(PelicanHTMLTranslator):
super().__init__(*args, *kwargs)
def depart_section(self, node):
if node.hasattr('class') and self.section_level == 1:
self.body.append('</div>\n')
......@@ -373,7 +405,8 @@ class myHTMLTranslator(PelicanHTMLTranslator):
def visit_section(self, node):
super(myHTMLTranslator, self).visit_section(node)
if node.hasattr('class') and self.section_level == 1:
self.body.append('<div class="container-fluid part {}">\n'.format(' '.join(node['class'])))
self.body.append(
'<div class="container-fluid part {}">\n'.format(' '.join(node['class'])))
self.body.append('<div class="block">\n')
def depart_schedule(self, node):
......@@ -388,13 +421,14 @@ class myHTMLTranslator(PelicanHTMLTranslator):
self.body.append('</div>\n')
def visit_day(self, node):
self.body.append('<div class="day %s">\n'%' '.join(node['classes']))
self.body.append('<div class="day %s">\n' % ' '.join(node['classes']))
def depart_break_event(self, node):
pass
def visit_break_event(self, node):
self.body.extend(self.template.render(node=node, node_type='break').splitlines(True))
self.body.extend(self.template.render(
node=node, node_type='break').splitlines(True))
if (isinstance(node.parent, day) and node.parent['show']):
node.parent['show'] = False
......@@ -403,14 +437,16 @@ class myHTMLTranslator(PelicanHTMLTranslator):
self.body.append('</div>\n')
def visit_event_content(self, node):
self.body.append('<div id="collapseDetails%d" class="row collapse in pb-3">\n'%node['id'])
self.body.append(
'<div id="collapseDetails%d" class="row collapse in pb-3">\n' % node['id'])
self.body.append('<div class="offset-md-3 col-md-9 event-content">\n')
def depart_event(self, node):
pass
def visit_event(self, node):
self.body.extend(self.template.render(node=node, node_type='speaker').splitlines(True))
self.body.extend(self.template.render(
node=node, node_type='speaker').splitlines(True))
if (isinstance(node.parent, day) and node.parent['show']):
node.parent['show'] = False
......@@ -418,7 +454,8 @@ class myHTMLTranslator(PelicanHTMLTranslator):
self.body.append('<div class="savoirplus">\n')
if node['blank']:
self.body.append('<a href={} target="_blank" rel="noopener noreferrer">\n'.format(node['target']))
self.body.append(
'<a href={} target="_blank" rel="noopener noreferrer">\n'.format(node['target']))
else:
self.body.append('<a href={}>\n'.format(node['target']))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment