def decrypt_epubx(epubx_path): with ZipFile(epubx_path, 'r') as myzip: param_files = fnmatch.filter(myzip.namelist(), "mash") if len(param_files) == 0: param_files = fnmatch.filter(myzip.namelist(), "**/mash") if len(param_files) == 0: print("Warn: mash file not found. Ebook may be in different format. Resorting to bruteforcing magic offsets up to 2^16") magic_offsets = range(2**16) else: if len(param_files) > 1: print(f"Warn: more than one mash file found, using {param_files[0]}") magic_offsets = extract_parameters(myzip.open(param_files[0]).read(0x30)) for da2_path in fnmatch.filter(myzip.namelist(), "**/aText.dat"): print(f"Found da2 file {da2_path}") myzip.extract(da2_path) print("Attempting decryption (may take a while for large books due to slow implementation)") for magic_offset in magic_offsets: key = generate_key(da2_path, magic_offset) fout = decrypt_da2(da2_path, key) if fout is not None: print(f"Decrypted to {fout}") break else: print("Unable to decrypt :(")
def _clean_filenames(self, filenames): import os if self.options.exclude: # TODO do we have to take this from root dir? from glob import fnmatch for pattern in self.options.exclude: filenames.difference_update(fnmatch.filter(filenames, pattern)) return sorted(os.path.join(self.root, f) for f in filenames)
def _list_conda_packages(local_dir): """List the conda packages (*.tar.bz2 files) in `local_dir` Parameters ---------- local_dir : str Some local directory with (hopefully) some conda packages in it Returns ------- list List of conda packages in `local_dir` """ contents = os.listdir(local_dir) return fnmatch.filter(contents, "*.tar.bz2")
def find(topdir, filepat): for path, dirlst, filelst in os.walk(topdir): for name in fnmatch.filter(filelst, filepat): yield os.path.join(path, name)
from torch.utils.data import Dataset import numpy as np from torch import Tensor from glob import glob, fnmatch from sklearn.model_selection import train_test_split from PIL import Image data_path = '/home/aftaab/MylanDatasets/breast-histopathology-images/IDC_histopathology_data/**/*.png' all_files = glob(data_path, recursive=True) positives = fnmatch.filter(all_files, '*class1.png') negatives = fnmatch.filter(all_files, '*class0.png') print("No of files total={}".format(len(all_files))) print("No of positives={}".format(len(positives))) print("No of negatives={}".format(len(negatives))) train_files, test_files = train_test_split(all_files, test_size=0.05) class TrainDataset(Dataset): def __init__(self): self.train_files = train_files def __getitem__(self, index): file_name = self.train_files[index] img = np.array(Image.open(file_name).convert('RGB').resize([50, 50])) y = 0 if file_name in positives: y = 1 if file_name in negatives: y = 0
from PIL import Image import numpy as np from torchvision import transforms from torch.utils.data import Dataset from sklearn.model_selection import train_test_split from glob import glob, fnmatch data = glob('/home/aftaab/MylanDatasets/colorectal-histology-mnist/Patches/*/*.tif') positives = fnmatch.filter(data, '/home/aftaab/MylanDatasets/colorectal-histology-mnist/Patches/01_TUMOR/*.tif') negatives = [] for file in data: if file not in positives: negatives.append(file) print('Negatives=%d' % len(negatives)) print('Positives=%d' % len(positives)) train, test = train_test_split(data, test_size=0.2) class TrainDataset(Dataset): def __init__(self): super().__init__() self.train = train self.transform = transforms.Compose([transforms.ToPILImage(), transforms.Normalize([0, 0, 0], [1, 1, 1]), transforms.ToTensor()]) def __getitem__(self, index): img = Image.open(train[index], 'r').convert('RGB') img = img.resize([150, 150]) img = np.array(img).reshape(3, 150, 150)
def main(): print("\nS3 Browser - github.com/replon/s3b") print("============================================") boto_session = None try: if len(sys.argv) >= 2: try: boto_session = boto3.Session(profile_name=sys.argv[1]) except Exception: # botocore.exceptions.ProfileNotFound print( ConsoleColors.FAIL, "[FAIL] Cannot found AWS credential profile '" + sys.argv[1] + "'", ) print(" \nUsage: s3b [profile_name]\n") exit() else: boto_session = boto3.Session() if boto_session.get_credentials() is None: print(ConsoleColors.FAIL, "[FAIL] Cannot found default AWS access key.") print(" \nUsage: s3b [profile_name]\n") print( " \nCreate a file '~/.aws/credentials' and store your key like this.." ) print( "\n[default]\naws_access_key_id = your_access_key_id\naws_secret_access_key = " "your_secret_access_key\n ") exit() print("Connecting to S3 using...") print(" profile_name:", boto_session.profile_name) print(" aws_access_key_id:", boto_session.get_credentials().access_key) print(" aws_secret_access_key:", boto_session.get_credentials().secret_key) s3 = None bucket_names = [] try: s3 = boto_session.resource("s3") bucket_names = [bucket.name for bucket in s3.buckets.all()] except Exception: print(ConsoleColors.FAIL, "[FAIL] Unable to connect to S3 \nbye :(") exit() bucket_select = "" while not bucket_select.isnumeric() or int(bucket_select) not in range( len(bucket_names)): print("\nYour bucket list") for i, bucket in enumerate(bucket_names): print( ConsoleColors.BOLD + " [" + str(i) + "]" + ConsoleColors.END_COLOR, ConsoleColors.OK_GREEN + bucket + ConsoleColors.END_COLOR, ) bucket_select = input("\n" + ConsoleColors.BOLD + "select bucket(0-" + str(len(bucket_names) - 1) + "): " + ConsoleColors.END_COLOR) if bucket_select.strip() == "": print("Bye!") exit() print("============================================") bucket_name = bucket_names[int(bucket_select)] bucket = s3.Bucket(bucket_name) object_list = list(bucket.objects.all()) browser = dict() for obj in object_list: splited = obj.key.split("/") current = browser for i, foldername in enumerate(splited): if i + 1 == len(splited): # last one is filename if foldername != "": current[foldername] = obj.key else: if foldername not in current: current[foldername] = dict() current = current[foldername] current = browser current_path = "" parents = [] parents_path = [] def print_current_folder(limit=20): print() if len(current) == 0: print(" (directory is empty)") else: print("(" + str(len(current)) + " items)") for k, item in enumerate( sorted(current.keys(), key=lambda x: -int(type(current[x]) is dict))): if k == limit: print(" ...") print(" (command 'l' to see the full list)") break if type(current[item]) is dict: print( f' {ConsoleColors.BOLD}{item:30}{ConsoleColors.END_COLOR} {"<dir>":>10}' ) else: print( f" {ConsoleColors.BOLD}{item:30}{ConsoleColors.END_COLOR} {human_size(bucket.Object(current_path + item).content_length):>10}\t{str(bucket.Object(current_path + item).last_modified):} " ) print() print_current_folder() while True: cmd = input(ConsoleColors.OK_GREEN + "<" + bucket_name + "> " + ConsoleColors.END_COLOR + ConsoleColors.HEADER + "~/" + current_path + "$ " + ConsoleColors.END_COLOR) if cmd.startswith("!"): os.system(cmd[1:]) elif cmd.startswith("l"): print_current_folder(-1) continue elif cmd.startswith("cd"): splited = cmd.split() if len(splited) != 2: print("\n usage: cd dir_name\n") continue foldername = splited[1] if foldername.endswith("/"): foldername = foldername[:-1] if foldername == "..": if len(parents) > 0: current_path = parents_path.pop() current = parents.pop() elif foldername == "~": parents.clear() parents_path.clear() current = browser current_path = "" elif foldername in current and type( current[foldername]) is dict: parents.append(current) parents_path.append(current_path) current = current[foldername] current_path = current_path + foldername + "/" else: print( ConsoleColors.FAIL, "[FAIL] No such dir in the current path:", foldername, ) continue print("<" + bucket_name + "> ~/" + current_path + "$") print_current_folder() elif cmd.startswith("up"): splited = cmd.split() if len(splited) not in [2, 3]: print("\n usage: up local_file [remote_name]\n") continue filelist = glob(splited[1]) if len(filelist) == 0: print( ConsoleColors.FAIL, "[FAIL] No such file in local:", cmd.split()[1], ) elif len(filelist) == 1: filepath = filelist[0] if len(splited) == 3: filename = splited[2] else: filename = filepath.split("/")[-1] bucket.Object(current_path + filename).upload_file(filepath) current[filename] = current_path + filename print(ConsoleColors.OK_BLUE, "[SUCCESS] uploaded", filename) else: # multiple file upload if len(splited) == 3: print( ConsoleColors.FAIL, "[FAIL] Cannot set remote_name if the matched local files are more than one", ) print("\n usage: up local_file [remote_name]\n") continue for filepath in filelist: filename = filepath.split("/")[-1] bucket.Object(current_path + filename).upload_file(filename) current[filename] = current_path + filename print( ConsoleColors.OK_BLUE, "[SUCCESS] uploaded " + str(len(filelist)) + " files", ) elif cmd.startswith("down"): splited = cmd.split() if len(splited) not in [2, 3]: print("\n usage: down remote_file [local_name]\n") continue filename = splited[1] get_name = filename if len(splited) >= 3: get_name = splited[2] if filename in current and type(current[filename]) is str: bucket.Object(current[filename]).download_file(get_name) print(ConsoleColors.OK_BLUE, "[SUCCESS] downloaded ", get_name) else: matched_list = fnmatch.filter( filter(lambda x: type(current[x]) is str, current.keys()), filename, ) if len(matched_list) > 0: # multiple file download if len(splited) >= 3: print( ConsoleColors.FAIL, "[FAIL] Cannot set local_name if the matched remote files are more than one", ) print("\n usage: down remote_file [local_name]\n") continue print("downloading " + str(len(matched_list)) + " matched files") for name in matched_list: bucket.Object(current[name]).download_file(name) print(ConsoleColors.OK_BLUE, "[SUCCESS] done!") else: print( ConsoleColors.FAIL, "[FAIL] No such file in the current path:", filename, ) elif cmd.startswith("mkdir"): splited = cmd.split() if len(splited) != 2: print("\n usage: mkdir dir_name\n") continue new_folder_name = splited[1] bucket.put_object(Key=current_path + new_folder_name + "/") current[new_folder_name] = dict() print_current_folder() elif cmd.startswith("rm"): splited = cmd.split() if len(splited) != 2: print("\n usage: rm file_or_dir_name\n") continue filename = splited[1] if filename in current: if type(current[filename]) is str: if (input(" Are you sure you want to delete " + filename + "? (y/n) ").lower() == "y"): bucket.Object(current[filename]).delete() current.pop(filename) print("deleted " + filename) print_current_folder() elif type(current[filename]) is dict: if (input( " '" + filename + "/' is a directory. Are you sure you want to delete it? (y/n) " ).lower() == "y"): cnt = 0 for obj in filter( lambda x: x.key.startswith(current_path + filename + "/"), list(bucket.objects.all()), ): obj.delete() cnt += 1 current.pop(filename) print("deleted " + str(cnt) + " files") print_current_folder() else: matched_list = fnmatch.filter( filter(lambda x: type(current[x]) is str, current.keys()), filename, ) if len(matched_list) > 0: if (input(" Are you sure you want to delete " + str(len(matched_list)) + " file(s)? (y/n) ").lower() == "y"): for name in matched_list: bucket.Object(current[name]).delete() current.pop(name) print("deleted " + str(len(matched_list)) + " files") print_current_folder() else: print( ConsoleColors.FAIL, "[FAIL] No such file in the current path:", filename, ) elif cmd.startswith("exit") or cmd.startswith("q"): break elif cmd.strip() == "": continue else: print( "\n commands : cd, l=ls, mkdir, rm, up=upload, down=download, q=exit\n" ) except KeyboardInterrupt: pass print("\nBye :)")
def get_files(path, pattern): """Return all the files on a path that follow the patterns.""" for dirpath, dirnames, filenames in os.walk(path): for f in fnmatch.filter(filenames, pattern): yield os.path.join(dirpath, f)
def get_same_files(path, pattern): """Return all files in a directory with the same pattern.""" for dirpath, dirnames, filenames in os.walk(path): for f in fnmatch.filter(filenames, pattern): yield os.path.join(dirpath, f)