示例#1
0
def show_profile_package_to_textbox():
    print("profile package option menu selected")
    profile_package_filename_selected = om_profile_package_var.get()
    print(profile_package_filename_selected)
    clear_textbox()
    print_to_textbox("Profile package selected: " +
                     profile_package_filename_selected)
    #open profile to load content, load back data to variable
    global home_path

    with open(
            os.path.join(home_path, "profile", "profile_package",
                         profile_package_filename_selected)) as json_file:
        data_dict = json.load(json_file)
        #print(data_dict)
        profile_package_name = data_dict['package_name']
        profiles_filepath = data_dict['profiles_filepath']
        #print(profile_package_name)
        #print(profiles_filepath)
        print_to_textbox("profile_package_name " + profile_package_name)

        print_to_textbox("-----------------------------------------")
        print_to_textbox("profile_filepaths: ")
        print("profile_filepaths: ")
        for profile_filepath in profiles_filepath:
            #print_to_textbox(profile_filepath)
            print_to_textbox(get_filename(profile_filepath))
            print(get_filename(profile_filepath))

    profile_package_select.configure(bg="green2")
    text_var_status.set("STATUS: PACKAGE SELECTED")
    loadprofile_package_button.configure(state='normal')
示例#2
0
def ls_name(folder_path, e=""):
    '''
	e = except_filter_folder 
	get of file name of a folder
	sort file by decent modify time, newest file on the first
	'''
    filenames = []
    filepaths = []
    if e == "":
        for path, subdirs, files in os.walk(folder_path):
            for name in files:
                if "~$" not in name:
                    filepaths.append(os.path.join(path, name))
                    #filenames.append(name)
        #sorted(filenames)

        sorted_by_mtime_ascending = sorted(filepaths,
                                           key=lambda t: os.stat(t).st_mtime,
                                           reverse=True)
        for filepath in sorted_by_mtime_ascending:
            filenames.append(get_filename(filepath))
        return filenames
    else:
        for path, subdirs, files in os.walk(folder_path):
            for name in files:
                if "~$" not in name and e not in path:
                    filepaths.append(os.path.join(path, name))
                    #filenames.append(name)
        sorted_by_mtime_ascending = sorted(filepaths,
                                           key=lambda t: os.stat(t).st_mtime,
                                           reverse=True)
        for filepath in sorted_by_mtime_ascending:
            filenames.append(get_filename(filepath))
        return filenames
示例#3
0
def show_profile_to_textbox():
    profile_filename_selected = om_profile_var.get()
    print(om_profile_var.get())
    print_to_textbox(om_profile_var.get())
    #open profile to load content, load back data to variable
    global home_path
    clear_textbox()
    with open(os.path.join(home_path, "profile",
                           profile_filename_selected)) as json_file:
        data_dict = json.load(json_file)
        print(data_dict)

        template_filepath = data_dict["template_filepath"]
        input_file_path = data_dict["input_file_path"]
        selected_sheet = data_dict["selected_sheet"]
        folder_tag = data_dict["folder_tag"]
        foldersplit = data_dict['foldersplit']
        filenametag = data_dict['filenametag']
        mergefile = data_dict['mergefile']

        print_to_textbox("Excel Input: " + get_filename(input_file_path))
        print_to_textbox("Sheet Name: " + selected_sheet)
        print_to_textbox("Template Input: " + get_filename(template_filepath))
        print_to_textbox("Folder_tag: " + folder_tag)
        print_to_textbox("foldersplit: " + str(foldersplit))
        print_to_textbox("filenametag: " + str(filenametag))
        print_to_textbox("mergefile: " + str(mergefile))

        print_to_textbox("-------------------------")
    profile_select.configure(bg="green2")
    loadprofile_button.configure(state='normal')
示例#4
0
def loadprofile_package():
    #cho vang cai nut de biet la bat dau ==> de troubleshooting
    loadprofile_package_button.configure(bg='yellow')
    t.start()
    t1.start()
    print("loadprofile_package button press!!")
    profile_package_filename_selected = om_profile_package_var.get()
    print(profile_package_filename_selected)
    clear_textbox()
    print_to_textbox("Profile package selected: " +
                     profile_package_filename_selected)
    #open profile to load content, load back data to variable
    global home_path

    with open(
            os.path.join(home_path, "profile", "profile_package",
                         profile_package_filename_selected)) as json_file:
        data_dict = json.load(json_file)
        #print(data_dict)
        profile_package_name = data_dict['package_name']
        profiles_filepath = data_dict['profiles_filepath']
        no_of_profile = len(profiles_filepath)
        print(profile_package_name)
        print(profiles_filepath)
        #print_to_textbox("profile_package_name "+profile_package_name)
        #print_to_textbox("-----------------------------------------")

        count_profile = 0
        for profile_filepath in profiles_filepath:
            count_profile += 1
            #print_to_textbox(profile_filepath)

            print("Loading ", get_filename(profile_filepath), count_profile,
                  "/", no_of_profile, "profiles")
            print_to_textbox("----------------------------------------")
            print_to_textbox("Loading " + get_filename(profile_filepath) +
                             " " + str(count_profile) + "/" +
                             str(no_of_profile) + " profiles")

            loadprofile_procedure(profile_filepath)

    #cho xanh cai nut , de biet la chay thanh cong
    loadprofile_package_button.configure(bg='green2')
    text_var_status.set("FINISHED:" + profile_package_filename_selected)
    print("FINISHED", profile_package_filename_selected, "!!!")
    t.stop()
    t1.stop()
示例#5
0
def browse_datainput_button():
    status_label.configure(background=orig_color)

    global input_file_path
    #global root_path
    #root_path = filedialog.askdirectory()
    #filedialog.askopenfilename(filetypes=[("Excel files", ".xlsx .xls")])
    input_file_path = filedialog.askopenfilename(filetypes=[("Excel files",
                                                             ".xlsx .xls")])

    print("button press!!")
    print(input_file_path)

    #update entry for input path
    text_var_inputpath.set(input_file_path)

    global sheet_options_list
    sheet_options_list = findallexcellsheet(input_file_path)
    #print(sheet_options_list)

    ########
    global om_var
    choices = sheet_options_list
    om_var = tk.StringVar(root)
    #om_var.set("02.Select sheet")
    #sheet_select = tk.OptionMenu(root, om_var, *choices)
    global sheet_select
    sheet_select = tk.OptionMenu(root,
                                 om_var,
                                 *choices,
                                 command=lambda _: sheet_select_command())

    sheet_select.grid(row=0, column=3, sticky="ew")
    ########

    #update button label
    input_filename = get_filename(input_file_path)[:20]
    btn_browse_text.set(input_filename)
    browse_button.configure(bg='green2')
    text_var_status.set("SELECTED EXCEL FILE")
    foldersplit_checkbox.configure(state='normal')
示例#6
0
def loadprofile_procedure(profile_filepath):
    print("---------------------creating script for profile", profile_filepath,
          "--------------------")
    #print_to_textbox("creating script for profile:" + get_filename(profile_filepath))
    print_to_textbox("creating script for profile:" + profile_filepath)
    text_var_status.set("loading " + get_filename(profile_filepath))

    with open(profile_filepath) as json_file:
        data_dict = json.load(json_file)
        print(data_dict)

        template_filepath = data_dict["template_filepath"]
        input_file_path = data_dict["input_file_path"]
        selected_sheet = data_dict["selected_sheet"]
        folder_tag = data_dict["folder_tag"]
        foldersplit = data_dict['foldersplit']
        filenametag = data_dict['filenametag']
        mergefile = data_dict['mergefile']

        input_filename = get_filename(input_file_path)[:20]
    #print("#######################troubbleshooting#####################")
    #print(template_filepath)
    #print_to_textbox("template_filepath: "+template_filepath)
    #time.sleep(10)
    #print("#######################end troubbleshooting#####################")

    print("selected_sheet:", selected_sheet)

    try:
        df = pd.read_excel(input_file_path,
                           header=0,
                           sheet_name=selected_sheet)
        df = df.rename(columns=lambda x: x.strip())

        #remove decription data in CIQ template if exit
        if "Parameter" in df.columns:
            i = df[((df.Parameter == 'Area'))].index
            df = df.drop(i)
        #thay doi ":" bang dau ":"
        #df.columns = df.columns.str.replace(r"[:]", "_")
        df.columns = df.columns.str.replace(r"[:=\-]", "_")
        print("SUMMARY DATA INPUT TABLE:")
        print(df)
        global column_header
        column_headers = list(df.columns.values)
    except:
        print(input_file_path + " does not exists")
        print_to_textbox(input_file_path + " does not exists")
        return  #stop method

    try:
        f = open(template_filepath, "r")
        input_file_content = f.read()
        f.close()
    except FileNotFoundError:
        print(template_filepath, "does not exists")
        print_to_textbox(template_filepath + " does not exists")
        return  #stop method

    folder, template_filename = os.path.split(template_filepath)

    #find all variable in template
    with open(template_filepath) as infile:
        lines = infile.readlines()

        #find all variable string
        var_set = set()
        for index, line in enumerate(lines):
            line = line.strip()
            regex = '\$' + "(\w+)"
            variables = re.findall(regex, line)
            for item in variables:
                var_set.add(item)
            #####
            regex2 = '\$\{' + "(\w+)" + '\}'
            variables2 = re.findall(regex2, line)
            #print(variables2)
            for item in variables2:
                var_set.add(item)
            #####

            #####
            #cai tien co dau : ben trong variable
            regex3 = '%' + "([\w:=\-]+)" + '%'
            variables3 = re.findall(regex3, line)
            for item in variables3:
                var_set.add(item)
            #####
        print("variables in ", template_filename)
        print(var_set)

    #update input_file_content (replace variable from %string% to ${string}
    regex = '%' + "([\w:=\-]+)" + '%'  #update to support : inside variablename
    variables_percent = re.findall(regex, input_file_content)
    print("#######################################")
    print("variables_percent:", variables_percent)
    print("#######################################")

    for var in variables_percent:
        #print (var)
        new_var = var.replace(
            ":", "_")  #phai thay ":" bang "_" vi substitudte ko duoc
        new_var = new_var.replace(
            "-", "_")  #phai thay ":" bang "_" vi substitudte ko duoc
        new_var = new_var.replace(
            "=", "_")  #phai thay ":" bang "_" vi substitudte ko duoc
        print("new variable:", new_var)
        #new_s = re.sub('%'+var+'%','${'+var+'}',input_file_content)
        new_s = re.sub('%' + var + '%', '${' + new_var + '}',
                       input_file_content)
        input_file_content = new_s

    #substitute data from each row of excel into to template
    for index, row in df.iterrows():
        data = {}
        for column_name in var_set:
            column_name = column_name.replace(":", "_")  #remove ":" in column
            column_name = column_name.replace("=", "_")  #remove ":" in column
            column_name = column_name.replace("-", "_")  #remove ":" in column
            if column_name in column_headers:
                data[column_name] = row[column_name]
        rowname = filenametag

        src = Template(input_file_content)
        try:
            result = src.substitute(data)
        except:
            #stop timer, so that script can be run again
            t.stop()
            t1.stop()

        #split script follow folder
        templatename, ext = get_file_name_ext(template_filename)
        if not foldersplit:
            output_filepath = os.path.join(
                home_path, "output_script",
                templatename + "_" + str(row[rowname]) + "." + ext)
        else:
            #neu folder ko ton tai thi tao them folder
            if not os.path.exists(
                    os.path.join(home_path, "output_script",
                                 str(row[folder_tag]))):
                os.mkdir(
                    os.path.join(home_path, "output_script",
                                 str(row[folder_tag])))

            #rule dat script name, co row, co template name
            output_filepath = os.path.join(
                home_path, "output_script", str(row[folder_tag]),
                templatename + "_" + row[rowname] + "." + ext)

        if mergefile:
            output_text_file = open(output_filepath, "a")
        else:
            output_text_file = open(output_filepath, "w")
        output_text_file.write(result)
        output_text_file.close()
        print("write successful", data, "to", output_filepath)
示例#7
0
def save_profile_press():
    global template_filepath, input_file_path, var_foldersplit, var_mergefile
    selected_sheet = om_var.get()
    folder_tag = om_folder_var.get()
    package_name = text_var_packagename.get()
    print("save_profile_press!!")
    #init a dict
    data = {}
    path, template_filename = os.path.split(template_filepath)
    profile_filename = template_filename + "_" + get_now_stamp(
    ) + ".json"  ##'20200514_094015'
    data['template_filepath'] = template_filepath
    data['input_file_path'] = input_file_path
    data['selected_sheet'] = selected_sheet
    data['folder_tag'] = folder_tag
    data['foldersplit'] = var_foldersplit.get()
    data['filenametag'] = filenametag_var.get()
    data['mergefile'] = var_mergefile.get()

    print("data to be save as below")
    print(data)
    profile_filepath = os.path.join(home_path, 'profile', profile_filename)
    #with open(os.path.join(home_path,'profile',profile_filename), 'w') as outfile:
    with open(profile_filepath, 'w') as outfile:
        json.dump(data, outfile)
    text_var_status.set("Save profile to " + profile_filename)

    #####################
    data2 = {}
    path, template_filename = os.path.split(template_filepath)

    #neu o packag_ename co fill text thi moi update vao package name, khong thi thoi
    if package_name != "":
        package_filename = package_name + ".json"
        package_filepath = os.path.join(home_path, 'profile',
                                        'profile_package', package_filename)
        data2['package_name'] = package_name

        print("package data to be save as below")
        print(data2)
        #neu file ko ton tai thi tao moi
        if not os.path.exists(package_filepath):
            data2['profiles_filepath'] = [profile_filepath]
            with open(package_filepath, 'w') as outfile:
                json.dump(data2, outfile)
        else:  #neu file co ton tai
            with open(package_filepath) as json_file:
                data_dict = json.load(json_file)
                print(data_dict)
            #profile_package_name = data_dict['package_name']
            profiles_filepath = data_dict['profiles_filepath']
            if profile_filepath not in profiles_filepath:
                profiles_filepath.append(profile_filepath)

            data2['profiles_filepath'] = profiles_filepath
        #save new package profile or update it with new data
        with open(package_filepath, 'w') as outfile:
            json.dump(data2, outfile)
        text_var_status.set(text_var_status.get() + " | Package to: " +
                            get_filename(package_filename))
        #text_var_status.set("Save profile to "+ profile_filename)
        #####################

    #update option menu profile with new profile
    global om_profile_var
    om_profile_var.set('')
    profile_select['menu'].delete(0, 'end')
    #profile_files = ls_name(os.path.join(home_path,'profile'))
    profile_files = ls_name(
        os.path.join(home_path, 'profile'),
        e='profile_package')  #filter child folder profile_package
    #ls_name(folder_path, except_filter_folder="")
    new_choices = profile_files
    for choice in new_choices:
        profile_select['menu'].add_command(label=choice,
                                           command=tk._setit(
                                               om_profile_var, choice))
    ##############

    ########
    #update new profile package
    global om_profile_package_var
    om_profile_package_var.set('')
    profile_package_select['menu'].delete(0, 'end')
    #profile_files = ls_name(os.path.join(home_path,'profile'))
    profile_package_files = ls_name(
        os.path.join(home_path, 'profile',
                     'profile_package'))  #no filter folder
    new_choices = profile_package_files
    for choice in new_choices:
        profile_package_select['menu'].add_command(label=choice,
                                                   command=tk._setit(
                                                       om_profile_package_var,
                                                       choice))
示例#8
0
        call_moshell(dcgm_path)
    else:
        print("okay, no moshell")


print("extract path, nodename, date, sw")
headers = ["path", "nodename", "date", "sw"]
data = []

count = 0
for path in dcgm_paths:
    nodename = "#"
    date = "#"
    sw = "#"
    count += 1
    filename = get_filename(path)

    #find node name
    regex = "(\S+)_modump.zip"
    m = re.search(regex, filename)
    if m:
        nodename = m.group(1)

    regex = "(\w+)_(\d{6}_\d{6})\S+(CXP\S+)_dcgm.zip"  #"gHI03733T_190417_185011_+07_MSRBS-N_CXP2010045-1_R17B32_dcgm.zip"
    m = re.search(regex, filename)
    if m:
        nodename = m.group(1)
        #date = m.group(2)
        sw = m.group(3)

    #tim ngay thang
示例#9
0
def loadprofile_procedure(profile_filepath):
    print("---------------------creating script for profile", profile_filepath,
          "--------------------")
    print_to_textbox("---------------------------")
    print_to_textbox("creating script for profile:" +
                     get_filename(profile_filepath))
    text_var_status.set("loading " + get_filename(profile_filepath))
    with open(profile_filepath) as json_file:
        data_dict = json.load(json_file)
        print(data_dict)
        template_filepath = data_dict["template_filepath"]
        input_file_path = data_dict["input_file_path"]
        selected_sheet = data_dict["selected_sheet"]
        folder_tag = data_dict["folder_tag"]
        foldersplit = data_dict['foldersplit']
        filenametag = data_dict['filenametag']
        mergefile = data_dict['mergefile']

        input_filename = get_filename(input_file_path)[:20]

    print("selected_sheet:", selected_sheet)
    df = pd.read_excel(input_file_path, header=0, sheet_name=selected_sheet)
    print("SUMMARY DATA INPUT TABLE:")
    print(df)
    global column_header
    column_headers = list(df.columns.values)

    f = open(template_filepath, "r")
    input_file_content = f.read()

    f.close()

    folder, template_filename = os.path.split(template_filepath)

    #find all variable in template
    with open(template_filepath) as infile:
        lines = infile.readlines()

        #find all variable string
        var_set = set()
        for index, line in enumerate(lines):
            line = line.strip()
            regex = '\$' + "(\w+)"
            variables = re.findall(regex, line)
            for item in variables:
                var_set.add(item)
            #####
            regex2 = '\$\{' + "(\w+)" + '\}'
            variables2 = re.findall(regex2, line)
            #print(variables2)
            for item in variables2:
                var_set.add(item)
            #####
        print("variables in ", template_filename)
        print(var_set)

    #substitute data from each row of excel into to template
    for index, row in df.iterrows():
        data = {}
        for column_name in var_set:
            if column_name in column_headers:
                #print(index,column_name,row[column_name])
                data[column_name] = row[column_name]
        #print(data)
        rowname = filenametag

        src = Template(input_file_content)
        result = src.substitute(data)

        #split script follow folder
        templatename, ext = get_file_name_ext(template_filename)
        if not foldersplit:
            output_filepath = os.path.join(
                home_path, "output_script",
                templatename + "_" + str(row[rowname]) + "." + ext)
        else:
            #neu folder ko ton tai thi tao them folder
            if not os.path.exists(
                    os.path.join(home_path, "output_script",
                                 str(row[folder_tag]))):
                os.mkdir(
                    os.path.join(home_path, "output_script", row[folder_tag]))

            #rule dat script name, co row, co template name
            output_filepath = os.path.join(
                home_path, "output_script", row[folder_tag],
                templatename + "_" + row[rowname] + "." + ext)

        if mergefile:
            output_text_file = open(output_filepath, "a")
        else:
            output_text_file = open(output_filepath, "w")
        output_text_file.write(result)
        output_text_file.close()
        print("write successful", data, "to", output_filepath)
示例#10
0
def loadprofile():
    print("loadprofile press!!!")
    log_textbox.delete("1.0", "end")
    #print profile name to text box
    print_to_textbox("profile selected:")
    profile_filename_selected = om_profile_var.get()
    print_to_textbox(profile_filename_selected)

    #open profile to load content, load back data to variable
    global home_path
    with open(os.path.join(home_path, "profile",
                           profile_filename_selected)) as json_file:
        data_dict = json.load(json_file)
        print(data_dict)
        #print_to_textbox(str(data_dict))
        template_filepath = data_dict["template_filepath"]
        input_file_path = data_dict["input_file_path"]
        selected_sheet = data_dict["selected_sheet"]
        folder_tag = data_dict["folder_tag"]
        foldersplit = data_dict['foldersplit']
        filenametag = data_dict['filenametag']
        mergefile = data_dict['mergefile']

        print_to_textbox("Excel Input: " + get_filename(input_file_path))
        print_to_textbox("Sheet Name: " + selected_sheet)
        print_to_textbox("Template Input: " + get_filename(template_filepath))
        print_to_textbox("Folder_tag: " + folder_tag)
        print_to_textbox("foldersplit: " + str(foldersplit))
        print_to_textbox("filenametag: " + str(filenametag))
        print_to_textbox("mergefile: " + str(mergefile))

        print_to_textbox("-------------------------")

        input_filename = get_filename(input_file_path)[:20]
        btn_browse_text.set(input_filename)
        browse_button.configure(bg='green2')

    ################
    #selected_sheet = om_var.get()
    print("selected_sheet:", selected_sheet)

    df = pd.read_excel(input_file_path, header=0, sheet_name=selected_sheet)
    print("SUMMARY DATA INPUT TABLE:")
    print(df)
    global column_header
    column_headers = list(df.columns.values)

    f = open(template_filepath, "r")
    input_file_content = f.read()

    f.close()

    folder, template_filename = os.path.split(template_filepath)

    #####
    #find all variable in template
    with open(template_filepath) as infile:
        lines = infile.readlines()

        #find all variable string
        var_set = set()
        for index, line in enumerate(lines):
            line = line.strip()
            regex = '\$' + "(\w+)"
            variables = re.findall(regex, line)
            for item in variables:
                var_set.add(item)

            #####
            regex2 = '\$\{' + "(\w+)" + '\}'
            variables2 = re.findall(regex2, line)
            print(variables2)
            for item in variables2:
                var_set.add(item)
            #####

        print(
            "----------------all variable string_from load profile--------------"
        )
        print(
            var_set
        )  #{'smtcOffset', 'smtcPeriodicity', 'smtcDuration', 'smtcScs', 'arfcnValueNRDl', 'nRFrequencyId'}

    #####

    for index, row in df.iterrows():
        data = {}
        for column_name in var_set:
            if column_name in column_headers:
                #print(index,column_name,row[column_name])
                data[column_name] = row[column_name]
        print(data)
        #will turning later
        #rowname = "row"+str(index)
        rowname = filenametag

        src = Template(input_file_content)
        result = src.substitute(data)
        #home_path = os.path.dirname(os.path.realpath(__file__))
        #output_filepath=os.path.join(home_path, "output_script", str(rowname))

        #split script follow folder
        templatename, ext = get_file_name_ext(template_filename)
        if not foldersplit:
            #output_filepath = os.path.join(home_path, "output_script",template_filename + "_" + rowname)
            output_filepath = os.path.join(
                home_path, "output_script",
                templatename + "_" + str(row[rowname]) + "." + ext)
        else:
            #neu folder ko ton tai thi tao them folder
            if not os.path.exists(
                    os.path.join(home_path, "output_script",
                                 str(row[folder_tag]))):
                os.mkdir(
                    os.path.join(home_path, "output_script", row[folder_tag]))

            #rule dat script name, co row, co template name
            #output_filepath = os.path.join(home_path, "output_script",row[folder_tag], template_filename + "_" + rowname)
            output_filepath = os.path.join(
                home_path, "output_script", row[folder_tag],
                templatename + "_" + row[rowname] + "." + ext)

        #if var_mergefile.get():
        if mergefile:

            output_text_file = open(output_filepath, "a")
        else:
            output_text_file = open(output_filepath, "w")
        output_text_file.write(result)
        output_text_file.close()
        print("write successful", data, "to", output_filepath)

        print_to_textbox("write successful script " + output_filepath)

    text_var_status.set("FINISHED!!!")
    #create_button.configure(bg="green2")
    loadprofile_button.configure(bg="green2")
    status_label.configure(bg="green2")