def path_l_remove(path_l, to_remove_str_or_l, removal_mode='basename_equals'): eu.error_if_param_type_not_in_whitelist(path_l, ['list', 'tuple']) eu.error_if_param_type_not_in_whitelist(to_remove_str_or_l, ['list', 'tuple', 'str']) eu.error_if_param_key_not_in_whitelist(removal_mode, [ 'basename_equals', 'in_basename', 'paths_equal', 'in_path', 'starts_with', 'is_component_name' ]) if isinstance(to_remove_str_or_l, str): to_remove_str_or_l = [to_remove_str_or_l] def path_l_remove__basename_equals(): return [ path for path in path_l if not get_basename_from_path(path) in to_remove_str_or_l ] def path_l_remove__if_not_paths_compare(): return [ path for path in path_l if not paths_compare( path, to_remove_str_or_l, compare_mode=removal_mode) ] if removal_mode == 'basename_equals': return path_l_remove__basename_equals() elif removal_mode in ['paths_equal', 'starts_with', 'is_component_name']: return path_l_remove__if_not_paths_compare() else: raise Exception('ERROR: NOT IMPLEMENTED')
def set_tool_bar_image_to_match_iconphoto_if_exists( file_obj, want_duplicate_apps_to_stack_in_toolbar=True): ''' gtu.set_tool_bar_image_to_match_iconphoto_if_exists(__file__, want_duplicate_apps_to_stack_in_toolbar = True) If no iconphoto is set, all this does is set the app_id based on input. ''' # eu.error_if_not__file__(file_obj) # this broke when tried to make into an app: ERROR: Can't be __file__ Because File Does Not Exist: "C:\projects\version_control_scripts\CE\app\dist\main\GUI.pyc" must point to an existing file. eu.error_if_param_type_not_in_whitelist( want_duplicate_apps_to_stack_in_toolbar, ['bool']) app_id = get_app_id_unique_to_this_file( file_obj, want_duplicate_apps_to_stack_in_toolbar) set_app_id(app_id)
def root_msg_box(type_num, title, msg, icon, output_define_d, app_id): eu.error_if_param_type_not_in_whitelist(msg, ['str']) eu.error_if_param_type_not_in_whitelist(icon, ['str', 'NoneType']) eu.error_if_param_key_not_in_whitelist(icon, [None] + list(ICON_KEY_TYPE_NUM_D.keys())) eu.error_if_param_type_not_in_whitelist(output_define_d, ['dict', 'NoneType']) eu.error_if_param_type_not_in_whitelist(app_id, ['str', 'NoneType']) # add icon if given if icon != None: type_num = type_num | ICON_KEY_TYPE_NUM_D[icon] # sets tool bar icon to match parent's if any if app_id != None: ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id) # create msg_box MessageBox = ctypes.windll.user32.MessageBoxW out_num = MessageBox(None, msg, title, type_num) out_str = BTN_NUM_NAME_D[out_num] # change return if output_define_d given if output_define_d == None: return out_str else: if out_str in output_define_d.keys(): return output_define_d[out_str] else: raise Exception( 'ERROR: "' + out_str + '" returned by the msg box is not a key in given output_define_d: ' + output_define_d)
def set_iconphoto_if_not_None(master, img_path): if img_path == None: return eu.error_if_param_type_not_in_whitelist(master, ['tkinter.Tk', 'tkinter.Toplevel']) # eu.error_if_not_is_file(img_path) # this broke when tried to make into an app: ERROR: Can't be __file__ Because File Does Not Exist: "C:\projects\version_control_scripts\CE\app\dist\main\GUI.pyc" must point to an existing file. img_path_ext = os.path.splitext(img_path)[1] if img_path_ext == '.ico': master.iconbitmap(img_path) else: photo_img = PhotoImage(file=img_path) master.iconphoto(master, photo_img)
def path_l_to_path_basename_ntl(path_l): eu.error_if_param_type_not_in_whitelist(path_l, ['list', 'tuple', 'str']) # correct to list if given single path if isinstance(path_l, str): path_l = [path_l] # fill ntl path_basename_ntl = [] for path in path_l: # print(path)#`````````````````````````````````````````````````````````` basename = get_basename_from_path(path) path_basename_ntl.append(Path_basename_nt(path, basename)) return path_basename_ntl
def set_child_tk_gui_iconphoto_and_app_id(master, photo_img_path, app_id): ''' Parent GUI does not need to pass it's photo_img_path if it passes it's app_id - ONLY IF USING ICONPHOTO (png) can work with either .png or .ico, but if you use a .ico, you need to pass the photo_img_path down to all sub-guis, no clue why but will only inherit iconphoto (png), not iconbitmap(ico) from gui with same app_id ''' if photo_img_path != None: # eu.error_if_not_is_file(photo_img_path) # this broke when tried to make into an app: ERROR: Can't be __file__ Because File Does Not Exist: "C:\projects\version_control_scripts\CE\app\dist\main\GUI.pyc" must point to an existing file. pass eu.error_if_param_type_not_in_whitelist(app_id, ['str', 'NoneType']) if app_id != None: set_app_id(app_id) set_iconphoto_if_not_None(master, photo_img_path)
def copy_object_to_dest_then_rename(path_str, dest_parent_dir_path, new_object_name, copy_dir_content=True): """ Deprecated: Do Not Touch! Just use copy_object_to_path instead """ eu.error_if_param_type_not_in_whitelist(path_str, ['str']) # only 1 at a time eu.error_if_param_type_not_in_whitelist(new_object_name, ['str']) copy_objects_to_dest(path_str, dest_parent_dir_path, copy_dir_content) basename = get_basename_from_path(path_str, include_ext=True) og_dest_obj_path = os.path.join(dest_parent_dir_path, basename) new_dest_obj_path = os.path.join(dest_parent_dir_path, new_object_name) rename_file_overwrite(og_dest_obj_path, new_dest_obj_path)
def copy_objects_to_dest(path_l_or_str, dest_parent_dir_path, copy_dir_content=True): eu.error_if_param_type_not_in_whitelist(path_l_or_str, ['str', 'list']) eu.error_if_param_type_not_in_whitelist(dest_parent_dir_path, ['str']) eu.error_if_param_type_not_in_whitelist(copy_dir_content, ['bool']) def ig_f(dir, files): return [f for f in files if os.path.isfile(os.path.join(dir, f))] if isinstance(path_l_or_str, str): path_l_or_str = [path_l_or_str] make_dir_if_not_exist(dest_parent_dir_path) for path in path_l_or_str: eu.error_if_not_is_file_or_is_dir(path) if os.path.isdir(path): path_basename = get_basename_from_path(path) dest_dir_path = dest_parent_dir_path + '//' + path_basename delete_if_exists(dest_dir_path) if copy_dir_content: shutil.copytree(path, dest_dir_path) else: shutil.copytree(path, dest_dir_path, ignore=ig_f) elif os.path.isfile(path): shutil.copy(path, dest_parent_dir_path)
def get_app_id_unique_to_this_file(file_obj, want_duplicate_apps_to_stack_in_toolbar=True ): ''' gtu.get_app_id_unique_to_this_file(__file__) if 2 GUIs use the same iconphoto, but have different app_ids, they will show as 2 different applications in the tool bar, if they use the same app_id, their application windows will stack in the tool bar ''' # eu.error_if_not__file__(file_obj) # this broke when tried to make into an app: ERROR: Can't be __file__ Because File Does Not Exist: "C:\projects\version_control_scripts\CE\app\dist\main\GUI.pyc" must point to an existing file. eu.error_if_param_type_not_in_whitelist( want_duplicate_apps_to_stack_in_toolbar, ['bool']) if want_duplicate_apps_to_stack_in_toolbar: raw = '_app_id__' + os.path.dirname( os.path.abspath(file_obj)) + '__app_id_' # arbitrary string else: raw = '_app_id__{}__{}__app_id_'.format( os.path.dirname(os.path.abspath(__file__)), os.getpid()) # arbitrary string return raw.replace('\\', '//')
def rename_dir_contents(dir_path, replace_d, object_type='all', recurs_dirs=False): ''' Only renames basenames ''' eu.error_if_param_type_not_in_whitelist(dir_path, ['str']) eu.error_if_not_is_dir(dir_path) eu.error_if_param_type_not_in_whitelist(replace_d, ['dict']) eu.error_if_param_type_not_in_whitelist(object_type, ['str']) eu.error_if_param_key_not_in_whitelist(object_type, ['all', 'file', 'dir']) eu.error_if_param_type_not_in_whitelist(recurs_dirs, ['bool']) raw_dir_content_abs_path_l = get_dir_content_l(dir_path, object_type, 'abs_path', recurs_dirs) # must sort list by length so you don't rename a dir above another object sorted_dir_content_abs_path_l = sorted(raw_dir_content_abs_path_l, key=len, reverse=True) sorted_dir_content_abs_path_path_basename_ntl = path_l_to_path_basename_ntl( sorted_dir_content_abs_path_l) for sorted_dir_content_abs_path_path_basename_nt in sorted_dir_content_abs_path_path_basename_ntl: new_basename = sorted_dir_content_abs_path_path_basename_nt.basename for replace_str, replace_with_this_str in replace_d.items(): new_basename = new_basename.replace(replace_str, replace_with_this_str) # only rename if something was replaced if new_basename != sorted_dir_content_abs_path_path_basename_nt.basename: parent_dir_path = os.path.dirname( sorted_dir_content_abs_path_path_basename_nt.path) new_path = os.path.join(parent_dir_path, new_basename) rename_file_overwrite( sorted_dir_content_abs_path_path_basename_nt.path, new_path)
def copy_object_to_path(src_path_str, dest_path_str, copy_dir_content=True): """ Copies the file or dir at src_path_str to dest_path_str """ eu.error_if_param_type_not_in_whitelist(src_path_str, ['str']) # only 1 at a time eu.error_if_param_type_not_in_whitelist(dest_path_str, ['str']) eu.error_if_param_type_not_in_whitelist(copy_dir_content, ['bool']) eu.error_if_not_is_file_or_is_dir(src_path_str) if os.path.isfile(src_path_str): shutil.copy(src_path_str, dest_path_str) elif os.path.isdir(src_path_str): raise Exception("ERROR: NOT YET IMPLEMENTED") # shutil.copy(src_path_str, dest_path_str) # does not work, perm error else: raise Exception("ERROR: How did you even get here?")