def __init__(self, path: str): self.id = next(self._ids) self.orig_path = path self.real_path = path self.is_enc = self.is_path_enc(path) self.is_file = os.path.isfile(legalize_path(path)) self.dec_name = path if not self.is_enc else None self.enc_name = path if self.is_enc else None
def enc_file(self, path: str, remove_old=False): try: new_path = self.enc_dec.encrypt(path) except ValueError: print(f"Bad Password for {path}") return None if new_path and remove_old and path != new_path: os.remove(legalize_path(path)) return new_path
def allfiles(path: str) -> (list, list): allFiles = [] allFolders = [] for root, subfiles, files in os.walk(legalize_path(path), topdown=False): for names in files: allFiles.append(os.path.join(root, names)) for folder in subfiles: allFolders.append(os.path.join(root, folder)) return allFiles, allFolders
def _launch_single_enc_dec(self, enc: bool, remove_old: bool, path: EncPath, sema: Semaphore, send_msg: connection.PipeConnection): f_orig_path = path.real_path res = "" try: res = path.encrypt(self.enc_dec) if enc else path.decrypt( self.enc_dec) if path.is_file and res and remove_old and res != f_orig_path: os.remove(legalize_path(f_orig_path)) except ValueError as e: print(f"Bad Password for file: {f_orig_path}") finally: if send_msg: send_msg.send(res) if sema: sema.release()
def enc_files(self, remove_old=False): paths = self.split_to_types() # Start with encrypting files existing_enc_files = [ p.get_dec_name(self.enc_dec) for p in paths['enc_file_list'] ] # Work only on files that doesnt have encrypted version yet files_to_enc = [ p for p in paths['norm_file_list'] if p.get_dec_name(self.enc_dec) not in existing_enc_files ] # TODO: maybe make this list subtraction with implementing == on EncPath class if len(files_to_enc) != len(paths['norm_file_list']) and self.verbose: files_not_to_enc = [ p for p in paths['norm_file_list'] if p.get_dec_name(self.enc_dec) in existing_enc_files ] pp = pprint.PrettyPrinter(indent=4, width=300) print("Files already encrypted:") pp.pprint([p.get_dec_name(self.enc_dec) for p in files_not_to_enc]) # TODO: implement the __str__ and __repr__ for EncPath to make this easier to print self._enc_dec_list(files_to_enc, enc=True, remove_old=remove_old) # delete orig files if already had encrypted version and we want to remove old if remove_old: files_to_clear = [ p for p in paths['norm_file_list'] if p.get_dec_name(self.enc_dec) in existing_enc_files ] for path in files_to_clear: f_orig_path = path.real_path os.remove(legalize_path(f_orig_path)) # TODO: make sure to update path with correct enc path now, or remove from list because file was deleted # path.update_new_path(path.) # Now encrypt folder names self._enc_dec_folders(paths['norm_folder_list'], enc=True)