datacomshell 0.26.5__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: datacomshell
3
- Version: 0.26.5
3
+ Version: 0.26.7
4
4
  Summary: Add your description here
5
5
  Author: Saven
6
6
  Author-email: Saven <2416844857@qq.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "datacomshell"
3
- version = "0.26.5"
3
+ version = "0.26.7"
4
4
  description = "Add your description here"
5
5
  authors = [
6
6
  { name = "Saven", email = "2416844857@qq.com" },
@@ -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 = datetime.now().timestamp()
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
@@ -120,7 +120,10 @@ class AsyncTelnetClientUtil(object):
120
120
  )
121
121
 
122
122
  await instance.__initConnect()
123
- await instance.__create_log_file()
123
+
124
+ if instance.__is_log:
125
+ await instance.__create_log_file()
126
+
124
127
  print(f"""
125
128
  [I] connect success: {instance.__host}:{instance.__port}
126
129
  [I] login type: {instance.__auth.get_auth_type().name}
@@ -128,8 +131,11 @@ class AsyncTelnetClientUtil(object):
128
131
  """)
129
132
  return instance
130
133
 
134
+ def __datetime(self):
135
+ return datetime.now(timezone.utc)
136
+
131
137
  def __command_wrapper(self, command: str) -> str:
132
- return command + '\r\n'
138
+ return command + '\n'
133
139
 
134
140
  async def __read_wait_wrapper(self, read) -> bytes:
135
141
  return await asyncio.wait_for(read, timeout=self.__exc_command_timout)
@@ -185,37 +191,42 @@ class AsyncTelnetClientUtil(object):
185
191
 
186
192
  async def __close_log_file(self):
187
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} ================')
188
196
  await self.__log_file.close()
189
197
 
190
198
  async def __create_log_file(self):
191
199
  await self.__close_log_file()
192
- filename = datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f') + '.log'
200
+ filename = self.__datetime().strftime('%Y-%m-%d_%H-%M-%S-%f') + '.log'
193
201
 
194
202
  if self.__log_folder_path == None:
195
203
  raise RuntimeError('[IE] log_folder_path error')
196
204
 
197
- self.__timestemp = datetime.now().timestamp()
205
+ self.__timestemp = self.__datetime().timestamp()
198
206
  en_path = self.__log_folder_path + '/' + filename
199
207
  self.__log_file = await aiofiles.open(en_path, 'a', encoding='utf-8')
200
208
 
201
- await self.__log_file.write(f'================ {filename} ================')
209
+ await self.__log_file.write(f'================ [start]:{filename} ================')
202
210
 
203
211
  async def __log(self, message: str):
204
212
  if self.__is_log != True:
205
213
  return
206
214
 
207
- curren_timestemp: float = datetime.now().timestamp()
215
+ curren_timestemp: float = self.__datetime().timestamp()
208
216
  diff = curren_timestemp - self.__timestemp
209
217
 
210
218
  # 新建
211
- if diff > self.__log_new_second:
219
+ if diff > self.__log_new_second or self.__log_file == None:
212
220
  await self.__create_log_file()
213
221
 
222
+ lines = message.splitlines()
223
+ non_empty_lines = [line for line in lines if line.strip() != '']
224
+
214
225
  # 记录
215
226
  if self.__log_file == None:
216
227
  raise RuntimeError('[IE] log_file is None')
217
228
  else:
218
- await self.__log_file.write(message)
229
+ await self.__log_file.write('\n'.join(non_empty_lines) + '\n')
219
230
 
220
231
  async def execute(self, command: str) -> bytes:
221
232
  if self.__reader == None and self.__writer == None:
@@ -225,7 +236,7 @@ class AsyncTelnetClientUtil(object):
225
236
  _reader = cast(_READER_TYPE, self.__reader)
226
237
  _writer = cast(TelnetWriterUnicode, self.__writer)
227
238
 
228
- _writer.write('\r\n')
239
+ _writer.write('\n')
229
240
  _writer.write(self.__command_wrapper(command))
230
241
  _writer.write(_DEFAULT_ENDING)
231
242
  await _writer.drain()
@@ -251,7 +262,7 @@ class AsyncTelnetClientUtil(object):
251
262
  _writer = cast(TelnetWriterUnicode, self.__writer)
252
263
 
253
264
  if self.__log_file != None:
254
- await self.__log_file.close()
265
+ await self.__close_log_file()
255
266
 
256
267
  _reader.close()
257
268
  _writer.close()
File without changes