mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 03:46:55 +00:00
Address some pylint issues (#1155)
* Address cyclic-import. * Address redefined-builtin. * Address redefined-argument-from-local. * Address many redefined-outer-name. * Address pointless-string-statement. * No longer needed due to separate bugfix. * Address useless-return. * Address possibly-used-before-assignment. * Add TODOs. * Address super-init-not-called. * Address function-redefined. * Address unspecified-encoding. * Clean up more imports.
This commit is contained in:
@@ -86,13 +86,13 @@ except ImportError:
|
||||
|
||||
def fail_on_missing_imports():
|
||||
if REQUESTS_IMPORT_ERROR is not None:
|
||||
from .errors import MissingRequirementException
|
||||
from .errors import MissingRequirementException # pylint: disable=cyclic-import
|
||||
|
||||
raise MissingRequirementException(
|
||||
"You have to install requests", "requests", REQUESTS_IMPORT_ERROR
|
||||
)
|
||||
if URLLIB3_IMPORT_ERROR is not None:
|
||||
from .errors import MissingRequirementException
|
||||
from .errors import MissingRequirementException # pylint: disable=cyclic-import
|
||||
|
||||
raise MissingRequirementException(
|
||||
"You have to install urllib3", "urllib3", URLLIB3_IMPORT_ERROR
|
||||
|
||||
@@ -289,14 +289,14 @@ class APIClient(_Session, DaemonApiMixin):
|
||||
except _HTTPError as e:
|
||||
create_api_error_from_http_exception(e)
|
||||
|
||||
def _result(self, response, json=False, binary=False):
|
||||
if json and binary:
|
||||
def _result(self, response, get_json=False, get_binary=False):
|
||||
if get_json and get_binary:
|
||||
raise AssertionError("json and binary must not be both True")
|
||||
self._raise_for_status(response)
|
||||
|
||||
if json:
|
||||
if get_json:
|
||||
return response.json()
|
||||
if binary:
|
||||
if get_binary:
|
||||
return response.content
|
||||
return response.text
|
||||
|
||||
@@ -360,12 +360,12 @@ class APIClient(_Session, DaemonApiMixin):
|
||||
else:
|
||||
# Response is not chunked, meaning we probably
|
||||
# encountered an error immediately
|
||||
yield self._result(response, json=decode)
|
||||
yield self._result(response, get_json=decode)
|
||||
|
||||
def _multiplexed_buffer_helper(self, response):
|
||||
"""A generator of multiplexed data blocks read from a buffered
|
||||
response."""
|
||||
buf = self._result(response, binary=True)
|
||||
buf = self._result(response, get_binary=True)
|
||||
buf_length = len(buf)
|
||||
walker = 0
|
||||
while True:
|
||||
@@ -478,7 +478,7 @@ class APIClient(_Session, DaemonApiMixin):
|
||||
return (
|
||||
self._stream_raw_result(res)
|
||||
if stream
|
||||
else self._result(res, binary=True)
|
||||
else self._result(res, get_binary=True)
|
||||
)
|
||||
|
||||
self._raise_for_status(res)
|
||||
@@ -551,13 +551,13 @@ class APIClient(_Session, DaemonApiMixin):
|
||||
def get_binary(self, pathfmt, *args, **kwargs):
|
||||
return self._result(
|
||||
self._get(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
||||
binary=True,
|
||||
get_binary=True,
|
||||
)
|
||||
|
||||
def get_json(self, pathfmt, *args, **kwargs):
|
||||
return self._result(
|
||||
self._get(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
||||
json=True,
|
||||
get_json=True,
|
||||
)
|
||||
|
||||
def get_text(self, pathfmt, *args, **kwargs):
|
||||
@@ -581,7 +581,7 @@ class APIClient(_Session, DaemonApiMixin):
|
||||
def delete_json(self, pathfmt, *args, **kwargs):
|
||||
return self._result(
|
||||
self._delete(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
||||
json=True,
|
||||
get_json=True,
|
||||
)
|
||||
|
||||
def post_call(self, pathfmt, *args, **kwargs):
|
||||
@@ -603,7 +603,7 @@ class APIClient(_Session, DaemonApiMixin):
|
||||
self._post_json(
|
||||
self._url(pathfmt, *args, versioned_api=True), data, **kwargs
|
||||
),
|
||||
binary=True,
|
||||
get_binary=True,
|
||||
)
|
||||
|
||||
def post_json_to_json(self, pathfmt, *args, **kwargs):
|
||||
@@ -612,7 +612,7 @@ class APIClient(_Session, DaemonApiMixin):
|
||||
self._post_json(
|
||||
self._url(pathfmt, *args, versioned_api=True), data, **kwargs
|
||||
),
|
||||
json=True,
|
||||
get_json=True,
|
||||
)
|
||||
|
||||
def post_json_to_text(self, pathfmt, *args, **kwargs):
|
||||
@@ -670,5 +670,5 @@ class APIClient(_Session, DaemonApiMixin):
|
||||
def post_to_json(self, pathfmt, *args, **kwargs):
|
||||
return self._result(
|
||||
self._post(self._url(pathfmt, *args, versioned_api=True), **kwargs),
|
||||
json=True,
|
||||
get_json=True,
|
||||
)
|
||||
|
||||
@@ -33,7 +33,7 @@ class DaemonApiMixin(object):
|
||||
If the server returns an error.
|
||||
"""
|
||||
url = self._url("/system/df")
|
||||
return self._result(self._get(url), True)
|
||||
return self._result(self._get(url), get_json=True)
|
||||
|
||||
def info(self):
|
||||
"""
|
||||
@@ -47,7 +47,7 @@ class DaemonApiMixin(object):
|
||||
:py:class:`docker.errors.APIError`
|
||||
If the server returns an error.
|
||||
"""
|
||||
return self._result(self._get(self._url("/info")), True)
|
||||
return self._result(self._get(self._url("/info")), get_json=True)
|
||||
|
||||
def login(
|
||||
self,
|
||||
@@ -108,7 +108,7 @@ class DaemonApiMixin(object):
|
||||
response = self._post_json(self._url("/auth"), data=req_data)
|
||||
if response.status_code == 200:
|
||||
self._auth_configs.add_auth(registry or auth.INDEX_NAME, req_data)
|
||||
return self._result(response, json=True)
|
||||
return self._result(response, get_json=True)
|
||||
|
||||
def ping(self):
|
||||
"""
|
||||
@@ -137,4 +137,4 @@ class DaemonApiMixin(object):
|
||||
If the server returns an error.
|
||||
"""
|
||||
url = self._url("/version", versioned_api=api_version)
|
||||
return self._result(self._get(url), json=True)
|
||||
return self._result(self._get(url), get_json=True)
|
||||
|
||||
@@ -166,7 +166,7 @@ class AuthConfig(dict):
|
||||
if not config_file:
|
||||
return cls({}, credstore_env)
|
||||
try:
|
||||
with open(config_file) as f:
|
||||
with open(config_file, "rt", encoding="utf-8") as f:
|
||||
config_dict = json.load(f)
|
||||
except (IOError, KeyError, ValueError) as e:
|
||||
# Likely missing new Docker config file or it is in an
|
||||
@@ -351,7 +351,7 @@ def _load_legacy_config(config_file):
|
||||
log.debug("Attempting to parse legacy auth file format")
|
||||
try:
|
||||
data = []
|
||||
with open(config_file) as f:
|
||||
with open(config_file, "rt", encoding="utf-8") as f:
|
||||
for line in f.readlines():
|
||||
data.append(line.strip().split(" = ")[1])
|
||||
if len(data) < 2:
|
||||
|
||||
@@ -151,7 +151,7 @@ class ContextAPI(object):
|
||||
if filename == METAFILE:
|
||||
filepath = os.path.join(dirname, filename)
|
||||
try:
|
||||
with open(filepath, "r") as f:
|
||||
with open(filepath, "rt", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
name = data["Name"]
|
||||
if name == "default":
|
||||
|
||||
@@ -32,7 +32,7 @@ def get_current_context_name_with_source():
|
||||
docker_cfg_path = find_config_file()
|
||||
if docker_cfg_path:
|
||||
try:
|
||||
with open(docker_cfg_path) as f:
|
||||
with open(docker_cfg_path, "rt", encoding="utf-8") as f:
|
||||
return (
|
||||
json.load(f).get("currentContext", "default"),
|
||||
f"configuration file {docker_cfg_path}",
|
||||
@@ -53,7 +53,7 @@ def write_context_name_to_docker_config(name=None):
|
||||
config = {}
|
||||
if docker_cfg_path:
|
||||
try:
|
||||
with open(docker_cfg_path) as f:
|
||||
with open(docker_cfg_path, "rt", encoding="utf-8") as f:
|
||||
config = json.load(f)
|
||||
except Exception as e:
|
||||
return e
|
||||
@@ -67,7 +67,7 @@ def write_context_name_to_docker_config(name=None):
|
||||
if not docker_cfg_path:
|
||||
docker_cfg_path = get_default_config_file()
|
||||
try:
|
||||
with open(docker_cfg_path, "w") as f:
|
||||
with open(docker_cfg_path, "wt", encoding="utf-8") as f:
|
||||
json.dump(config, f, indent=4)
|
||||
except Exception as e:
|
||||
return e
|
||||
|
||||
@@ -133,7 +133,7 @@ class Context(object):
|
||||
|
||||
metadata = {}
|
||||
try:
|
||||
with open(meta_file) as f:
|
||||
with open(meta_file, "rt", encoding="utf-8") as f:
|
||||
metadata = json.load(f)
|
||||
except (OSError, KeyError, ValueError) as e:
|
||||
# unknown format
|
||||
@@ -189,7 +189,7 @@ class Context(object):
|
||||
meta_dir = get_meta_dir(self.name)
|
||||
if not os.path.isdir(meta_dir):
|
||||
os.makedirs(meta_dir)
|
||||
with open(get_meta_file(self.name), "w") as f:
|
||||
with open(get_meta_file(self.name), "wt", encoding="utf-8") as f:
|
||||
f.write(json.dumps(self.Metadata))
|
||||
|
||||
tls_dir = get_tls_dir(self.name)
|
||||
|
||||
@@ -222,7 +222,7 @@ class SSHHTTPAdapter(BaseHTTPAdapter):
|
||||
ssh_config_file = os.path.expanduser("~/.ssh/config")
|
||||
if os.path.exists(ssh_config_file):
|
||||
conf = paramiko.SSHConfig()
|
||||
with open(ssh_config_file) as f:
|
||||
with open(ssh_config_file, "rt", encoding="utf-8") as f:
|
||||
conf.parse(f)
|
||||
host_config = conf.lookup(base_url.hostname)
|
||||
if "proxycommand" in host_config:
|
||||
|
||||
@@ -12,12 +12,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
""" Resolves OpenSSL issues in some servers:
|
||||
https://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/
|
||||
https://github.com/kennethreitz/requests/pull/799
|
||||
"""
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._version import (
|
||||
LooseVersion,
|
||||
)
|
||||
@@ -26,6 +20,11 @@ from .._import_helper import HTTPAdapter, urllib3
|
||||
from .basehttpadapter import BaseHTTPAdapter
|
||||
|
||||
|
||||
# Resolves OpenSSL issues in some servers:
|
||||
# https://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/
|
||||
# https://github.com/kennethreitz/requests/pull/799
|
||||
|
||||
|
||||
PoolManager = urllib3.poolmanager.PoolManager
|
||||
|
||||
|
||||
|
||||
@@ -271,7 +271,7 @@ def process_dockerfile(dockerfile, path):
|
||||
0
|
||||
] or os.path.relpath(abs_dockerfile, path).startswith(".."):
|
||||
# Dockerfile not in context - read data to insert into tar later
|
||||
with open(abs_dockerfile) as df:
|
||||
with open(abs_dockerfile, "rt", encoding="utf-8") as df:
|
||||
return (f".dockerfile.{random.getrandbits(160):x}", df.read())
|
||||
|
||||
# Dockerfile is inside the context - return path relative to context root
|
||||
|
||||
@@ -80,7 +80,7 @@ def load_general_config(config_path=None):
|
||||
return {}
|
||||
|
||||
try:
|
||||
with open(config_file) as f:
|
||||
with open(config_file, "rt", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except (IOError, ValueError) as e:
|
||||
# In the case of a legacy `.dockercfg` file, we will not
|
||||
|
||||
@@ -10,9 +10,6 @@
|
||||
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
|
||||
# Do not use this from other collections or standalone plugins/modules!
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
"""Filename matching with shell patterns.
|
||||
|
||||
fnmatch(FILENAME, PATTERN) matches according to the local convention.
|
||||
@@ -25,6 +22,8 @@ The function translate(PATTERN) returns a regular expression
|
||||
corresponding to PATTERN. (It does not compile it.)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
|
||||
|
||||
@@ -452,7 +452,7 @@ def parse_env_file(env_file):
|
||||
"""
|
||||
environment = {}
|
||||
|
||||
with open(env_file, "r") as f:
|
||||
with open(env_file, "rt", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
|
||||
if line[0] == "#":
|
||||
|
||||
Reference in New Issue
Block a user