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