Repository navigation
Expand file tree
/
Copy pathtia-parser.py
More file actions
65 lines (57 loc) · 2.54 KB
/
Copy pathtia-parser.py
File metadata and controls
65 lines (57 loc) · 2.54 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
64
65
# Copyright (c) 2026, Nils Emmerich. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from tia import TiaProject
from hashlib import md5
from argparse import ArgumentParser
parser = ArgumentParser(prog='tia-parser', description='Parse TIA project files and convert it to json or body files')
parser.add_argument('filename')
parser.add_argument('-b', '--body', help='Output a body file into the file specified')
parser.add_argument('-j', '--json', help='Output a json file into the file specified')
args = parser.parse_args()
if args.body is None and args.json is None:
parser.error('At least body or json output must be specified')
with open(args.filename, 'rb') as f:
data = f.read()
project = TiaProject()
project.loadFromBytes(data)
if args.json is not None:
with open(args.json, 'w') as f:
for obj in project.getObjects():
f.write(obj.jsonDump())
f.write('\n')
def writeBodyLine(f, obj):
if obj.implements('Modified'):
name = ''
ctime = int(obj['created'].timestamp())
mtime = int(obj['modified'].timestamp())
elif obj.implements('Commit'):
name = obj['description'].decode('utf-8')
ctime = int(obj['timestamp'].timestamp())
mtime = int(obj['timestamp'].timestamp())
elif obj.implements('Close'):
name = obj['description'].decode('utf-8')
ctime = int(obj['timestamp'].timestamp())
mtime = int(obj['timestamp'].timestamp())
elif obj.implements('Siemens.Automation.ObjectFrame.ICoreAttributes'):
core = obj['Siemens.Automation.ObjectFrame.ICoreAttributes']
if 'CreationTime' in core:
ctime = int(core['CreationTime'].timestamp())
else:
ctime = 0
if 'ModifiedTime' in core:
mtime = int(core['ModifiedTime'].timestamp())
else:
mtime = 0
name = ""
if 'Name' in core:
name = core['Name'].decode('utf-8')
else:
return
# 3.X: MD5|name|inode|mode_as_string|UID|GID|size|atime|mtime|ctime|crtime
# 2.X: MD5|path/name|device|inode|mode_as_value|mode_as_string|num_of_links|UID|GID|rdev|size|atime|mtime|ctime|block_size|num_of_blocks
f.write(f"{md5(obj.hash).hexdigest()}|{obj.type.name}/{obj.idAndType.id}/{name}|0|-|0|0|{obj.size}|0|{mtime}|{ctime}|0\n")
if args.body is not None:
with open(args.body, 'w') as f:
for obj in project.getObjects():
writeBodyLine(f, obj)