from libs.ext_mngr import glob,os from pathlib import Path from libs.var_mngr import unique # def is_path_exist(fil_path, empty=0): if os.path.exists(fil_path) and (empty or not os.stat(fil_path).st_size == 0): return 1 return 0 def get_using_dict(list1, dir2, ext=".rtcl"): files = [f[:-len(ext)] for f in list_dir(dir2) if len(f) > len(ext) and ext == f[-len(ext):]] dic = dict() for ar in load_files(list1): try: dic.update({eval(ar)[0]:eval(ar)[1]}) except: pass return [[fil,dic[fil]] for fil in files if fil in dic.keys()] # def con_write(fil_path, data, header=1): make_path(fil_path, is_file=1) with open(fil_path, 'a',newline='\n',encoding='utf-8-sig') as f2s: if header: f2s.write("#[TIME]: " + str( + "\n") for ind in data: f2s.writelines(str(ind) + '\n') f2s.close() return 0 # def correct_path(*elems): path = os.path.join(*elems) if path and not path[-1] == "/": path = path + "/" return path # def correct_fpath(*elems): return correct_path(*elems)[:-1] # def del_path(path): if os.path.isfile(path): os.remove(path) elif os.path.isdir(path): shutil.rmtree(path) return 0 # def load_files(file_name, as_it=0, recursive=0): if type(file_name) == str: if os.path.isdir(file_name) and recursive: file_name = correct_path(file_name) + "**/*.*" files = [f for f in glob.iglob(file_name, recursive=recursive)] else: files = file_name data = load_files_helper(files, as_it) if as_it: if len(data): return "\n\n".join(data) else: return "" return data # def load_files_helper(paths, as_it=0): lines = [] for path in paths: files = [] if os.path.isdir(path): for _, _, f in os.walk(path): for fil in f: files.append(correct_fpath(path, fil)) elif os.path.isfile(path): files.append(path) for fil in files: data = load_txt(fil, as_it=as_it) lines.extend(data) return unique(lines) # def load_txt(file_path, as_it=0): with open(file_path, 'r', encoding='utf-8') as txt_fil: # , encoding='ISO-8859-1' data = txt_fil.close() if len(data) > 1 and "\ufeff" == data[0]: data = data[1:] if as_it: data = [data] else: data = data.splitlines() data = [dta for dta in data if not "[TIME]: " in dta and not dta == ""] return data # def make_path(path, is_file=0): path = os.path.dirname(path) if path and not os.path.isdir(path): os.makedirs(path, exist_ok=True) # def test_folder_in_dir(main_dir, folder): return [path for path in Path(main_dir).rglob(folder) if is_path_exist(correct_fpath(path, "prog.txt"))] # path = glob.glob(correct_path(main_dir, folder)) # if not path: # path = glob.glob(correct_path(main_dir, "**", folder)) def is_dir(path): return os.path.isdir(path) # def list_dir(path): if path: if is_dir(path) and not path[-1] == os.sep: path += os.sep return [p[len(path):] + os.sep if len(p) and is_dir(p) and p[-1] != os.sep else p[len(path):] for p in glob.glob(path + '*')] else: return None def list_files(path): txtfiles = [] for file in glob.glob(path): txtfiles.append(file) def current_dir(): return os.getcwd() # def home_dir(): return os.path.expanduser("~") def force_exit(): os._exit(1)