scripts/autoid.py
author amit@thunder
Mon, 25 Jan 2010 18:56:45 +0530
changeset 0 8083d21c0020
permissions -rw-r--r--
The first commit of all the required files for the review app
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     1
#!/usr/bin/env python
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     2
#
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     3
# Add unique ID attributes to para tags.  This script should only be
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     4
# run by one person, since otherwise it introduces the possibility of
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     5
# chaotic conflicts among tags.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     6
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     7
import glob, os, re, sys
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     8
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     9
tagged = re.compile('<para[^>]* id="x_([0-9a-f]+)"[^>]*>', re.M)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    10
untagged = re.compile('<para>')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    11
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    12
names = glob.glob('ch*.docbook') 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    13
# First pass: find the highest-numbered paragraph ID.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    14
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    15
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    16
chapter=None
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    17
seen = set()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    18
errs = 0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    19
beginning="p_list= "
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    20
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    21
id_file=open('p_list.py','w')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    22
dictionary={}
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    23
id_list=[]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    24
for name in names:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    25
    for m in tagged.finditer(open(name).read()):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    26
        i = int(m.group(1),16)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    27
        if i in seen:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    28
            print >> sys.stderr, '%s: duplication of ID %s' % (name, i)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    29
            errs += 1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    30
        seen.add(i)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    31
        if i > biggest_id:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    32
            biggest_id = i
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    33
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    34
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    35
def retag(s):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    36
    global biggest_id
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    37
    global chapter   
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    38
    biggest_id += 1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    39
     
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    40
    id_name="%s_%x" % (chapter,biggest_id)   
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    41
    id_list.append(id_name)    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    42
    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    43
    return '<para id="%s">' %id_name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    44
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    45
# Second pass: add IDs to paragraphs that currently lack them.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    46
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    47
for name in names:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    48
    biggest_id=0 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    49
    chapter=name.split('.')[0]    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    50
    id_list=[]    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    51
    f = open(name).read()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    52
    f1 = untagged.sub(retag, f )
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    53
    dictionary[chapter]=id_list       
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    54
    if f1 != f:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    55
        tmpname = name + '.tmp'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    56
        fp = open(tmpname, 'w')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    57
        fp.write(f1)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    58
        fp.close()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    59
        os.rename(tmpname, name)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    60
p_lists_string=beginning+str(dictionary)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    61
id_file.write(p_lists_string)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    62
sys.exit(errs)