def gen_random_recursive_data(**kwargs): # tuple can only be leaf , coz tuple mechanism is a little different if ('max_depth' in kwargs): max_depth = kwargs['max_depth'] else: max_depth = 6 if ('root' in kwargs): if (utils.is_recursive_type(kwargs['root'])): root = kwargs['root'] try: root.clear() except: root = () else: pass else: root = gen_random_root() else: root = gen_random_root() unhandled = [root] if (utils.is_tuple(root)): root = unhandled root_is_tuple = 1 else: root_is_tuple = 0 lv = -1 while (lv < max_depth): next_unhandled = [] for i in range(0, unhandled.__len__()): if (utils.is_recursive_type(unhandled[i])): unhandled[i] = gen_children(unhandled[i]) if (utils.is_dict(unhandled[i])): for key in unhandled[i]: v = unhandled[i][key] if (utils.is_recursive_type(v)): next_unhandled.append(v) elif (utils.is_list(unhandled[i])): for j in range(0, unhandled[i].__len__()): v = unhandled[i][j] if (utils.is_recursive_type(v)): next_unhandled.append(v) elif (utils.is_tuple(unhandled[i])): for j in range(0, unhandled[i].__len__()): v = unhandled[i][j] if (utils.is_recursive_type(v)): next_unhandled.append(v) else: pass else: pass unhandled = next_unhandled lv = lv + 1 if (root_is_tuple): root = root[0] else: pass return (root)
def gen_children(recursive, **kwargs): if ('max_value_seq' in kwargs): max_value_seq = kwargs['max_value_seq'] else: max_value_seq = 8 if ('max_siblings' in kwargs): max_siblings = kwargs['max_siblings'] else: max_siblings = 19 if ('d' in kwargs): rd = kwargs['d'] else: rd = 5 if ('l' in kwargs): rl = kwargs['l'] else: rl = 5 if ('t' in kwargs): rt = kwargs['t'] else: rt = 1 if ('s' in kwargs): rs = kwargs['s'] else: rs = 1 if ('v' in kwargs): rv = kwargs['v'] else: rv = 25 max_siblings = gen_random_siblings_size(1, max_siblings + 1) if (utils.is_list(recursive)): for i in range(0, max_siblings): data_type = gen_random_type_via_ratio(d=rd, l=rl, t=rt, s=rs, v=rv) next = gen_possible_leaf(data_type, max_value_seq) recursive.append(next) elif (utils.is_tuple(recursive)): temp = list(recursive) for i in range(0, max_siblings): data_type = gen_random_type_via_ratio(d=rd, l=rl, t=rt, s=rs, v=rv) next = gen_possible_leaf(data_type, max_value_seq) temp.append(next) recursive = tuple(temp) elif (utils.is_set(recursive)): for i in range(0, max_siblings): data_type = gen_random_type_via_ratio(d=0, l=0, t=0, s=0, v=rv) next = gen_possible_leaf(data_type, max_value_seq) recursive.add(next) if (utils.is_dict(recursive)): keys = gen_random_prefix_array('key', max_siblings) for i in range(0, max_siblings): data_type = gen_random_type_via_ratio(d=rd, l=rl, t=rt, s=rs, v=rv) next = gen_possible_leaf(data_type, max_value_seq) recursive[keys[i]] = next else: pass return (recursive)
def get_dflt_block_op_pairs_dict(**kwargs): if('block_op_pairs' in kwargs): block_op_pairs = kwargs['block_op_pairs'] if(utils.is_dict(block_op_pairs)): block_op_pairs_dict = block_op_pairs else: block_op_pairs_dict = block.get_block_op_pairs(block_op_pairs) else: block_op_pairs_dict = block.get_block_op_pairs('{}[]()') return(block_op_pairs_dict)
def get_dflt_quotes_pairs_dict(**kwargs): if('quotes_pairs' in kwargs): quotes_pairs = kwargs['quotes_pairs'] if(utils.is_dict(quotes_pairs)): quotes_pairs_dict = quotes_pairs else: quotes_pairs_dict = quote.get_quotes_pairs(quotes_pairs) else: quotes_pairs_dict = quote.get_quotes_pairs('""\'\'') return(quotes_pairs_dict)
def print_j_str(j_str,**kwargs): if('display' in kwargs): display = kwargs['display'] else: display = 0 if('spaces' in kwargs): spaces = kwargs['spaces'] else: spaces = [' ','\t'] if('colons' in kwargs): colons = kwargs['colons'] else: colons = [':'] if('commas' in kwargs): commas = kwargs['commas'] else: commas = [','] if('line_sps' in kwargs): line_sps = kwargs['line_sps'] else: line_sps = ['\r','\n'] #if('quotes' in kwargs): # quotes = kwargs['quotes'] #else: # quotes = ['"',"'"] ########## ############ if('path_sps' in kwargs): path_sps = kwargs['path_sps'] else: path_sps = ['/'] if('fixed_indent' in kwargs): fixed_indent = kwargs['fixed_indent'] else: fixed_indent =0 if('indent' in kwargs): indent = kwargs['indent'] else: indent =4 if('sp' in kwargs): sp = kwargs['sp'] else: sp = '/' if('with_color' in kwargs): with_color = kwargs['with_color'] else: with_color = 1 if(with_color): if('key_color' in kwargs): key_color = kwargs['key_color'] else: key_color=KEY_COLOR if('value_color' in kwargs): value_color = kwargs['value_color'] else: value_color=VALUE_COLOR if('list_ele_color' in kwargs): list_ele_color = kwargs['list_ele_color'] else: list_ele_color=LIST_ELE_COLOR if('op_color' in kwargs): op_color = kwargs['op_color'] else: op_color=OP_COLOR if('default_color' in kwargs): default_color = kwargs['default_color'] else: default_color=DEFAULT_COLOR ###### if('quotes_pairs' in kwargs): quotes_pairs = kwargs['quotes_pairs'] if(utils.is_dict(quotes_pairs)): quotes_pairs_dict = quotes_pairs else: quotes_pairs_dict = quote.get_quotes_pairs(quotes_pairs) else: quotes_pairs_dict = quote.get_quotes_pairs('""\'\'') if('block_op_pairs' in kwargs): block_op_pairs = kwargs['block_op_pairs'] if(utils.is_dict(block_op_pairs)): block_op_pairs_dict = block_op_pairs else: block_op_pairs_dict = block.get_block_op_pairs(block_op_pairs) else: block_op_pairs_dict = block.get_block_op_pairs('{}[]()') lps = get_print_lines_and_paths(j_str,sp=sp,block_op_pairs_dict=block_op_pairs_dict,quotes_pairs_dict=quotes_pairs_dict,spaces=spaces,colons=colons,commas=commas,line_sps=line_sps,path_sps=path_sps,fixed_indent=fixed_indent,indent=indent) lines = lps['lines'] paths = lps['paths'] try: start = args[0] except: start = 0 else: pass try: end = args[1] except: end = lines.__len__()-1 else: pass if('start' in kwargs): start = kwargs['start'] else: pass if('end' in kwargs): end = kwargs['end'] else: pass if(end > lines.__len__()): end = lines.__len__()-1 #-----------------------------------------careful paths not escaped but lines escaped====>>> painted_lines = {} for i in range(start,end+1): line = lines[i] if(with_color): color_sec = get_line_color_sec(line,paths[i],block_op_pairs=block_op_pairs_dict,quotes_pairs=quotes_pairs_dict,key_color=key_color,value_color=value_color,list_ele_color=list_ele_color,op_color=op_color,default_color=default_color,spaces=spaces,colons=colons,commas=commas,line_sps = line_sps,path_sps = path_sps,sp=sp) painted_string = paint(line,color_sec=color_sec,rtrn=True,mode=8) #-------fix issues--- when pobj({'resp_body_bytes': b''c'}) painted_string = html.unescape(painted_string) #-------fix issues--- when pobj({'resp_body_bytes': b''c'}) else: color_sec = None #-------fix issues--- when pobj({'resp_body_bytes': b''c'}) painted_string = html.unescape(line) #-------fix issues--- when pobj({'resp_body_bytes': b''c'}) #not implemented yet # if(fixed_indent): # indent_num = paths[i].split(sp).__len__() # curr_head_len = indent * indent_num # else: painted_lines[i] = painted_string #### #if(display): # print(painted_lines[i]) #else: # pass if(IS_WIN): pass else: if(display): s = elel.join(painted_lines,"\n") print(s) else: pass return(painted_lines)
def get_line_color_sec(line,path,**kwargs): sp = eftl.dflt_kwargs("sp",'/',**kwargs) spaces = eftl.dflt_kwargs("spaces",[' ','\t'],**kwargs) colons = eftl.dflt_kwargs("colons",[':'],**kwargs) commas = eftl.dflt_kwargs("commas",[','],**kwargs) line_sps = eftl.dflt_kwargs("line_sps",['\r','\n'],**kwargs) block_op_pairs_dict = eftl.dflt_kwargs('block_op_pairs_dict',block.get_block_op_pairs('{}[]()'),**kwargs) quotes_pairs_dict = eftl.dflt_kwargs('quotes_pairs_dict',quote.get_quotes_pairs('""\'\'')) path_sps = eftl.dflt_kwargs('path_sps',['/'],**kwargs) fixed_indent = eftl.dflt_kwargs('fixed_indent',0,**kwargs) indent = eftl.dflt_kwargs('indent',4,**kwargs) #### if('block_op_pairs' in kwargs): block_op_pairs = kwargs['block_op_pairs'] if(utils.is_dict(block_op_pairs)): block_op_pairs_dict = block_op_pairs else: block_op_pairs_dict = block.get_block_op_pairs(block_op_pairs) else: block_op_pairs_dict = block.get_block_op_pairs('{}[]()') if('block_non_ordered_op_pairs' in kwargs): block_non_ordered_op_pairs = kwargs['block_non_ordered_op_pairs'] if(utils.is_dict(block_non_ordered_op_pairs)): block_non_ordered_op_pairs_dict = block_non_ordered_op_pairs else: block_non_ordered_op_pairs_dict = block.get_block_op_pairs(block_non_ordered_op_pairs) else: block_non_ordered_op_pairs_dict = block.get_block_op_pairs('{}') #### if('quotes_pairs' in kwargs): quotes_pairs = kwargs['quotes_pairs'] if(utils.is_dict(quotes_pairs)): quotes_pairs_dict = quotes_pairs else: quotes_pairs_dict = quote.get_quotes_pairs(quotes_pairs) else: quotes_pairs_dict = quote.get_quotes_pairs('""\'\'') lquotes = [] rquotes = [] quotes = [] for i in range(1,quotes_pairs_dict.__len__()+1): lquotes.append(quotes_pairs_dict[i][0]) rquotes.append(quotes_pairs_dict[i][1]) quotes.append(quotes_pairs_dict[i][0]) quotes.append(quotes_pairs_dict[i][1]) #### if('key_color' in kwargs): key_color = kwargs['key_color'] else: key_color=KEY_COLOR if('value_color' in kwargs): value_color = kwargs['value_color'] else: value_color=VALUE_COLOR if('list_ele_color' in kwargs): list_ele_color = kwargs['list_ele_color'] else: list_ele_color=LIST_ELE_COLOR if('op_color' in kwargs): op_color = kwargs['op_color'] else: op_color=OP_COLOR if('default_color' in kwargs): default_color = kwargs['default_color'] else: default_color=DEFAULT_COLOR #------------------------------------------------------------ line_len = line.__len__() si = 0 ei = 0 color_sec = {} color_sec_seq = 1 colon_meeted = 0 byte_meeted = 0 #------------------------------------------------------------- ops = [] for i in range(1,block_op_pairs_dict.__len__()+1): ops.append(block_op_pairs_dict[i][0]) ops.append(block_op_pairs_dict[i][1]) ###### machine = fsm.FSM() # #machine.enable_debug = True # regex_lquotes = fsm.creat_regex_from_arr(lquotes) regex_rquotes = fsm.creat_regex_from_arr(rquotes) regex_b = re.compile('b') regex_spaces = fsm.creat_regex_from_arr(spaces) regex_colons = fsm.creat_regex_from_arr(colons) regex_commas = fsm.creat_regex_from_arr(commas) regex_slash = re.compile("\\\\") regex_ops = fsm.creat_regex_from_arr(ops) LqRqBSpColComSlOp_arr = ['b','\\\\'] LqRqBSpColComSlOp_arr = LqRqBSpColComSlOp_arr + lquotes + rquotes + spaces + colons+commas + ops regex_not_LqRqBSpColComSlOp = fsm.creat_regex_not_from_arr(LqRqBSpColComSlOp_arr) #------------------------------------------------------------------- head = utils.path_string_get_head(path) head_last = utils.str_rstrip(head,sp,1) head_last = utils.str_rstrip(head_last," ",1) if(head_last == ''): pass else: head_last = head_last[-1] #------------------------------------------------------ def do_throw(curr_state,trigger_checker,input_symbol,line,path,index): msg = "curr_state: " + curr_state + "\n" msg = msg + "trigger_checker: "+trigger_checker.__str__() + "\n" msg = msg + "input_symbol: "+ input_symbol.__str__() + "\n" msg = msg + "triggered ERROR" + "\n" msg = msg + line + "\n" msg = msg + path + "\n" mas = msg + "index : " + str(index) raise Exception(msg) def do_open_quote(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted): byte_meeted = 0 #### ei = cursor curr_color = default_color color_sec[color_sec_seq] = (si,ei,curr_color) color_sec_seq = color_sec_seq + 1 si = cursor + 1 return(si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted) def do_close_quote(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted): ei = cursor - 1 if(colon_meeted): curr_color = value_color else: if(block.is_non_ordered_op(head_last,block_op_pairs_dict,block_non_ordered_op_pairs_dict)): curr_color = key_color elif(is_colon(head_last)): curr_color = value_color elif(head_last==sp): curr_color = value_color else: curr_color = list_ele_color colon_meeted = 0 color_sec[color_sec_seq] = (si,ei,curr_color) color_sec_seq = color_sec_seq + 1 si = cursor return(si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted) def do_open_var(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted): ei = cursor - 1 curr_color = default_color color_sec[color_sec_seq] = (si,ei,curr_color) color_sec_seq = color_sec_seq + 1 si = cursor return(si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted) def do_open_var_bytes(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted): byte_meeted = 1 ei = cursor - 1 curr_color = default_color color_sec[color_sec_seq] = (si,ei,curr_color) color_sec_seq = color_sec_seq + 1 si = cursor return(si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted) def do_clear_byte_meeted(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted): byte_meeted = 0 return(si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted) def do_close_var(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted): ei = cursor - 1 if(colon_meeted): curr_color = value_color else: if(block.is_non_ordered_op(head_last,block_op_pairs_dict,block_non_ordered_op_pairs_dict)): curr_color = key_color elif(is_colon(head_last)): curr_color = value_color elif(head_last==sp): curr_color = value_color else: curr_color = list_ele_color colon_meeted = 0 color_sec[color_sec_seq] = (si,ei,curr_color) color_sec_seq = color_sec_seq + 1 si = cursor return(si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted) def do_colons(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted): colon_meeted = 1 return(si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted) def do_op(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted): ei = cursor - 1 curr_color = default_color if(ei >= si): color_sec[color_sec_seq] = (si,ei,curr_color) color_sec_seq = color_sec_seq + 1 else: pass si = cursor ei = cursor if(block.is_non_ordered_op(head_last,block_op_pairs_dict,block_non_ordered_op_pairs_dict)): curr_color = value_color elif(is_colon(head_last)): curr_color = value_color elif(head_last==sp): curr_color = value_color else: curr_color = list_ele_color color_sec[color_sec_seq] = (si,ei,curr_color) color_sec_seq = color_sec_seq + 1 si = cursor + 1 return(si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted) def do_close_var_colon(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted): colon_meeted = 1 return(do_close_var(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted)) #--------------------------------------------------------------------------------------- #### machine.add("INIT",regex_b,do_open_var_bytes,"BYTES") machine.add("INIT",regex_spaces,None,"INIT") machine.add("INIT",regex_colons,do_colons,"INIT") machine.add("INIT",regex_commas,None,"INIT") machine.add("INIT",regex_slash,do_open_var,"SLASHINIT") machine.add("INIT",regex_ops,do_op,"INIT") machine.add("INIT",regex_not_LqRqBSpColComSlOp,do_open_var,"OTHER") #### machine.add("BYTES",regex_b,do_clear_byte_meeted,"OTHER") machine.add("BYTES",regex_spaces,do_close_var,"BYTES") machine.add("BYTES",regex_ops,do_throw,"ERROR") machine.add("BYTES",regex_colons,do_close_var_colon,"INIT") machine.add("BYTES",regex_commas,do_close_var,"INIT") machine.add("BYTES",regex_slash,do_clear_byte_meeted,"SLASHBYTES") machine.add("BYTES",regex_not_LqRqBSpColComSlOp,do_clear_byte_meeted,"OTHER") #### #### machine.add("SLASHINIT",re.compile("."),None,"OTHER") #### machine.add("SLASHBYTES",re.compile("."),None,"OTHER") #### machine.add("OTHER",regex_b,None,"OTHER") machine.add("OTHER",regex_spaces,do_close_var,"OTHER") machine.add("OTHER",regex_ops,do_throw,"ERROR") machine.add("OTHER",regex_colons,do_close_var_colon,"INIT") machine.add("OTHER",regex_commas,do_close_var,"INIT") machine.add("OTHER",regex_slash,None,"SLASHOTHER") machine.add("OTHER",regex_not_LqRqBSpColComSlOp,None,"OTHER") #### machine.add("SLASHOTHER",re.compile("."),None,"OTHER") #### regex_lquote_array = fsm.creat_regexes_array(lquotes) regex_rquote_array = fsm.creat_regexes_array(rquotes) ### for i in range(0,lquotes.__len__()): ####INIT -lq_n-> LQ_n sn = ''.join(("LQ",'_',str(i))) machine.add("INIT",regex_lquote_array[i],do_open_quote,sn) for i in range(0,rquotes.__len__()): ####INIT -rq_n-> ERROR if(regex_rquote_array[i] == regex_lquote_array[i]): pass else: sn = ''.join(("LQ",'_',str(i))) machine.add("INIT",regex_rquote_array[i],do_throw,'ERROR') #### for i in range(0,lquotes.__len__()): ####BYTES -lq_n-> LQ_n sn = ''.join(("LQ",'_',str(i))) machine.add("BYTES",regex_lquote_array[i],do_open_quote,sn) for i in range(0,rquotes.__len__()): ####BYTES -rq_n-> ERROR if(rquotes[i] == lquotes[i]): pass else: sn = ''.join(("LQ",'_',str(i))) machine.add("BYTES",regex_rquote_array[i],do_throw,'ERROR') #### for i in range(0,lquotes.__len__()): ####OTHER -lq_n-> ERROR sn = ''.join(("LQ",'_',str(i))) machine.add("OTHER",regex_lquote_array[i],do_throw,'ERROR') for i in range(0,rquotes.__len__()): ####OTHER -rq_n-> ERROR if(rquotes[i] == lquotes[i]): pass else: sn = ''.join(("LQ",'_',str(i))) machine.add("OTHER",regex_rquote_array[i],do_throw,'ERROR') #### for i in range(0,lquotes.__len__()): ####LQ_n -lq_n-> ERROR sn = ''.join(("LQ",'_',str(i))) if(lquotes[i] == rquotes[i]): pass else: machine.add(sn,regex_lquote_array[i],do_throw,'ERROR') ####LQ_n -rq_n-> INIT machine.add(sn,regex_rquote_array[i],do_close_quote,'INIT') #####LQ_n -b-> LQ_n machine.add(sn,regex_b,None,sn) #####LQ_n -spaces-> LQ_n machine.add(sn,regex_spaces,None,sn) #####LQ_n -ops-> LQ_n machine.add(sn,regex_ops,do_throw,'ERROR') ####LQ_n -colons-> LQ_n machine.add(sn,regex_colons,do_throw,'ERROR') ####LQ_n -commas-> LQ_n machine.add(sn,regex_commas,do_throw,'ERROR') #####LQ_n -slash -> SLASHLQ_n slashlq = ''.join(("SLASHLQ",'_',str(i))) machine.add(sn,re.compile("\\\\"),None,slashlq) ####SLASHLQ_n -any-> LQ_n machine.add(slashlq,re.compile("."),None,sn) #####LQ_n -others-> LQ_n tmp_arr = ['b','\\\\'] + ops + colons + commas + spaces tmp_arr_rq = [rquotes[i]] tmp_arr_lq = [lquotes[i]] if(lquotes[i] == rquotes[i]): tmp_arr_rq = [] else: pass tmp_final_arr = tmp_arr + tmp_arr_rq + tmp_arr_lq #### tmp_regex = fsm.creat_regex_not_from_arr(tmp_final_arr) machine.add(sn,tmp_regex,None,sn) ##### #---------------------------------------------------------------- curr_state = "INIT" prev_symbol = '' for i in range(0,line.__len__()): cursor = i input_symbol = line[i] action,next_state,trigger_checker = machine.search(curr_state,input_symbol) #print('----------') #print(curr_state,trigger_checker,input_symbol,action,next_state) #print(line) #print(path) #print(i) if(action == do_throw): action(curr_state,trigger_checker,input_symbol,line,path,i) elif(action == None): pass else: si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted = action(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted) curr_state = next_state prev_symbol = input_symbol #-----Final handle------------------------------------------------------------ ####### if(curr_state == "INIT"): cursor = cursor + 1 if(block.is_op(input_symbol)): pass else: si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted = do_open_var(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted) curr_state = "INIT" ####### state_is_bytes = (curr_state == "BYTES") state_is_other = (curr_state == "OTHER") state_is_slashinit = (curr_state == "SLASHINIT") state_is_slashbytes = (curr_state == "SLASHBYTES") state_is_slashother = (curr_state == "SLASHOTHER") cond = (state_is_bytes|state_is_other|state_is_slashinit|state_is_slashbytes|state_is_slashother) ####### if(cond): cursor = cursor + 1 si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted = do_close_var(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted) curr_state = "INIT" ###### cond = ("SLASHLQ" in curr_state) if(cond): cursor = cursor + 1 si,ei,color_sec,color_sec_seq,colon_meeted,byte_meeted = do_open_quote(cursor,si,ei,color_sec,color_sec_seq,colon_meeted,prev_symbol,byte_meeted) curr_state = "INIT" return(color_sec)