Skip to content

Commit 5f28ab7

Browse files
ljl66-66hsluoyz
andauthored
feat: format the code, fix CI errors (#3)
* fix: Repair CI not working properly (#1) * fix: add file package.json * fix: Modify package.json for testing * Update test_record.py --------- Co-authored-by: hsluoyz <[email protected]>
1 parent bd4c999 commit 5f28ab7

13 files changed

+181
-125
lines changed

.github/workflows/build.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
pip install .
3232
3333
- name: Run linter
34-
run: git diff --name-only HEAD~10 HEAD | xargs pre-commit run --files
34+
run: pre-commit run --all-files
3535

3636
- name: Run tests
3737
run: python -m unittest discover src/tests -v
@@ -52,7 +52,9 @@ jobs:
5252
node-version: '20'
5353

5454
- name: Setup semantic-release
55-
run: npm install -g semantic-release @semantic-release/github @semantic-release/changelog @semantic-release/commit-analyzer @semantic-release/git @semantic-release/release-notes-generator semantic-release-pypi
55+
run: |
56+
npm init -y
57+
npm install -g semantic-release @semantic-release/github @semantic-release/commit-analyzer @semantic-release/release-notes-generator
5658
5759
- name: Set up Python
5860
uses: actions/setup-python@v2
@@ -65,5 +67,5 @@ jobs:
6567
- name: Release
6668
env:
6769
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
68-
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
70+
# PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
6971
run: npx semantic-release

.pre-commit-config.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
default_stages: [ pre-commit ]
2+
fail_fast: true
3+
4+
repos:
5+
- repo: local
6+
hooks:
7+
- id: black
8+
name: black
9+
entry: black
10+
language: system
11+
types: [ python ]
12+
description: "Black code formatter"
13+
14+
- id: ruff
15+
name: ruff
16+
description: "Ruff code checker"
17+
entry: ruff check
18+
args: [ --fix, --exit-non-zero-on-fix ]
19+
language: system
20+
types: [ python ]
21+
22+
23+
- repo: https://github.com/commitizen-tools/commitizen
24+
rev: v3.2.2
25+
hooks:
26+
- id: commitizen
27+
stages: [ commit-msg ]

package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "casvisor-python-sdk",
3+
"version": "0.1.0",
4+
"repository": {
5+
"type": "git",
6+
"url": "https://github.com/ljl66-66/casvisor-python-sdk.git"
7+
},
8+
"release": {
9+
"branches": [
10+
"master"
11+
],
12+
"plugins": [
13+
"@semantic-release/commit-analyzer",
14+
"@semantic-release/release-notes-generator",
15+
[
16+
"@semantic-release/github",
17+
{
18+
"successComment": false,
19+
"failComment": false,
20+
"failTitle": false,
21+
"releasedLabels": false,
22+
"addReleases": "bottom"
23+
}
24+
]
25+
]
26+
}
27+
}

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,4 +15,3 @@
1515
from setuptools import setup
1616

1717
setup()
18-

src/casvisor/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
#
1515
from .main import CasvisorSDK
16-
from .base import BaseClient,Response
16+
from .base import BaseClient, Response
1717
from .record import Record, _RecordSDK
1818

19-
__all__ = ["CasvisorSDK","BaseClient", "Response", "Record", "_RecordSDK"]
19+
__all__ = ["CasvisorSDK", "BaseClient", "Response", "Record", "_RecordSDK"]

src/casvisor/base.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,13 +22,17 @@ class HttpClient:
2222
def do(self, request):
2323
pass
2424

25+
2526
class Response:
26-
def __init__(self, status: str, msg: str, data: Union[Dict, List], data2: Union[Dict, List]):
27+
def __init__(
28+
self, status: str, msg: str, data: Union[Dict, List], data2: Union[Dict, List]
29+
):
2730
self.status = status
2831
self.msg = msg
2932
self.data = data
3033
self.data2 = data2
3134

35+
3236
# Global HTTP client
3337
client = requests.Session()
3438

@@ -49,7 +53,9 @@ def do_get_response(self, url: str) -> Response:
4953
response = json.loads(resp_bytes)
5054
if response["status"] != "ok":
5155
raise Exception(response["msg"])
52-
return Response(response["status"], response["msg"], response["data"], response["data2"])
56+
return Response(
57+
response["status"], response["msg"], response["data"], response["data2"]
58+
)
5359

5460
def do_get_bytes(self, url: str) -> bytes:
5561
response = self.do_get_response(url)
@@ -62,33 +68,42 @@ def do_get_bytes_raw(self, url: str) -> bytes:
6268
raise Exception(response["msg"])
6369
return resp_bytes
6470

65-
def do_post(self, action: str, query_map: Dict[str, str], post_bytes: bytes, is_form: bool, is_file: bool) -> Response:
71+
def do_post(
72+
self,
73+
action: str,
74+
query_map: Dict[str, str],
75+
post_bytes: bytes,
76+
is_form: bool,
77+
is_file: bool,
78+
) -> Response:
6679
url = util.get_url(self.endpoint, action, query_map)
6780
content_type, body = self.prepare_body(post_bytes, is_form, is_file)
6881
resp_bytes = self.do_post_bytes_raw(url, content_type, body)
6982
response = json.loads(resp_bytes)
7083
if response["status"] != "ok":
7184
raise Exception(response["msg"])
72-
return Response(response["status"], response["msg"], response["data"], response["data2"])
85+
return Response(
86+
response["status"], response["msg"], response["data"], response["data2"]
87+
)
7388

7489
def do_post_bytes_raw(self, url: str, content_type: str, body: bytes) -> bytes:
7590
if not content_type:
7691
content_type = "text/plain;charset=UTF-8"
7792
headers = {
7893
"Content-Type": content_type,
79-
"Authorization": f"Basic {self.client_id}:{self.client_secret}"
94+
"Authorization": f"Basic {self.client_id}:{self.client_secret}",
8095
}
8196
resp = client.post(url, headers=headers, data=body)
8297
return resp.content
8398

8499
def do_get_bytes_raw_without_check(self, url: str) -> bytes:
85-
headers = {
86-
"Authorization": f"Basic {self.client_id}:{self.client_secret}"
87-
}
100+
headers = {"Authorization": f"Basic {self.client_id}:{self.client_secret}"}
88101
resp = client.get(url, headers=headers)
89102
return resp.content
90103

91-
def prepare_body(self, post_bytes: bytes, is_form: bool, is_file: bool) -> Tuple[str, bytes]:
104+
def prepare_body(
105+
self, post_bytes: bytes, is_form: bool, is_file: bool
106+
) -> Tuple[str, bytes]:
92107
if is_form:
93108
if is_file:
94109
return util.create_form_file({"file": post_bytes})

src/casvisor/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,

src/casvisor/record.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -69,22 +69,20 @@ def __init__(self, base_client: BaseClient, organization_name: str):
6969
self.organization_name = organization_name
7070

7171
def get_records(self) -> List[Record]:
72-
query_map = {
73-
"owner": self.organization_name
74-
}
72+
query_map = {"owner": self.organization_name}
7573
url = util.get_url(self.base_client.endpoint, "get-records", query_map)
7674
bytes = self.base_client.do_get_bytes(url)
7775
return [Record.from_dict(record) for record in json.loads(bytes)]
7876

7977
def get_record(self, name: str) -> Record:
80-
query_map = {
81-
"id": f"{self.organization_name}/{name}"
82-
}
78+
query_map = {"id": f"{self.organization_name}/{name}"}
8379
url = util.get_url(self.base_client.endpoint, "get-record", query_map)
8480
bytes = self.base_client.do_get_bytes(url)
8581
return Record.from_dict(json.loads(bytes))
8682

87-
def get_pagination_records(self, p: int, page_size: int, query_map: Dict[str, str]) -> Tuple[List[Record], int]:
83+
def get_pagination_records(
84+
self, p: int, page_size: int, query_map: Dict[str, str]
85+
) -> Tuple[List[Record], int]:
8886
query_map["owner"] = self.organization_name
8987
query_map["p"] = str(p)
9088
query_map["page_size"] = str(page_size)
@@ -108,14 +106,14 @@ def delete_record(self, record: Record) -> bool:
108106
_, affected = self.modify_record("delete-record", record, None)
109107
return affected
110108

111-
def modify_record(self, action: str, record: Record, columns: Optional[List[str]]) -> tuple[Dict, bool]:
112-
query_map = {
113-
"id": f"{record.owner}/{record.name}"
114-
}
109+
def modify_record(
110+
self, action: str, record: Record, columns: Optional[List[str]]
111+
) -> Tuple[Dict, bool]:
112+
query_map = {"id": f"{record.owner}/{record.name}"}
115113
if columns:
116114
query_map["columns"] = ",".join(columns)
117115
if not record.owner:
118116
record.owner = "admin"
119117
post_bytes = json.dumps(record.to_dict()).encode("utf-8")
120118
resp = self.base_client.do_post(action, query_map, post_bytes, False, False)
121-
return resp, resp["data"] == "Affected"
119+
return resp, resp["data"] == "Affected"

src/casvisor/util.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,21 +13,20 @@
1313
# limitations under the License.
1414

1515

16-
from io import BytesIO
1716
from requests_toolbelt.multipart.encoder import MultipartEncoder
18-
from typing import Dict
17+
from typing import Dict, Tuple
1918

2019

2120
def get_url(base_url: str, action: str, query_map: Dict[str, str]) -> str:
2221
query = "&".join([f"{k}={v}" for k, v in query_map.items()])
2322
return f"{base_url}/api/{action}?{query}"
2423

25-
def create_form_file(form_data: Dict[str, bytes]) -> tuple[str, bytes]:
26-
body = BytesIO()
24+
25+
def create_form_file(form_data: Dict[str, bytes]) -> Tuple[str, bytes]:
2726
encoder = MultipartEncoder(fields={k: ("file", v) for k, v in form_data.items()})
2827
return encoder.content_type, encoder.to_string()
2928

30-
def create_form(form_data: Dict[str, str]) -> tuple[str, bytes]:
31-
body = BytesIO()
29+
30+
def create_form(form_data: Dict[str, str]) -> Tuple[str, bytes]:
3231
encoder = MultipartEncoder(fields=form_data)
33-
return encoder.content_type, encoder.to_string()
32+
return encoder.content_type, encoder.to_string()

src/tests/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
13-
# limitations under the License.
13+
# limitations under the License.

0 commit comments

Comments
 (0)