This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filequeue.py
78 lines (66 loc) · 2.12 KB
/
filequeue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'''
Add files to queue and get files from queue
'''
import queue
import os
class EntityFile:
'''
Element of file queue
'''
def __init__(self, isdir, subpath, depth):
self.isdir = isdir
self.subpath = subpath
self.depth = depth
class FileQueue:
'''
Queue of files
'''
def __init__(self):
self.file_queue = queue.Queue(maxsize=0) # maxsize=0则队列大小无限制
# self.delete_dir_queue = queue.Queue(maxsize=0)
def __add_file_to_queue(self, sourcepath, subpath, depth, maxdepth):
if os.path.isfile(sourcepath):
self.file_queue.put(EntityFile(False, subpath, depth))
else:
# self.delete_dir_queue.put(sourcepath)
if depth < maxdepth:
self.file_queue.put(EntityFile(True, subpath, depth))
for file in os.listdir(sourcepath):
self.__add_file_to_queue(
os.path.join(sourcepath, file),
os.path.join(subpath, file),
depth+1,
maxdepth
)
else:
self.file_queue.put(EntityFile(True, subpath, depth))
def empty(self):
'''
Return True if queue is empty
'''
return self.file_queue.empty()
def get(self):
'''
Get file from queue
'''
return self.file_queue.get()
# def to_delete_dir_empty(self):
# '''
# Return True if delete_dir_queue is empty
# '''
# return self.delete_dir_queue.empty()
# def get_to_delete_dir(self):
# '''
# Get file from delete_dir_queue
# '''
# return self.delete_dir_queue.get()
def add_all_to_queue(self, sourcepath, max_depth) -> int:
'''
Add files to queue in recursive form
@param sourcepath: source path
@param max_depth: max depth of recursion
@return: size of queue
'''
self.__add_file_to_queue(
sourcepath, os.path.basename(sourcepath), 0, max_depth)
return self.file_queue.qsize()