Debian Codegraph @

scripts/gen_mapfiles.py

raw
1 """
2 scripts/gen_mapfiles.py
3 ~~~~~~~~~~~~~~~~~~~~~~~
4
5 Regenerate mapping files.
6
7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9 """
10
11 from importlib import import_module
12 from pathlib import Path
13 import sys
14
15 top_src_dir = Path(__file__).parent.parent
16 pygments_package = top_src_dir / 'pygments'
17 sys.path.insert(0, str(pygments_package.parent.resolve()))
18
19 from pygments.util import docstring_headline # noqa: E402
20
21 def main():
22 for key in ['lexers', 'formatters', 'styles']:
23 lines = []
24 for file in (pygments_package / key).glob('[!_]*.py'):
25 module_name = '.'.join(file.relative_to(pygments_package.parent).with_suffix('').parts)
26 print(module_name)
27 module = import_module(module_name)
28 for obj_name in module.__all__:
29 obj = getattr(module, obj_name)
30 desc = (module_name, obj.name, tuple(obj.aliases))
31 if key == 'lexers':
32 desc += (tuple(obj.filenames), tuple(obj.mimetypes),)
33 elif key == 'formatters':
34 desc += (tuple(obj.filenames), docstring_headline(obj),)
35 elif key == 'styles':
36 pass
37 else:
38 assert False
39 lines.append(f' {obj_name!r}: {desc!r},')
40 # Sort to make diffs minimal.
41 lines.sort()
42 new_dict = '\n'.join(lines)
43 content = f'''# Automatically generated by scripts/gen_mapfiles.py.
44 # DO NOT EDIT BY HAND; run `tox -e mapfiles` instead.
45
46 {key.upper()} = {{
47 {new_dict}
48 }}
49 '''
50 (pygments_package / key / '_mapping.py').write_text(content, encoding='utf8')
51 print(f'=== {len(lines)} {key} processed.')
52
53 if __name__ == '__main__':
54 main()