import sys import readwrite fileIncludes= [] Objects = {} """ this function takes pair_a and pair_b and a string it looks for pair_a and pair_b. If there are more pair_b's than pair_a's any time in the program, it stops. """ def get_pair(pair_type_a, pair_type_b, string): pair_list = [] for i in range(0, len(string)): if string[i] == pair_type_a or string[i] == pair_type_b: pair_list.append(string[i]) if len([1 for x in pair_list if x == pair_type_a]) < len([1 for x in pair_list if x == pair_type_b]): return i return False class convertNeoMatrix(object): def __init__(self, name, height, width): self.height = height self.width = width self.name = name #def findVar(Var, File): #placeholder def convertObject(line_parse, Object, String): sArray = line_parse.split() digits = "0123456789" for i in range(0, len(sArray)): print(sArray[i]) if Object in sArray[i]: sArray[i].replace(Object, String) return " ".join(sArray) def takeOutIncludes(inFile, outFile, includes): try: fileObject = readwrite.read_file(inFile) except: print("File Not Valid") input("press enter to exit") return 0 fileArray = fileObject.split("\n") included = False for i in range(0, len(fileArray)): if fileArray[i][0:8] == "#include": fileArray[i] = fileArray[i].strip(" ") fileArray[i] = fileArray[i].strip("<") fileArray[i] = fileArray[i].strip(">") fileArray[i] = fileArray[i].strip("\"") fileArray[i] = fileArray[i].lower() if "Adafruit_NeoPixel".lower() in fileArray[i]: fileArray[i] = "#include <converted_neopixel.h>" elif "Adafruit_NeoMatrix".lower() in fileArray[i]: fileArray[i] = "#include <convert_neomatrix.h>" elif "Adafruit_GFX".lower() in fileArray[i]: fileArray[i] = "#include <convert_gfx.h>" elif "cstdint" in fileArray[i]: included = True elif fileArray[i] not in includes: fileArray[i] = "" if (included != True): fileArray.insert(0, "#include <cstdint>") #This is need for int16_t, int8_t, etc. readwrite.write_file("\n".join(fileArray), outFile) def removeDefines(String): sLower = String.lower() if "avr".lower() in sLower or "#endif" in sLower or "(_AVR_ATtiny85__)".lower() in sLower or "f_cpu" in sLower or "PSTR".lower() in sLower: return f"//taken out: {String}" else: return String def checkComment(String): rThis = 0 if String[0:2] == "//": return 1 if "/*" in String: rThis = 2 if "*/" in String: rThis = 3 return rThis def misc(inFile, outFile): fileObject = readwrite.read_file(inFile) fileArray = fileObject.split("\n") many = False for i in range(0, len(fileArray)): fileArray[i] = convertObject(fileArray[i], "Adafruit_Noepixel", "converted_neopixel") if checkComment(fileArray[i]) == 2: many = True if many != True and checkComment(fileArray[i]) != 1: fileArray[i] = removeDefines(fileArray[i]) readwrite.write_file("\n".join(fileArray), outFile) def convertAll(inFile, outFile, includes): takeOutIncludes(inFile, outFile, includes) misc(inFile, outFile) if __name__ == "__main__": arguments = sys.argv #python convert.py testprogram output.cpp if len(arguments) > 3: try: includes = readwrite.read_file(arguments[3]).split("\n") except: print("not valid file") input("press enter to exit") convertAll(arguments[1], arguments[2], includes) else: print("Invalid argument")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question