(no commit message)
[hombase.git] / other-sh / otlsplit.py
bloba04a6a229d5f99d14387be1603bb812accc3cebb
1 #!/usr/bin/python
2 # otlslit.py
3 # split an outline into several files.
5 # Copyright 2005 Noel Henson All rights reserved
7 # $Revision: 1.9 $
8 # $Date: 2006/02/19 14:39:06 $
9 # $Author: noel $
10 # $Source: /home/noel/active/otlsplit/RCS/otlsplit.py,v $
11 # $Locker: $
13 ###########################################################################
14 # Basic function
16 # This program accepts text outline files and splits them into
17 # several smaller files. The output file names are produced from the
18 # heading names of the parents.
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 ###########################################################################
34 # include whatever mdules we need
36 import sys
37 from string import *
38 from re import *
40 ###########################################################################
41 # global variables
43 debug = 0
44 subdir = ""
45 level = 1
46 title = 0
47 inputfile = ""
49 ###########################################################################
50 # function definitions# usage
52 # print debug statements
53 # input: string
54 # output: string printed to standard out
56 def dprint(*vals):
57 global debug
58 if debug != 0: print vals
60 # usage
61 # print the simplest form of help
62 # input: none
63 # output: simple command usage is printed on the console
65 def showUsage():
66 print
67 print "Usage:"
68 print "otlsplit.py [options] inputfile"
69 print "Options"
70 print " -l level The number of levels to split down to. The default is 1"
71 print " -D dir Specifiy a target directory for the output files"
72 print " -t Include a title line (the parerent heading) in split files"
73 print " -v Print version (RCS) information."
74 print " -h Show help."
75 print "output is on STDOUT"
76 print
78 # version
79 # print the RCS version information
80 # input: none
81 # output: RSC version information is printed on the console
83 def showVersion():
84 print
85 print "RCS"
86 print " $Revision: 1.9 $"
87 print " $Date: 2006/02/19 14:39:06 $"
88 print " $Author: noel $"
89 print
91 # getArgs
92 # Check for input arguments and set the necessary switches
93 # input: none
94 # output: possible console output for help, switch variables may be set
96 def getArgs():
97 global debug, level, inputfile, title, subdir
98 if (len(sys.argv) == 1):
99 showUsage()
100 sys.exit()()
101 else:
102 for i in range(len(sys.argv)):
103 if (i != 0):
104 if (sys.argv[i] == "-d"): debug = 1 # test for debug flag
105 elif (sys.argv[i] == "-?"): # test for help flag
106 showUsage() # show the help
107 sys.exit() # exit
108 elif (sys.argv[i] == "-l"): # test for the level flag
109 level = int(sys.argv[i+1]) # get the level
110 i = i + 1 # increment the pointer
111 elif (sys.argv[i] == "-D"): # test for the subdir flag
112 subdir = sys.argv[i+1] # get the subdir
113 i = i + 1 # increment the pointer
114 elif (sys.argv[i] == "-t"): title = 1 # test for title flag
115 elif (sys.argv[i] == "--help"):
116 showUsage()
117 sys.exit()
118 elif (sys.argv[i] == "-h"):
119 showUsage()
120 sys.exit()
121 elif (sys.argv[i] == "-v"):
122 showVersion()
123 sys.exit()
124 elif (sys.argv[i][0] == "-"):
125 print "Error! Unknown option. Aborting"
126 sys.exit()
127 else: # get the input file name
128 inputfile = sys.argv[i]
130 # getLineLevel
131 # get the level of the current line (count the number of tabs)
132 # input: linein - a single line that may or may not have tabs at the beginning
133 # output: returns a number 1 is the lowest
135 def getLineLevel(linein):
136 strstart = lstrip(linein) # find the start of text in line
137 x = find(linein,strstart) # find the text index in the line
138 n = count(linein,"\t",0,x) # count the tabs
139 return(n) # return the count + 1 (for level)
141 # convertSensitiveChars
142 # get the level of the current line (count the number of tabs)
143 # input: line - a single line that may or may not have tabs at the beginning
144 # output: returns a string
146 def convertSensitiveChars(line):
147 line = lstrip(rstrip(line))
148 line = sub('\W','_',line)
149 return(line)
151 # makeFileName
152 # make a file name from the string array provided
153 # input: line - a single line that may or may not have tabs at the beginning
154 # output: returns a string
156 def makeFileName(nameParts):
158 global debug, level, subdir
160 filename = ""
161 for i in range(level):
162 filename = filename + lstrip(rstrip(convertSensitiveChars(nameParts[i]))) + "-"
163 filename = filename[:-1]+ ".otl"
164 if subdir != "": filename = subdir + "/" + filename
165 return(lower(filename))
167 # processFile
168 # split an outline file
169 # input: file - the filehandle of the file we are splitting
170 # output: output files
172 def processFile(file):
174 global debug, level, title
176 nameparts = []
177 for i in range(10):
178 nameparts.append("")
180 outOpen = 0
182 line = file.readline() # read the outline title
183 # and discard it
184 line = file.readline() # read the first parent heading
185 dprint(level)
186 while (line !=""):
187 linelevel = getLineLevel(line)
188 if (linelevel < level):
189 if outOpen == 1:
190 ofile.close()
191 outOpen = 0
192 nameparts[linelevel] = line
193 dprint(level,linelevel,line)
194 else:
195 if outOpen == 0:
196 ofile = open(makeFileName(nameparts),"w")
197 outOpen = 1
198 if title == 1:
199 dprint("title:",title)
200 ofile.write(nameparts[level-1])
201 ofile.write(line[level:])
202 line = file.readline()
204 # main
205 # split an outline
206 # input: args and input file
207 # output: output files
209 def main():
210 global inputfile, debug
211 getArgs()
212 file = open(inputfile,"r")
213 processFile(file)
214 file.close()
216 main()