-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddMacroToMpc.py
More file actions
63 lines (53 loc) · 2.2 KB
/
addMacroToMpc.py
File metadata and controls
63 lines (53 loc) · 2.2 KB
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
import fileinput
import sys
import os
#------------------------------------------
def addMacroToFile(filename, newStatement):
#print("called addMacroToFile:", filename, determinePositionOfLastBrace(filename))
returnValue = "fail"
posLastBrace = determinePositionOfLastBrace(filename)
posCurrentLine = 1
for line in fileinput.input(filename, inplace=True):
# add additional line in case of matching last brace
if posCurrentLine == posLastBrace:
sys.stdout.write(newStatement)
sys.stdout.write("\n") # newline, else it would be put just before the brace
returnValue = "success"
sys.stdout.write(line) # prints without additional newline (on windows)
posCurrentLine += 1
return returnValue
#------------------------------------------
# return the position of the last brace.
# possible improvement: in case of some malformed mpc: skip ..
def determinePositionOfLastBrace(filename):
lastPos = -1
position = 1
with open(filename, "r") as file: #has implicit close - which is nice
for line in file:
if line.__contains__("}"):
lastPos = position
position += 1
return lastPos
# ------------------------------------------
# find all mpb/mpc files
def fixAllFilesRecursively(path, suffix, newStatement):
for dirname, dirnames, filenames in os.walk(path):
for filename in filenames:
full_path = os.path.join(dirname, filename)
if full_path.endswith(suffix):
print(full_path)
wasSuccessful = addMacroToFile(full_path, newStatement)
if wasSuccessful == "success":
print(" modified :)")
# prevent recursion into those subdirs with meta-data
if '.svn' in dirnames:
dirnames.remove('.svn')
if '.git' in dirnames:
dirnames.remove('.git')
#------------------------------------------
#------------------------------------------
path = "D:\Repo_INS_RADARNX"
suffix = "mpc"
newStatement = " macros += QT_MESSAGELOGCONTEXT"
# TODO possible improvement: prevent double application of the added macro ..
fixAllFilesRecursively(path, suffix, newStatement)