PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vimoutliner/scripts/otl2table.py

https://github.com/companygardener/vimoutliner
Python | 222 lines | 176 code | 7 blank | 39 comment | 5 complexity | a52d2060a5df39b49068d75268c40648 MD5 | raw file
  1. #!/usr/bin/python
  2. # otl2table.py
  3. # convert a tab-formatted outline from VIM to tab-delimited table
  4. #
  5. # Copyright (c) 2004 Noel Henson All rights reserved
  6. #
  7. # ALPHA VERSION!!!
  8. # $Revision: 1.2 $
  9. # $Date: 2005/09/25 14:24:28 $
  10. # $Author: noel $
  11. # $Source: /home/noel/active/otl2table/RCS/otl2table.py,v $
  12. # $Locker: $
  13. ###########################################################################
  14. # Basic function
  15. #
  16. # This program accepts text outline files and converts them
  17. # the tab-delimited text tables.
  18. # This:
  19. # Test
  20. # Dog
  21. # Barks
  22. # Howls
  23. # Cat
  24. # Meows
  25. # Yowls
  26. # Becomes this:
  27. # Test Dog Barks
  28. # Test Dog Howls
  29. # Test Cat Meows
  30. # Test Cat Yowls
  31. #
  32. # This will make searching for groups of data and report generation easier.
  33. #
  34. ###########################################################################
  35. # include whatever mdules we need
  36. import sys
  37. from string import *
  38. #from time import *
  39. ###########################################################################
  40. # global variables
  41. level = 0
  42. inputFile = ""
  43. formatMode = "tab"
  44. noTrailing = 0
  45. columns = []
  46. ###########################################################################
  47. # function definitions
  48. # usage
  49. # print the simplest form of help
  50. # input: none
  51. # output: simple command usage is printed on the console
  52. def showUsage():
  53. print
  54. print "Usage:"
  55. print "otl2table.py [options] inputfile > outputfile"
  56. print "Options"
  57. print " -n Don't include trailing columns."
  58. print " -t type Specify field separator type."
  59. print " Types:"
  60. print " tab - separate fields with tabs (default)"
  61. print " csv - separate fields with ,"
  62. print " qcsv - separate fields with \",\""
  63. print " bullets - uses HTML tags <ul> and <li>"
  64. print " -v Print version (RCS) information."
  65. print "output is on STDOUT"
  66. print
  67. # version
  68. # print the RCS version information
  69. # input: none
  70. # output: RSC version information is printed on the console
  71. def showVersion():
  72. print
  73. print "RCS"
  74. print " $Revision: 1.2 $"
  75. print " $Date: 2005/09/25 14:24:28 $"
  76. print " $Author: noel $"
  77. print " $Source: /home/noel/active/otl2table/RCS/otl2table.py,v $"
  78. print
  79. # getArgs
  80. # Check for input arguments and set the necessary switches
  81. # input: none
  82. # output: possible console output for help, switch variables may be set
  83. def getArgs():
  84. global inputfile, debug, noTrailing, formatMode
  85. if (len(sys.argv) == 1):
  86. showUsage()
  87. sys.exit()()
  88. else:
  89. for i in range(len(sys.argv)):
  90. if (i != 0):
  91. if (sys.argv[i] == "-d"): debug = 1 # test for debug flag
  92. if (sys.argv[i] == "-n"): noTrailing = 1 # test for noTrailing flag
  93. elif (sys.argv[i] == "-?"): # test for help flag
  94. showUsage() # show the help
  95. sys.exit() # exit
  96. elif (sys.argv[i] == "--help"):
  97. showUsage()
  98. sys.exit()
  99. elif (sys.argv[i] == "-h"):
  100. showUsage()
  101. sys.exit()
  102. elif (sys.argv[i] == "-v"):
  103. showVersion()
  104. sys.exit()
  105. elif (sys.argv[i] == "-t"): # test for the type flag
  106. formatMode = sys.argv[i+1] # get the type
  107. i = i + 1 # increment the pointer
  108. elif (sys.argv[i][0] == "-"):
  109. print "Error! Unknown option. Aborting"
  110. sys.exit()
  111. else: # get the input file name
  112. inputfile = sys.argv[i]
  113. # getLineLevel
  114. # get the level of the current line (count the number of tabs)
  115. # input: linein - a single line that may or may not have tabs at the beginning
  116. # output: returns a number 1 is the lowest
  117. def getLineLevel(linein):
  118. strstart = lstrip(linein) # find the start of text in line
  119. x = find(linein,strstart) # find the text index in the line
  120. n = count(linein,"\t",0,x) # count the tabs
  121. return(n+1) # return the count + 1 (for level)
  122. # getLineTextLevel
  123. # get the level of the current line (count the number of tabs)
  124. # input: linein - a single line that may or may not have tabs at the beginning
  125. # output: returns a number 1 is the lowest
  126. def getLineTextLevel(linein):
  127. strstart = lstrip(linein) # find the start of text in line
  128. x = find(linein,strstart) # find the text index in the line
  129. n = count(linein,"\t",0,x) # count the tabs
  130. n = n + count(linein," ",0,x) # count the spaces
  131. return(n+1) # return the count + 1 (for level)
  132. # closeLevels
  133. # print the assembled line
  134. # input: columns - an array of 10 lines (for 10 levels)
  135. # level - an integer between 1 and 9 that show the current level
  136. # (not to be confused with the level of the current line)
  137. # noTrailing - don't print trailing, empty columns
  138. # output: through standard out
  139. def closeLevels():
  140. global level,columns,noTrailing,formatMode
  141. if noTrailing == 1 :
  142. colcount = level
  143. else:
  144. colcount = 10
  145. if formatMode == "tab":
  146. for i in range(1,colcount+1):
  147. print columns[i] + "\t",
  148. print
  149. elif formatMode == "csv":
  150. output = ""
  151. for i in range(1,colcount):
  152. output = output + columns[i] + ","
  153. output = output + columns[colcount]
  154. print output
  155. elif formatMode == "qcsv":
  156. output = "\""
  157. for i in range(1,colcount):
  158. output = output + columns[i] + "\",\""
  159. output = output + columns[colcount] + "\""
  160. print output
  161. for i in range(level+1,10):
  162. columns[i] = ""
  163. # processLine
  164. # process a single line
  165. # input: linein - a single line that may or may not have tabs at the beginning
  166. # format - a string indicating the mode to use for formatting
  167. # level - an integer between 1 and 9 that show the current level
  168. # (not to be confused with the level of the current line)
  169. # output: through standard out
  170. def processLine(linein):
  171. global level, noTrailing, columns
  172. if (lstrip(linein) == ""): return
  173. lineLevel = getLineLevel(linein)
  174. if (lineLevel > level):
  175. columns[lineLevel] = lstrip(rstrip(linein))
  176. level = lineLevel
  177. elif (lineLevel == level):
  178. closeLevels()
  179. columns[lineLevel] = lstrip(rstrip(linein))
  180. else:
  181. closeLevels()
  182. level = lineLevel
  183. columns[lineLevel] = lstrip(rstrip(linein))
  184. def main():
  185. global columns
  186. getArgs()
  187. file = open(inputfile,"r")
  188. for i in range(11):
  189. columns.append("")
  190. linein = lstrip(rstrip(file.readline()))
  191. while linein != "":
  192. processLine(linein)
  193. linein = file.readline()
  194. closeLevels()
  195. file.close()
  196. main()