SQL file glue script for dad


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/env python
  2.  
  3. # Author: Ben Bradley - [email protected]
  4. # Program for making some changes to a sql file, i dunno
  5.  
  6.  
  7. import sys
  8.  
  9. def ioerror(reason=None):
  10. '''
  11. Used to clean up when any of the file operations raise exceptions
  12. '''
  13. if reason:
  14. if reason == 'input':
  15. print ("Error: couldn't open input file for reading")
  16. elif reason == 'output':
  17. print ("Error: couldn't open output file for writing")
  18. else:
  19. print ("Error: cause unknown")
  20. sys.exit(1)
  21.  
  22.  
  23. def linenumber(filelinesarray, currentelement):
  24. '''
  25. If a file has been read into an array of lines with file.readlines()
  26. it is useful to know which element corresponds to which line number.
  27. '''
  28. # Use str rather than int because linenumber() is always for printing
  29. # saves many str() calls
  30. return str(filelinesarray.index(currentelement)+1)
  31.  
  32.  
  33. def insertafter(infilelines, targetline, conf):
  34. '''
  35. Use to insert statements, variables etc into an array of file lines
  36. '''
  37. # Pad with a newline just in case. Consider:
  38. # )
  39. # <some config>
  40. # ;
  41. # == vs ==
  42. # )
  43. # <some config>;
  44. if type(conf).__name__ == 'str':
  45. if conf.endswith('\n') == False:
  46. conf += '\n'
  47.  
  48. for l in infilelines:
  49.  
  50. if l.lower().startswith(targetline.lower()):
  51. # Comment out (or delete) print statements if ya want
  52. print ('Target line "%s" found @ file line #%s' %
  53. (targetline,
  54. (linenumber(infilelines, l))))
  55.  
  56. print ('Inserting "%s" after file line #%s' %
  57. (conf[:-1],
  58. linenumber(infilelines, l)))
  59.  
  60. offset = infilelines.index(l)+1
  61. if type(conf).__name__ == 'str':
  62. infilelines.insert(offset, conf)
  63. elif type(conf).__name__ == 'list':
  64. for el in conf:
  65. infilelines.insert(offset, el)
  66. offset += 1
  67. break
  68.  
  69. return infilelines
  70.  
  71.  
  72. def do3changes(infilelines, confstring):
  73. '''
  74. Applies the necessary changes as specd by dad
  75. '''
  76. # Major config here; be careful
  77. ALL_CHANGES = {")" : confstring,
  78. " CONSTRAINT" : "using index local",
  79. "CREATE INDEX" :"local"}
  80.  
  81. tmp = infilelines
  82. for k in ALL_CHANGES:
  83. # Rebinds tmp to the new value with change k applied
  84. tmp = insertafter(tmp, k, ALL_CHANGES[k])
  85. return tmp
  86.  
  87.  
  88. def usage():
  89. print "Usage: %s infile configstring outfile" % sys.argv[0]
  90.  
  91. def help():
  92. print ("This is a glue script; just txt or email ben")
  93.  
  94. def version():
  95. print ("This program won't get developed any more, so lets say 0.67")
  96.  
  97.  
  98. def main():
  99. '''
  100. Handle args, run main functionality.
  101. '''
  102. if len(sys.argv) < 2:
  103. usage()
  104.  
  105. elif 1 < len(sys.argv) < 4:
  106. if '-h' in sys.argv or '--help' in sys.argv:
  107. help(); usage()
  108. if '-v' in sys.argv or '--version' in sys.argv:
  109. version()
  110.  
  111. elif len(sys.argv) == 4:
  112. try:
  113. infile = open(sys.argv[1], 'r')
  114. infilelines = infile.readlines()
  115. infile.close()
  116. except:
  117. ioerror('input')
  118.  
  119. try:
  120. insertfile = open(sys.argv[2], 'r')
  121. insertlines = insertfile.readlines()
  122. insertfile.close()
  123. except:
  124. ioerror()
  125.  
  126. try: outfile = open(sys.argv[3], 'w')
  127. except: ioerror('output')
  128.  
  129. edited = do3changes(infilelines, insertlines)
  130.  
  131. try:
  132. for line in edited:
  133. outfile.write(line)
  134. outfile.flush(); outfile.close()
  135. except:
  136. ioerror('output')
  137.  
  138.  
  139. # God forbid anyone actually import this module, but just in case...
  140. if __name__ == '__main__':
  141. main()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.