File: fixup_headers.py

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (76 lines) | stat: -rwxr-xr-x 2,624 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# Utility script to fix headers in doc pages and generate "Accelerator Styles" portion
import os
import shutil
import re
import argparse

index_pattern = re.compile(r"^.. index:: (compute|fix|pair_style|angle_style|bond_style|dihedral_style|improper_style|kspace_style)\s+([a-zA-Z0-9/_]+)$")
pattern = re.compile(r"^(compute|fix|pair_style|angle_style|bond_style|dihedral_style|improper_style|kspace_style)\s+([a-zA-Z0-9/_]+)\s+command$")

parser = argparse.ArgumentParser(description='Fixup headers in docs')
parser.add_argument('files', metavar='FILE', nargs='+', help='files to fix')
args = parser.parse_args()

for orig_file in args.files:
    command_type = "unknown"
    styles = []
    headings = {}
    new_file = f"{orig_file}.tmp"
    found_syntax = False

    with open(orig_file, 'r') as reader, open(new_file, 'w') as writer:
        for line in reader:
            if line.startswith("Syntax"):
                found_syntax = True
                break

            m = index_pattern.match(line)

            if not m:
                m = pattern.match(line)

            if m:
                command_type = m.group(1)
                style        = m.group(2)

                if style not in styles:
                    styles.append(style)

                base_name = '/'.join(style.split('/')[0:-1])
                ext = style.split('/')[-1]

                if ext not in ('omp', 'intel', 'kk', 'gpu', 'opt'):
                    if style not in headings:
                        headings[style] = []
                elif style not in headings[base_name]:
                    headings[base_name].append(style)

        if found_syntax:
            # write new header
            for s in styles:
                print(f".. index:: {command_type} {s}", file=writer)

            print(file=writer)

            for s, variants in headings.items():
                header = f"{command_type} {s} command"
                print(header, file=writer)
                print("="*len(header), file=writer)
                print(file=writer)

                if len(variants) > 0:
                    print("Accelerator Variants: ", end="", file=writer)
                    print(", ".join([f"*{v}*" for v in variants]), file=writer)
                    print(file=writer)

            # write rest of reader
            print(line, end="", file=writer)
            for line in reader:
                print(line, end="", file=writer)

    if found_syntax and len(styles) > 0:
        # override original file
        shutil.move(new_file, orig_file)
    else:
        os.remove(new_file)