solax-py-library 0.1.1__tar.gz → 1.0.0.2__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.
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/PKG-INFO +3 -1
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/pyproject.toml +3 -1
- solax_py_library-1.0.0.2/solax_py_library/main.py +7 -0
- solax_py_library-1.0.0.2/solax_py_library/upload/core/data_adapter/csv.py +26 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/core/upload_service/base.py +2 -2
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/core/upload_service/ftp.py +18 -9
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/test/test_ftp.py +4 -3
- solax_py_library-1.0.0.2/solax_py_library/upload/types/client.py +19 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/types/ftp.py +7 -2
- solax_py_library-0.1.1/solax_py_library/upload/core/data_adapter/csv.py +0 -19
- solax_py_library-0.1.1/solax_py_library/upload/types/client.py +0 -13
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/README.md +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/__init__.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/__init__.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/api/__init__.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/api/service.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/core/__init__.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/core/data_adapter/__init__.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/core/data_adapter/base.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/core/upload_service/__init__.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/errors/__init__.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/errors/base.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/errors/upload_error.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/test/__init__.py +0 -0
- {solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/types/__init__.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: solax-py-library
|
3
|
-
Version: 0.
|
3
|
+
Version: 1.0.0.2
|
4
4
|
Summary: some common tool
|
5
5
|
Author: shenlvyu
|
6
6
|
Author-email: 13296718439@163.com
|
@@ -12,6 +12,8 @@ Classifier: Programming Language :: Python :: 3.10
|
|
12
12
|
Classifier: Programming Language :: Python :: 3.11
|
13
13
|
Classifier: Programming Language :: Python :: 3.12
|
14
14
|
Classifier: Programming Language :: Python :: 3.13
|
15
|
+
Requires-Dist: pydantic (>=1.10.0,<2.0.0)
|
16
|
+
Requires-Dist: typing-extensions (==4.7.1)
|
15
17
|
Description-Content-Type: text/markdown
|
16
18
|
|
17
19
|
### solax common module
|
@@ -1,12 +1,14 @@
|
|
1
1
|
[tool.poetry]
|
2
2
|
name = "solax-py-library"
|
3
|
-
version = "0.
|
3
|
+
version = "1.0.0.2"
|
4
4
|
description = "some common tool"
|
5
5
|
authors = ["shenlvyu <13296718439@163.com>"]
|
6
6
|
readme = "README.md"
|
7
7
|
|
8
8
|
[tool.poetry.dependencies]
|
9
9
|
python = "^3.8"
|
10
|
+
pydantic = "^1.10.0"
|
11
|
+
typing-extensions = "==4.7.1"
|
10
12
|
|
11
13
|
[build-system]
|
12
14
|
requires = ["poetry-core"]
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import os.path
|
2
|
+
import sys
|
3
|
+
|
4
|
+
current_directory = os.path.dirname(os.path.abspath(__file__))
|
5
|
+
parent_directory = os.path.abspath(os.path.join(current_directory, '..'))
|
6
|
+
target_file_path = os.path.join(parent_directory, "dependency")
|
7
|
+
sys.path.append(target_file_path)
|
@@ -0,0 +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="utf8"
|
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 open(temp_file_path, "rb")
|
@@ -14,8 +14,8 @@ class BaseUploadService(metaclass=ABCMeta):
|
|
14
14
|
def connect(self):
|
15
15
|
...
|
16
16
|
|
17
|
-
def upload(self, data):
|
18
|
-
upload_data = self._parse(data)
|
17
|
+
def upload(self, data: UploadData):
|
18
|
+
upload_data = self._parse(data.build_data())
|
19
19
|
return self._upload(upload_data)
|
20
20
|
|
21
21
|
@abstractmethod
|
@@ -4,8 +4,17 @@ import os
|
|
4
4
|
from .base import BaseUploadService
|
5
5
|
from solax_py_library.upload.types.client import UploadType, UploadData
|
6
6
|
from solax_py_library.upload.core.data_adapter import CSVDataAdapter
|
7
|
-
from solax_py_library.upload.types.ftp import
|
8
|
-
|
7
|
+
from solax_py_library.upload.types.ftp import (
|
8
|
+
FTPServiceConfig,
|
9
|
+
FTPFileType,
|
10
|
+
FTPData,
|
11
|
+
FTPParsedData,
|
12
|
+
)
|
13
|
+
from solax_py_library.upload.errors.upload_error import (
|
14
|
+
SendDataError,
|
15
|
+
ConnectError,
|
16
|
+
ConfigurationError,
|
17
|
+
)
|
9
18
|
|
10
19
|
|
11
20
|
class FTPUploadService(BaseUploadService):
|
@@ -15,7 +24,7 @@ class FTPUploadService(BaseUploadService):
|
|
15
24
|
super().__init__(**kwargs)
|
16
25
|
config = self._init_config(**kwargs)
|
17
26
|
self.host = config.host
|
18
|
-
self.
|
27
|
+
self.user = config.user
|
19
28
|
self.password = config.password
|
20
29
|
self.remote_path = config.remote_path
|
21
30
|
|
@@ -31,7 +40,7 @@ class FTPUploadService(BaseUploadService):
|
|
31
40
|
self.close()
|
32
41
|
try:
|
33
42
|
self._client = ftplib.FTP(self.host)
|
34
|
-
self._client.login(self.
|
43
|
+
self._client.login(self.user, self.password)
|
35
44
|
print(f"Connected to {self.host}")
|
36
45
|
self._is_connect = True
|
37
46
|
except Exception as e:
|
@@ -45,14 +54,14 @@ class FTPUploadService(BaseUploadService):
|
|
45
54
|
self._is_connect = False
|
46
55
|
print("Connection closed.")
|
47
56
|
|
48
|
-
def _parse(self, upload_data:
|
49
|
-
|
50
|
-
|
51
|
-
parsed_data = CSVDataAdapter.parse_data(data.data)
|
57
|
+
def _parse(self, upload_data: FTPData) -> FTPParsedData:
|
58
|
+
if upload_data.file_type == FTPFileType.CSV:
|
59
|
+
parsed_data = CSVDataAdapter.parse_data(upload_data.data)
|
52
60
|
else:
|
53
61
|
raise NotImplementedError
|
54
62
|
return FTPParsedData(
|
55
|
-
file_name=
|
63
|
+
file_name=upload_data.build_full_path(self.remote_path) + upload_data.file_type.get_file_suffix(),
|
64
|
+
data=parsed_data
|
56
65
|
)
|
57
66
|
|
58
67
|
def _upload(self, data: FTPParsedData):
|
{solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/test/test_ftp.py
RENAMED
@@ -9,7 +9,7 @@ class FTPTest(unittest.TestCase):
|
|
9
9
|
def test_ftp_upload(self):
|
10
10
|
ftp_config = {
|
11
11
|
"host": "10.1.31.181", # 测试host
|
12
|
-
"
|
12
|
+
"user": "solax",
|
13
13
|
"password": "sly402",
|
14
14
|
"remote_path": "/xixi",
|
15
15
|
}
|
@@ -17,10 +17,11 @@ class FTPTest(unittest.TestCase):
|
|
17
17
|
upload_type=UploadType.FTP,
|
18
18
|
configuration=ftp_config,
|
19
19
|
upload_data=UploadData(
|
20
|
-
|
20
|
+
upload_type=UploadType.FTP,
|
21
|
+
data=dict(
|
21
22
|
file_type=FTPFileType.CSV,
|
22
23
|
file_name="new_file.csv",
|
23
|
-
data=[
|
24
|
+
data=[{'EMS1000序列号': 'XMG11A011L', 'EMS1000本地时间': '2025-02-11 15:39:10', 'EMS1000版本号': 'V007.11.1', '电站所在国家和地区': None, '电站所在当前时区': None, '电站系统类型': None}],
|
24
25
|
),
|
25
26
|
),
|
26
27
|
)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
from enum import Enum
|
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(str, Enum):
|
9
|
+
FTP = "FTP"
|
10
|
+
|
11
|
+
|
12
|
+
class UploadData(BaseModel):
|
13
|
+
data: Optional[Union[dict, FTPData]]
|
14
|
+
upload_type: Optional[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)
|
@@ -6,7 +6,12 @@ from pydantic import BaseModel
|
|
6
6
|
|
7
7
|
|
8
8
|
class FTPFileType(str, Enum):
|
9
|
-
CSV = "
|
9
|
+
CSV = "CSV"
|
10
|
+
|
11
|
+
def get_file_suffix(self):
|
12
|
+
return {
|
13
|
+
FTPFileType.CSV: ".csv",
|
14
|
+
}.get(self)
|
10
15
|
|
11
16
|
|
12
17
|
class FTPData(BaseModel):
|
@@ -20,7 +25,7 @@ class FTPData(BaseModel):
|
|
20
25
|
|
21
26
|
class FTPServiceConfig(BaseModel):
|
22
27
|
host: str
|
23
|
-
|
28
|
+
user: Optional[str]
|
24
29
|
password: Optional[str]
|
25
30
|
remote_path: str
|
26
31
|
|
@@ -1,19 +0,0 @@
|
|
1
|
-
import csv
|
2
|
-
import tempfile
|
3
|
-
|
4
|
-
from solax_py_library.upload.core.data_adapter.base import BaseDataAdapter
|
5
|
-
|
6
|
-
|
7
|
-
class CSVDataAdapter(BaseDataAdapter):
|
8
|
-
@classmethod
|
9
|
-
def parse_data(cls, data):
|
10
|
-
with tempfile.NamedTemporaryFile(
|
11
|
-
delete=False, mode="w", newline="", encoding="utf8"
|
12
|
-
) as temp_file:
|
13
|
-
writer = csv.writer(temp_file)
|
14
|
-
writer.writerows(data)
|
15
|
-
|
16
|
-
temp_file_path = temp_file.name
|
17
|
-
temp_file.seek(0)
|
18
|
-
|
19
|
-
return open(temp_file_path, "rb")
|
@@ -1,13 +0,0 @@
|
|
1
|
-
from enum import Enum
|
2
|
-
from pydantic import BaseModel
|
3
|
-
from typing import Optional
|
4
|
-
|
5
|
-
from solax_py_library.upload.types.ftp import FTPData
|
6
|
-
|
7
|
-
|
8
|
-
class UploadType(str, Enum):
|
9
|
-
FTP = "FTP"
|
10
|
-
|
11
|
-
|
12
|
-
class UploadData(BaseModel):
|
13
|
-
ftp_data: Optional[FTPData]
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/core/__init__.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/errors/__init__.py
RENAMED
File without changes
|
File without changes
|
{solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/errors/upload_error.py
RENAMED
File without changes
|
{solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/test/__init__.py
RENAMED
File without changes
|
{solax_py_library-0.1.1 → solax_py_library-1.0.0.2}/solax_py_library/upload/types/__init__.py
RENAMED
File without changes
|