-
Notifications
You must be signed in to change notification settings - Fork 1
/
mixin.py
152 lines (111 loc) · 4.67 KB
/
mixin.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
145
146
147
148
149
150
151
152
from setting import *
import logging
import os
logger = logging.getLogger(LOGGER_NAME)
"""
자주 쓰이는 함수들 모음
"""
class SEQFileMixin():
# save splited file
def _save_seq_file(self, seq_name : str, seq_num : int, data : bytes):
file_name = TMP_FILE_PATH + seq_name + TMP_FILE_EXTENSION.format(seq_num)
if os.path.isfile(file_name):
raise SEQNumError('SEQNumError : seq num has overlap..!')
try:
with open(file_name, 'wb') as f:
f.write(data)
pass
except:
pass
def _save_none_seq_file(self, data, seq_name : str):
try:
#만약 시퀀스 명이 fullpath이면 \ 를 제거
if '\\' in seq_name:
seq_name = seq_name.split('\\')[-1]
with open(FILE_PATH+seq_name, 'wb') as fw:
fw.write(data)
except Exception as e:
logger.error('none seq file save error : {}'.format(str(e)))
return False
return True
# sequence data merge
def _merge_seq_files(self, seq_name : str):
seq_file_list = [f for f in os.listdir(TMP_FILE_PATH) if f.startswith(seq_name)]
seq_file_list.sort(key=lambda name : int(name.split('.')[-1]))
next_seq_num = 1
# 한 seq씩 저장하도록 변경
try:
with open(FILE_PATH+seq_name, 'wb') as merge_f:
for file in seq_file_list:
file_seq_num = file.split('.')[-1]
if str(next_seq_num) != file_seq_num and file_seq_num != '4294967295':
raise SEQNumError('SEQNumError : seq num is not sequentially [expect={0} / seq={1}]'.format(next_seq_num, file_seq_num))
with open(TMP_FILE_PATH+file, 'rb') as seq_f:
merge_f.write(seq_f.read())
next_seq_num += 1
self._delete_tmp_files(seq_name)
except Exception as e:
raise SEQSaveError('seq save error : {}\npath : {}\nname:{}'.format(str(e), FILE_PATH, seq_name))
def _split_sending_file(self, file_path, target_ip):
try:
# TODO 시간되면 f.seek 형식으로 변경 (대용량 파일 전송시 스택에 너무 많은 데이터가 들어감)
with open(file_path, 'rb') as f:
data = f.read()
if '\\' in file_path:
file_path = file_path.split('\\')[-1]
if '/' in file_path:
file_path = file_path.split('/')[-1]
if '.' in file_path:
file_path = file_path.split('.')[0]
data_len = len(data)
send_count = int(data_len//SEQ_SIZE)
send_count += 1 if (data_len % SEQ_SIZE) > 0 else 0
if send_count == 0 or send_count == 1:
self._save_seq_file(target_ip + '_' + file_path, 0, data)
return
for i in range(1, send_count):
self._save_seq_file(target_ip + '_' + file_path, i, data[SEQ_SIZE*(i-1):SEQ_SIZE*i])
self._save_seq_file(target_ip + '_' + file_path, 0xffffffff, data[SEQ_SIZE*(send_count-1):])
del data # 빠른 데이터 제거
return
except Exception as e:
print(e)
def _get_sending_seq_data(self, file_name):
seq_file_list = [f for f in os.listdir(TMP_FILE_PATH) if f.startswith(file_name)]
if len(seq_file_list) < 1:
raise SEQLoadError('no file exist')
seq_file_list.sort(key=lambda name : int(name.split('.')[-1]))
try:
with open(TMP_FILE_PATH+seq_file_list[0], 'rb') as f:
data = f.read()
os.remove(TMP_FILE_PATH+seq_file_list[0])
return [seq_file_list[0].split('.')[-1], data]
except Exception as e:
raise SEQLoadError('no file exist')
# remove tmp file
def _delete_tmp_files(self, seq_name : str):
seq_file_list = [f for f in os.listdir(TMP_FILE_PATH) if f.startswith(seq_name)]
for tmp_file in seq_file_list:
os.remove(TMP_FILE_PATH+tmp_file)
class EncodingMixin():
# 1st encoding이 안됐을 경우 실행할 함수
@classmethod
def decoding(cls, data : bytes) -> str:
result = ""
try:
result = data.decode(ENCODING)
except:
try:
result = data.decode(SUB_ENCODING)
except:
return ""
return result
"""
Custom Exceptions
"""
class SEQNumError(Exception):
pass
class SEQSaveError(Exception):
pass
class SEQLoadError(Exception):
pass