Marc-André Lemburg | 92b201d | 2005-10-21 13:47:03 +0000 | [diff] [blame] | 1 | """ List all available codec modules. |
| 2 | |
| 3 | (c) Copyright 2005, Marc-Andre Lemburg (mal@lemburg.com). |
| 4 | |
| 5 | Licensed to PSF under a Contributor Agreement. |
| 6 | |
| 7 | """ |
| 8 | |
| 9 | import os, codecs, encodings |
| 10 | |
| 11 | _debug = 0 |
| 12 | |
| 13 | def listcodecs(dir): |
| 14 | names = [] |
| 15 | for filename in os.listdir(dir): |
| 16 | if filename[-3:] != '.py': |
| 17 | continue |
| 18 | name = filename[:-3] |
| 19 | # Check whether we've found a true codec |
| 20 | try: |
| 21 | codecs.lookup(name) |
| 22 | except LookupError: |
| 23 | # Codec not found |
| 24 | continue |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 25 | except Exception as reason: |
Marc-André Lemburg | 92b201d | 2005-10-21 13:47:03 +0000 | [diff] [blame] | 26 | # Probably an error from importing the codec; still it's |
| 27 | # a valid code name |
| 28 | if _debug: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 29 | print('* problem importing codec %r: %s' % \ |
| 30 | (name, reason)) |
Marc-André Lemburg | 92b201d | 2005-10-21 13:47:03 +0000 | [diff] [blame] | 31 | names.append(name) |
| 32 | return names |
| 33 | |
| 34 | |
| 35 | if __name__ == '__main__': |
| 36 | names = listcodecs(encodings.__path__[0]) |
| 37 | names.sort() |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 38 | print('all_codecs = [') |
Marc-André Lemburg | 92b201d | 2005-10-21 13:47:03 +0000 | [diff] [blame] | 39 | for name in names: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 40 | print(' %r,' % name) |
| 41 | print(']') |