Skip to content

Commit

Permalink
Improve pylint unspecified-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-eq committed Jan 12, 2024
1 parent 8dd0c84 commit 6e99cc5
Show file tree
Hide file tree
Showing 20 changed files with 58 additions and 41 deletions.
4 changes: 3 additions & 1 deletion komodo/extract_dep_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@


def run(pkgfile, base_pkgfile, repofile, outfile=None):
with open(pkgfile) as p, open(base_pkgfile) as bp, open(repofile) as r:
with open(pkgfile, encoding="utf-8") as p, open(
base_pkgfile, encoding="utf-8"
) as bp, open(repofile, encoding="utf-8") as r:
yaml = YAML()
pkgs, base_pkgs, repo = (
yaml.load(p),
Expand Down
2 changes: 1 addition & 1 deletion komodo/lint_maturity.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_packages_info(release_file: ReleaseFile, tag_exceptions_package):


def read_yaml_file(file_path):
with open(file_path) as yml_file:
with open(file_path, encoding="utf-8") as yml_file:
return yaml.safe_load(yml_file)


Expand Down
2 changes: 1 addition & 1 deletion komodo/maintainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def maintainers(pkgfile, repofile):
with open(pkgfile) as p, open(repofile) as r:
with open(pkgfile, encoding="utf-8") as p, open(repofile, encoding="utf-8") as r:
yml = YAML()
pkgs, repo = yml.load(p), yml.load(r)

Expand Down
4 changes: 2 additions & 2 deletions komodo/post_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_inline_messages(messages, dst_path):
for msg in messages:
filename = hashlib.md5(msg.encode()).hexdigest()
filename = "0Z" + filename # for orderings sake
with open(os.path.join(dst_path, filename), "w") as new_file:
with open(os.path.join(dst_path, filename), "w", encoding="utf-8") as new_file:
new_file.write(msg)


Expand Down Expand Up @@ -80,7 +80,7 @@ def main(args=None):
if not os.path.isfile(args.motd_db):
msg = f"ERROR: The message-database {args.motd_db} was not found"
raise SystemExit(msg)
with open(args.motd_db) as motd_db_file:
with open(args.motd_db, encoding="utf-8") as motd_db_file:
yml = YAML()
motd_db = yml.load(motd_db_file)
motd_path = os.path.dirname(args.motd_db)
Expand Down
8 changes: 4 additions & 4 deletions komodo/prettier.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def prettified_yaml(filepath, check_only=True):
If `check_only` is False, the input file will be "prettified" in place if necessary.
"""
print(f"Checking {filepath}... ", end="")
with open(file=filepath) as input_file:
with open(file=filepath, encoding="utf-8") as input_file:
yaml_original = input_file.read()

yaml_input = load_yaml(filepath)
Expand All @@ -90,7 +90,7 @@ def prettified_yaml(filepath, check_only=True):
if yaml_prettified_string != yaml_original:
print(f"{'would be' if check_only else ''} reformatted!")
if not check_only:
with open(filepath, "w") as fh:
with open(filepath, "w", encoding="utf-8") as fh:
fh.write(yaml_prettified_string)
return False

Expand All @@ -107,7 +107,7 @@ def write_to_string(repository, check_type=True):

def write_to_file(repository, filename, check_type=True):
output_str = write_to_string(repository, check_type)
with open(filename, mode="w") as output_file:
with open(filename, mode="w", encoding="utf-8") as output_file:
output_file.write(output_str)


Expand All @@ -125,7 +125,7 @@ def load_yaml(filename):
ruamel_instance.width = 1000 # Avoid ruamel wrapping long

try:
with open(filename) as repo_handle:
with open(filename, encoding="utf-8") as repo_handle:
return ruamel_instance.load(repo_handle)

except (
Expand Down
6 changes: 4 additions & 2 deletions komodo/reverse_dep_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@


def run(base_pkgfile, repofile, dot, display_pkg, out):
with open(base_pkgfile) as bp, open(repofile) as r:
with open(base_pkgfile, encoding="utf-8") as bp, open(
repofile, encoding="utf-8"
) as r:
base_pkgs, repo = yaml.safe_load(bp), yaml.safe_load(r)

reverse = build_reverse(base_pkgs, repo)
Expand Down Expand Up @@ -150,7 +152,7 @@ def main():
print(f"you entered {pkg}")

if args.out:
with open(args.out, "w") as out:
with open(args.out, "w", encoding="utf-8") as out:
run(args.base_pkgs, args.repo, args.dot, pkg, out)
elif args.display_dot:
try:
Expand Down
2 changes: 1 addition & 1 deletion komodo/shebang.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fixup_python_shebangs(prefix, release):
# executables with wrong shebang
for bin_ in os.listdir(binpath):
try:
with open(os.path.join(binpath, bin_)) as f:
with open(os.path.join(binpath, bin_), encoding="utf-8") as f:
shebang = f.readline().strip()
if _is_shebang(shebang):
bins_.append(bin_)
Expand Down
12 changes: 8 additions & 4 deletions komodo/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ def create_activator_switch(data, prefix, release):

os.makedirs(release_path)

with open(os.path.join(release_path, "enable"), "w") as activator, open(
data.get("activator_switch.tmpl"),
with open(
os.path.join(release_path, "enable"), "w", encoding="utf-8"
) as activator, open(
data.get("activator_switch.tmpl"), encoding="utf-8"
) as activator_tmpl:
activator.write(
Template(activator_tmpl.read(), keep_trailing_newline=True).render(
Expand All @@ -44,8 +46,10 @@ def create_activator_switch(data, prefix, release):
),
)

with open(os.path.join(release_path, "enable.csh"), "w") as activator, open(
data.get("activator_switch.csh.tmpl"),
with open(
os.path.join(release_path, "enable.csh"), "w", encoding="utf-8"
) as activator, open(
data.get("activator_switch.csh.tmpl"), encoding="utf-8"
) as activator_tmpl:
activator.write(
Template(activator_tmpl.read(), keep_trailing_newline=True).render(
Expand Down
2 changes: 1 addition & 1 deletion komodo/symlink/create_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def symlink_main():
if not os.path.isfile(args.config):
sys.exit(f"The file {args.config} can not be found")

with open(args.config) as input_file:
with open(args.config, encoding="utf-8") as input_file:
input_dict = json.load(input_file)

errors = verify_integrity(input_dict)
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ disable = [
"too-many-locals",
"too-many-statements",
"unnecessary-lambda",
"unspecified-encoding",
"unused-argument",
"use-a-generator",
"use-implicit-booleaness-not-comparison",
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ def _load_yaml(filename):
offset=2,
)
ruamel_instance.width = 1000 # Avoid ruamel wrapping long
with open(filename) as repo_handle:
with open(filename, encoding="utf-8") as repo_handle:
return ruamel_instance.load(repo_handle)
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_main(args, tmpdir):
assert os.path.exists(downloaded_file)
assert os.access(downloaded_file, os.X_OK)

with open(os.path.join(release_path, release_name)) as releasedoc:
with open(os.path.join(release_path, release_name), encoding="utf-8") as releasedoc:
releasedoc_content = releasedoc.read()

# test specifically for the regression introduced by
Expand Down
10 changes: 5 additions & 5 deletions tests/test_enable_scipts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


def _load_envs():
with open("pre_source.env") as pre:
with open("pre_source.env", encoding="utf-8") as pre:
pre_env = json.loads(pre.read())
with open("sourced.env") as sourced:
with open("sourced.env", encoding="utf-8") as sourced:
sourced_env = json.loads(sourced.read())
with open("post_disable.env") as post:
with open("post_disable.env", encoding="utf-8") as post:
post_env = json.loads(post.read())

return pre_env, sourced_env, post_env
Expand All @@ -36,7 +36,7 @@ def test_enable_bash_nopresets(tmpdir):
with tmpdir.as_cwd():
Path("bleeding").mkdir()
create_enable_scripts(komodo_prefix="prefix", komodo_release="bleeding")
with open("test_enable.sh", "w") as test_file:
with open("test_enable.sh", "w", encoding="utf-8") as test_file:
test_file.write(
TEST_SCRIPT_SIMPLE.format(
set_envs=CLEAN_BASH_ENV,
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_enable_csh_no_presets(tmpdir):
with tmpdir.as_cwd():
Path("bleeding").mkdir()
create_enable_scripts(komodo_prefix="prefix", komodo_release="bleeding")
with open("test_enable.sh", "w") as test_file:
with open("test_enable.sh", "w", encoding="utf-8") as test_file:
test_file.write(
TEST_SCRIPT_SIMPLE.format(
set_envs=CLEAN_CSH_ENV,
Expand Down
10 changes: 6 additions & 4 deletions tests/test_link_io_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_integration(tmpdir):
test_folder = _get_test_root()
shutil.copy(os.path.join(test_folder, "data/links.json"), tmpdir)
with tmpdir.as_cwd():
with open("links.json") as link_file:
with open("links.json", encoding="utf-8") as link_file:
input_dict = json.load(link_file)

input_dict["root_folder"] = os.path.realpath(input_dict["root_folder"])
Expand All @@ -150,7 +150,7 @@ def test_root_links(tmpdir):
test_folder = _get_test_root()
shutil.copy(os.path.join(test_folder, "data/links.json"), tmpdir)
with tmpdir.as_cwd():
with open("links.json") as link_file:
with open("links.json", encoding="utf-8") as link_file:
input_dict = json.load(link_file)

assert_root_nodes(input_dict)
Expand Down Expand Up @@ -257,12 +257,14 @@ def test_executables(tmpdir):
sys.argv = ["run", "links_test.json"]
try:
test_folder = _get_test_root()
with open(os.path.join(test_folder, "data/links_full.json")) as input_file:
with open(
os.path.join(test_folder, "data/links_full.json"), encoding="utf-8"
) as input_file:
input_data = json.load(input_file)

with tmpdir.as_cwd():
input_data["root_folder"] = str(tmpdir)
with open("links_test.json", "w") as test_file:
with open("links_test.json", "w", encoding="utf-8") as test_file:
test_file.write(json.dumps(input_data))

with pytest.raises(SystemExit):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_lint_maturity.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _create_tmp_test_files(release_sample, file_names_sample):
for file_name in file_names_sample:
list_files.append(folder_name + file_name)

with open(folder_name + file_name, "w") as file_sample:
with open(folder_name + file_name, "w", encoding="utf-8") as file_sample:
file_sample.write(release_sample)

return list_files
Expand Down
16 changes: 12 additions & 4 deletions tests/test_non_deployed.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ def test_non_deployed(tmpdir):
os.makedirs(os.path.join(install_root, "2019.11.05-py38-rhel7/root"))

# Create matrix files
with open(os.path.join(release_folder, "2019.11.05.yml"), "a"):
with open(
os.path.join(release_folder, "2019.11.05.yml"), "a", encoding="utf-8"
):
pass
with open(os.path.join(release_folder, "2019.12.02.yml"), "a"):
with open(
os.path.join(release_folder, "2019.12.02.yml"), "a", encoding="utf-8"
):
pass

expected = ("2019.12.02",)
Expand All @@ -74,9 +78,13 @@ def test_links_ignored(tmpdir):
),
root=install_root,
)
with open(os.path.join(release_folder, "2019.12.01.yml"), "a"):
with open(
os.path.join(release_folder, "2019.12.01.yml"), "a", encoding="utf-8"
):
pass
with open(os.path.join(release_folder, "2022.02.02.yml"), "a"):
with open(
os.path.join(release_folder, "2022.02.02.yml"), "a", encoding="utf-8"
):
pass

non_deployed = fetch_non_deployed(install_root, release_folder)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_post_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def test_get_messages():
motd_db_file = os.path.join(_get_test_root(), "data/test_messages/motd_db.yml")
yaml = YAML()
with open(motd_db_file) as f:
with open(motd_db_file, encoding="utf-8") as f:
motd_db = yaml.load(f)

release = "2020.01.01-py27-rhel6"
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_main_success_symlinks(tmpdir):
os.path.join("2020.01.01-py27-rhel6", "motd", "scripts"),
)
yaml = YAML()
with open(motd_db_file) as f:
with open(motd_db_file, encoding="utf-8") as f:
motd_db = yaml.load(f)
msg = motd_db["stable"]["inline"][0]
filename = hashlib.md5(msg.encode()).hexdigest()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_release_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_write_to_file(tmpdir):
unused_versions = find_unused_versions(used_versions, repository)
remove_unused_versions(repository, unused_versions)
write_to_file(repository, "output_repo.yml")
with open("output_repo.yml") as output:
with open("output_repo.yml", encoding="utf-8") as output:
assert output.read() == expected_result


Expand Down
2 changes: 1 addition & 1 deletion tests/test_release_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_build_release_matrix_py_coords(
build_matrix_file(release_base, release_folder, builtins, py_coords_input)
new_release_file = f"{release_base}.yml"
assert os.path.isfile(new_release_file)
with open(new_release_file) as f:
with open(new_release_file, encoding="utf-8") as f:
release_matrix = yaml.safe_load(f)

assert release_matrix["lib1"] == builtins["lib1"]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_up_to_date_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def test_check_up_to_date_file_output(monkeypatch, tmpdir, capsys):
)
result = {}

with open(f"{base_path}/repository_file.yml") as fin:
with open(f"{base_path}/repository_file.yml", encoding="utf-8") as fin:
result["updated_repo"] = yaml.load(fin)

assert result == {
Expand Down Expand Up @@ -441,9 +441,9 @@ def test_run_up_to_date(

result = {}
yaml = YAML()
with open("new_file") as fin:
with open("new_file", encoding="utf-8") as fin:
result["release"] = yaml.load(fin)
with open("repository_file") as fin:
with open("repository_file", encoding="utf-8") as fin:
result["repo"] = yaml.load(fin)

assert result == expected
Expand Down

0 comments on commit 6e99cc5

Please sign in to comment.