solax-py-library 1.0.0.23__py3-none-any.whl → 1.0.0.24__py3-none-any.whl

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,26 +1,26 @@
1
- import csv
2
- import tempfile
3
- from typing import Any, Union, List, Dict
4
-
5
- from solax_py_library.upload.core.data_adapter.base import BaseDataAdapter
6
-
7
-
8
- class CSVDataAdapter(BaseDataAdapter):
9
- @classmethod
10
- def parse_data(cls, data: Union[List[List[Any]], List[Dict[str, Any]]]):
11
- with tempfile.NamedTemporaryFile(
12
- delete=False, mode="w", newline="", encoding="utf-8-sig"
13
- ) as temp_file:
14
- if isinstance(data[0], list):
15
- writer = csv.writer(temp_file)
16
- writer.writerows(data)
17
- else:
18
- headers = list(data[0].keys())
19
- writer = csv.DictWriter(temp_file, fieldnames=headers)
20
- writer.writeheader()
21
- writer.writerows(data)
22
-
23
- temp_file_path = temp_file.name
24
- temp_file.seek(0)
25
-
26
- return temp_file_path
1
+ import csv
2
+ import tempfile
3
+ from typing import Any, Union, List, Dict
4
+
5
+ from solax_py_library.upload.core.data_adapter.base import BaseDataAdapter
6
+
7
+
8
+ class CSVDataAdapter(BaseDataAdapter):
9
+ @classmethod
10
+ def parse_data(cls, data: Union[List[List[Any]], List[Dict[str, Any]]]):
11
+ with tempfile.NamedTemporaryFile(
12
+ delete=False, mode="w", newline="", encoding="utf-8-sig"
13
+ ) as temp_file:
14
+ if isinstance(data[0], list):
15
+ writer = csv.writer(temp_file)
16
+ writer.writerows(data)
17
+ else:
18
+ headers = list(data[0].keys())
19
+ writer = csv.DictWriter(temp_file, fieldnames=headers)
20
+ writer.writeheader()
21
+ writer.writerows(data)
22
+
23
+ temp_file_path = temp_file.name
24
+ temp_file.seek(0)
25
+
26
+ return temp_file_path
@@ -1,15 +1,15 @@
1
- from .base import BaseUploadService
2
- from .ftp import FTPUploadService
3
-
4
-
5
- upload_service_map = {}
6
-
7
-
8
- def _register(upload_obj):
9
- upload_service_map[upload_obj.upload_type] = upload_obj
10
-
11
-
12
- _register(FTPUploadService)
13
-
14
-
15
- __all__ = ["BaseUploadService", "FTPUploadService"]
1
+ from .base import BaseUploadService
2
+ from .ftp import FTPUploadService
3
+
4
+
5
+ upload_service_map = {}
6
+
7
+
8
+ def _register(upload_obj):
9
+ upload_service_map[upload_obj.upload_type] = upload_obj
10
+
11
+
12
+ _register(FTPUploadService)
13
+
14
+
15
+ __all__ = ["BaseUploadService", "FTPUploadService"]
@@ -1,43 +1,43 @@
1
- from abc import ABCMeta, abstractmethod
2
-
3
- from solax_py_library.upload.types.client import UploadData
4
-
5
-
6
- class BaseUploadService(metaclass=ABCMeta):
7
- upload_type = None
8
-
9
- def __init__(self, **kwargs) -> None:
10
- self._client = None
11
- self._is_connect = False
12
- self.timeout = 5
13
-
14
- @abstractmethod
15
- def connect(self):
16
- ...
17
-
18
- async def upload(self, data: UploadData):
19
- upload_data = self._parse(data.build_data())
20
- return self._upload(upload_data)
21
-
22
- @abstractmethod
23
- def close(self):
24
- ...
25
-
26
- @property
27
- def is_connect(self):
28
- return self._is_connect
29
-
30
- def __enter__(self):
31
- self.connect()
32
- return self
33
-
34
- def __exit__(self, exc_type, exc_value, traceback):
35
- self.close()
36
-
37
- @abstractmethod
38
- def _upload(self, data):
39
- ...
40
-
41
- @abstractmethod
42
- def _parse(self, upload_data: UploadData):
43
- ...
1
+ from abc import ABCMeta, abstractmethod
2
+
3
+ from solax_py_library.upload.types.client import UploadData
4
+
5
+
6
+ class BaseUploadService(metaclass=ABCMeta):
7
+ upload_type = None
8
+
9
+ def __init__(self, **kwargs) -> None:
10
+ self._client = None
11
+ self._is_connect = False
12
+ self.timeout = 5
13
+
14
+ @abstractmethod
15
+ def connect(self):
16
+ ...
17
+
18
+ async def upload(self, data: UploadData):
19
+ upload_data = self._parse(data.build_data())
20
+ return self._upload(upload_data)
21
+
22
+ @abstractmethod
23
+ def close(self):
24
+ ...
25
+
26
+ @property
27
+ def is_connect(self):
28
+ return self._is_connect
29
+
30
+ def __enter__(self):
31
+ self.connect()
32
+ return self
33
+
34
+ def __exit__(self, exc_type, exc_value, traceback):
35
+ self.close()
36
+
37
+ @abstractmethod
38
+ def _upload(self, data):
39
+ ...
40
+
41
+ @abstractmethod
42
+ def _parse(self, upload_data: UploadData):
43
+ ...
@@ -79,6 +79,7 @@ class FTPUploadService(BaseUploadService):
79
79
  try:
80
80
  if not self._is_connect:
81
81
  raise ConnectError(message="not connect yet")
82
+ # self._client.set_pasv(False)
82
83
  self._cwd_folder()
83
84
  with open(data.file_path, "rb") as f:
84
85
  self._client.storbinary(f"STOR {data.file_name}", f)
@@ -1,113 +1,113 @@
1
- import asyncio
2
- import unittest
3
-
4
- from solax_py_library.upload.api.service import upload
5
- from solax_py_library.upload.core.upload_service import FTPUploadService
6
- from solax_py_library.upload.exceptions import ConnectError, LoginError
7
- from solax_py_library.upload.types.client import UploadType, UploadData
8
- from solax_py_library.upload.types.ftp import FTPFileType
9
-
10
-
11
- class FTPTest(unittest.TestCase):
12
- def test_connect(self):
13
- ftp_config = {
14
- "host": "10.1.31.181", # 测试host
15
- "port": 21,
16
- "user": "solax",
17
- "password": "123456",
18
- "remote_path": "/xixi",
19
- }
20
- ftp = FTPUploadService(**ftp_config)
21
- ftp.connect()
22
-
23
- def test_connect_error_1(self):
24
- ftp_config = {
25
- "host": "10.1.31.182", # 测试host
26
- "port": 21,
27
- "user": "solax",
28
- "password": "123456",
29
- "remote_path": "/xixi",
30
- }
31
- ftp = FTPUploadService(**ftp_config)
32
- try:
33
- ftp.connect()
34
- except ConnectError:
35
- ...
36
-
37
- def test_connect_error_2(self):
38
- ftp_config = {
39
- "host": "10.1.31.181", # 测试host
40
- "port": 21,
41
- "user": "solax123",
42
- "password": "123456",
43
- "remote_path": "/xixi",
44
- }
45
- ftp = FTPUploadService(**ftp_config)
46
- try:
47
- ftp.connect()
48
- except LoginError:
49
- ...
50
-
51
- def test_ftp_upload_to_windows(self):
52
- ftp_config = {
53
- "host": "10.1.31.181", # 测试host
54
- "port": 21,
55
- "user": "solax",
56
- "password": "123456",
57
- "remote_path": "嘻嘻",
58
- }
59
- asyncio.run(
60
- upload(
61
- upload_type=UploadType.FTP,
62
- configuration=ftp_config,
63
- upload_data=UploadData(
64
- upload_type=UploadType.FTP,
65
- data=dict(
66
- file_type=FTPFileType.CSV,
67
- file_name="中文",
68
- data=[
69
- {
70
- "EMS1000序列号": "XMG11A011L",
71
- "EMS1000本地时间": "2025-02-11 15:39:10",
72
- "EMS1000版本号": "V007.11.1",
73
- "电站所在国家和地区": None,
74
- "电站所在当前时区": None,
75
- "电站系统类型": None,
76
- }
77
- ],
78
- ),
79
- ),
80
- )
81
- )
82
-
83
- def test_ftp_upload_to_linux(self):
84
- ftp_config = {
85
- "host": "920729yofx76.vicp.fun", # 测试host
86
- "port": 59477,
87
- "user": "test",
88
- "password": "test123456",
89
- "remote_path": "test",
90
- }
91
- asyncio.run(
92
- upload(
93
- upload_type=UploadType.FTP,
94
- configuration=ftp_config,
95
- upload_data=UploadData(
96
- upload_type=UploadType.FTP,
97
- data=dict(
98
- file_type=FTPFileType.CSV,
99
- file_name="中文",
100
- data=[
101
- {
102
- "EMS1000序列号": "XMG11A011L",
103
- "EMS1000本地时间": "2025-02-11 15:39:10",
104
- "EMS1000版本号": "V007.11.1",
105
- "电站所在国家和地区": None,
106
- "电站所在当前时区": None,
107
- "电站系统类型": None,
108
- }
109
- ],
110
- ),
111
- ),
112
- )
113
- )
1
+ import asyncio
2
+ import unittest
3
+
4
+ from solax_py_library.upload.api.service import upload
5
+ from solax_py_library.upload.core.upload_service import FTPUploadService
6
+ from solax_py_library.upload.exceptions import ConnectError, LoginError
7
+ from solax_py_library.upload.types.client import UploadType, UploadData
8
+ from solax_py_library.upload.types.ftp import FTPFileType
9
+
10
+
11
+ class FTPTest(unittest.TestCase):
12
+ def test_connect(self):
13
+ ftp_config = {
14
+ "host": "10.1.31.181", # 测试host
15
+ "port": 21,
16
+ "user": "solax",
17
+ "password": "123456",
18
+ "remote_path": "/xixi",
19
+ }
20
+ ftp = FTPUploadService(**ftp_config)
21
+ ftp.connect()
22
+
23
+ def test_connect_error_1(self):
24
+ ftp_config = {
25
+ "host": "10.1.31.182", # 测试host
26
+ "port": 21,
27
+ "user": "solax",
28
+ "password": "123456",
29
+ "remote_path": "/xixi",
30
+ }
31
+ ftp = FTPUploadService(**ftp_config)
32
+ try:
33
+ ftp.connect()
34
+ except ConnectError:
35
+ ...
36
+
37
+ def test_connect_error_2(self):
38
+ ftp_config = {
39
+ "host": "10.1.31.181", # 测试host
40
+ "port": 21,
41
+ "user": "solax123",
42
+ "password": "123456",
43
+ "remote_path": "/xixi",
44
+ }
45
+ ftp = FTPUploadService(**ftp_config)
46
+ try:
47
+ ftp.connect()
48
+ except LoginError:
49
+ ...
50
+
51
+ def test_ftp_upload_to_windows(self):
52
+ ftp_config = {
53
+ "host": "10.1.31.181", # 测试host
54
+ "port": 21,
55
+ "user": "solax",
56
+ "password": "123456",
57
+ "remote_path": "嘻嘻",
58
+ }
59
+ asyncio.run(
60
+ upload(
61
+ upload_type=UploadType.FTP,
62
+ configuration=ftp_config,
63
+ upload_data=UploadData(
64
+ upload_type=UploadType.FTP,
65
+ data=dict(
66
+ file_type=FTPFileType.CSV,
67
+ file_name="中文",
68
+ data=[
69
+ {
70
+ "EMS1000序列号": "XMG11A011L",
71
+ "EMS1000本地时间": "2025-02-11 15:39:10",
72
+ "EMS1000版本号": "V007.11.1",
73
+ "电站所在国家和地区": None,
74
+ "电站所在当前时区": None,
75
+ "电站系统类型": None,
76
+ }
77
+ ],
78
+ ),
79
+ ),
80
+ )
81
+ )
82
+
83
+ def test_ftp_upload_to_linux(self):
84
+ ftp_config = {
85
+ "host": "920729yofx76.vicp.fun", # 测试host
86
+ "port": 59477,
87
+ "user": "test",
88
+ "password": "test123456",
89
+ "remote_path": "test",
90
+ }
91
+ asyncio.run(
92
+ upload(
93
+ upload_type=UploadType.FTP,
94
+ configuration=ftp_config,
95
+ upload_data=UploadData(
96
+ upload_type=UploadType.FTP,
97
+ data=dict(
98
+ file_type=FTPFileType.CSV,
99
+ file_name="中文",
100
+ data=[
101
+ {
102
+ "EMS1000序列号": "XMG11A011L",
103
+ "EMS1000本地时间": "2025-02-11 15:39:10",
104
+ "EMS1000版本号": "V007.11.1",
105
+ "电站所在国家和地区": None,
106
+ "电站所在当前时区": None,
107
+ "电站系统类型": None,
108
+ }
109
+ ],
110
+ ),
111
+ ),
112
+ )
113
+ )
@@ -1,11 +1,11 @@
1
- from .client import UploadType, UploadData
2
- from .ftp import FTPData, FTPParsedData, FTPFileType, FTPServiceConfig
3
-
4
- __all__ = [
5
- "FTPData",
6
- "FTPParsedData",
7
- "FTPServiceConfig",
8
- "UploadType",
9
- "UploadData",
10
- "FTPFileType",
11
- ]
1
+ from .client import UploadType, UploadData
2
+ from .ftp import FTPData, FTPParsedData, FTPFileType, FTPServiceConfig
3
+
4
+ __all__ = [
5
+ "FTPData",
6
+ "FTPParsedData",
7
+ "FTPServiceConfig",
8
+ "UploadType",
9
+ "UploadData",
10
+ "FTPFileType",
11
+ ]
@@ -1,19 +1,19 @@
1
- from enum import IntEnum
2
- from pydantic import BaseModel
3
- from typing import Optional, Union
4
-
5
- from solax_py_library.upload.types.ftp import FTPData
6
-
7
-
8
- class UploadType(IntEnum):
9
- FTP = 1
10
-
11
-
12
- class UploadData(BaseModel):
13
- data: Optional[Union[dict, FTPData]]
14
- upload_type: UploadType
15
-
16
- def build_data(self):
17
- dict_obj_data = self.data if isinstance(self.data, dict) else self.data.dict()
18
- if self.upload_type == UploadType.FTP:
19
- return FTPData(**dict_obj_data)
1
+ from enum import IntEnum
2
+ from pydantic import BaseModel
3
+ from typing import Optional, Union
4
+
5
+ from solax_py_library.upload.types.ftp import FTPData
6
+
7
+
8
+ class UploadType(IntEnum):
9
+ FTP = 1
10
+
11
+
12
+ class UploadData(BaseModel):
13
+ data: Optional[Union[dict, FTPData]]
14
+ upload_type: UploadType
15
+
16
+ def build_data(self):
17
+ dict_obj_data = self.data if isinstance(self.data, dict) else self.data.dict()
18
+ if self.upload_type == UploadType.FTP:
19
+ return FTPData(**dict_obj_data)
@@ -1,37 +1,37 @@
1
- import os
2
- from enum import IntEnum
3
- from typing import Any, Optional
4
-
5
- from pydantic import BaseModel
6
-
7
-
8
- class FTPFileType(IntEnum):
9
- CSV = 1
10
-
11
- @classmethod
12
- def get_file_suffix(cls, value):
13
- return {
14
- FTPFileType.CSV: ".csv",
15
- }.get(value)
16
-
17
-
18
- class FTPData(BaseModel):
19
- file_type: FTPFileType
20
- file_name: str
21
- data: Any
22
-
23
- def build_full_path(self, remote_path) -> str:
24
- return os.path.join(remote_path, self.file_name)
25
-
26
-
27
- class FTPServiceConfig(BaseModel):
28
- host: str
29
- port: int
30
- user: Optional[str]
31
- password: Optional[str]
32
- remote_path: str
33
-
34
-
35
- class FTPParsedData(BaseModel):
36
- file_name: str
37
- file_path: str
1
+ import os
2
+ from enum import IntEnum
3
+ from typing import Any, Optional
4
+
5
+ from pydantic import BaseModel
6
+
7
+
8
+ class FTPFileType(IntEnum):
9
+ CSV = 1
10
+
11
+ @classmethod
12
+ def get_file_suffix(cls, value):
13
+ return {
14
+ FTPFileType.CSV: ".csv",
15
+ }.get(value)
16
+
17
+
18
+ class FTPData(BaseModel):
19
+ file_type: FTPFileType
20
+ file_name: str
21
+ data: Any
22
+
23
+ def build_full_path(self, remote_path) -> str:
24
+ return os.path.join(remote_path, self.file_name)
25
+
26
+
27
+ class FTPServiceConfig(BaseModel):
28
+ host: str
29
+ port: int
30
+ user: Optional[str]
31
+ password: Optional[str]
32
+ remote_path: str
33
+
34
+
35
+ class FTPParsedData(BaseModel):
36
+ file_name: str
37
+ file_path: str
@@ -1,38 +1,38 @@
1
- import asyncio
2
- from decimal import Decimal, ROUND_HALF_UP
3
-
4
-
5
- def retry(max_attempts=3, delay=0.5, assign_exception=Exception):
6
- def decorator(func):
7
- async def wrapper(*args, **kwargs):
8
- attempts = 0
9
- while attempts < max_attempts:
10
- try:
11
- return await func(*args, **kwargs)
12
- except Exception as e:
13
- print(f"尝试 {attempts+1} 失败: {e}")
14
- await asyncio.sleep(delay)
15
- attempts += 1
16
- raise assign_exception(f"操作失败 {max_attempts} 次")
17
-
18
- return wrapper
19
-
20
- return decorator
21
-
22
-
23
- def round_value(value, decimal_places=0):
24
- """
25
- 实现四舍五入 代替round函数 遇到5向上取整
26
- @param value: 传入数值
27
- @param decimal_places: 保留几位小数
28
- @return:
29
- """
30
- if isinstance(value, int):
31
- return value
32
- else:
33
- # 优化小数位 Decimal函数太耗时 曲线数据直接使用round函数
34
- # rounded_value = ("{:.%df}" % decimal_places).format(value)
35
- rounded_value = Decimal(str(value)).quantize(
36
- Decimal("1e-" + str(decimal_places)), rounding=ROUND_HALF_UP
37
- )
38
- return float(rounded_value)
1
+ import asyncio
2
+ from decimal import Decimal, ROUND_HALF_UP
3
+
4
+
5
+ def retry(max_attempts=3, delay=0.5, assign_exception=Exception):
6
+ def decorator(func):
7
+ async def wrapper(*args, **kwargs):
8
+ attempts = 0
9
+ while attempts < max_attempts:
10
+ try:
11
+ return await func(*args, **kwargs)
12
+ except Exception as e:
13
+ print(f"尝试 {attempts+1} 失败: {e}")
14
+ await asyncio.sleep(delay)
15
+ attempts += 1
16
+ raise assign_exception(f"操作失败 {max_attempts} 次")
17
+
18
+ return wrapper
19
+
20
+ return decorator
21
+
22
+
23
+ def round_value(value, decimal_places=0):
24
+ """
25
+ 实现四舍五入 代替round函数 遇到5向上取整
26
+ @param value: 传入数值
27
+ @param decimal_places: 保留几位小数
28
+ @return:
29
+ """
30
+ if isinstance(value, int):
31
+ return value
32
+ else:
33
+ # 优化小数位 Decimal函数太耗时 曲线数据直接使用round函数
34
+ # rounded_value = ("{:.%df}" % decimal_places).format(value)
35
+ rounded_value = Decimal(str(value)).quantize(
36
+ Decimal("1e-" + str(decimal_places)), rounding=ROUND_HALF_UP
37
+ )
38
+ return float(rounded_value)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: solax-py-library
3
- Version: 1.0.0.23
3
+ Version: 1.0.0.24
4
4
  Summary: some common tool
5
5
  Author: shenlvyu
6
6
  Author-email: 13296718439@163.com