def rename_folder(self, oldname: str = None, newname: str = None, parent_folder: str = None) -> bool: """Rename email folder :param oldname: current folder name :param newname: new name for the folder :param parent_folder: name for the parent folder, by default INBOX :return: True if operation was successful, False if not """ if oldname is None or newname is None: raise KeyError( "'oldname' and 'newname' are required for rename folder") if parent_folder is None or parent_folder is self.account.inbox: parent = self.account.inbox else: parent = self.account.inbox / parent_folder self.logger.info( "Rename folder '%s' to '%s'", oldname, newname, ) items = self._get_all_items_in_folder(oldname, parent_folder) old_folder = Folder(parent=parent, name=oldname) old_folder.name = newname old_folder.save() items.move(to_folder=parent / newname) self.delete_folder(oldname, parent_folder)
def create_subfolder(account, parent, folder): folders = folder.split("/") # Any subfolders left to process? if folders[0] != "": # Refresh Inbox account.root.refresh() # Check for Folder check = get_folder(account.inbox, parent + "/" + folders[0]) p_folder = get_folder(account.inbox, parent) if check is None: # Folder doesn't exist try: f = Folder(parent=p_folder, name=folders[0]) f.save() f = None except errors.ErrorFolderExists: print("Tried to create folder: " + parent + "/" + folders[0]) print("It already exists!") cui.pause() # Process Next SubFolder parent = parent + "/" + folders[0] folders.pop(0) create_subfolder(account, parent, "/".join(folders))
def create_folders(self, folder_list: List[str], base_folder_path: str = None): """Create folders in the mailbox. Parameters ---------- folder_list : list Create folders in the mailbox base_folder_path : str New folders will be created inside at path base_folder_path (Defaults to inbox) """ self.folder_list = folder_list # Setup base folder base_folder = self._get_mailbox_path(base_folder_path) # Check existing folders existing_folders = [f.name for f in base_folder.children] # Create new folders base_folder_name = base_folder_path or "Inbox" for folder_name in folder_list: if folder_name not in existing_folders: f = Folder(parent=base_folder, name=folder_name) f.save() logger.info( f"Created subfolder {folder_name} in folder {base_folder_name}" )
def create_folder(account, folder): folders = folder.split("/") account.root.refresh() f_inst = get_folder(account.inbox, folder) if f_inst is None: check = get_folder(account.inbox, folders[0]) if check is None: f = Folder(parent=account.inbox, name=folders[0]) f.save() f = None parent = folders[0] folders.pop(0) create_subfolder(account, parent, "/".join(folders))
def create_folder(self, folder_name: str = None, parent_folder: str = None) -> bool: """Create email folder :param folder_name: name for the new folder :param parent_folder: name for the parent folder, by default INBOX :return: True if operation was successful, False if not """ if folder_name is None: raise KeyError("'folder_name' is required for create folder") if parent_folder is None or parent_folder is self.account.inbox: parent = self.account.inbox else: parent = self.account.inbox / parent_folder / folder_name self.logger.info( "Create folder '%s'", folder_name, ) new_folder = Folder(parent=parent, name=folder_name) new_folder.save()
def set_isolate_folder(self,folder='isolate',task_id=''): """ param folder: str, folder name return: None """ isolate_folder = None try: try: isolate_folder = self.admin_account.inbox / folder #i_folder = self.admin_account.inbox.glob(folder) #print('i_folder=',i_folder,i_folder._folders) if self.logger!=None: self.logger.info('isolated folder exist:{}'.format(isolate_folder)) except Exception as e: f = Folder(parent=self.admin_account.inbox, name=folder) print 'isolate_folder=',f f.save() f.name = folder f.save() isolate_folder = f print 'isolate_folder=',f if self.logger!=None: self.logger.info('create isolated folder: inbox/{0}'.format(folder)) if task_id: #create isolated folder based on task if not exist try: task_fold = f / task_id isolate_folder = task_fold if self.logger!=None: self.logger.info('folder-task_id={0} already existed'.format(task_id)) except Exception as e: t = Folder(parent=isolate_folder, name=task_id) t.save() t.name = task_id t.save() isolate_folder = t if self.logger!=None: self.logger.info('Create folder-task_id={0}.'.format(task_id)) else: pass except Exception as e: if self.logger!=None: self.logger.info(e) self.logger.info('this_isolated_folder={0}.'.format(isolate_folder)) self.isolate_folder = isolate_folder return
def _build_folder(self, main_folder): """Builds folder tree for email username@domain [inbox [to_send :param main_folder: (string) The address that will be the NAME of the new folder (NOT THE ACCOUNT THAT THE NEW FOLDER WILL BE MADE IN) :return: (folder,folder) Returns two folders: inbox folder, tosend folder """ # This will hold the folder object f = '' #Check for matching folders: # We are going to assume that there are two subfolders (inbox, to_send) if the folder already exists. If the folder is # made in this script, then that will be the case f_inbox = "" f_tosend = "" print("The main_folder is: " + main_folder) for item in self._red_account.inbox.glob(main_folder): # If the folder exists, set the target folders to the ones that are already there. if item.name == main_folder: f = item for kid in f.children: if kid.name == "inbox": f_inbox = kid elif kid.name == "to_send": f_tosend = kid return f_inbox, f_tosend # If the folder doesn't exist, then make it, and it's subfolders f = Folder(parent=self._red_account.inbox, name=main_folder) f.save() f_inbox = Folder(parent=f, name="inbox") f_inbox.save() f_tosend = Folder(parent=f, name="to_send") f_tosend.save() return f_inbox, f_tosend
鈹斺攢鈹� archive 鈹溾攢鈹� Last Job 鈹溾攢鈹� exchangelib issues 鈹斺攢鈹� Mom ''' # Folders have some useful counters: a.inbox.total_count a.inbox.child_folder_count a.inbox.unread_count # Update the counters a.inbox.refresh() # Folders can be created, updated and deleted: f = Folder(parent=a.inbox, name='My New Folder') f.save() f.name = 'My New Subfolder' f.save() f.delete() # Delete all items in a folder f.empty() # Also delete all subfolders in the folder f.empty(delete_sub_folders=True) # Recursively delete all items in a folder, and all subfolders and their content. This is # like `empty(delete_sub_folders=True)` but attempts to protect distinguished folders from # being deleted. Use with caution! f.wipe() ###Dates, datetimes and timezones