Skip to content

Commit

Permalink
Use python3.6+ constructs (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
q0w committed Nov 15, 2021
1 parent 0b52c7b commit 3e40e64
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 87 deletions.
12 changes: 4 additions & 8 deletions aiodocker/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .utils import clean_filters, clean_map


class DockerConfigs(object):
class DockerConfigs:
def __init__(self, docker):
self.docker = docker

Expand Down Expand Up @@ -79,9 +79,7 @@ async def inspect(self, config_id: str) -> Mapping[str, Any]:
a dict with info about a config
"""

response = await self.docker._query_json(
"configs/{config_id}".format(config_id=config_id), method="GET"
)
response = await self.docker._query_json(f"configs/{config_id}", method="GET")
return response

async def delete(self, config_id: str) -> bool:
Expand All @@ -95,9 +93,7 @@ async def delete(self, config_id: str) -> bool:
True if successful
"""

async with self.docker._query(
"configs/{config_id}".format(config_id=config_id), method="DELETE"
):
async with self.docker._query(f"configs/{config_id}", method="DELETE"):
return True

async def update(
Expand Down Expand Up @@ -147,7 +143,7 @@ async def update(
request_data = json.dumps(clean_map(spec))

await self.docker._query_json(
"configs/{config_id}/update".format(config_id=config_id),
f"configs/{config_id}/update",
method="POST",
data=request_data,
params=params,
Expand Down
30 changes: 15 additions & 15 deletions aiodocker/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .utils import identical, parse_result


class DockerContainers(object):
class DockerContainers:
def __init__(self, docker):
self.docker = docker

Expand Down Expand Up @@ -93,7 +93,7 @@ async def run(

async def get(self, container, **kwargs):
data = await self.docker._query_json(
"containers/{container}/json".format(container=container),
f"containers/{container}/json",
method="GET",
params=kwargs,
)
Expand Down Expand Up @@ -130,7 +130,7 @@ def log(self, *, stdout=False, stderr=False, follow=False, **kwargs):
params.update(kwargs)

cm = self.docker._query(
"containers/{self._id}/logs".format(self=self), method="GET", params=params
f"containers/{self._id}/logs", method="GET", params=params
)

if follow:
Expand All @@ -155,7 +155,7 @@ async def _logs_list(self, cm):

async def get_archive(self, path: str) -> tarfile.TarFile:
async with self.docker._query(
"containers/{self._id}/archive".format(self=self),
f"containers/{self._id}/archive",
method="GET",
params={"path": path},
) as response:
Expand All @@ -164,7 +164,7 @@ async def get_archive(self, path: str) -> tarfile.TarFile:

async def put_archive(self, path, data):
async with self.docker._query(
"containers/{self._id}/archive".format(self=self),
f"containers/{self._id}/archive",
method="PUT",
data=data,
headers={"content-type": "application/json"},
Expand All @@ -175,20 +175,20 @@ async def put_archive(self, path, data):

async def show(self, **kwargs):
data = await self.docker._query_json(
"containers/{self._id}/json".format(self=self), method="GET", params=kwargs
f"containers/{self._id}/json", method="GET", params=kwargs
)
self._container = data
return data

async def stop(self, **kwargs):
async with self.docker._query(
"containers/{self._id}/stop".format(self=self), method="POST", params=kwargs
f"containers/{self._id}/stop", method="POST", params=kwargs
):
pass

async def start(self, **kwargs):
async with self.docker._query(
"containers/{self._id}/start".format(self=self),
f"containers/{self._id}/start",
method="POST",
headers={"content-type": "application/json"},
data=kwargs,
Expand All @@ -200,21 +200,21 @@ async def restart(self, timeout=None):
if timeout is not None:
params["t"] = timeout
async with self.docker._query(
"containers/{self._id}/restart".format(self=self),
f"containers/{self._id}/restart",
method="POST",
params=params,
):
pass

async def kill(self, **kwargs):
async with self.docker._query(
"containers/{self._id}/kill".format(self=self), method="POST", params=kwargs
f"containers/{self._id}/kill", method="POST", params=kwargs
):
pass

async def wait(self, *, timeout=None, **kwargs):
data = await self.docker._query_json(
"containers/{self._id}/wait".format(self=self),
f"containers/{self._id}/wait",
method="POST",
params=kwargs,
timeout=timeout,
Expand All @@ -223,13 +223,13 @@ async def wait(self, *, timeout=None, **kwargs):

async def delete(self, **kwargs):
async with self.docker._query(
"containers/{self._id}".format(self=self), method="DELETE", params=kwargs
f"containers/{self._id}", method="DELETE", params=kwargs
):
pass

async def rename(self, newname):
async with self.docker._query(
"containers/{self._id}/rename".format(self=self),
f"containers/{self._id}/rename",
method="POST",
headers={"content-type": "application/json"},
params={"name": newname},
Expand All @@ -239,7 +239,7 @@ async def rename(self, newname):
async def websocket(self, **params):
if not params:
params = {"stdin": True, "stdout": True, "stderr": True, "stream": True}
path = "containers/{self._id}/attach/ws".format(self=self)
path = f"containers/{self._id}/attach/ws"
ws = await self.docker._websocket(path, **params)
return ws

Expand Down Expand Up @@ -296,7 +296,7 @@ async def port(self, private_port):

def stats(self, *, stream=True):
cm = self.docker._query(
"containers/{self._id}/stats".format(self=self),
f"containers/{self._id}/stats",
params={"stream": "1" if stream else "0"},
)
if stream:
Expand Down
2 changes: 1 addition & 1 deletion aiodocker/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def _canonicalize_url(
)
)
else:
return URL("{self.docker_host}/{path}".format(self=self, path=path))
return URL(f"{self.docker_host}/{path}")

async def _check_version(self) -> None:
if self.api_version == "auto":
Expand Down
20 changes: 7 additions & 13 deletions aiodocker/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .utils import clean_map, compose_auth_header


class DockerImages(object):
class DockerImages:
def __init__(self, docker):
self.docker = docker

Expand All @@ -38,7 +38,7 @@ async def inspect(self, name: str) -> Mapping:
Args:
name: name of the image
"""
response = await self.docker._query_json("images/{name}/json".format(name=name))
response = await self.docker._query_json(f"images/{name}/json")
return response

async def get(self, name: str) -> Mapping:
Expand All @@ -51,9 +51,7 @@ async def get(self, name: str) -> Mapping:
return await self.inspect(name)

async def history(self, name: str) -> Mapping:
response = await self.docker._query_json(
"images/{name}/history".format(name=name)
)
response = await self.docker._query_json(f"images/{name}/history")
return response

@overload
Expand Down Expand Up @@ -179,7 +177,7 @@ def push( # noqa: F811
)
headers["X-Registry-Auth"] = compose_auth_header(auth, registry)
cm = self.docker._query(
"images/{name}/push".format(name=name),
f"images/{name}/push",
"POST",
params=params,
headers=headers,
Expand All @@ -201,7 +199,7 @@ async def tag(self, name: str, repo: str, *, tag: str = None) -> bool:
params["tag"] = tag

async with self.docker._query(
"images/{name}/tag".format(name=name),
f"images/{name}/tag",
"POST",
params=params,
headers={"content-type": "application/json"},
Expand All @@ -225,9 +223,7 @@ async def delete(
List of deleted images
"""
params = {"force": force, "noprune": noprune}
return await self.docker._query_json(
"images/{name}".format(name=name), "DELETE", params=params
)
return await self.docker._query_json(f"images/{name}", "DELETE", params=params)

@staticmethod
async def _stream(fileobj: BinaryIO) -> AsyncIterator[bytes]:
Expand Down Expand Up @@ -364,9 +360,7 @@ def export_image(self, name: str):
Returns:
Streamreader of tarball image
"""
return _ExportCM(
self.docker._query("images/{name}/get".format(name=name), "GET")
)
return _ExportCM(self.docker._query(f"images/{name}/get", "GET"))

def import_image(self, data, stream: bool = False):
"""
Expand Down
2 changes: 1 addition & 1 deletion aiodocker/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def run(self, **params: Any) -> None:
params2 = ChainMap(forced_params, params, default_params)
try:
self.response = await self.docker._query(
"containers/{self.container._id}/logs".format(self=self), params=params2
f"containers/{self.container._id}/logs", params=params2
)
assert self.response is not None
while True:
Expand Down
14 changes: 5 additions & 9 deletions aiodocker/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ async def create(self, config: Dict[str, Any]) -> "DockerNetwork":
return DockerNetwork(self.docker, data["Id"])

async def get(self, net_specs: str) -> "DockerNetwork":
data = await self.docker._query_json(
"networks/{net_specs}".format(net_specs=net_specs), method="GET"
)
data = await self.docker._query_json(f"networks/{net_specs}", method="GET")
return DockerNetwork(self.docker, data["Id"])


Expand All @@ -49,25 +47,23 @@ def __init__(self, docker, id_):
self.id = id_

async def show(self) -> Dict[str, Any]:
data = await self.docker._query_json("networks/{self.id}".format(self=self))
data = await self.docker._query_json(f"networks/{self.id}")
return data

async def delete(self) -> bool:
async with self.docker._query(
"networks/{self.id}".format(self=self), method="DELETE"
) as resp:
async with self.docker._query(f"networks/{self.id}", method="DELETE") as resp:
return resp.status == 204

async def connect(self, config: Dict[str, Any]) -> None:
bconfig = json.dumps(config, sort_keys=True).encode("utf-8")
await self.docker._query_json(
"networks/{self.id}/connect".format(self=self), method="POST", data=bconfig
f"networks/{self.id}/connect", method="POST", data=bconfig
)

async def disconnect(self, config: Dict[str, Any]) -> None:
bconfig = json.dumps(config, sort_keys=True).encode("utf-8")
await self.docker._query_json(
"networks/{self.id}/disconnect".format(self=self),
f"networks/{self.id}/disconnect",
method="POST",
data=bconfig,
)
10 changes: 4 additions & 6 deletions aiodocker/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .utils import clean_filters


class DockerSwarmNodes(object):
class DockerSwarmNodes:
def __init__(self, docker):
self.docker = docker

Expand Down Expand Up @@ -36,9 +36,7 @@ async def inspect(self, *, node_id: str) -> Mapping[str, Any]:
node_id: The ID or name of the node
"""

response = await self.docker._query_json(
"nodes/{node_id}".format(node_id=node_id), method="GET"
)
response = await self.docker._query_json(f"nodes/{node_id}", method="GET")
return response

async def update(
Expand All @@ -62,7 +60,7 @@ async def update(
assert spec["Availability"] in {"active", "pause", "drain"}

response = await self.docker._query_json(
"nodes/{node_id}/update".format(node_id=node_id),
f"nodes/{node_id}/update",
method="POST",
params=params,
data=spec,
Expand All @@ -80,6 +78,6 @@ async def remove(self, *, node_id: str, force: bool = False) -> Mapping[str, Any
params = {"force": force}

response = await self.docker._query_json(
"nodes/{node_id}".format(node_id=node_id), method="DELETE", params=params
f"nodes/{node_id}", method="DELETE", params=params
)
return response
12 changes: 4 additions & 8 deletions aiodocker/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .utils import clean_filters, clean_map


class DockerSecrets(object):
class DockerSecrets:
def __init__(self, docker):
self.docker = docker

Expand Down Expand Up @@ -82,9 +82,7 @@ async def inspect(self, secret_id: str) -> Mapping[str, Any]:
a dict with info about a secret
"""

response = await self.docker._query_json(
"secrets/{secret_id}".format(secret_id=secret_id), method="GET"
)
response = await self.docker._query_json(f"secrets/{secret_id}", method="GET")
return response

async def delete(self, secret_id: str) -> bool:
Expand All @@ -98,9 +96,7 @@ async def delete(self, secret_id: str) -> bool:
True if successful
"""

async with self.docker._query(
"secrets/{secret_id}".format(secret_id=secret_id), method="DELETE"
):
async with self.docker._query(f"secrets/{secret_id}", method="DELETE"):
return True

async def update(
Expand Down Expand Up @@ -155,7 +151,7 @@ async def update(
request_data = json.dumps(clean_map(spec))

await self.docker._query_json(
"secrets/{secret_id}/update".format(secret_id=secret_id),
f"secrets/{secret_id}/update",
method="POST",
data=request_data,
params=params,
Expand Down

0 comments on commit 3e40e64

Please sign in to comment.