-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsaver.py
144 lines (117 loc) · 5.47 KB
/
saver.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import abc
import datetime
import os
import re
from emailinfo import EmailInfo
# 附件储存类_基类
class Saver(metaclass=abc.ABCMeta):
__SUBJECT_MAX_LENGTH = 51
@abc.abstractmethod
def __init__(self, root_path, file_name, file_data):
self._root_path = root_path
self._file_name = file_name
self._file_data = file_data
def _save_file(self, directory_path):
# 储存文件,directory_path是绝对路径,不包含文件名
if not os.path.exists(directory_path):
os.makedirs(directory_path)
self._file_name = Saver.file_name_check_and_update(directory_path, self._file_name)
file = open(os.path.join(directory_path, self._file_name), 'wb')
file.write(self._file_data)
file.close()
@staticmethod
# 检查文件名,如果相同则自动递增编号。返回文件名,不包含路径。
def file_name_check_and_update(path, file_name):
file_number = 2
exist_file_list = os.listdir(path)
pure_name, extension = os.path.splitext(file_name)
while file_name in exist_file_list:
file_name = pure_name + '_' + str(file_number) + extension
file_number += 1
return file_name
@staticmethod
# 检查文件夹名词,去除非法字符并控制长度
def normalize_directory_name(directory_name):
normalized_name = re.sub('[*"/:?|<>\n]', '', directory_name, 0)
normalized_name = normalized_name[0:min(Saver.__SUBJECT_MAX_LENGTH, len(normalized_name))].strip()
return normalized_name
@abc.abstractmethod
def save(self):
pass
# 模式0:所有附件存入一个文件夹
class MergeSaver(Saver):
def __init__(self, root_path, file_name, file_data):
super().__init__(root_path, file_name, file_data)
def save(self):
self._save_file(self._root_path)
# 模式1:每个邮箱地址一个文件夹
class AddressClassifySaver(Saver):
def __init__(self, root_path, file_name, file_data, email_address):
super().__init__(root_path, file_name, file_data)
self._email_address = self.normalize_directory_name(email_address)
def save(self):
self._save_file(os.path.join(self._root_path, self._email_address))
# 模式2:每个邮件主题一个文件夹
class SubjectClassifySaver(Saver):
def __init__(self, root_path, file_name, file_data, email_subject):
super().__init__(root_path, file_name, file_data)
self._email_subject = self.normalize_directory_name(email_subject)
def save(self):
self._save_file(os.path.join(self._root_path, self._email_subject))
# 模式3:每个发件人的每个邮件主题一个文件夹
class AddressSubjectClassifySaver(Saver):
def __init__(self, root_path, file_name, file_data, email_address, email_subject):
super().__init__(root_path, file_name, file_data)
self._email_address = self.normalize_directory_name(email_address)
self._email_subject = self.normalize_directory_name(email_subject)
def save(self):
self._save_file(os.path.join(self._root_path, self._email_address, self._email_subject))
# 模式4:每个发件人昵称一个文件夹
class AliasClassifySaver(Saver):
def __init__(self, root_path, file_name, file_data, from_alias):
super().__init__(root_path, file_name, file_data)
self._from_alias = self.normalize_directory_name(from_alias)
def save(self):
self._save_file(os.path.join(self._root_path, self._from_alias))
# 模式5:每个邮件主题带上日期前缀的一个文件夹
class DateSubjectClassifySaver(Saver):
def __init__(self, root_path, file_name, file_data, email_subject, email_date):
super().__init__(root_path, file_name, file_data)
self._email_subject = self.normalize_directory_name(email_subject)
date = datetime.datetime.fromtimestamp(email_date)
formatted_date = date.strftime("%m-%d")
self._email_date = formatted_date
def save(self):
self._save_file(
os.path.join(self._root_path, self._email_date + "_" + self._email_subject)
)
# 储存器工厂
class SaverFactor:
def __init__(self, mode: int):
self.__mode = mode
def __call__(self, root_path, file_name, file_data, email_info: EmailInfo):
"""
保存模式 SAVE_MODE
【0:所有附件存入一个文件夹】
【1:每个邮箱地址一个文件夹】
【2:每个邮件主题一个文件夹】
【3:每个发件人的每个邮件主题一个文件夹】
【4:每个发件人昵称一个文件夹】
"""
if self.__mode == 0:
return MergeSaver(root_path, file_name, file_data)
elif self.__mode == 1:
return AddressClassifySaver(root_path, file_name, file_data, email_info.from_address)
elif self.__mode == 2:
return SubjectClassifySaver(root_path, file_name, file_data, email_info.subject)
elif self.__mode == 3:
return AddressSubjectClassifySaver(root_path, file_name, file_data, email_info.from_address,
email_info.subject)
elif self.__mode == 4:
return AliasClassifySaver(root_path, file_name, file_data, email_info.from_name)
elif self.__mode == 5:
return DateSubjectClassifySaver(
root_path, file_name, file_data, email_info.subject, email_info.date
)
else:
return None