def handle_pSubs_onTheLeft(param, varobjects): if isinstance(param, pFunc): name = varNameGenerator(varnamesRead) pvar = pVar(name) dealWithStatement(param, varobjects=[pvar]) param = copy.deepcopy(pvar) if len(varobjects) > 1: raise Exception(Cyan(\ 'varobjects shouldn\'t have more than 1 varobject' )) fullstrTopsubs[varobjects[0].fullstr+mark] = (param, st_id) paramlist = [param] param.add_varobject(varobjects[0]) replaceAndFindParentsOfParams(paramlist, 0, param, 'subs') if isinstance(param, pVar): param = paramlist[0] if not param.varobjects: param.add_varobject(varobjects[0]) prefixlist = [varobjects[0].prefix[0]] FindParentsOfVarobjects(param, prefixlist, 'subs-prefix') varobjects[0].prefix[0] = prefixlist[0] if isinstance(varobjects[0].content[0], pVar): contentlist = [varobjects[0].content[0]] FindParentsOfVarobjects(param, contentlist, 'subs-content') varobjects[0].content[0] = contentlist[0]
def decompose_multi_varobjects(len_varobjects, param, varobjects): name = varNameGenerator(varnamesRead) pvar = pVar(name) dealWithStatement(param, varobjects=[pvar]) if isinstance(param, pFunc): for i in range(len_varobjects): varobject = varobjects[i] psubs = pSubs() psubs.add_prefix(copy.deepcopy(pvar)) psubs.add_content(pNumber(i)) psubs.add_fullstr(name + '[' + str(i) + ']') dealWithStatement(psubs, [varobject]) else: for i in range(len_varobjects): varobject = varobjects[i] dealWithStatement(param, [varobject])
def handle_pVar_onTheLeft(param, varobjects): if isinstance(param, pFunc): name = varNameGenerator(varnamesRead) pvar = pVar(name) dealWithStatement(param, varobjects=[pvar]) param = copy.deepcopy(pvar) if len(varobjects) > 1: raise Exception(Cyan(\ 'varobjects shouldn\'t have more than 1 varobject' )) clsInstanceToParam[varobjects[0].name+mark] = (param, st_id) paramlist = [param] replaceAndFindParentsOfParams(paramlist, 0, param, 'cls') if isinstance(param, pVar): param = paramlist[0] FindParentsOfVarobjects(param, varobjects, 'cls')
def generateFuncLeftPart_varTonothing(varobject, string): name = varNameGenerator(varPool) leftname = '' if not varobject.restname: leftname = name string += name + ',' else: if varobject.varTofunc: randit_ = random.randint(0, len(varobject.varTofunc) - 1) param_ = list(varobject.varTofunc)[randit_] leftnamesTuple = funcPool[param_] leftname = leftnamesTuple[\ random.randint(0, len(leftnamesTuple)-1)] string += leftname + varobject.restname + ',' else: leftname = name + varobject.restname string += name + varobject.restname + ',' return string, leftname
def buildCallString(ele, fullstr, indent, surround, rv=False): params = fillInParams(ele.args, ele.keywords, indent, surround) pfunc = pFunc(funcName=fullstr, Type='function', indent=indent, surround=surround, params=params) if rv: return pfunc name_func = varNameGenerator(varnamesRead) varobject_func = pVar(name_func) dealWithStatement(param=pfunc, varobjects=[varobject_func]) fullstr = name_func return fullstr
def recognizeCall(value, indent, outsideFunction, outsideList, outsideDict, insideTuple, outCalculation, outsideSet, surround): pfunc = nestplus(value, indent, surround) pfunc.add_indent(indent) if outsideFunction or \ outsideList or \ outsideDict or \ insideTuple or \ outCalculation or \ outsideSet: pfunc.add_surround(surround) name = varNameGenerator(varnamesRead) varobject = pVar(name) dealWithStatement(param=pfunc, varobjects=[varobject]) return copy.deepcopy(varobject) return pfunc
def buildFullString(rcur, indent, surround): fullstr = '' len_rcur = len(rcur) cnt = 0 for ele in rcur[::-1]: cnt += 1 if isinstance(ele, ast.Name): fullstr = buildNameString(ele, fullstr) elif isinstance(ele, ast.Attribute): fullstr = buildAttributeString(ele, fullstr) elif isinstance(ele, ast.BinOp) or isinstance(ele, ast.UnaryOp): param = recognizeMultiAssignment(ele, indent=indent) rn = varNameGenerator(varnamesRead) varobject = pVar(rn) dealWithStatement(param=param, varobjects=[varobject]) fullstr += rn elif isinstance(ele, ast.Subscript): if cnt < len_rcur: fullstr = buildSubscriptString(ele, fullstr, indent) else: return buildSubscriptString(ele, fullstr, indent, True) elif isinstance(ele, ast.Call): if cnt < len_rcur: fullstr = buildCallString(ele, fullstr, indent, surround) else: return buildCallString(ele, fullstr, indent, surround, True) elif isinstance(ele, ast.Constant): fullstr += '\'\'\'' + ele.value + '\'\'\'' return fullstr
def buildSubscriptString(ele, fullstr, indent, judge=False): name = varNameGenerator(varnamesRead) varobject = pVar(name) psubs = pSubs() psubs.add_prefix(pVar(fullstr)) slice = ele.slice rv = recognizeMultiAssignment(slice, outsideSlice=True) if isinstance(rv, pVar): fullstr += '[' + rv.name + ']' varnamesRead.add(rv.name) psubs.add_content(rv) elif isinstance(rv, pNumber): fullstr += '[' + str(rv.num) + ']' psubs.add_content(rv) elif isinstance(rv, pConst): fullstr += '[\'' + str(rv.const) + '\']' psubs.add_content(rv) elif isinstance(rv, pBinop) or isinstance(rv, pUop): rn = varNameGenerator(varnamesRead) varobject = pVar(rn) dealWithStatement(param=rv, varobjects=[varobject]) psubs.add_content(varobject) fullstr += '[' + rn + ']' elif isinstance(rv, pTuple): ''' a[:, 2] = ... ''' psubs.add_content(rv) fullstr += '[' for ele in rv.content: if isinstance(ele, pConst): fullstr += ele.const + ',' elif isinstance(ele, pNumber): fullstr += str(ele.num) + ',' elif isinstance(ele, pVar): fullstr += ele.name + ',' elif isinstance(ele, pSlice): fullstr += ':,' if rv.content: fullstr = fullstr[:-1] fullstr += ']' else: rn = varNameGenerator(varnamesRead) pvar = pVar(rn) dealWithStatement(param=rv, varobjects=[pvar]) fullstr += '[' + rn + ']' psubs.add_content(pvar) psubs.add_fullstr(fullstr) psubs.add_indent(indent) if judge: return psubs dealWithStatement(param=psubs, varobjects=[varobject]) fullstr = name return fullstr