view scripts/source_format.py @ 8931:33f1faa7fe04 stable

setup: bump Sphinx version Sphinx 3.0.4 doesn't version its dependencies correctly, and it fails with latest jinja 3.1.2: ImportError: cannot import name 'environmentfilter' from 'jinja2' (.../site-packages/jinja2/__init__.py) Latest Sphinx version supports latest Jinja version and seems to render the documentation correctly.
author Mads Kiilerich <mads@kiilerich.com>
date Sat, 17 Sep 2022 20:37:24 +0200
parents f8971422795e
children
line wrap: on
line source

#!/usr/bin/env python3

# hg files 'set:!binary()&grep("^#!.*python")' 'set:**.py' | xargs scripts/source_format.py

import re
import sys


filenames = sys.argv[1:]

for fn in filenames:
    with open(fn) as f:
        org_content = f.read()

    mod_name = fn[:-3] if fn.endswith('.py') else fn
    mod_name = mod_name[:-9] if mod_name.endswith('/__init__') else mod_name
    mod_name = mod_name.replace('/', '.')
    def f(m):
        return '"""\n%s\n%s\n' % (mod_name, '~' * len(mod_name))
    new_content = re.sub(r'^"""\n(kallithea\..*\n)(~+\n)?', f, org_content, count=1, flags=re.MULTILINE)

    if new_content != org_content:
        with open(fn, 'w') as f:
            f.write(new_content)