datacomshell 0.26.6__tar.gz → 0.26.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {datacomshell-0.26.6 → datacomshell-0.26.7}/PKG-INFO +1 -1
- {datacomshell-0.26.6 → datacomshell-0.26.7}/pyproject.toml +1 -1
- {datacomshell-0.26.6 → datacomshell-0.26.7}/src/datacomshell/AsyncTelnetClientUtil.py +19 -11
- {datacomshell-0.26.6 → datacomshell-0.26.7}/README.md +0 -0
- {datacomshell-0.26.6 → datacomshell-0.26.7}/src/datacomshell/Auth.py +0 -0
- {datacomshell-0.26.6 → datacomshell-0.26.7}/src/datacomshell/__init__.py +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from typing import Optional, Union, Tuple, cast
|
|
2
2
|
from datacomshell.Auth import Auth, AuthState, UsernamePasswordAuth
|
|
3
3
|
from telnetlib3 import TelnetReader, TelnetReaderUnicode, TelnetWriter, TelnetWriterUnicode, open_connection
|
|
4
|
-
from datetime import datetime
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
5
|
import asyncio
|
|
6
6
|
import aiofiles
|
|
7
7
|
from aiofiles.threadpool.text import AsyncTextIOWrapper
|
|
@@ -74,7 +74,7 @@ class AsyncTelnetClientUtil(object):
|
|
|
74
74
|
|
|
75
75
|
self.__log_folder_path: str | None = log_folder_path
|
|
76
76
|
self.__log_new_second: float = log_new_second
|
|
77
|
-
self.__timestemp: float =
|
|
77
|
+
self.__timestemp: float = self.__datetime().timestamp()
|
|
78
78
|
self.__log_file: AsyncTextIOWrapper | None = None
|
|
79
79
|
|
|
80
80
|
self.__is_log = True if log_folder_path != None else False
|
|
@@ -131,8 +131,11 @@ class AsyncTelnetClientUtil(object):
|
|
|
131
131
|
""")
|
|
132
132
|
return instance
|
|
133
133
|
|
|
134
|
+
def __datetime(self):
|
|
135
|
+
return datetime.now(timezone.utc)
|
|
136
|
+
|
|
134
137
|
def __command_wrapper(self, command: str) -> str:
|
|
135
|
-
return command + '\
|
|
138
|
+
return command + '\n'
|
|
136
139
|
|
|
137
140
|
async def __read_wait_wrapper(self, read) -> bytes:
|
|
138
141
|
return await asyncio.wait_for(read, timeout=self.__exc_command_timout)
|
|
@@ -188,37 +191,42 @@ class AsyncTelnetClientUtil(object):
|
|
|
188
191
|
|
|
189
192
|
async def __close_log_file(self):
|
|
190
193
|
if self.__log_file != None:
|
|
194
|
+
end_datetime = self.__datetime().strftime('%Y-%m-%d_%H-%M-%S-%f')
|
|
195
|
+
await self.__log_file.write(f'================ [end]:{end_datetime} ================')
|
|
191
196
|
await self.__log_file.close()
|
|
192
197
|
|
|
193
198
|
async def __create_log_file(self):
|
|
194
199
|
await self.__close_log_file()
|
|
195
|
-
filename =
|
|
200
|
+
filename = self.__datetime().strftime('%Y-%m-%d_%H-%M-%S-%f') + '.log'
|
|
196
201
|
|
|
197
202
|
if self.__log_folder_path == None:
|
|
198
203
|
raise RuntimeError('[IE] log_folder_path error')
|
|
199
204
|
|
|
200
|
-
self.__timestemp =
|
|
205
|
+
self.__timestemp = self.__datetime().timestamp()
|
|
201
206
|
en_path = self.__log_folder_path + '/' + filename
|
|
202
207
|
self.__log_file = await aiofiles.open(en_path, 'a', encoding='utf-8')
|
|
203
208
|
|
|
204
|
-
await self.__log_file.write(f'================ {filename} ================')
|
|
209
|
+
await self.__log_file.write(f'================ [start]:{filename} ================')
|
|
205
210
|
|
|
206
211
|
async def __log(self, message: str):
|
|
207
212
|
if self.__is_log != True:
|
|
208
213
|
return
|
|
209
214
|
|
|
210
|
-
curren_timestemp: float =
|
|
215
|
+
curren_timestemp: float = self.__datetime().timestamp()
|
|
211
216
|
diff = curren_timestemp - self.__timestemp
|
|
212
217
|
|
|
213
218
|
# 新建
|
|
214
|
-
if diff > self.__log_new_second:
|
|
219
|
+
if diff > self.__log_new_second or self.__log_file == None:
|
|
215
220
|
await self.__create_log_file()
|
|
216
221
|
|
|
222
|
+
lines = message.splitlines()
|
|
223
|
+
non_empty_lines = [line for line in lines if line.strip() != '']
|
|
224
|
+
|
|
217
225
|
# 记录
|
|
218
226
|
if self.__log_file == None:
|
|
219
227
|
raise RuntimeError('[IE] log_file is None')
|
|
220
228
|
else:
|
|
221
|
-
await self.__log_file.write(
|
|
229
|
+
await self.__log_file.write('\n'.join(non_empty_lines) + '\n')
|
|
222
230
|
|
|
223
231
|
async def execute(self, command: str) -> bytes:
|
|
224
232
|
if self.__reader == None and self.__writer == None:
|
|
@@ -228,7 +236,7 @@ class AsyncTelnetClientUtil(object):
|
|
|
228
236
|
_reader = cast(_READER_TYPE, self.__reader)
|
|
229
237
|
_writer = cast(TelnetWriterUnicode, self.__writer)
|
|
230
238
|
|
|
231
|
-
_writer.write('\
|
|
239
|
+
_writer.write('\n')
|
|
232
240
|
_writer.write(self.__command_wrapper(command))
|
|
233
241
|
_writer.write(_DEFAULT_ENDING)
|
|
234
242
|
await _writer.drain()
|
|
@@ -254,7 +262,7 @@ class AsyncTelnetClientUtil(object):
|
|
|
254
262
|
_writer = cast(TelnetWriterUnicode, self.__writer)
|
|
255
263
|
|
|
256
264
|
if self.__log_file != None:
|
|
257
|
-
await self.
|
|
265
|
+
await self.__close_log_file()
|
|
258
266
|
|
|
259
267
|
_reader.close()
|
|
260
268
|
_writer.close()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|