Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
update_modelines.py
Go to the documentation of this file.
1#!/bin/python
2# This file is part of darktable,
3# Copyright (C) 2022 Martin Bařinka.
4# Copyright (C) 2022 Miloš Komarčević.
5#
6# darktable is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# darktable is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with darktable. If not, see <http://www.gnu.org/licenses/>.
18#
19#
20#
21#
22#
23#
24#
25#
26#
27#
28import re
29import sys
30from enum import Enum
31import shlex
32import subprocess
33
34CLANG_OFF='// clang-format off\n'
35NOTIFICATION_LINE='// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py\n'
36VIM_MODELINE='// vim: shiftwidth=2 expandtab tabstop=2 cindent\n'
37KATE_MODELINE='// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;\n'
38CLANG_ON='// clang-format on\n'
39
40class estate(Enum):
41 none=1
42 start=2
43 inside=3
44 end=4
45
47 def __init__(self, filename):
48 self.filename = filename
49 self.state = estate.none
50 self.begin = -1
51 self.end = -1
52 self.lines = []
53
54 def load_lines(self):
55 with open(self.filename,'r') as file:
56 self.lines = file.readlines()
57
58 def process_line(self, line, lineno):
59 if(self.state == estate.none):
60 if(line.startswith(CLANG_OFF)):
61 self.state=estate.start
62 return
63 elif(self.state == estate.start):
64 if(line.startswith('// modelines:')):
65 self.state=estate.inside
66 self.begin = lineno-1
67 else:
68 self.state=estate.none
69 return
70 elif(self.state == estate.inside):
71 if(line.startswith(CLANG_ON)):
72 self.state=estate.end
73 self.end = lineno
74 return
75 elif(self.state == estate.end):
76 if(not line.strip()):
77 self.end = lineno
78 else:
79 self.state=estate.none
80 return
81
82 def remove_lines(self):
83 with open(self.filename,'w') as file:
84 for lineno,line in enumerate(self.lines):
85 if((lineno >= self.begin) and (lineno <= self.end)):
86 continue
87 if(line.startswith('// modelines')):
88 continue
89 if(line.startswith('// vim')):
90 continue
91 if(line.startswith('// kate')):
92 continue
93 file.write(line)
94
95 def write_file(self):
96 with open(self.filename,'a') as file:
97 file.write(CLANG_OFF)
98 file.write(NOTIFICATION_LINE)
99 file.write(VIM_MODELINE)
100 file.write(KATE_MODELINE)
101 file.write(CLANG_ON)
102
103 def update(self):
104 print('parsing {}'.format(self.filename));
105 self.load_lines()
106 for lineno,line in enumerate(self.lines):
107 self.process_line(line, lineno)
108 if((self.begin == -1) != (self.end == -1)):
109 raise RuntimeError("parsing error")
110 print('removing old modelines')
111 self.remove_lines()
112 print('writing file')
113 self.write_file()
114
115
116
117if __name__ == "__main__":
118 files = []
119 args = shlex.split('sh -c \'find src/ -name "*.c" -or -name "*.cc" -or -name "*.h" | grep -v src/external\'')
120 ret = subprocess.run(args,capture_output=True)
121 if ret.returncode != 0:
122 print(ret.stderr.decode(sys.stderr.encoding))
123 raise RuntimeError("error listing files")
124 files = ret.stdout.decode(sys.stdout.encoding).split('\n')
125 for file in files:
126 if (not file.strip()):
127 continue
128 updater = modelines_updater_t(file.strip())
129 updater.update()
130
131
if(L< 0.5f) C