pleiser
Senior Member
I decided to try to write a preprocessor for #include files for use with AXEpad on MacOS and Linux (presumably Windows too, if for some reason you're not using PE6), and found it was surprisingly easy to implement in python! This is an early revision right now, it doesn't currently support absolute file paths, but works great with relative paths for files in the same folder!
It works by opening the file passed as an argument, then copying it one line at a time into the output file, checking each line for #include statements, when it finds one it recursively opens that file and repeats the same process.
Any feedback and suggestions are welcome, hope you guys find it useful
here's the source code, save it as picaxeinclude.py in the same folder as the picaxe files you want to include to/from
use it in a terminal like the following example:
Hope this is useful! I'm short on time right now, but will be continuing to improve it, and welcome suggestions and feedback.
It works by opening the file passed as an argument, then copying it one line at a time into the output file, checking each line for #include statements, when it finds one it recursively opens that file and repeats the same process.
Any feedback and suggestions are welcome, hope you guys find it useful
here's the source code, save it as picaxeinclude.py in the same folder as the picaxe files you want to include to/from
Code:
#!/usr/bin/python
#PICAXE #include preprocessor
#Created by Patrick Leiser
import sys, getopt, os
inputfilename = ''
outputfilename = 'compiled.bas'
def main(argv):
global inputfilename
global outputfilename
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfilename = arg
elif opt in ("-o", "--ofile"):
outputfilename = arg
print 'Input file is ', inputfilename
print 'Output file is ', outputfilename
path=os.path.dirname(os.path.abspath(__file__))
with open (path+'/'+outputfilename, 'w') as output_file:
output_file.write("'-----PREPROCESSED BY picaxeinclude.py-----\n\n")
output_file.write("'----SAVING AS "+outputfilename+" ----\n")
output_file.write("'---BEGIN "+inputfilename+" ---\n")
progparse(inputfilename)
def progparse(curfilename):
print curfilename
path=os.path.dirname(os.path.abspath(__file__))
with open(path+'/'+curfilename) as input_file:
for i, line in enumerate(input_file):
workingline=line.lstrip()
if workingline.lower().startswith("#include"):
print"Found an include!"#TODO: Include logic
workingline=workingline[9:].lstrip().split()[0]
workingline=workingline.strip('"')
print(workingline)
with open (path+'/'+outputfilename, 'a') as output_file:
output_file.write("'---BEGIN "+workingline+" ---\n")
progparse(workingline)
else:
with open (path+'/'+outputfilename, 'a') as output_file:
output_file.write(line)
print line,
print "{0} line(s) printed".format(i+1)
with open (path+'/'+outputfilename, 'a') as output_file:
output_file.write("\n'---END "+curfilename+"---\n")
if __name__ == "__main__":
main(sys.argv[1:])
Code:
/Path/to/program/directory/picaxeinclude.py -i infile.bas -o compiledoutput.bas