Ejemplo n.º 1
0
 def __init__(self, dirty_path='E:\\room\\forawhile',clean_path='E:\\room\\libs'):
     # 设定类对象的变量
     # 待整理的文件目录
     self.__dirty_path = Path(dirty_path)
     # 整理完成的文件目录
     self.__clean_path = Path(clean_path)
     # 待移动的文件名
     self._to_be_moved = deque()
     # PathTraversal类对象,用来遍历整理完成的文件目录,并把相关信息更新到数据库
     self.__path_traversal = PathTraversal(clean_path)
     # 文件路径数据库
     self.__path_db = PathDB()
     # 文件数据库
     self.__file_db = FileDB()
Ejemplo n.º 2
0
    def __init__(self, absolute_path):
        # 设定类对象的变量
        # 设置文件库路径名称
        self.__path = Path(absolute_path)

        # 初始化数据库集合
        self.path_db = PathDB()
        self.file_db = FileDB()
        self.tag_db = TagDB()

        # path,file和tag数据库中所有文档的_id集合
        self.path_set = set(item['_id'] for item in self.path_db.collection.find({},{'_id':1}))
        self.file_set = set(item['_id'] for item in self.file_db.collection.find({},{'_id':1}))
        self.tag_set = set(item['_id'] for item in self.tag_db.collection.find({},{'_id':1}))
Ejemplo n.º 3
0
class FileRobot:
    """ FileRobot类用来进行文件整理、归档和查询工作

    """
    def __init__(self, dirty_path='E:\\room\\forawhile',clean_path='E:\\room\\libs'):
        # 设定类对象的变量
        # 待整理的文件目录
        self.__dirty_path = Path(dirty_path)
        # 整理完成的文件目录
        self.__clean_path = Path(clean_path)
        # 待移动的文件名
        self._to_be_moved = deque()
        # PathTraversal类对象,用来遍历整理完成的文件目录,并把相关信息更新到数据库
        self.__path_traversal = PathTraversal(clean_path)
        # 文件路径数据库
        self.__path_db = PathDB()
        # 文件数据库
        self.__file_db = FileDB()

    def scan_and_copy(self):
        """ 扫描待整理的文件目录,要任务是打包目录或者更新文件名,然后复制到目标目录

        :return: 无返回值
        """
        packed = set()
        special_path = set()

        # 利用os.walk函数遍历文件夹
        for path, _, files in os.walk(self.__dirty_path.absolute_path):
            path_obj = Path(path,self.__dirty_path.absolute_path)
            skipped = False

            # 检验是否是打包目录下的子目录,如果是,那么略过
            for packed_item in packed:
                if path_obj.is_child_of(packed_item.absolute_path):
                    skipped = True
                    break
            if skipped:
                continue

            if path_obj.parser.is_packed:
                self._to_be_moved.append(path_obj.compress())
                packed.add(path_obj)
                continue

            # 对于具有特殊字符的目录进行处理
            if path_obj.parser.is_having_special_character:
                files = path_obj.append_file_suffix(path_obj.parser.special_character_part)
                self._to_be_moved.extend([File(file) for file in files])
                packed.add(path_obj)
                special_path.add(path_obj)
                continue

            self._to_be_moved.extend([File(os.path.join(path,file)) for file in files])

        # 把待移动的文件复制到指定的目录
        for file_moved in self._to_be_moved:
            print(file_moved.parser.path_name)
            destination_path = self.__clean_path.find_one(file_moved.parser.dirs)

            if destination_path is None:
                print('Can not find the destination path!')
                raise FileNotFoundError
            OSOperator.copy_to(file_moved.parser.path_name_with_absolute_path,destination_path.absolute_path)

        # 移除原有路径中具有特殊字符路径下文件的特殊字符后缀
        for sp in special_path:
            sp.remove_append_file_suffix()

    def update_database(self):
        """ 更新数据库

        :return: 无返回值
        """
        self.__path_traversal.to_traverse_path()
        self.__path_traversal.close_db()

    def path_tree(self):
        """ 返回来自数据库信息的目录树

        :return: 返回目录树
        :rtype: list
        """
        return self.__file_db.get_files_according_to_path_list(self.__path_db.path_tree())

    def find(self,open_path=r'E:\temp',**condition):
        """ 查询和打开文件窗口

        :param str open_path: 文件打开路径
        :param dict condition: 查询条件
        :return: 返回成功或失败信息
        :rtype: str
        """
        return self.__file_db.find_and_open(self.__clean_path.absolute_path,open_path,**condition)

    @property
    def destination_path(self):
        """ 返回目标路径

        :return: 返回目录路径
        :rtype: Path对象
        """
        return self.__clean_path
Ejemplo n.º 4
0
class PathTraversal:
    """ PathTraversal类遍历文件夹更新数据库

    """
    def __init__(self, absolute_path):
        # 设定类对象的变量
        # 设置文件库路径名称
        self.__path = Path(absolute_path)

        # 初始化数据库集合
        self.path_db = PathDB()
        self.file_db = FileDB()
        self.tag_db = TagDB()

        # path,file和tag数据库中所有文档的_id集合
        self.path_set = set(item['_id'] for item in self.path_db.collection.find({},{'_id':1}))
        self.file_set = set(item['_id'] for item in self.file_db.collection.find({},{'_id':1}))
        self.tag_set = set(item['_id'] for item in self.tag_db.collection.find({},{'_id':1}))

    def to_update_path_and_file_db(self):
        pass

    def to_traverse_path(self):
        """ 遍历目录,更新数据库

        :return: 无返回值
        """
        # path是绝对路径,files是文件列表
        # self.path.included返回的是os.walk函数的结果,即列表,列表的三个元素分别是绝对路径,子目录名称以及文件
        for path,_,files in self.path.included:
            # Path类对象,输入参数为绝对路径
            path_obj = Path(path,self.path.absolute_path)
            # pathid是相对路径的数据库_id
            pathid = self.path_db.update(path_obj)
            # 如果path存在于数据库中,则删除集合path_set中对应的path,这一步是为了删除数据库中无用的路径
            if pathid is not None:
                self.path_set.remove(pathid)

            # 插入文件信息
            if len(files) > 0:
                for file in files:
                    # 更新文件信息
                    # 调用FileDB类对象的update方法,输入参数为文件的File对象及其所在路径的Path对象
                    fileid = self.file_db.update(File(os.path.join(path,file)),path_obj)
                    if fileid is not None:
                        self.file_set.remove(fileid)

        # 删除数据库中无用的路径信息
        self.path_db.delete_many(self.path_set)
        # 删除数据库中无用的文件信息
        self.file_db.delete_many(self.file_set)
        # 设置路径的子目录集合
        self.path_db.traverse_and_update()

        tags = self.file_db.make_tag_document()
        for tag in tags:
            tag_dict = {'tag':tag,'files':tags[tag]}
            tagid = self.tag_db.update(tag_dict)
            if tagid is not None:
                self.tag_set.remove(tagid)
        self.tag_db.delete_many(self.tag_set)

    @property
    def path(self):
        """ 返回路径对象

        :return: 返回路径对象
        :rtype: Path对象
        """
        return self.__path

    def close_db(self):
        """ 关闭数据库连接

        :return: 无返回值
        """
        self.path_db.close()
        self.file_db.close()
        self.tag_db.close()