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