File: parsetpl.py

package info (click to toggle)
vtk-dicom 0.8.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,032 kB
  • sloc: cpp: 113,806; python: 2,041; makefile: 43; tcl: 10
file content (84 lines) | stat: -rw-r--r-- 1,498 bytes parent folder | download | duplicates (2)
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
"""
Parse one of Dave Clunie's tpl files and generate a flat file.
"""

import sys
import string

blocks = {}

def parseline(line):
  if line[0] != '(':
    return
  entries = {}
  tag = line[0:11]
  i = 12
  while 1:
    try:
      while line[i] in string.whitespace:
        i = i + 1
    except IndexError:
      break
    j = i
    try:
      while line[j] != '=':
        j = j + 1
    except IndexError:
      break
    key = line[i:j]
    if line[j+1] != '\"':
      break
    i = j + 2
    j = i
    while line[j] != '\"':
      j = j + 1
    val = line[i:j]
    i = j + 1
    entries[key] = val.strip()

  try:
    entries['Name']
    entries['Keyword']
    entries['VR']
    entries['VM']
    entries['Owner']
  except KeyError:
    sys.stderr.write("Missing key: %s\n" % (line,))
    return

  try:
    block = blocks[entries['Owner']]
  except KeyError:
    block = {}
    blocks[entries['Owner']] = block

  block[tag] = entries

  #g = int(tag[1:5], 16)
  #e = int(tag[6:10], 16)

  #tag = "(%04x,%04x)" % (g,e)

for filename in sys.argv[1:]:
  f = open(filename)
  for line in f.readlines():
    parseline(line)

#print(len(blocks))

for owner, block in blocks.items():
  lines = []
  tags = block.keys()

  tags.sort()
  for tag in tags:
    entries = block[tag]
    lines.append(tag)
    lines.append(entries['Name'])
    lines.append(entries['Keyword'])
    lines.append(entries['VR'])
    lines.append(entries['VM'])
    lines.append(owner)

  for line in lines:
    print(line)