clicklist.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. Render click.yaml config file into human-readable list of supported sites
  3. """
  4. import pkg_resources, yaml
  5. from docutils import nodes
  6. from docutils.parsers.rst import Directive
  7. from yarl import URL
  8. class ClickList (Directive):
  9. def run(self):
  10. # XXX: do this once only
  11. fd = pkg_resources.resource_stream ('crocoite', 'data/click.yaml')
  12. config = list (yaml.safe_load_all (fd))
  13. l = nodes.definition_list ()
  14. for site in config:
  15. urls = set ()
  16. v = nodes.definition ()
  17. vl = nodes.bullet_list ()
  18. v += vl
  19. for s in site['selector']:
  20. i = nodes.list_item ()
  21. i += nodes.paragraph (text=s['description'])
  22. vl += i
  23. urls.update (map (lambda x: URL(x).with_path ('/'), s.get ('urls', [])))
  24. item = nodes.definition_list_item ()
  25. term = ', '.join (map (lambda x: x.host, urls)) if urls else site['match']
  26. k = nodes.term (text=term)
  27. item += k
  28. item += v
  29. l += item
  30. return [l]
  31. def setup(app):
  32. app.add_directive ("clicklist", ClickList)
  33. return {
  34. 'version': '0.1',
  35. 'parallel_read_safe': True,
  36. 'parallel_write_safe': True,
  37. }