renumber.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Misc » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Mobile » Python for PalmOS 
Python for PalmOS » Python 1.5.2 reduced 1.0 » Misc » renumber.py
#! /usr/bin/env python

# Renumber the Python FAQ

import string
import regex
import sys
import os

FAQ = 'FAQ'

chapterprog = regex.compile('^\([1-9][0-9]*\)\. ')
questionprog = regex.compile('^\([1-9][0-9]*\)\.\([1-9][0-9]*\)\. ')
newquestionprog = regex.compile('^Q\. ')
blankprog = regex.compile('^[ \t]*$')
indentedorblankprog = regex.compile('^\([ \t]+\|[ \t]*$\)')

def main():
  print 'Reading lines...'
  lines = open(FAQ, 'r').readlines()
  print 'Renumbering in memory...'
  oldlines = lines[:]
  after_blank = 1
  chapter = 0
  question = 0
  chapters = ['\n']
  questions = []
  for i in range(len(lines)):
    line = lines[i]
    if after_blank:
      n = chapterprog.match(line)
      if n >= 0:
        chapter = chapter + 1
        question = 0
        line = `chapter` + '. ' + line[n:]
        lines[i] = line
        chapters.append(' ' + line)
        questions.append('\n')
        questions.append(' ' + line)
        afterblank = 0
        continue
      n = questionprog.match(line)
      if n < 0: n = newquestionprog.match(line) - 3
      if n >= 0:
        question = question + 1
        number = '%d.%d. '%(chapter, question)
        line = number + line[n:]
        lines[i] = line
        questions.append('  ' + line)
        # Add up to 4 continuations of the question
        n = len(number)
        for j in range(i+1, i+5):
          if blankprog.match(lines[j]) >= 0:
            break
          questions.append(' '*(n+2) + lines[j])
        afterblank = 0
        continue
    afterblank = (blankprog.match(line) >= 0)
  print 'Inserting list of chapters...'
  chapters.append('\n')
  for i in range(len(lines)):
    line = lines[i]
    if regex.match(
        '^This FAQ is divided in the following chapters',
        line) >= 0:
      i = i+1
      while 1:
        line = lines[i]
        if indentedorblankprog.match(line) < 0:
          break
        del lines[i]
      lines[i:i] = chapters
      break
  else:
    print '*** Can\'t find header for list of chapters'
    print '*** Chapters found:'
    for line in chapters: print line,
  print 'Inserting list of questions...'
  questions.append('\n')
  for i in range(len(lines)):
    line = lines[i]
    if regex.match('^Here.s an overview of the questions',
        line) >= 0:
      i = i+1
      while 1:
        line = lines[i]
        if indentedorblankprog.match(line) < 0:
          break
        del lines[i]
      lines[i:i] = questions
      break
  else:
    print '*** Can\'t find header for list of questions'
    print '*** Questions found:'
    for line in questions: print line,
  if lines == oldlines:
    print 'No changes.'
    return
  print 'Writing new file...'
  f = open(FAQ + '.new', 'w')
  for line in lines:
    f.write(line)
  f.close()
  print 'Making backup...'
  os.rename(FAQ, FAQ + '~')
  print 'Moving new file...'
  os.rename(FAQ + '.new', FAQ)
  print 'Done.'

main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.