talks.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. '''
  2. Manage talks scheduling in a semantic way
  3. '''
  4. from __future__ import print_function
  5. import os
  6. import io
  7. from functools import wraps
  8. import logging
  9. import re
  10. import datetime
  11. import shutil
  12. import time
  13. from copy import copy
  14. import locale
  15. from contextlib import contextmanager
  16. from babel.dates import format_date, format_datetime, format_time
  17. import markdown
  18. from docutils import nodes
  19. from docutils.parsers.rst import directives, Directive
  20. from pelican import signals, generators
  21. import jinja2
  22. pelican = None # This will be set during register()
  23. def memoize(function):
  24. '''decorators to cache'''
  25. memo = {}
  26. @wraps(function)
  27. def wrapper(*args):
  28. if args in memo:
  29. return memo[args]
  30. else:
  31. rv = function(*args)
  32. memo[args] = rv
  33. return rv
  34. return wrapper
  35. @contextmanager
  36. def setlocale(name):
  37. saved = locale.setlocale(locale.LC_ALL)
  38. try:
  39. yield locale.setlocale(locale.LC_ALL, name)
  40. finally:
  41. locale.setlocale(locale.LC_ALL, saved)
  42. @memoize
  43. def get_talk_names():
  44. return [name for name in os.listdir(pelican.settings['TALKS_PATH'])
  45. if not name.startswith('_') and
  46. get_talk_data(name) is not None
  47. ]
  48. def all_talks():
  49. return [get_talk_data(tn) for tn in get_talk_names()]
  50. def unique_attr(iterable, attr):
  51. return {x[attr] for x in iterable
  52. if attr in x}
  53. @memoize
  54. def get_global_data():
  55. fname = os.path.join(pelican.settings['TALKS_PATH'], 'meta.yaml')
  56. if not os.path.isfile(fname):
  57. return None
  58. with io.open(fname, encoding='utf8') as buf:
  59. try:
  60. data = yaml.load(buf)
  61. except Exception:
  62. logging.exception("Syntax error reading %s; skipping", fname)
  63. return None
  64. if data is None:
  65. return None
  66. if 'startdate' not in data:
  67. logging.error("Missing startdate in global data")
  68. data['startdate'] = datetime.datetime.now()
  69. return data
  70. @memoize
  71. def get_talk_data(talkname):
  72. fname = os.path.join(pelican.settings['TALKS_PATH'], talkname, 'meta.yaml')
  73. if not os.path.isfile(fname):
  74. return None
  75. with io.open(fname, encoding='utf8') as buf:
  76. try:
  77. data = yaml.load(buf)
  78. except:
  79. logging.exception("Syntax error reading %s; skipping", fname)
  80. return None
  81. if data is None:
  82. return None
  83. try:
  84. gridstep = pelican.settings['TALKS_GRID_STEP']
  85. if 'title' not in data:
  86. logging.warn("Talk <{}> has no `title` field".format(talkname))
  87. data['title'] = talkname
  88. if 'text' not in data:
  89. logging.warn("Talk <{}> has no `text` field".format(talkname))
  90. data['text'] = ''
  91. if 'duration' not in data:
  92. logging.info("Talk <{}> has no `duration` field (50min used)"
  93. .format(talkname))
  94. data['duration'] = 50
  95. data['duration'] = int(data['duration'])
  96. if data['duration'] < gridstep:
  97. logging.info("Talk <{}> lasts only {} minutes; changing to {}"
  98. .format(talkname, data['duration'], gridstep))
  99. data['duration'] = gridstep
  100. if 'links' not in data or not data['links']:
  101. data['links'] = []
  102. if 'contacts' not in data or not data['contacts']:
  103. data['contacts'] = []
  104. if 'needs' not in data or not data['needs']:
  105. data['needs'] = []
  106. if 'room' not in data:
  107. logging.warn("Talk <{}> has no `room` field".format(talkname))
  108. if 'time' not in data or 'day' not in data:
  109. logging.warn("Talk <{}> has no `time` or `day`".format(talkname))
  110. if 'time' in data:
  111. del data['time']
  112. if 'day' in data:
  113. del data['day']
  114. if 'day' in data:
  115. data['day'] = get_global_data()['startdate'] + \
  116. datetime.timedelta(days=data['day'])
  117. if 'time' in data and 'day' in data:
  118. timeparts = re.findall(r'\d+', str(data['time']))
  119. if 4 > len(timeparts) > 0:
  120. timeparts = [int(p) for p in timeparts]
  121. data['time'] = datetime.datetime.combine(
  122. data['day'], datetime.time(*timeparts))
  123. else:
  124. logging.error("Talk <%s> has malformed `time`", talkname)
  125. data['id'] = talkname
  126. resdir = os.path.join(pelican.settings['TALKS_PATH'], talkname,
  127. pelican.settings['TALKS_ATTACHMENT_PATH'])
  128. if os.path.isdir(resdir) and os.listdir(resdir):
  129. data['resources'] = resdir
  130. return data
  131. except:
  132. logging.exception("Error on talk %s", talkname)
  133. raise
  134. @memoize
  135. def jinja_env():
  136. env = jinja2.Environment(
  137. loader=jinja2.FileSystemLoader(os.path.join(pelican.settings['TALKS_PATH'], '_templates')),
  138. autoescape=True,
  139. )
  140. env.filters['markdown'] = lambda text: \
  141. jinja2.Markup(markdown.Markdown(extensions=['meta']).
  142. convert(text))
  143. env.filters['dateformat'] = format_date
  144. env.filters['datetimeformat'] = format_datetime
  145. env.filters['timeformat'] = format_time
  146. return env
  147. class TalkListDirective(Directive):
  148. final_argument_whitespace = True
  149. has_content = True
  150. option_spec = {
  151. 'lang': directives.unchanged
  152. }
  153. def run(self):
  154. lang = self.options.get('lang', 'C')
  155. tmpl = jinja_env().get_template('talk.html')
  156. def _sort_date(name):
  157. '''
  158. This function is a helper to sort talks by start date
  159. When no date is available, put at the beginning
  160. '''
  161. d = get_talk_data(name)
  162. if 'time' in d:
  163. return d['time']
  164. return datetime.datetime(1, 1, 1)
  165. return [
  166. nodes.raw('', tmpl.render(lang=lang, **get_talk_data(n)),
  167. format='html')
  168. for n in sorted(get_talk_names(),
  169. key=_sort_date)
  170. ]
  171. class TalkDirective(Directive):
  172. required_arguments = 1
  173. final_argument_whitespace = True
  174. has_content = True
  175. option_spec = {
  176. 'lang': directives.unchanged
  177. }
  178. def run(self):
  179. lang = self.options.get('lang', 'C')
  180. tmpl = jinja_env().get_template('talk.html')
  181. data = get_talk_data(self.arguments[0])
  182. if data is None:
  183. return []
  184. return [
  185. nodes.raw('', tmpl.render(lang=lang, **data),
  186. format='html')
  187. ]
  188. class TalkGridDirective(Directive):
  189. '''A complete grid'''
  190. final_argument_whitespace = True
  191. has_content = True
  192. option_spec = {
  193. 'lang': directives.unchanged
  194. }
  195. def run(self):
  196. lang = self.options.get('lang', 'C')
  197. tmpl = jinja_env().get_template('grid.html')
  198. output = []
  199. days = unique_attr(all_talks(), 'day')
  200. gridstep = pelican.settings['TALKS_GRID_STEP']
  201. for day in sorted(days):
  202. talks = {talk['id'] for talk in all_talks()
  203. if talk.get('day', None) == day
  204. and 'time' in talk
  205. and 'room' in talk}
  206. if not talks:
  207. continue
  208. talks = [get_talk_data(t) for t in talks]
  209. rooms = set()
  210. for t in talks:
  211. if type(t['room']) is list:
  212. for r in t['room']:
  213. rooms.add(r)
  214. else:
  215. rooms.add(t['room'])
  216. rooms = list(sorted(rooms))
  217. # room=* is not a real room.
  218. # Remove it unless that day only has special rooms
  219. if '*' in rooms and len(rooms) > 1:
  220. del rooms[rooms.index('*')]
  221. mintime = min({talk['time'].hour * 60 +
  222. talk['time'].minute
  223. for talk in talks}) // gridstep * gridstep
  224. maxtime = max({talk['time'].hour * 60 +
  225. talk['time'].minute +
  226. talk['duration']
  227. for talk in talks})
  228. times = {}
  229. for t in range(mintime, maxtime, gridstep):
  230. times[t] = [None] * len(rooms)
  231. for talk in sorted(talks, key=lambda x: x['time']):
  232. talktime = talk['time'].hour * 60 + talk['time'].minute
  233. position = talktime // gridstep * gridstep # round
  234. assert position in times
  235. if talk['room'] == '*':
  236. roomnums = range(len(rooms))
  237. elif type(talk['room']) is list:
  238. roomnums = [rooms.index(r) for r in talk['room']]
  239. else:
  240. roomnums = [rooms.index(talk['room'])]
  241. for roomnum in roomnums:
  242. if times[position][roomnum] is not None:
  243. logging.error("Talk %s and %s overlap! (room %s)",
  244. times[position][roomnum]['id'],
  245. talk['id'],
  246. rooms[roomnum]
  247. )
  248. continue
  249. times[position][roomnum] = copy(talk)
  250. times[position][roomnum]['skip'] = False
  251. for i in range(1, talk['duration'] // gridstep):
  252. times[position + i*gridstep][roomnum] = copy(talk)
  253. times[position + i*gridstep][roomnum]['skip'] = True
  254. render = tmpl.render(times=times,
  255. rooms=rooms,
  256. mintime=mintime, maxtime=maxtime,
  257. timestep=gridstep,
  258. lang=lang,
  259. )
  260. output.append(nodes.raw(
  261. '', u'<h4>%s</h4>' % format_date(day, format='full',
  262. locale=lang),
  263. format='html'))
  264. output.append(nodes.raw('', render, format='html'))
  265. return output
  266. def talks_to_ics():
  267. content = u'BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:pelican\n'
  268. for t in all_talks():
  269. try:
  270. content += talk_to_ics(t)
  271. except:
  272. logging.exception("Error producing calendar for talk %s", t['id'])
  273. content += 'END:VCALENDAR\n'
  274. return content
  275. def talk_to_ics(talk):
  276. if 'time' not in talk or 'duration' not in talk:
  277. return ''
  278. start = talk['time']
  279. end = start + datetime.timedelta(minutes=talk['duration'])
  280. content = 'BEGIN:VEVENT\n'
  281. content += "UID:%s@%d.hackmeeting.org\n" % (talk['id'], talk['day'].year)
  282. content += "SUMMARY:%s\n" % talk['title']
  283. content += "DTSTAMP:%s\n" % time.strftime('%Y%m%dT%H%M%SZ',
  284. time.gmtime(float(
  285. start.strftime('%s'))))
  286. content += "DTSTART:%s\n" % time.strftime('%Y%m%dT%H%M%SZ',
  287. time.gmtime(float(
  288. start.strftime('%s'))))
  289. content += "DTEND:%s\n" % time.strftime('%Y%m%dT%H%M%SZ',
  290. time.gmtime(float(
  291. end.strftime('%s'))))
  292. content += "LOCATION:%s\n" % (talk['room'] if 'room' in talk else 'todo')
  293. content += 'END:VEVENT\n'
  294. return content
  295. class TalksGenerator(generators.Generator):
  296. def __init__(self, *args, **kwargs):
  297. self.talks = []
  298. super(TalksGenerator, self).__init__(*args, **kwargs)
  299. def generate_context(self):
  300. self.talks = {n: get_talk_data(n) for n in get_talk_names()}
  301. self._update_context(('talks',))
  302. def generate_output(self, writer=None):
  303. for talkname in self.talks:
  304. if 'resources' in self.talks[talkname]:
  305. outdir = os.path.join(self.output_path,
  306. pelican.settings['TALKS_PATH'], talkname,
  307. pelican.settings['TALKS_ATTACHMENT_PATH'])
  308. if os.path.isdir(outdir):
  309. shutil.rmtree(outdir)
  310. shutil.copytree(self.talks[talkname]['resources'], outdir)
  311. with io.open(os.path.join(self.output_path, pelican.settings.get('TALKS_ICS')),
  312. 'w',
  313. encoding='utf8') as buf:
  314. buf.write(talks_to_ics())
  315. def add_talks_option_defaults(pelican):
  316. pelican.settings.setdefault('TALKS_PATH', 'talks')
  317. pelican.settings.setdefault('TALKS_ATTACHMENT_PATH', 'res')
  318. pelican.settings.setdefault('TALKS_ICS', 'schedule.ics')
  319. pelican.settings.setdefault('TALKS_GRID_STEP', 30)
  320. def get_generators(gen):
  321. return TalksGenerator
  322. def pelican_init(pelicanobj):
  323. global pelican
  324. pelican = pelicanobj
  325. try:
  326. import yaml
  327. except ImportError:
  328. print('ERROR: yaml not found. Talks plugins will be disabled')
  329. def register():
  330. pass
  331. else:
  332. def register():
  333. signals.initialized.connect(pelican_init)
  334. signals.get_generators.connect(get_generators)
  335. signals.initialized.connect(add_talks_option_defaults)
  336. directives.register_directive('talklist', TalkListDirective)
  337. directives.register_directive('talk', TalkDirective)
  338. directives.register_directive('talkgrid', TalkGridDirective)