mklabels.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. webencodings.mklabels
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Regenarate the webencodings.labels module.
  5. :copyright: Copyright 2012 by Simon Sapin
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import json
  9. try:
  10. from urllib import urlopen
  11. except ImportError:
  12. from urllib.request import urlopen
  13. def assert_lower(string):
  14. assert string == string.lower()
  15. return string
  16. def generate(url):
  17. parts = ['''\
  18. """
  19. webencodings.labels
  20. ~~~~~~~~~~~~~~~~~~~
  21. Map encoding labels to their name.
  22. :copyright: Copyright 2012 by Simon Sapin
  23. :license: BSD, see LICENSE for details.
  24. """
  25. # XXX Do not edit!
  26. # This file is automatically generated by mklabels.py
  27. LABELS = {
  28. ''']
  29. labels = [
  30. (repr(assert_lower(label)).lstrip('u'),
  31. repr(encoding['name']).lstrip('u'))
  32. for category in json.loads(urlopen(url).read().decode('ascii'))
  33. for encoding in category['encodings']
  34. for label in encoding['labels']]
  35. max_len = max(len(label) for label, name in labels)
  36. parts.extend(
  37. ' %s:%s %s,\n' % (label, ' ' * (max_len - len(label)), name)
  38. for label, name in labels)
  39. parts.append('}')
  40. return ''.join(parts)
  41. if __name__ == '__main__':
  42. print(generate('http://encoding.spec.whatwg.org/encodings.json'))