Return to Snippet

Revision: 29574
at August 3, 2010 05:53 by roflrazzi


Updated Code
#!/usr/bin/env python

# Author: Ben Bradley - [email protected]
# Program for making some changes to a sql file, i dunno


import sys

def ioerror(reason=None):
	'''
	Used to clean up when any of the file operations raise exceptions
	'''
	if reason:
		if reason == 'input':
			print ("Error: couldn't open input file for reading")
		elif reason == 'output':
			print ("Error: couldn't open output file for writing")
	else:
		print ("Error: cause unknown")
	sys.exit(1)


def linenumber(filelinesarray, currentelement):
	'''
	If a file has been read into an array of lines with file.readlines()
	it is useful to know which element corresponds to which line number.
	'''
	# Use str rather than int because linenumber() is always for printing
	# saves many str() calls
	return str(filelinesarray.index(currentelement)+1)


def insertafter(infilelines, targetline, conf):
	'''
	Use to insert statements, variables etc into an array of file lines
	'''
	# Pad with a newline just in case. Consider:
	# )
	# <some config>
	# ;
	# == vs ==
	# )
	# <some config>;
	if type(conf).__name__ == 'str':
		if conf.endswith('\n') == False:
			conf += '\n'

	for l in infilelines:
				
		if l.lower().startswith(targetline.lower()):
			# Comment out (or delete) print statements if ya want
			print ('Target line "%s" found @ file line #%s' %
			       (targetline,
			        (linenumber(infilelines, l))))

			print ('Inserting "%s" after file line #%s' %
			       (conf[:-1],
			        linenumber(infilelines, l)))

			offset = infilelines.index(l)+1
			if type(conf).__name__ == 'str':
				infilelines.insert(offset, conf)
			elif type(conf).__name__ == 'list':
				for el in conf:
					infilelines.insert(offset, el)
					offset += 1
			break

	return infilelines


def do3changes(infilelines, confstring):
	'''
	Applies the necessary changes as specd by dad
	'''
	# Major config here; be careful
	ALL_CHANGES = {")"              : confstring,
	               "    CONSTRAINT" : "using index local",
		       "CREATE INDEX"   :"local"}

	tmp = infilelines
	for k in ALL_CHANGES:
		# Rebinds tmp to the new value with change k applied
		tmp = insertafter(tmp, k, ALL_CHANGES[k])
	return tmp


def usage():
	print "Usage: %s infile configstring outfile" % sys.argv[0]

def help():
	print ("This is a glue script; just txt or email ben")

def version():
	print ("This program won't get developed any more, so lets say 0.67")
	
	
def main():
	'''
	Handle args, run main functionality. 
	'''
	if len(sys.argv) < 2:
		usage()
	
	elif 1 < len(sys.argv) < 4:
		if '-h' in sys.argv or '--help' in sys.argv:
			help(); usage()
		if '-v' in sys.argv or '--version' in sys.argv:
			version()

	elif len(sys.argv) == 4:
		try:
			infile = open(sys.argv[1], 'r')
			infilelines = infile.readlines()
			infile.close()
		except:
			ioerror('input')
		
		try:
			insertfile = open(sys.argv[2], 'r')
			insertlines = insertfile.readlines()
			insertfile.close()
		except:
			ioerror()
		
		try:    outfile = open(sys.argv[3], 'w')
		except: ioerror('output')
		
		edited = do3changes(infilelines, insertlines)
		
		try:
			for line in edited:
				outfile.write(line)
			outfile.flush(); outfile.close()
		except:
			ioerror('output')


# God forbid anyone actually import this module, but just in case...
if __name__ == '__main__':
	main()

Revision: 29573
at August 3, 2010 05:12 by roflrazzi


Updated Code
#!/usr/bin/env python

# Author: Ben Bradley - [email protected]
# Program for making some changes to a sql file, i dunno


import sys

def ioerror(reason=None):
	'''
	Used to clean up when any of the file operations raise exceptions
	'''
	if reason:
		if reason == 'input':
			print ("Error: couldn't open input file for reading")
		elif reason == 'output':
			print ("Error: couldn't open output file for writing")
	else:
		print ("Error: cause unknown")
	sys.exit(1)


def linenumber(filelinesarray, currentelement):
	'''
	If a file has been read into an array of lines with file.readlines()
	it is useful to know which element corresponds to which line number.
	'''
	# Use str rather than int because linenumber() is always for printing
	# saves many str() calls
	return str(filelinesarray.index(currentelement)+1)


def insertafter(infilelines, targetline, conf):
	'''
	Use to insert statements, variables etc into an array of file lines
	'''
	# Pad with a newline just in case. Consider:
	# )
	# <some config>
	# ;
	# == vs ==
	# )
	# <some config>;
	if type(conf).__name__ == 'str':
		if conf.endswith('\n') == False:
			conf += '\n'

	for l in infilelines:
				
		if l.lower()startswith(targetline.lower()):
			# Comment out (or delete) print statements if ya want
			print ('Target line "%s" found @ file line #%s' %
			       (targetline,
			        (linenumber(infilelines, l))))

			print ('Inserting "%s" after file line #%s' %
			       (conf[:-1],
			        linenumber(infilelines, l)))

			offset = infilelines.index(l)+1
			if type(conf).__name__ == 'str':
				infilelines.insert(offset, conf)
			elif type(conf).__name__ == 'list':
				for el in conf:
					infilelines.insert(offset, el)
					offset += 1
			break

	return infilelines


def do3changes(infilelines, confstring):
	'''
	Applies the necessary changes as specd by dad
	'''
	# Major config here; be careful
	ALL_CHANGES = {")"              : confstring,
	               "    CONSTRAINT" : "using index local",
		       "CREATE INDEX"   :"local"}

	tmp = infilelines
	for k in ALL_CHANGES:
		# Rebinds tmp to the new value with change k applied
		tmp = insertafter(tmp, k, ALL_CHANGES[k])
	return tmp


def usage():
	print "Usage: %s infile configstring outfile" % sys.argv[0]

def help():
	print ("This is a glue script; just txt or email ben")

def version():
	print ("This program won't get developed any more, so lets say 0.67")
	
	
def main():
	'''
	Handle args, run main functionality. 
	'''
	if len(sys.argv) < 2:
		usage()
	
	elif 1 < len(sys.argv) < 4:
		if '-h' in sys.argv or '--help' in sys.argv:
			help(); usage()
		if '-v' in sys.argv or '--version' in sys.argv:
			version()

	elif len(sys.argv) == 4:
		try:
			infile = open(sys.argv[1], 'r')
			infilelines = infile.readlines()
			infile.close()
		except:
			ioerror('input')
		
		try:
			insertfile = open(sys.argv[2], 'r')
			insertlines = insertfile.readlines()
			insertfile.close()
		except:
			ioerror()
		
		try:    outfile = open(sys.argv[3], 'w')
		except: ioerror('output')
		
		edited = do3changes(infilelines, insertlines)
		
		try:
			for line in edited:
				outfile.write(line)
			outfile.flush(); outfile.close()
		except:
			ioerror('output')


# God forbid anyone actually import this module, but just in case...
if __name__ == '__main__':
	main()

Revision: 29572
at July 31, 2010 23:06 by roflrazzi


Updated Code
#!/usr/bin/env python

# Author: Ben Bradley - [email protected]
# Program for making some changes to a sql file, i dunno


import sys

def ioerror(reason=None):
	'''
	Used to clean up when any of the file operations raise exceptions
	'''
	if reason:
		if reason == 'input':
			print ("Error: couldn't open input file for reading")
		elif reason == 'output':
			print ("Error: couldn't open output file for writing")
	else:
		print ("Error: cause unknown")
	sys.exit(1)


def linenumber(filelinesarray, currentelement):
	'''
	If a file has been read into an array of lines with file.readlines()
	it is useful to know which element corresponds to which line number.
	'''
	# Use str rather than int because linenumber() is always for printing
	# saves many str() calls
	return str(filelinesarray.index(currentelement)+1)


def insertafter(infilelines, targetline, conf):
	'''
	Use to insert statements, variables etc into an array of file lines
	'''
	# Pad with a newline just in case. Consider:
	# )
	# <some config>
	# ;
	# == vs ==
	# )
	# <some config>;
	if type(conf).__name__ == 'str':
		if conf.endswith('\n') == False:
			conf += '\n'

	for l in infilelines:
				
		if l.startswith(targetline):
			# Comment out (or delete) print statements if ya want
			print ('Target line "%s" found @ file line #%s' %
			       (targetline,
			        (linenumber(infilelines, l))))

			print ('Inserting "%s" after file line #%s' %
			       (conf[:-1],
			        linenumber(infilelines, l)))

			offset = infilelines.index(l)+1
			if type(conf).__name__ == 'str':
				infilelines.insert(offset, conf)
			elif type(conf).__name__ == 'list':
				for el in conf:
					infilelines.insert(offset, el)
					offset += 1
			break

	return infilelines


def do3changes(infilelines, confstring):
	'''
	Applies the necessary changes as specd by dad
	'''
	# Major config here; be careful
	ALL_CHANGES = {")"              : confstring,
	               "    CONSTRAINT" : "using index local",
		       "CREATE INDEX"   :"local"}

	tmp = infilelines
	for k in ALL_CHANGES:
		# Rebinds tmp to the new value with change k applied
		tmp = insertafter(tmp, k, ALL_CHANGES[k])
	return tmp


def usage():
	print "Usage: %s infile configstring outfile" % sys.argv[0]

def help():
	print ("This is a glue script; just txt or email ben")

def version():
	print ("This program won't get developed any more, so lets say 0.67")
	
	
def main():
	'''
	Handle args, run main functionality. 
	'''
	if len(sys.argv) < 2:
		usage()
	
	elif 1 < len(sys.argv) < 4:
		if '-h' in sys.argv or '--help' in sys.argv:
			help(); usage()
		if '-v' in sys.argv or '--version' in sys.argv:
			version()

	elif len(sys.argv) == 4:
		try:
			infile = open(sys.argv[1], 'r')
			infilelines = infile.readlines()
			infile.close()
		except:
			ioerror('input')
		
		try:
			insertfile = open(sys.argv[2], 'r')
			insertlines = insertfile.readlines()
			insertfile.close()
		except:
			ioerror()
		
		try:    outfile = open(sys.argv[3], 'w')
		except: ioerror('output')
		
		edited = do3changes(infilelines, insertlines)
		
		try:
			for line in edited:
				outfile.write(line)
			outfile.flush(); outfile.close()
		except:
			ioerror('output')


# God forbid anyone actually import this module, but just in case...
if __name__ == '__main__':
	main()

Revision: 29571
at July 31, 2010 23:05 by roflrazzi


Initial Code
#!/usr/bin/env python

# Author: Ben Bradley - [email protected]
# Program for making some changes to a sql file, i dunno


import sys

def ioerror(reason=None):
	'''
	Used to clean up when any of the file operations raise exceptions
	'''
	if reason:
		if reason == 'input':
			print ("Error: couldn't open input file for reading")
		elif reason == 'output':
			print ("Error: couldn't open output file for writing")
	else:
		print ("Error: cause unknown")
	sys.exit(1)


def linenumber(filelinesarray, currentelement):
	'''
	If a file has been read into an array of lines with file.readlines()
	it is useful to know which element corresponds to which line number.
	'''
	# Use str rather than int because linenumber() is always for printing
	# saves many str() calls
	return str(filelinesarray.index(currentelement)+1)


def insertafter(infilelines, targetline, conf):
	'''
	Use to insert statements, variables etc into an array of file lines
	'''
	# Pad with a newline just in case. Consider:
	# )
	# <some config>
	# ;
	# == vs ==
	# )
	# <some config>;
	if type(conf).__name__ == 'str':
		if conf.endswith('\n') == False:
			conf += '\n'

	for l in infilelines:
				
		if l.startswith(targetline):
			# Comment out (or delete) print statements if ya want
			print ('Target line "%s" found @ file line #%s' %
			       (targetline,
			        (linenumber(infilelines, l))))

			print ('Inserting "%s" after file line #%s' %
			       (conf[:-1],
			        linenumber(infilelines, l)))

			offset = infilelines.index(l)+1
			if type(conf).__name__ == 'str':
				infilelines.insert(offset, conf)
			elif type(conf).__name__ == 'list':
				for el in conf:
					infilelines.insert(offset, el)
					offset += 1
			break

	return infilelines


def do3changes(infilelines, confstring):
	'''
	Applies the necessary changes as spec'd by dad
	'''
	# Major config here; be careful
	ALL_CHANGES = {")"              : confstring,
	               "    CONSTRAINT" : "using index local",
		       "CREATE INDEX"   :"local"}

	tmp = infilelines
	for k in ALL_CHANGES:
		# Rebinds tmp to the new value with change k applied
		tmp = insertafter(tmp, k, ALL_CHANGES[k])
	return tmp


def usage():
	print "Usage: %s infile configstring outfile" % sys.argv[0]

def help():
	print ("This is a glue script; just txt or email ben")

def version():
	print ("This program won't get developed any more, so lets say 0.67")
	
	
def main():
	'''
	Handle args, run main functionality. 
	'''
	if len(sys.argv) < 2:
		usage()
	
	elif 1 < len(sys.argv) < 4:
		if '-h' in sys.argv or '--help' in sys.argv:
			help(); usage()
		if '-v' in sys.argv or '--version' in sys.argv:
			version()

	elif len(sys.argv) == 4:
		try:
			infile = open(sys.argv[1], 'r')
			infilelines = infile.readlines()
			infile.close()
		except:
			ioerror('input')
		
		try:
			insertfile = open(sys.argv[2], 'r')
			insertlines = insertfile.readlines()
			insertfile.close()
		except:
			ioerror()
		
		try:    outfile = open(sys.argv[3], 'w')
		except: ioerror('output')
		
		edited = do3changes(infilelines, insertlines)
		
		try:
			for line in edited:
				outfile.write(line)
			outfile.flush(); outfile.close()
		except:
			ioerror('output')


# God forbid anyone actually import this module, but just in case...
if __name__ == '__main__':
	main()

Initial URL


Initial Description


Initial Title
SQL file glue script for dad

Initial Tags


Initial Language
Python