Skip to content

Commit 6876745

Browse files
authored
Merge pull request #26 from tj-python/chore/reformatted-and-restructured-modules
chore: reformatted and restructured modules
2 parents 8f17800 + 1742c2d commit 6876745

File tree

6 files changed

+105
-126
lines changed

6 files changed

+105
-126
lines changed

github_deploy/commands/_http_utils.py

+1-59
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import ssl
2-
import certifi
3-
4-
import asyncclick as click
52

6-
from github_deploy.commands._constants import REPOS_URL
3+
import certifi
74

85

96
async def get(*, session, url, headers=None, skip_missing=False):
@@ -51,58 +48,3 @@ async def delete(*, session, url, data, headers=None):
5148
) as response:
5249
value = await response.json()
5350
return value
54-
55-
56-
async def list_repos(*, session, org, token):
57-
headers = {
58-
"Authorization": "Bearer {token}".format(token=token),
59-
"Accept": "application/vnd.github+json",
60-
}
61-
url = REPOS_URL.format(org=org)
62-
click.echo("Retrieving repos at {}".format(url))
63-
response = await get(session=session, url=url, headers=headers)
64-
return response
65-
66-
67-
async def delete_content(
68-
*,
69-
session,
70-
repo,
71-
dest,
72-
token,
73-
semaphore,
74-
exists,
75-
current_sha,
76-
):
77-
headers = {
78-
"Authorization": "Bearer {token}".format(token=token),
79-
"Accept": "application/vnd.github+json",
80-
}
81-
82-
data = {"message": "Deleted {}".format(dest)}
83-
if exists:
84-
data["sha"] = current_sha
85-
86-
url = BASE_URL.format(repo=repo, path=dest)
87-
88-
async with semaphore:
89-
response = await delete(
90-
session=session, url=url, data=data, headers=headers
91-
)
92-
93-
return response
94-
95-
96-
async def check_exists(*, session, repo, dest, token, semaphore, skip_missing):
97-
headers = {"Authorization": "Bearer {token}".format(token=token)}
98-
url = BASE_URL.format(repo=repo, path=dest)
99-
100-
async with semaphore:
101-
response = await get(
102-
session=session,
103-
url=url,
104-
headers=headers,
105-
skip_missing=skip_missing,
106-
)
107-
108-
return response

github_deploy/commands/_repo_utils.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import base64
2+
3+
import aiofiles
4+
import asyncclick as click
5+
6+
from github_deploy.commands._constants import REPOS_URL, BASE_URL
7+
from github_deploy.commands._http_utils import get, delete, put
8+
from github_deploy.commands._utils import get_headers
9+
10+
11+
async def list_repos(*, session, org, token):
12+
url = REPOS_URL.format(org=org)
13+
click.echo("Retrieving repos at {}".format(url))
14+
response = await get(session=session, url=url, headers=get_headers(token=token))
15+
return response
16+
17+
18+
async def delete_content(
19+
*,
20+
session,
21+
repo,
22+
dest,
23+
token,
24+
semaphore,
25+
exists,
26+
current_sha,
27+
):
28+
data = {"message": "Deleted {}".format(dest)}
29+
if exists:
30+
data["sha"] = current_sha
31+
32+
url = BASE_URL.format(repo=repo, path=dest)
33+
34+
async with semaphore:
35+
response = await delete(
36+
session=session, url=url, data=data, headers=get_headers(token=token)
37+
)
38+
39+
return response
40+
41+
42+
async def check_exists(*, session, repo, dest, token, semaphore, skip_missing):
43+
url = BASE_URL.format(repo=repo, path=dest)
44+
45+
async with semaphore:
46+
response = await get(
47+
session=session,
48+
url=url,
49+
headers=get_headers(token=token),
50+
skip_missing=skip_missing,
51+
)
52+
53+
return response
54+
55+
56+
async def upload_content(
57+
*,
58+
session,
59+
repo,
60+
source,
61+
dest,
62+
token,
63+
semaphore,
64+
exists,
65+
current_sha,
66+
current_content
67+
):
68+
async with semaphore:
69+
async with aiofiles.open(source, mode="rb") as f:
70+
output = await f.read()
71+
base64_content = base64.b64encode(output).decode("ascii")
72+
73+
if current_content == base64_content:
74+
click.echo("Skipping: Contents are the same.")
75+
return
76+
77+
data = {
78+
"message": "Updated {}".format(dest)
79+
if exists
80+
else "Added {}".format(dest),
81+
"content": base64_content,
82+
}
83+
if exists:
84+
data["sha"] = current_sha
85+
86+
url = BASE_URL.format(repo=repo, path=dest)
87+
88+
async with semaphore:
89+
response = await put(
90+
session=session, url=url, data=data, headers=get_headers(token=token)
91+
)
92+
93+
return response

github_deploy/commands/_utils.py

+7
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ def can_upload(*, repo, include_private):
88
if include_private and repo["private"] is True
99
else not repo["private"]
1010
)
11+
12+
13+
def get_headers(*, token):
14+
return {
15+
"Authorization": "Bearer {token}".format(token=token),
16+
"Accept": "application/vnd.github+json",
17+
}

github_deploy/commands/delete.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import aiohttp
44
import asyncclick as click
55

6-
from github_deploy.commands._constants import BASE_URL
7-
from github_deploy.commands._http_utils import delete, get, list_repos, delete_contents, check_exists
6+
from github_deploy.commands._repo_utils import list_repos, delete_content, check_exists
87
from github_deploy.commands._utils import get_repo
98

109

github_deploy/commands/upload.py

+1-64
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,12 @@
11
import asyncio
2-
import base64
32

4-
import aiofiles
53
import aiohttp
64
import asyncclick as click
75

8-
from github_deploy.commands._constants import BASE_URL
9-
from github_deploy.commands._http_utils import put, list_repos, get
6+
from github_deploy.commands._repo_utils import list_repos, check_exists, upload_content
107
from github_deploy.commands._utils import get_repo, can_upload
118

129

13-
async def upload_content(
14-
*,
15-
session,
16-
repo,
17-
source,
18-
dest,
19-
token,
20-
semaphore,
21-
exists,
22-
current_sha,
23-
current_content
24-
):
25-
headers = {
26-
"Authorization": "token {token}".format(token=token),
27-
"Accept": "application/vnd.github.v3+json",
28-
}
29-
30-
async with semaphore:
31-
async with aiofiles.open(source, mode="rb") as f:
32-
output = await f.read()
33-
base64_content = base64.b64encode(output).decode("ascii")
34-
35-
if current_content == base64_content:
36-
click.echo("Skipping: Contents are the same.")
37-
return
38-
39-
data = {
40-
"message": "Updated {}".format(dest)
41-
if exists
42-
else "Added {}".format(dest),
43-
"content": base64_content,
44-
}
45-
if exists:
46-
data["sha"] = current_sha
47-
48-
url = BASE_URL.format(repo=repo, path=dest)
49-
50-
async with semaphore:
51-
response = await put(
52-
session=session, url=url, data=data, headers=headers
53-
)
54-
55-
return response
56-
57-
58-
async def check_exists(*, session, repo, dest, token, semaphore, skip_missing):
59-
headers = {"Authorization": "token {token}".format(token=token)}
60-
url = BASE_URL.format(repo=repo, path=dest)
61-
62-
async with semaphore:
63-
response = await get(
64-
session=session,
65-
url=url,
66-
headers=headers,
67-
skip_missing=skip_missing,
68-
)
69-
70-
return response
71-
72-
7310
async def handle_file_upload(
7411
*, repo, source, dest, overwrite, token, semaphore, session
7512
):

github_deploy/main.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import asyncclick as click
21
import os
32

3+
import asyncclick as click
4+
45
plugin_folder = os.path.join(os.path.dirname(__file__), "commands")
56

67

0 commit comments

Comments
 (0)