def static(urlFormat, defaultLocale, staticFiles, localeFiles, rootFiles, workingDir, manifest, manifestAll): global importedLibs locales = {} output.action(_("buildRootFiles")) for root, subdirs, files in os.walk(rootFiles): importedLibs = [] neededPath = os.path.join("build", os.path.relpath(root, manifest["rootFiles"])) if not os.path.exists(neededPath): os.makedirs(neededPath) for i in range(0, len(files)): infile = open(os.path.join(root, files[i]), "rb") infileData = infile.read() infile.close() outfile = open(os.path.join(neededPath, files[i]), "wb") if files[i].endswith(".js"): outfile.write( js( infileData.decode("utf-8"), os.getcwd().replace("\\", "/") + "/" + root.replace("\\", "/")).encode("utf-8")) elif files[i].endswith(".html"): outfile.write( html( "<script>var _manifest = " + json.dumps(manifestAll).replace( "</script>", "<\/script>") + ";</script>" + infileData.decode("utf-8"), os.getcwd().replace("\\", "/") + "/" + root.replace("\\", "/")).encode( "utf-8", root.replace("\\", "/"))) elif files[i].endswith(".css"): outfile.write(css(infileData.decode("utf-8")).encode("utf-8")) else: outfile.write(infileData) outfile.close() for root, subdirs, files in os.walk(localeFiles): for i in range(0, len(files)): if not (files[i] in locales.keys()): locales[files[i].split(".")[0]] = os.path.join(root, files[i]) else: output.warning( _("doubleLanguageFile", [root.replace("\\", "/") + files[i]])) supportedLocales = list(locales.keys()) for i in range(0, len(supportedLocales)): output.action(_("buildLocale", [supportedLocales[i]])) openLocaleFile = open(locales[supportedLocales[i]], "r") openLocaleFileData = json.load(openLocaleFile) openLocaleFile.close() openDefaultLocaleFile = open(locales[manifest["defaultLocale"]], "r") openDefaultLocaleFileData = json.load(openDefaultLocaleFile) openDefaultLocaleFile.close() for root, subdirs, files in os.walk(staticFiles): importedLibs = [] splittablePath = ("build/" + manifest["urlFormat"].replace( "<locale>", supportedLocales[i]).replace( "<path>", os.path.relpath( root, manifest["staticFiles"]))).split("/") neededPath = os.path.join(*splittablePath) if not os.path.exists(neededPath): os.makedirs(neededPath) for j in range(0, len(files)): infile = open(os.path.join(root, files[j]), "rb") infileData = infile.read() infile.close() outfile = open(os.path.join(neededPath, files[j]), "wb") if files[j].endswith(".js"): outfile.write( js( infileData.decode("utf-8"), os.getcwd().replace("\\", "/") + "/" + root.replace("\\", "/")).encode("utf-8")) elif files[j].endswith(".html"): outfile.write( translate( html( "<script>var _manifest = " + json.dumps(manifestAll).replace( "</script>", "<\/script>") + ";</script>" + "<script>var _locale = " + json.dumps(openLocaleFileData) + ";</script>" + infileData.decode("utf-8"), os.getcwd().replace("\\", "/") + "/" + root.replace("\\", "/")), openLocaleFileData, openDefaultLocaleFileData, supportedLocales[i]).encode("utf-8")) elif files[j].endswith(".css"): outfile.write( css(infileData.decode("utf-8")).encode("utf-8")) else: outfile.write(infileData) outfile.close()
def js(content, location): initialContentLines = content.split("\n") finalContentLines = [] firstLocation = location finalContentLines.append("var _assets = {};") while len(initialContentLines) > 0: location = firstLocation initialContentLines[0] = initialContentLines[0].strip() if initialContentLines[0].startswith("// @import"): try: library = initialContentLines[0][11:] libraryName = "" if not ((location + "/" + library) in importedLibs): importedLibs.append(location + "/" + library) if location.startswith("http://") or location.startswith( "https://"): if not (library.startswith("http://") or library.startswith("https://")): library = location + "/" + library libraryName = library.split("/")[-1].split(".")[0] finalURL = urllib.parse.urljoin( library, ".") + library.split("/")[-1] if not finalURL.endswith(".js"): finalURL += ".js" output.action( _("importLibrary", [libraryName, finalURL])) location = urllib.parse.urljoin( "/".join( _replaceExceptFirst(library, "//", "/").split("/")), ".").rstrip("/") try: siteData = cache.get(finalURL) if siteData != False: finalContentLines.append( js(siteData.decode("utf-8"), location)) else: output.warning( _("unknownImport", [initialContentLines[0][3:]])) except: output.warning( _("unknownImport", [initialContentLines[0][3:]])) elif library.startswith("http://") or library.startswith( "https://"): libraryName = library.split("/")[-1].split(".")[0] output.action( _("importLibrary", [libraryName, library])) if not (location.startswith("http://") or location.startswith("https://")): location = urllib.parse.urljoin( "/".join( _replaceExceptFirst(library, "//", "/").split("/")), ".").rstrip("/") if not library.endswith(".js"): library += ".js" try: siteData = cache.get(library) if siteData != False: finalContentLines.append( js(siteData.decode("utf-8"), location)) else: output.warning( _("unknownImport", [initialContentLines[0][3:]])) except: output.warning( _("unknownImport", [initialContentLines[0][3:]])) else: oldLibrary = library library = location + "/" + library libraryName = library.split("/")[-1] if "/" in oldLibrary: location += "/" + "/".join( oldLibrary.split("/")[:-1]) output.action( _("importLibrary", [libraryName, library + ".js"])) try: libPath = library.split("/") libPath[-1] += ".js" file = open( os.path.join(*[os.sep, *libPath]).replace( ":", ":\\"), "r") fileData = file.read() file.close() finalContentLines.append(js(fileData, location)) except: output.warning( _("unknownImport", [initialContentLines[0][3:]])) else: output.action(_("circularImport", [library])) except: output.warning(_("illegalImport", [initialContentLines[0][3:]])) elif initialContentLines[0].startswith("// @import!"): try: library = initialContentLines[0][12:] libraryName = "" if location.startswith("http://") or location.startswith( "https://"): if not (library.startswith("http://") or library.startswith("https://")): library = location + "/" + library libraryName = library.split("/")[-1].split(".")[0] finalURL = urllib.parse.urljoin( library, ".") + library.split("/")[-1] if not finalURL.endswith(".js"): finalURL += ".js" output.action(_("importLibrary", [libraryName, finalURL])) location = urllib.parse.urljoin( "/".join( _replaceExceptFirst(library, "//", "/").split("/")), ".").rstrip("/") try: siteData = cache.get(finalURL) if siteData != False: finalContentLines.append( js(siteData.decode("utf-8"), location)) else: output.warning( _("unknownImport", [initialContentLines[0][3:]])) except: output.warning( _("unknownImport", [initialContentLines[0][3:]])) elif library.startswith("http://") or library.startswith( "https://"): libraryName = library.split("/")[-1].split(".")[0] output.action(_("importLibrary", [libraryName, library])) if not (location.startswith("http://") or location.startswith("https://")): location = urllib.parse.urljoin( "/".join( _replaceExceptFirst(library, "//", "/").split("/")), ".").rstrip("/") if not library.endswith(".js"): library += ".js" try: siteData = cache.get(library) if siteData != False: finalContentLines.append( js(siteData.decode("utf-8"), location)) else: output.warning( _("unknownImport", [initialContentLines[0][3:]])) except: output.warning( _("unknownImport", [initialContentLines[0][3:]])) else: oldLibrary = library library = location + "/" + library libraryName = library.split("/")[-1] if "/" in oldLibrary: location += "/" + "/".join(oldLibrary.split("/")[:-1]) output.action( _("importLibrary", [libraryName, library + ".js"])) try: libPath = library.split("/") libPath[-1] += ".js" file = open( os.path.join(*["/", *libPath]).replace(":", ":\\"), "r") fileData = file.read() file.close() finalContentLines.append(js(fileData, location)) except: output.warning( _("unknownImport", [initialContentLines[0][3:]])) except: output.warning(_("illegalImport", [initialContentLines[0][3:]])) elif initialContentLines[0].startswith("// @asset"): try: asset = initialContentLines[0][10:] assetName = "" initPath = asset.split("/")[:-1] if os.name == "nt": fullPath = os.path.join(location.replace("/", "\\"), asset) else: fullPath = urllib.parse.urljoin(location + "/", ".") + asset if not (fullPath in importedAssets): importedAssets.append(fullPath) if location.startswith("http://") or location.startswith( "https://"): originalAsset = asset if not (asset.startswith("http://") or asset.startswith("https://")): asset = location + "/" + asset assetName = asset.split("/")[-1].split(".")[0] output.action(_("importAsset", [assetName, asset])) location = urllib.parse.urljoin( "/".join( _replaceExceptFirst(library, "//", "/").split("/")), ".").rstrip("/") try: siteData = cache.get(asset) if siteData != False: finalContentLines.append( "_assets[\"" + originalAsset.replace("\"", "-") + "\"] = \"" + base64.b64encode( siteData).decode("utf-8") + "\";") else: output.warning( _("unknownAsset", [initialContentLines[0][3:]])) except: output.warning( _("unknownAsset", [initialContentLines[0][3:]])) elif asset.startswith("http://") or asset.startswith( "https://"): assetName = asset.split("/")[-1] output.action(_("importAsset", [assetName, asset])) try: siteData = cache.get(asset) if siteData != False: finalContentLines.append( "_assets[\"" + asset.replace("\"", "-") + "\"] = \"" + base64.b64encode( siteData).decode("utf-8") + "\";") else: output.warning( _("unknownAsset", [initialContentLines[0][3:]])) except: output.warning( _("unknownAsset", [initialContentLines[0][3:]])) else: oldAsset = asset assetName = asset.split("/")[-1] if "/" in asset: location += "/" + "/".join(asset.split("/")[:-1]) output.action(_("importAsset", [assetName, asset])) try: asset = asset.split("/")[-1] file = open(fullPath, "rb") fileData = file.read() file.close() finalContentLines.append( "_assets[\"" + assetName.replace("\"", "-") + "\"] = \"" + base64.b64encode(fileData).decode("utf-8") + "\";") except: output.warning( _("unknownAsset", [initialContentLines[0][3:]])) except: output.warning(_("illegalAsset", [initialContentLines[0][3:]])) else: finalContentLines.append(initialContentLines[0]) initialContentLines.pop(0) return jsmin.jsmin("\n".join(finalContentLines))
def html(content, location): minified = htmlmin.minify(content) imports = re.findall(r"\{\{ @import (.*?) \}\}", minified) for i in range(0, len(imports)): importStatement = "{{ @import " + imports[i] + " }}" library = imports[i] libraryName = "" if location.startswith("http://") or location.startswith("https://"): library = location + "/" + library libraryName = library.split("/")[-1].split(".")[0] finalURL = urllib.parse.urljoin(library, ".") + library.split("/")[-1] if not finalURL.endswith(".html"): finalURL += ".html" output.action(_("importLibrary", [libraryName, finalURL])) location = urllib.parse.urljoin( "/".join(_replaceExceptFirst(library, "//", "/").split("/")), ".").rstrip("/") try: siteData = cache.get(finalURL) if siteData != False: finalContentLines.append( js(siteData.decode("utf-8"), location)) else: output.warning( _("unknownImport", [initialContentLines[0][3:]])) except: output.warning(_("unknownImport", [initialContentLines[0][3:]])) elif library.startswith("http://") or library.startswith("https://"): libraryName = library.split("/")[-1].split(".")[0] output.action(_("importLibrary", [libraryName, library])) if not (location.startswith("http://") or location.startswith("https://")): location = urllib.parse.urljoin( "/".join( _replaceExceptFirst(library, "//", "/").split("/")), ".").rstrip("/") if not library.endswith(".html"): library += ".html" try: siteData = cache.get(library) if siteData != False: minified = minified.replace( importStatement, html(siteData.decode("utf-8"), location)) else: output.warning( _("unknownImport", ["@import " + imports[i]])) except: output.warning(_("unknownImport", [imports[i]])) else: library = location + "/" + library libraryName = library.split("/")[-1] output.action(_("importLibrary", [libraryName, library + ".html"])) try: libPath = library.split("/") libPath[-1] += ".html" file = open( os.path.join(*["/", *libPath]).replace(":", ":\\"), "r") fileData = file.read() file.close() minified = minified.replace(importStatement, html(fileData, location)) except: output.warning(_("unknownImport", [imports[i]])) return minified
def scan(content): contentLines = content.split("\n") generatedDocs = {} targetName = "" targetParams = [] targetReturn = {} targetShortDescription = "" targetLongDescription = "" while len(contentLines) > 0: contentLines[0] = contentLines[0].strip() contentBody = contentLines[0].split(" ") if len(contentBody) > 0: if contentBody[0] == "@name": if len(contentBody) > 1: if targetName != "": generatedDocs[targetName] = { "params": targetParams, "return": targetReturn, "shortDescription": targetShortDescription, "longDescription": targetLongDescription } targetName = "" targetParams = [] targetReturn = {} targetShortDescription = "" targetLongDescription = "" targetName = " ".join(contentBody[1:]) else: output.warning(_("genDocsIllegal", [contentLines[0]])) elif contentBody[0] == "@param": if len(contentBody) >= 4: if targetName != "": targetParams.append({ "name": contentBody[1], "type": contentBody[2], "description": " ".join(contentBody[3:]) }) else: output.warning( _("genDocsUnspecified", [contentLines[0]])) else: output.warning(_("genDocsIllegal", [contentLines[0]])) elif contentBody[0] == "@return": if len(contentBody) >= 3: if targetName != "": targetReturn = { "type": contentBody[1], "description": " ".join(contentBody[2:]) } else: output.warning( _("genDocsUnspecified", [contentLines[0]])) else: output.warning(_("genDocsIllegal", [contentLines[0]])) elif contentBody[0] == "@shortDescription": if len(contentBody) > 1: if targetName != "": targetShortDescription = " ".join(contentBody[1:]) else: output.warning( _("genDocsUnspecified", [contentLines[0]])) else: output.warning(_("genDocsIllegal", [contentLines[0]])) elif contentBody[0] == "@longDescription": if len(contentBody) > 1: if targetName != "": if targetLongDescription == "": targetLongDescription = " ".join(contentBody[1:]) else: targetLongDescription += "\n" + " ".join( contentBody[1:]) else: output.warning( _("genDocsUnspecified", [contentLines[0]])) else: output.warning(_("genDocsIllegal", [contentLines[0]])) contentLines.pop(0) if targetName != "": generatedDocs[targetName] = { "params": targetParams, "return": targetReturn, "shortDescription": targetShortDescription, "longDescription": targetLongDescription } return generatedDocs
def compile(infile, outfile, size=6144, definitions={}): file = open(infile, "r") code = file.read() file.close() sequence = code.split("\n") assembled = [0] * size position = 0 print("") for i in range(0, len(sequence)): currentLine = sequence[i].strip() currentLineSplit = currentLine.split(";")[0].replace( " ", "").replace(" ", "").split(" ") output.action(currentLine) for j in range(0, len(currentLineSplit)): if len(currentLineSplit[j]) > 1 and ( currentLineSplit[j][0] == "." or (currentLineSplit[j][0] == "@" and currentLineSplit[j][1] == ".")): # Data from definition if len(currentLineSplit[j] [1]) > 0 and currentLineSplit[j][0] == "@": if currentLineSplit[j][2:] in definitions: currentLineSplit[j] = "@" + definitions[ currentLineSplit[j][2:]] else: output.warning(" " + _("definitionDoesNotExist", [i + 1])) elif len(currentLineSplit[j][1]) > 0: if currentLineSplit[j][1:] in definitions: currentLineSplit[j] = definitions[currentLineSplit[j] [1:]] else: output.warning(" " + _("definitionDoesNotExist", [i + 1])) if currentLineSplit[0] == "#define": print(" " + _("definition")) original = currentLineSplit[1] replacement = " ".join(currentLineSplit[2:]) print(" " + _("definitionOriginal", [original])) print(" " + _("definitionReplacement", [original])) definitions[original] = replacement elif currentLineSplit[0] == "#data": print(" " + _("data")) startingAddress = int(currentLineSplit[1], 16) print(" " + _("dataAddress", [hex(startingAddress)[2:].zfill(2)])) if currentLineSplit[2][0] == "\"": # string print(" " + _("dataTypeString")) allocation = "" inString = False for j in range(0, len(currentLine)): if currentLine[j] == "\"": if inString: break else: inString = True elif inString: allocation += currentLine[j] allocation = allocation.replace("\\n", "\n") for j in range(0, len(allocation)): assembled[startingAddress + j] = ord(allocation[j]) elif len(currentLineSplit[2]) == 2: # byte print(" " + _("dataTypeByte")) assembled[startingAddress] = int(currentLineSplit[2], 16) elif len(currentLineSplit[2]) == 4: # int/uint print(" " + _("dataTypeIntUint")) assembled[startingAddress] = int(currentLineSplit[2][0:2], 16) assembled[startingAddress + 1] = int(currentLineSplit[2][2:4], 16) else: output.error(_("invalidDataStructure", [i + 1])) return elif currentLineSplit[0] == "#at": position = int(currentLineSplit[1], 16) elif currentLine != "" and currentLine[0] != ";": print(" " + _("instruction")) instructions = { "halt": 0x00, "allocate": 0x01, "copy": 0x02, "jump": 0x03, "write": 0x04, "read": 0x05, "add": 0x06, "sub": 0x07, "mul": 0x08, "div": 0x09, "mod": 0x0A, "equ": 0x0B, "neq": 0x0C, "ltn": 0x0D, "gtn": 0x0E, "func": 0x0F, "ret": 0x10, "sreg": 0x11, "sgp": 0x12, "cgp": 0x13, "dwrite": 0x14, "dread": 0x15, "bsl": 0x16, "bsr": 0x17, "and": 0x18, "or": 0x19, "xor": 0x1A, "onec": 0x1B, "outbin": 0xA0, "outdec": 0xA1, "outhex": 0xA2, "outasc": 0xA3, "in": 0xA4, "len": 0xA5, "strnum": 0xA6, "numstr": 0xA7, "dnumstr": 0xA8, "fopen": 0xB0, "fclose": 0xB1, "fwrite": 0xB2, "fwriter": 0xB3, "fappend": 0xB4, "fread": 0xB5, "freadr": 0xB6, "fsize": 0xB7, "fdel": 0xB8, "fmd": 0xB9, "frd": 0xBA, "fstart": 0xBB, "fnext": 0xBC, "fex": 0xBD, "fdir": 0xBE, "gpos": 0xC0, "gsize": 0xC1, "ginit": 0xC2, "gfill": 0xC3, "gpixel": 0xC4, "gline": 0xC5, "gfline": 0xC6, "grect": 0xC7, "gcircle": 0xC8, "gbin": 0xC9, "gdec": 0xCA, "ghex": 0xCB, "gasc": 0xCC, "gbmp": 0xCD, "gtouch": 0xCE, "sleep": 0xD0, "gyear": 0xD1, "gmonth": 0xD2, "gdate": 0xD3, "gday": 0xD4, "ghour": 0xD5, "gmin": 0xD6, "gsec": 0xD7, "sdate": 0xD8, "stime": 0xD9 } if currentLineSplit[0] in instructions: instruction = instructions[currentLineSplit[0]] params = [] print(" " + _("instructionInstruction", [currentLineSplit[0], hex(instruction)[2:].zfill(2)])) for i in range(0, len(currentLineSplit) - 1): params.append(currentLineSplit[i + 1]) print(" " + _("instructionParameters", [" ".join(str(k) for k in params)])) for i in range(0, len(params)): if params[i].strip() != "": if params[i][0] == "@": assembled[position] = 0xFF assembled[position + 1] = i if len(params[i]) == 3: assembled[position + 2] = 0 assembled[position + 3] = int( params[i][1:], 16) else: assembled[position + 2] = int( params[i][1:3], 16) assembled[position + 3] = int( params[i][3:5], 16) elif params[i][0] == "$": registerConversions = { "PC": 0, "RM": 1, "AR": 2, "ER": 3, "FR": 4, "PM1": 5, "PM2": 6, "PM3": 7, "GX": 8, "GY": 9, "GW": 10, "GH": 11, "GP1": 12, "GP2": 13, "GP3": 14, "GP4": 15, "GP5": 16, "GP6": 17, "GP7": 18, "GP8": 19 } assembled[position] = 0xFE assembled[position + 1] = i assembled[position + 2] = 0 assembled[position + 3] = registerConversions[params[i][1:]] else: assembled[position] = 0xFD assembled[position + 1] = i if len(params[i].strip()) == 2: assembled[position + 2] = 0 assembled[position + 3] = int( params[i].strip(), 16) else: assembled[position + 2] = int( params[i].strip()[0:2], 16) assembled[position + 3] = int( params[i].strip()[2:4], 16) position += 4 assembled[position] = instruction position += 1 else: output.error(_("invalidInstruction", [str(i + 1)])) sys.exit(1) while assembled[len(assembled) - 1] == 0: assembled.pop() file = open(outfile, "wb") file.write(bytearray(assembled)) file.close() return { "definitions": definitions, "stats": [len(assembled), size, (len(assembled) / size) * 100] }
VERSION = "V0.1.0" args = sys.argv requiredInstalls = False if (len(args) > 1 and (args[1] == "--hide" or args[1] == "-h")): args.pop(1) elif storage.read("hide") != "true": print(_("welcome", [colours.get("lred"), colours.get("white"), VERSION])) print(_("copyright")) print("") if storage.read("locale") == None: output.warning(_("setLocaleWarning", [lang.getLocale()])) print("") # Install libraries for when they don't exist def installLib(name, description): global requiredInstalls requiredInstalls = True output.action(_("installRequiredLibs", [description])) returns = subprocess.call([sys.executable, "-m", "pip", "install", name], stdout = subprocess.PIPE, stderr = subprocess.PIPE) if returns != 0: output.error(_("installRequiredLibsError"))