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 get_print_lines_and_paths(j_str,**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) #step0 quote j_str = convert_token_in_quote( j_str, 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 ) #step1 format j_str = fmt_jstr.format_j_str(j_str,block_op_pairs_dict) #step2 char-level-of-block j_lv_str = tokenize_block.get_j_str_lvs_dict(j_str,block_op_pairs_dict) orig_lines = j_str.split('\n') line_start_indexes = get_line_start_index_in_j_str(orig_lines) new_lines = {} #step 1: line 0 prev_lv = int(j_lv_str[line_start_indexes[0]]) prev_path = line_to_path_init(orig_lines[0],block_op_pairs_dict,sp,commas,colons) #----------should not unescape here for secarino:{'resp_body_bytes': b''c'} # which will cause {'resp_body_bytes': b''c'} new_lines[0] = orig_lines[0] #----------should not unescape here paths = {} paths = {0: prev_path} for i in range(1, orig_lines.__len__()): curr_lv = int(j_lv_str[line_start_indexes[i]]) curr_path = line_to_path(orig_lines[i],curr_lv,prev_lv,prev_path,block_op_pairs_dict,sp,commas,colons) paths[i] = curr_path curr_head = utils.path_string_get_head(curr_path,delimiter=sp) if(fixed_indent): prepend = " " * indent * (curr_head.count(sp) - 1) else: curr_head = curr_head.replace(sp,'') #---escaped to calculate the real prepend spaces curr_head = html.unescape(curr_head) #---escaped to calculate the real prepend spaces curr_head_len = curr_head.__len__() prepend = " " * curr_head_len #------------should not unescape here new_lines[i] = ''.join((prepend,orig_lines[i])) #------------should not unescape here prev_lv = curr_lv prev_path = curr_path return({'lines':new_lines,'paths':paths})
def get_jdict_token_set(**kwargs): ''' from xdict.jprint import get_jdict_token_set get_jdict_token_set(quotes_pairs_dict={1: ('"', '"'), 2: ("<", ">")}) ''' 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('""\'\''), **kwargs) lquotes, rquotes, quotes = quote.get_lrquotes(quotes_pairs_dict) path_sps = eftl.dflt_kwargs("path_sps", ['/']) ####### d = {} s = set({}) def add_bi_table(s, d, x): for each in x: k = each v = escape.html_number_escape_str(k) d[k] = v d[v] = k s.add(k) add_bi_table(s, d, spaces) add_bi_table(s, d, colons) add_bi_table(s, d, commas) add_bi_table(s, d, line_sps) add_bi_table(s, d, lquotes) add_bi_table(s, d, rquotes) add_bi_table(s, d, path_sps) for i in range(1, block_op_pairs_dict.__len__() + 1): s.add(block_op_pairs_dict[i][0]) s.add(block_op_pairs_dict[i][1]) recover_token_l = escape.html_number_escape_str( block_op_pairs_dict[i][0]) recover_token_r = escape.html_number_escape_str( block_op_pairs_dict[i][1]) d[block_op_pairs_dict[i][0]] = recover_token_l d[block_op_pairs_dict[i][1]] = recover_token_r d[recover_token_l] = block_op_pairs_dict[i][0] d[recover_token_r] = block_op_pairs_dict[i][1] return ({'token_set': s, 'replace_ref_dict': d})
def prepare_quotes_token_machine(j_str, **kwargs): ''' >>> from xdict.jprint import convert_token_in_quote >>> from xdict.jprint import help >>> >>> convert_token_in_quote('"a b":"cd"') '"a b":"cd"' >>> import html >>> html.unescape('"a b":"cd"') '"a b":"cd"' >>> convert_token_in_quote('"a b":cd') '"a b":cd' >>> >>> #help(convert_token_in_quote) convert_token_in_quote('<a b>:"cd"',quotes_pairs_dict={1: ('"', '"'), 2: ("<", ">")}) '<a b>:"cd"' ''' #### 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", get_block_op_pairs('{}[]()'), **kwargs) quotes_pairs_dict = eftl.dflt_kwargs("quotes_pairs_dict", get_quotes_pairs('""\'\''), **kwargs) lquotes, rquotes, quotes = get_lrquotes(quotes_pairs_dict) path_sps = eftl.dflt_kwargs("path_sps", ['/']) #### temp = get_jdict_token_set(**kwargs) token_set = temp['token_set'] replace_ref_dict = temp['replace_ref_dict'] #### # ----------------------------------------------------------------- # #### def do_replace(ch): if (ch in token_set): ch = replace_ref_dict[ch] return (ch) def do_throw(curr_state, trigger_checker, input_symbol): 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" raise Exception(msg) #### machine = fsm.FSM() 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("\\\\") ###### 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]) ###### 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) # ############################ # ############################ machine.add("INIT", regex_b, None, "BYTES") machine.add("INIT", regex_spaces, None, "INIT") machine.add("INIT", regex_ops, None, "INIT") machine.add("INIT", regex_colons, None, "INIT") machine.add("INIT", regex_commas, None, "INIT") machine.add("INIT", regex_slash, None, "SLASHINIT") machine.add("INIT", regex_not_LqRqBSpColComSlOp, do_replace, "OTHER") #### machine.add("BYTES", regex_b, None, "OTHER") machine.add("BYTES", regex_spaces, None, "BYTES") machine.add("BYTES", regex_ops, None, "INIT") machine.add("BYTES", regex_colons, None, "INIT") machine.add("BYTES", regex_commas, None, "INIT") machine.add("BYTES", regex_slash, None, "SLASHBYTES") machine.add("BYTES", regex_not_LqRqBSpColComSlOp, do_replace, "OTHER") #### machine.add("SLASHINIT", re.compile("."), do_replace, "OTHER") #### machine.add("SLASHBYTES", re.compile("."), do_replace, "OTHER") #### machine.add("OTHER", regex_b, None, "OTHER") machine.add("OTHER", regex_spaces, None, "OTHER") machine.add("OTHER", regex_ops, None, "INIT") machine.add("OTHER", regex_colons, None, "INIT") machine.add("OTHER", regex_commas, None, "INIT") machine.add("OTHER", regex_slash, None, "SLASHOTHER") machine.add("OTHER", regex_not_LqRqBSpColComSlOp, do_replace, "OTHER") #### machine.add("SLASHOTHER", re.compile("."), do_replace, "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], None, 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], None, 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-> LQ_n sn = ''.join(("LQ", '_', str(i))) machine.add("OTHER", regex_lquote_array[i], None, sn) 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-> READY machine.add(sn, regex_rquote_array[i], None, 'INIT') #####LQ_n -b-> LQ_n machine.add(sn, regex_b, None, sn) #####LQ_n -spaces-> LQ_n machine.add(sn, regex_spaces, do_replace, sn) #####LQ_n -ops-> LQ_n machine.add(sn, regex_ops, do_replace, sn) ####LQ_n -colons-> LQ_n machine.add(sn, regex_colons, do_replace, sn) ####LQ_n -commas-> LQ_n machine.add(sn, regex_commas, do_replace, sn) #####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("."), do_replace, 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, do_replace, sn) #### machine.orig_str = j_str machine.do_replace = do_replace machine.do_throw = do_throw #### return (machine)
def pdir(obj,*args,**kwargs): ''' >>> import os >>> from xdict.jprint import pdir >>> pdir(os,range=(1,10)) [ 'CLD_DUMPED', 'CLD_EXITED', 'CLD_TRAPPED', 'EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT' ] >>> ''' obj = dir(obj) try: start = args[0] except: start = 0 else: pass try: end = args[1] except: end = 2 ** 32 else: pass if('egrep' in kwargs): nobj = [] for each in obj: if(kwargs['egrep'] in each): nobj.append(each) else: pass obj = nobj else: pass if('range' in kwargs): start = kwargs['range'][0] end = kwargs['range'][1] else: pass 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) quotes_pairs_dict = get_dflt_quotes_pairs_dict(**kwargs) block_op_pairs_dict = get_dflt_block_op_pairs_dict(**kwargs) 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 else: pass if('start' in kwargs): start = kwargs['start'] else: pass if('end' in kwargs): end = kwargs['end'] else: pass ### obj = obj[start:end] ### if(with_color): pobj(obj,spaces=spaces,colons=colons,commas=commas,line_sps = line_sps,path_sps = path_sps,sp=sp,with_color=with_color,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,display=1,fixed_indent=fixed_indent,indent=indent) else: pobj(obj,spaces=spaces,colons=colons,commas=commas,line_sps = line_sps,path_sps = path_sps,sp=sp,with_color=with_color,block_op_pairs=block_op_pairs_dict,quotes_pairs=quotes_pairs_dict,fixed_indent=fixed_indent,indent=indent,display=1)
def pobj(obj,*args,**kwargs): ''' pobj(eles,quotes_pairs="''\"\"<>") pobj(eles,block_op_pairs="{}[]()") ''' try: start = args[0] except: start = 0 else: pass try: end = args[1] except: end = 2 ** 32 else: pass 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) quotes_pairs_dict = get_dflt_quotes_pairs_dict(**kwargs) block_op_pairs_dict = get_dflt_block_op_pairs_dict(**kwargs) 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('start' in kwargs): start = kwargs['start'] else: pass ########## ############1 if('end' in kwargs): end = kwargs['end'] else: pass if(utils.is_str(obj)): s = obj else: s = obj.__str__() if(with_color): print_j_str(s,spaces=spaces,colons=colons,commas=commas,line_sps = line_sps,path_sps = path_sps,sp=sp,with_color=with_color,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,display=1,start=start,end=end,fixed_indent=fixed_indent,indent=indent) else: print_j_str(s,spaces=spaces,colons=colons,commas=commas,line_sps = line_sps,path_sps = path_sps,sp=sp,with_color=with_color,block_op_pairs=block_op_pairs_dict,quotes_pairs=quotes_pairs_dict,start=start,end=end,fixed_indent=fixed_indent,indent=indent,display=1)
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)