-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathORTS-RollingStockScanner.py
374 lines (334 loc) · 16 KB
/
ORTS-RollingStockScanner.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python3
# ORTS-RollingStockScanner - search for eng and wag files and list key properties in CSV format
#
# Copyright (c) 2024 Roger Fischer. MIT License.
#
# Some of the more challengin tokens:
# - Mass ( "56.163t #23.186t empty, 90.163t full" ) -- comment inside quotes
# - Name ( "BNSF SD70ACe #8490" ) -- not a comment
# - DerailRailForce ( "2.5m/(s^2)*64t" ) -- non-trivial math
#
import argparse
import pathlib
import re
import sys
# global variables
numEng = numWag = numWarn = 0
heading = None
### get the content (package) directory, the one above TRAINS
def getContentDir( filePath) :
TRAINS = "TRAINS".casefold()
absPath = filePath.resolve()
for i in range(len(absPath.parents)) :
if absPath.parents[i].name.casefold() == TRAINS and i + 1 < len(absPath.parents) :
return absPath.parents[i+1].name
return None
### read a file that is either utf-16 or utf8
def readFile( filePath) :
enc = "utf-16"
bytes = filePath.read_bytes()
if 0 < bytes[0] < 128: enc = 'utf-8'
return bytes.decode(encoding = enc, errors = 'replace' )
#### read the eng or wag file and resolve includes
def readTrainsetFile(filePath, refDir) :
txt = readFile(filePath) # .replace('\n', ' ').replace('\r', '')
# resolve includes
ml = re.finditer( 'include\\s*\\(([^)]+)\\)', txt, flags=re.IGNORECASE)
for m in ml :
incPathName = m.group(1).strip().strip('"')
incPath = pathlib.Path(refDir, incPathName).resolve()
incTxt = readFile(incPath)
txt = txt.replace(m.group(0), incTxt, 1)
return txt
### get value for a token; exclude quotes
### is incorrect for nested tokens; stops at the closing parenthesis of a nested token
def getValue( token, txt) :
value = ''
start = 0 ; end = 0
startPat = token + '\\s*\\(\\s*.' ; endPat = '\\s*\\)'
m = re.search(startPat, txt, flags=re.IGNORECASE) # find opening parenthesis
if m :
start = m.end() - 1
if txt[start] == '"' :
start += 1
endPat = '"\\s*\\)'
m = re.search(endPat, txt[start:], flags=re.IGNORECASE) # find closing parenthesis
if m :
end = start + m.start()
value = txt[start:end]
return value
### parse eng or wag file and collect relevant data
def processFile(values, txt, filePath, isEngine) :
global numWarn
# separate the wagon and engin parts; assumes that wagon is always first
wagTxt = txt ; engTxt = ""
if isEngine :
m = re.search('Engine\\s*\\(\\s*', txt, flags=re.IGNORECASE)
if m :
engOffset = m.start() ; engTxt = txt[engOffset:] ; wagTxt = txt[:engOffset]
else :
numWarn += 1
print("Warning: Unable to find engine section in", filePath, file=sys.stderr)
# get wagon name, engine name; nested token, cannot use getValue()
name = 'Name' ; values[name] = '_'
m = re.search('Wagon\\s*\\(\\s*"?(\\w+)"?', txt, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
elif not isEngine :
numWarn += 1
print("Warning: Unable to find wagon name in", filePath, file=sys.stderr)
if isEngine :
m = re.search('Engine\\s*\\(\\s*"?(\\w+)"?', txt, flags=re.IGNORECASE)
if m is None or m.lastindex < 1 :
numWarn += 1
print("Warning: Unable to find engine name in", filePath, file=sys.stderr)
elif not values[name] :
values[name] = m.group(1)
numWarn += 1
print("Warning: Unable to find wagon name (using engine name) in", filePath, file=sys.stderr)
elif values[name] != m.group(1) :
numWarn += 1
print("Warning: Wagon name ({}) does not match engine name ({}) in {}".format(values[name], m.group(1), filePath), file=sys.stderr)
# display name, may contain spaces, may be quoted; either in wagon or engine section
name = 'DispName' ; values[name] = '_'
val = getValue('Name', txt)
if val : values[name] = val.strip()
else :
values[name] = values['Name'] + ' (dflt)' # default to name in Engine or Wagon token
if verbose > 0 : print("Info: Unable to find wagon or engine display name in", filePath, file=sys.stderr)
# wagon type (engine, freight, passenger, etc)
name = 'Type' ; values[name] = '_'
val = getValue('Type', wagTxt)
m = re.search('\\s*(\\w+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
else :
numWarn += 1
print("Warning: Unable to find wagon type in", filePath, file=sys.stderr)
# engine type (diesel, electric, steam, etc)
name = 'SubType' ; values[name] = '_'
if isEngine :
val = getValue('Type', engTxt)
m = re.search('\\s*(\\w+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
else :
numWarn += 1
print("Warning: Unable to find engine type in", filePath, file=sys.stderr)
# engine max velocity
name = 'MaxSpeed' ; values[name] = '_'
if isEngine :
val = getValue('MaxVelocity', engTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
else :
numWarn += 1
print("Warning: Unable to find engine max velocity in", filePath, file=sys.stderr)
# engine max power
name = 'MaxPower' ; values[name] = '_'
if isEngine :
val = getValue('MaximalPower', engTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = "OR " + m.group(1)
else :
val = getValue('MaxPower', engTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
else :
numWarn += 1
print("Warning: Unable to find engine max power in", filePath, file=sys.stderr)
# engine max force
name = 'MaxForce' ; values[name] = '_'
if isEngine :
val = getValue('MaxForce', engTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
else :
numWarn += 1
print("Warning: Unable to find engine max force in", filePath, file=sys.stderr)
# max brake force
name = 'MaxBrakeForce' ; values[name] = '_'
val = getValue('MaxBrakeForce', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
else :
numWarn += 1
print("Warning: Unable to find wagon max brake force in", filePath, file=sys.stderr)
# weight
name = 'Weight' ; values[name] = '_'
val = getValue( 'Mass', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*^()]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
else :
numWarn += 1
print("Warning: Unable to find wagon weight in", filePath, file=sys.stderr)
# length, third value
name = 'Length' ; values[name] = '_'
val = getValue( 'Size', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 3 : values[name] = m.group(3)
else :
numWarn += 1
print("Warning: Unable to find wagon size in", filePath, file=sys.stderr)
# number of wheels or axles (OR), wagon and engine; has ORTS variants
name = 'Wheels/Axles' ; values[name] = '_'
val = getValue( 'ORTSNumberAxles', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 :
values[name] = "OR " + m.group(1)
if isEngine:
val = getValue('ORTSNumberDriveAxles', engTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] += " | " + m.group(1)
else :
numWarn += 1
print("Warning: Unable to find engine ORTS number of wheels in", filePath, file=sys.stderr)
else :
val = getValue('NumWheels', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 :
values[name] = m.group(1)
if isEngine:
val = getValue('NumWheels', engTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1: values[name] += " | " + m.group(1)
else :
numWarn += 1
print("Warning: Unable to find engine number of wheels in", filePath, file=sys.stderr)
else :
numWarn += 1
print("Warning: Unable to find wagon number of wheels in", filePath, file=sys.stderr)
# coupler strength, may occur twice, use second value of each occurrence
name = 'CouplerStrength' ; values[name] = '_'
first = re.search( 'Coupling\\s*\\(', wagTxt, flags=re.IGNORECASE)
if not first :
numWarn += 1
print("Warning: Unable to find wagon coupler section in", filePath, file=sys.stderr)
else :
val = getValue('Break', wagTxt[first.start():])
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if not m or m.lastindex < 2 :
numWarn += 1
print("Warning: Unable to find wagon coupler strength in", filePath, file=sys.stderr)
else :
values[name] = m.group(2)
# look for optional second section
second = re.search( 'Coupling\\s*\\(', wagTxt[first.end():], flags=re.IGNORECASE)
if not second :
if verbose > 0 : print("Info: no second coupler section in", filePath, file=sys.stderr)
else :
val = getValue('Break', wagTxt[second.start():]) # search for second block
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if not m or m.lastindex < 2 :
numWarn += 1
print("Warning: Unable to find wagon second coupler strength in", filePath, file=sys.stderr)
else :
values[name] += " | " + m.group(2)
# friction, using the first 5 values only; has ORTS variant
name = 'Friction' ; values[name] = '_'
val = getValue( 'ORTSDavis_A', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 :
values[name] = "OR " + m.group(1)
val = getValue('ORTSDavis_B', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1:
values[name] += " | " + m.group(1)
val = getValue('ORTSDavis_C', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1:
values[name] += " | " + m.group(1)
else :
val = getValue('Friction', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s+', val, flags=re.IGNORECASE)
if m and m.lastindex >= 5 :
values[name] = m.group(1) + " | " + m.group(2) + " | " + m.group(3) + " | " + m.group(4) + " | " + m.group(5)
else :
numWarn += 1
print("Warning: Unable to find wagon friction values in", filePath, file=sys.stderr)
# adhesion, 3 values; has ORTS variant; is in wagon section, but only used for engines
name = 'Adhesion' ; values[name] = '_'
val = getValue( 'ORTSCurtius_Kniffler', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 4 :
values[name] = "OR " + m.group(1) + " | " + m.group(2) + " | " + m.group(3) + " | " + m.group(4)
else :
val2 = getValue('Adheasion', wagTxt)
m2 = re.search('\\s*([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s+([-+0-9.,a-z/*"]+)\\s+', val2, flags=re.IGNORECASE)
if m2 and m2.lastindex >= 3 : values[name] = m2.group(1) + " | " + m2.group(2) + " | " + m2.group(3)
elif isEngine :
numWarn += 1
print("Warning: Unable to find wagon adhesion values in", filePath, file=sys.stderr)
# derail rail force
name = "DerailRailForce" ; values[name] = '_'
val = getValue('DerailRailForce', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*^()]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
else :
numWarn += 1
print("Warning: Unable to find wagon derail rail force in", filePath, file=sys.stderr)
# derail buffer force
name = 'DerailBufferForce' ; values[name] = '_'
val = getValue('DerailBufferForce', wagTxt)
m = re.search('\\s*([-+0-9.,a-z/*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
else :
numWarn += 1
print("Warning: Unable to find wagon derail buffer force in", filePath, file=sys.stderr)
# length including couplers ORTS only
name = 'TotalLength' ; values[name] = '_'
val = getValue('ORTSLengthCouplerFace', wagTxt)
m = re.search('\\s*([-+0-9.,a-z /*"]+)\\s*', val, flags=re.IGNORECASE)
if m and m.lastindex >= 1 : values[name] = m.group(1)
return
### process a path
def processPath( path, isEngine) :
global heading, numWarn
rowValues = {}
packageName = getContentDir( path)
if not packageName :
print("Warning: ignoring {}, could not find package name".format(path), file=sys.stderr)
numWarn += 1
return # do not process files outside the TRAINS directory
rowValues["Package"] = packageName
rowValues["Directory"] = path.parent.name
rowValues["File"] = path.name
text = readTrainsetFile(path, path.parent)
processFile(rowValues, text, path, isEngine)
if heading is None :
heading = rowValues.keys()
print(*heading, sep=',')
print( *rowValues.values(), sep=',')
return
### main
parser = argparse.ArgumentParser()
parser.add_argument('dirPath', type=pathlib.Path, help='Directory where to search for eng and wag files.')
parser.add_argument('-f', '--filter',
help='Optional filter. "eng" limits to engines, "wag" limits to wagons, any other value is matched to the file name')
parser.add_argument('-v', '--verbose', action='count', default=0)
args = parser.parse_args()
dirPath = args.dirPath
filter = args.filter
verbose = args.verbose
if not dirPath.is_dir() :
print( "Error: {} is not a directory.".format(args.dirPath), file=sys.stderr)
sys.exit(1)
doEng = doWag = True
pattern = None
if filter == 'wag' : doEng = False
elif filter == 'eng' : doWag = False
elif filter : pattern = re.compile(filter, flags=re.IGNORECASE)
# process engine files
if doEng :
for path in dirPath.rglob( "*.eng") :
if pattern and not pattern.search(path.name) : continue
if verbose > 1 : print( "...processing engine ", path, file=sys.stderr)
numEng += 1
processPath( path, True)
# process wagon files
if doWag :
for path in dirPath.rglob( "*.wag") :
if pattern and not pattern.search(path.name) : continue
elif path.name == 'default.wag' : continue
if verbose > 1 : print( "...processing wagon ", path, file=sys.stderr)
numWag += 1
processPath( path, False)
print( "Processed {} Eng and {} Wag files, total {}; generated {} warnings".format( numEng, numWag, numEng + numWag, numWarn), file=sys.stderr)
exit(0)