File: conv.py

package info (click to toggle)
ding 1.7-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 14,256 kB
  • sloc: tcl: 27,236; sh: 152; perl: 122; makefile: 82; python: 38; xml: 28
file content (48 lines) | stat: -rwxr-xr-x 1,154 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/python
# By John Goerzen
# GPL
# Copyright (C) 2002 John Goerzen
from sys import argv, stdin, stdout
import re

reverse = 0

if len(argv) >= 2 and argv[1] == '-r':
    reverse = 1

def printdef(entry, definition):
    entrylist = entry.split(';')
    definition = definition.strip()
    for item in entrylist:
        bracetype = re.search('[(\[{].+[)\]}]', item)
        if bracetype:
            bracetype = " " + bracetype.group(0)
            item = re.sub('[(\[{].+[)\]}]', '', item)
        else:
            bracetype = ""
        item = item.strip()
        if reverse:
            if item.startswith("to "):
                item = item[3:]
        print ":%s:%s" % (item, bracetype)
        print "   %s" % (definition)

while 1:
    line = stdin.readline()
    if not line:
        break
    line.strip()
    if line[0] == '#':
        stdout.write(line)
        continue

    if line.find("::") == -1:
        continue
    entry, definition = line.split("::")
    if not entry.strip() or not definition.strip():
        continue
    if not reverse:
        printdef(entry, definition)
    else:
        printdef(definition, entry)