def _createPackage(templateDir, packageName): # Create new package output directory pkgRoot = os.path.join(config.bldDir(), packageName) os.mkdir(pkgRoot) formatSubs = {ixtemplate.tag: packageName} # Copy files over templateDirLen = len(templateDir) + 1 for root, dirs, files in os.walk(templateDir): # determine the new root name and make the appropriate name substitution newRoot, _ = ixre.subf(os.path.join(pkgRoot, root[templateDirLen:]), formatSubs) # Rename new directories that are created for d in dirs: newDir, _ = ixre.subf(d, formatSubs) os.mkdir(os.path.join(newRoot, newDir)) # Any files that are copied over have a format substitution for f in files: # Read the old file text = ixfs.read(os.path.join(root, f)) # Make the substitutions in new text text, _ = ixre.subf(text, formatSubs) # Get the new filename filename, _ = ixre.subf(f, formatSubs) # Save in a new file ixfs.write(os.path.join(newRoot, filename), text) return pkgRoot
def _modifyLocalService(serviceName, methodName, constMethodName, dataObj, fileDir): filepath = fs.findr(fileDir, f"{serviceName}LocalService.java") if filepath == None: print(f"Unable to find: {serviceName}LocalService.java") return False fileTxt = fs.read(filepath) match = _doMethodRE.search(fileTxt) doMethodBody = fileTxt[match.start(1):match.end(1)] lastIf = doMethodBody.rfind("if") endElseBlock = match.start(1) + javaClass.getCorresponding( doMethodBody[lastIf:], "{", "}") + lastIf + 1 classClose = fileTxt.rfind("}") doMethod = javaClass.Method("private", "ServiceResponseManifest", "doMethod_" + methodName) doMethod.addArgument("ServiceRequestManifest", "srvcReqMan") doMethod.body = "// " + config.TODO_Tag + " to be implemented.\n" newFile = open(filepath, "w+") newFile.write( fileTxt[:endElseBlock] + f""" if (srvcReqMan.getMethodName().equals({serviceName}Const.{_srvcMthdPrefix}{constMethodName})) {{ return doMethod_{methodName}(srvcReqMan); }}""" + fileTxt[endElseBlock:classClose] + "\n" + srvcMethod.createDoMethod(serviceName, methodName, dataObj) + "\n" + srvcMethod.createIsValidMethod(serviceName, methodName, dataObj) + "\n" + srvcMethod.createImplMethod(serviceName, methodName, dataObj) + "\n" + fileTxt[classClose:]) newFile.close() return True
def python(directory): file_pttrn = re.compile(r"\.py") method_pttrn = re.compile(r"(\s*)def(\s+)(.+)\((.+)\):\n") # class_pttrn = re.compile(r"(\s*)class(\s+)(.+)\((.+)\):\n") whitespace = re.compile(r"\s*") for root, _, files in os.walk(directory): for f in files: # Skip all non python files if file_pttrn.search(f) is None: continue filepath = os.path.join(root, f) text = ixfs.read(filepath) for m in method_pttrn.finditer(text): docNdxSt = text.find('"""', m.endpos) # Skip if it found a docstring if whitespace.match(text[m.endpos:docNdxSt]) is not None: continue print(f"Unable to find docstring for method({m.group()})" f"at line ({ixregex.get_line_number(text, m.start())}")
def append_to_line(fp, line_no, append_text): file_text = ixfs.read(fp) for ndx, match in enumerate(_line_pttrn.finditer(file_text, 0)): if ndx == line_no - 1: ixfs.write(fp, "".join([ file_text[:match.start()], append_text, file_text[match.start():]])) return print(f"Unable to find line number {line_no} in ({fp}")
def _modifyProviderFile(serviceName, methodName, constMethodName, isSync, dataObj, fileDir): filepath = fs.findr(fileDir, f"{serviceName}Provider.java") if filepath == None: print(f"Unable to find: {serviceName}Provider.java") return False fileTxt = fs.read(filepath) classClose = fileTxt.rfind("}") newFile = open(filepath, "w+") newFile.write( fileTxt[:classClose] + "\n" + srvcMethod.createProviderMethod( serviceName, methodName, constMethodName, isSync, dataObj) + fileTxt[classClose:]) newFile.close() return True
def _modifyConstFile(serviceName, methodName, constMethodName, fileDir): filepath = fs.findr(fileDir, f"{serviceName}Const.java") if filepath == None: print(f"Unable to find: {serviceName}Const.java") return False fileTxt = fs.read(filepath) lastConstEndLine = fileTxt.rfind(_srvcMthdPrefix) lastConstEndLine = fileTxt[lastConstEndLine:].find( ";") + lastConstEndLine + 1 newFile = open(filepath, "w+") newFile.write(fileTxt[:lastConstEndLine] + f""" public static final String {_srvcMthdPrefix}{constMethodName} = "{methodName}";""" + fileTxt[lastConstEndLine:]) newFile.close() return True
def create_package(template_dir): Name = ixre.to_pascal(request.args["name"]) name = ixre.to_camel(request.args["name"]) fmt_subs = ixre.to_template_dict(tag, name) fp = BytesIO() zf = ZipFile(fp, mode="w", compresslevel=ZIP_DEFLATED) template_dir_len = len(template_dir) + 1 for root, _, files in os.walk(template_dir): new_root = ixre.subkv(fmt_subs, root[template_dir_len:]) for f in files: text = ixre.subkv(fmt_subs, ixfs.read(os.path.join(root, f))) new_filename = ixre.subkv(fmt_subs, f) zf.writestr(os.path.join(new_root, new_filename), text) zf.close() fp.seek(0) return send_file(fp, as_attachment=True, attachment_filename=f"{Name}.zip")
def subr(directory, file_regex, search_regex, replace_regex): """Recursively search through a directory and search and replace in each file. Params: directory: str Directory to start the search search_regex: str pattern to match in the file replace_regex: str pattern to replace in the file file_regex: str regex pattern to match files """ # Compile patterns search_pttrn = re.compile(search_regex) file_pttrn = re.compile(file_regex) # Recursively search through directory total_subs = 0 for root, _, files in os.walk(directory): for f in files: # If a pattern is supplied and it isn't matched, skip the file if file_pttrn is not None and file_pttrn.search(f) is None: continue # Read the file filepath = os.path.join(root, f) text = fs.read(filepath) # Make the appropriate substitutions text, n = search_pttrn.subn(replace_regex, text) if n > 0: print(f"({n}) substitutions in file({filepath})") total_subs += n fs.write(filepath, text) print(f"Total substitutions: {total_subs}")
def apply_template(template, src_fp, dst_dir, fmt_subs=None): # Substitution map if fmt_subs is None: fmt_subs = {tag: template} else: fmt_subs[tag] = template # read the file text = ixfs.read(src_fp) # make the substitutions text, _ = ixre.subf(text, fmt_subs) # Get the templated name dst_fp = os.path.join(dst_dir, ixre.subf(os.path.basename(src_fp), fmt_subs)[0]) # write out the destination file ixfs.write(dst_fp, text) return dst_fp
def multiTemplate(srcFilename, dstFilename, *templates): """Create multiple files from one template""" # Creat substitution map fmt_subs = {tag: ""} # Get outfile name filepath = os.path.join(config.bldDir(), dstFilename) ixfs.write(filepath, "") # clear the file for template in templates: # Build sub map fmt_subs[tag] = template # read the file text = ixfs.read(ixfs.findr(config.srcDir(), srcFilename)) # make the substitutions text, _ = ixre.subf(text, fmt_subs) # append to file ixfs.append(filepath, text) return filepath
def generateTemplate(src_fp, dst_dir, key, value=None): # read the file text = ixfs.read(src_fp) # check if there is a different substitution value if value is None: value_camel = tag value_pascal = Tag value_const = TAG else: value_camel = ixre.toCamel(value) value_pascal = ixre.toPascal(value) value_const = ixre.toConst(value) # Make the substitutions text = ixre.to_template(ixre.toCamel(key), value_camel, text) text = ixre.to_template(ixre.toPascal(key), value_pascal, text) text = ixre.to_template(ixre.toConst(key), value_const, text) filename = ixre.to_template(key, Tag, os.path.basename(src_fp)) # write out the file ixfs.write(os.path.join(dst_dir, filename), text)
def findr(directory, file_regex, search_regex=None, **kwargs): """Search recursively in a directory for a file that matches file_regex. If a search regex is supplied then it will search the file for that pattern Parameters: directory: str Root directory to search in file_regex: str Regular expression to match file names search_regex: str Regular expression to match in the file Optional Parameters: filter: func():bool additional filter logic, return True on match print: str print in a different format {match, line} """ file_pttrn = re.compile(file_regex) filterMatch = kwargs['filter'] if 'filter' in kwargs else None printType = kwargs['print'] if 'print' in kwargs else 'line' fp_matches = [] if search_regex is not None: search_pttrn = re.compile(search_regex) fileMatches = 0 totalMatches = 0 for root, _, files in os.walk(directory): for f in files: if file_pttrn.search(f) is None: continue filepath = os.path.join(root, f) if search_regex is None: fp_matches.append(filepath) fileMatches += 1 print(f"Matched file({filepath}).") continue text = fs.read(filepath) matches = 0 for m in search_pttrn.finditer(text): lineNo = get_line_number(m.start(), text) if filterMatch is not None and not filterMatch(m): continue matches += 1 if printType == 'match': print(m.group()) elif printType == 'line': print(f"Match at line ({lineNo}) in file({filepath})") totalMatches += matches if matches > 0: fp_matches.append(filepath) fileMatches += 1 print(f"Found {totalMatches} matches in {fileMatches} file(s).") return fp_matches
def substitute_multi_template(dst_dir, key, value, *src_fps): subs = ixre.to_fmt_dict(key, value) for src_fp in src_fps: ixfs.write( os.path.join(dst_dir, ixre.subkv(subs, os.path.basename(src_fp))), ixre.subkv(subs, ixfs.read(src_fp)))
from io import BytesIO from zipfile import ZipFile, ZIP_DEFLATED from flask import request, send_file from flask_restful import Resource import os import ix.fs as ixfs import ix.regex as ixre from ix.code.template import tag packageData = ixfs.read("generate/src/CreatePackageData.java") loadCommandStr = ixfs.read("generate/src/LoadCommand.java") tcd = ixfs.read("generate/src/Tcd.java") tcdField = ixfs.read("generate/src/TcdField.java") class AdfPackage(Resource): def get(self): return create_package("generate/src/@AdfTemplate") class BxPackage(Resource): def get(self): return create_package("generate/src/@BxTemplate") class CreatePackageData(Resource): def get(self): return apply_template(packageData, "Create%sData.java") class LoadCommand(Resource):