Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ResourceWarning in tests (and more) #225

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions aeidon/agents/test/test_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ def test_open_main(self):

def test_open_main__bom(self):
path = self.new_subrip_file()
blob = open(path, "rb").read()
open(path, "wb").write(codecs.BOM_UTF8 + blob)
with open(path, "rb") as f:
blob = f.read()
with open(path, "wb") as f:
f.write(codecs.BOM_UTF8 + blob)
self.project.open_main(path, "ascii")
assert self.project.subtitles
assert self.project.main_file.encoding == "utf_8_sig"
Expand Down Expand Up @@ -61,8 +63,10 @@ def test_open_translation__align_position(self):

def test_open_translation__bom(self):
path = self.new_subrip_file()
blob = open(path, "rb").read()
open(path, "wb").write(codecs.BOM_UTF8 + blob)
with open(path, "rb") as f:
blob = f.read()
with open(path, "wb") as f:
f.write(codecs.BOM_UTF8 + blob)
self.project.open_translation(path, "ascii")
assert self.project.subtitles
assert self.project.tran_file.encoding == "utf_8_sig"
3 changes: 2 additions & 1 deletion aeidon/files/test/test_ass.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ def test_read(self):

def test_write(self):
self.file.write(self.file.read(), aeidon.documents.MAIN)
text = open(self.file.path, "r").read().strip()
with open(self.file.path, "r") as f:
text = f.read().strip()
assert text == self.get_sample_text(self.format)
3 changes: 2 additions & 1 deletion aeidon/files/test/test_lrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ def test_read(self):

def test_write(self):
self.file.write(self.file.read(), aeidon.documents.MAIN)
text = open(self.file.path, "r").read().strip()
with open(self.file.path, "r") as f:
text = f.read().strip()
assert text == self.get_sample_text(self.format, self.name)
3 changes: 2 additions & 1 deletion aeidon/files/test/test_microdvd.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ def test_read(self):

def test_write(self):
self.file.write(self.file.read(), aeidon.documents.MAIN)
text = open(self.file.path, "r").read().strip()
with open(self.file.path, "r") as f:
text = f.read().strip()
assert text == self.get_sample_text(self.format)
3 changes: 2 additions & 1 deletion aeidon/files/test/test_mpl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ def test_read(self):

def test_write(self):
self.file.write(self.file.read(), aeidon.documents.MAIN)
text = open(self.file.path, "r").read().strip()
with open(self.file.path, "r") as f:
text = f.read().strip()
assert text == self.get_sample_text(self.format)
3 changes: 2 additions & 1 deletion aeidon/files/test/test_ssa.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ def test_read(self):

def test_write(self):
self.file.write(self.file.read(), aeidon.documents.MAIN)
text = open(self.file.path, "r").read().strip()
with open(self.file.path, "r") as f:
text = f.read().strip()
assert text == self.get_sample_text(self.format)
3 changes: 2 additions & 1 deletion aeidon/files/test/test_subrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def test_read(self):

def test_write(self):
self.file.write(self.file.read(), aeidon.documents.MAIN)
text = open(self.file.path, "r").read().strip()
with open(self.file.path, "r") as f:
text = f.read().strip()
assert text == self.get_sample_text(self.format, self.name)


Expand Down
3 changes: 2 additions & 1 deletion aeidon/files/test/test_subviewer2.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ def test_read(self):

def test_write(self):
self.file.write(self.file.read(), aeidon.documents.MAIN)
text = open(self.file.path, "r").read().strip()
with open(self.file.path, "r") as f:
text = f.read().strip()
assert text == self.get_sample_text(self.format)
3 changes: 2 additions & 1 deletion aeidon/files/test/test_tmplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def test_read(self):

def test_write(self):
self.file.write(self.file.read(), aeidon.documents.MAIN)
text = open(self.file.path, "r").read().strip()
with open(self.file.path, "r") as f:
text = f.read().strip()
assert text == self.get_sample_text(self.format, self.name)


Expand Down
3 changes: 2 additions & 1 deletion aeidon/files/test/test_webvtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ def test_read(self):

def test_write(self):
self.file.write(self.file.read(), aeidon.documents.MAIN)
text = open(self.file.path, "r").read().strip()
with open(self.file.path, "r") as f:
text = f.read().strip()
assert text == self.get_sample_text(self.format, self.name)
30 changes: 20 additions & 10 deletions aeidon/test/test_encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,40 +163,50 @@ def test_detect_bom__none(self):
@patch("aeidon.encodings.is_valid_code", lambda x: True)
def test_detect_bom__utf_16_be(self):
path = self.new_subrip_file()
blob = open(path, "rb").read()
open(path, "wb").write(codecs.BOM_UTF16_BE + blob)
with open(path, "rb") as f:
blob = f.read()
with open(path, "wb") as f:
f.write(codecs.BOM_UTF16_BE + blob)
encoding = aeidon.encodings.detect_bom(path)
assert encoding == "utf_16_be"

@patch("aeidon.encodings.is_valid_code", lambda x: True)
def test_detect_bom__utf_16_le(self):
path = self.new_subrip_file()
blob = open(path, "rb").read()
open(path, "wb").write(codecs.BOM_UTF16_LE + blob)
with open(path, "rb") as f:
blob = f.read()
with open(path, "wb") as f:
f.write(codecs.BOM_UTF16_LE + blob)
encoding = aeidon.encodings.detect_bom(path)
assert encoding == "utf_16_le"

@patch("aeidon.encodings.is_valid_code", lambda x: True)
def test_detect_bom__utf_32_be(self):
path = self.new_subrip_file()
blob = open(path, "rb").read()
open(path, "wb").write(codecs.BOM_UTF32_BE + blob)
with open(path, "rb") as f:
blob = f.read()
with open(path, "wb") as f:
f.write(codecs.BOM_UTF32_BE + blob)
encoding = aeidon.encodings.detect_bom(path)
assert encoding == "utf_32_be"

@patch("aeidon.encodings.is_valid_code", lambda x: True)
def test_detect_bom__utf_32_le(self):
path = self.new_subrip_file()
blob = open(path, "rb").read()
open(path, "wb").write(codecs.BOM_UTF32_LE + blob)
with open(path, "rb") as f:
blob = f.read()
with open(path, "wb") as f:
f.write(codecs.BOM_UTF32_LE + blob)
encoding = aeidon.encodings.detect_bom(path)
assert encoding == "utf_32_le"

@patch("aeidon.encodings.is_valid_code", lambda x: True)
def test_detect_bom__utf_8_sig(self):
path = self.new_subrip_file()
blob = open(path, "rb").read()
open(path, "wb").write(codecs.BOM_UTF8 + blob)
with open(path, "rb") as f:
blob = f.read()
with open(path, "wb") as f:
f.write(codecs.BOM_UTF8 + blob)
encoding = aeidon.encodings.detect_bom(path)
assert encoding == "utf_8_sig"

Expand Down
6 changes: 4 additions & 2 deletions aeidon/test/test_temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ def test_create_directory(self):

def test_remove__directory(self):
path = aeidon.temp.create_directory()
open(os.path.join(path, "a"), "w").write("a")
open(os.path.join(path, "b"), "w").write("b")
with open(os.path.join(path, "a"), "w") as f:
f.write("a")
with open(os.path.join(path, "b"), "w") as f:
f.write("b")
os.makedirs(os.path.join(path, "c"))
aeidon.temp.remove(path)
assert not os.path.isdir(path)
Expand Down
43 changes: 26 additions & 17 deletions aeidon/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ def test_atomic_open__existing_file(self):
path = self.new_subrip_file()
with aeidon.util.atomic_open(path, "w") as f:
f.write("test\n")
text = open(path, "r").read()
with open(path, "r") as f:
text = f.read()
assert text == "test\n"

def test_atomic_open__no_existing_file(self):
path = aeidon.temp.create()
with aeidon.util.atomic_open(path, "w") as f:
f.write("test\n")
text = open(path, "r").read()
with open(path, "r") as f:
text = f.read()
assert text == "test\n"

def test_compare_versions(self):
Expand All @@ -48,19 +50,22 @@ def test_detect_format(self):

def test_detect_newlines__mac(self):
path = aeidon.temp.create()
open(path, "w", newline="").write("a\rb\rc\r")
with open(path, "w", newline="") as f:
f.write("a\rb\rc\r")
newlines = aeidon.util.detect_newlines(path)
assert newlines == aeidon.newlines.MAC

def test_detect_newlines__unix(self):
path = aeidon.temp.create()
open(path, "w", newline="").write("a\nb\nc\n")
with open(path, "w", newline="") as f:
f.write("a\nb\nc\n")
newlines = aeidon.util.detect_newlines(path)
assert newlines == aeidon.newlines.UNIX

def test_detect_newlines__windows(self):
path = aeidon.temp.create()
open(path, "w", newline="").write("a\r\nb\r\nc\r\n")
with open(path, "w", newline="") as f:
f.write("a\r\nb\r\nc\r\n")
newlines = aeidon.util.detect_newlines(path)
assert newlines == aeidon.newlines.WINDOWS

Expand Down Expand Up @@ -96,48 +101,52 @@ def test_get_unique__last(self):

def test_read__basic(self):
path = self.new_subrip_file()
text = open(path, "r", encoding="ascii").read().strip()
with open(path, "r", encoding="ascii") as f:
text = f.read().strip()
assert aeidon.util.read(path) == text

def test_read__fallback(self):
path = self.new_subrip_file()
open(path, "w", encoding="utf_8").write("\xc3\xb6\n")
with open(path, "w", encoding="utf_8") as f:
f.write("\xc3\xb6\n")
assert aeidon.util.read(path, "ascii") == "\xc3\xb6"

def test_readlines__basic(self):
path = self.new_subrip_file()
lines = [x.rstrip() for x in open(path, "r").readlines()]
with open(path, "r") as f:
lines = [x.rstrip() for x in f.readlines()]
assert aeidon.util.readlines(path) == lines

def test_readlines__fallback(self):
path = self.new_subrip_file()
open(path, "w", encoding="utf_8").write("\xc3\xb6\n")
with open(path, "w", encoding="utf_8") as f:
f.write("\xc3\xb6\n")
assert aeidon.util.readlines(path, "ascii") == ["\xc3\xb6"]

def test_write__basic(self):
text = "test\ntest\n"
path = self.new_subrip_file()
aeidon.util.write(path, text)
f = open(path, "r", encoding="ascii")
assert f.read() == text
with open(path, "r", encoding="ascii") as f:
assert f.read() == text

def test_write__fallback(self):
text = "\xc3\xb6\n"
path = self.new_subrip_file()
aeidon.util.write(path, text, "ascii")
f = open(path, "r", encoding="utf_8")
assert f.read() == text
with open(path, "r", encoding="utf_8") as f:
assert f.read() == text

def test_writelines__basic(self):
lines = ("test", "test")
path = self.new_subrip_file()
aeidon.util.writelines(path, lines)
f = open(path, "r", encoding="ascii")
assert f.read() == "test\ntest\n"
with open(path, "r", encoding="ascii") as f:
assert f.read() == "test\ntest\n"

def test_writelines__fallback(self):
text = "\xc3\xb6"
path = self.new_subrip_file()
aeidon.util.writelines(path, (text,), "ascii")
f = open(path, "r", encoding="utf_8")
assert f.read() == text + "\n"
with open(path, "r", encoding="utf_8") as f:
assert f.read() == text + "\n"
43 changes: 21 additions & 22 deletions gaupol/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,28 +474,27 @@ def _write_to_file(self):
encoding = aeidon.util.get_default_encoding()
# Ignore possible encoding errors, which are only related to
# saved file and directory names and not in any way critical.
f = open(self.path, "w", encoding=encoding, errors="replace")
root = self._flatten(self._root)
defaults = self._flatten(self._defaults)
for section in sorted(root):
f.write("\n[{}]\n".format(section))
for option in sorted(root[section]):
value = root[section][option]
json_value = json.dumps(
value, cls=EnumEncoder, ensure_ascii=False)
# Discard removed options, but always keep
# all options of all extensions.
if (not section.startswith("extensions::")
and (not section in defaults or
not option in defaults[section])): continue
# Write options that remain at their default value
# (perhaps, but not necessarily unset) as commented out.
if (section in defaults
and option in defaults[section]
and value == defaults[section][option]):
f.write("# ")
f.write("{} = {}\n".format(option, json_value))
f.close()
with open(self.path, "w", encoding=encoding, errors="replace") as f:
root = self._flatten(self._root)
defaults = self._flatten(self._defaults)
for section in sorted(root):
f.write("\n[{}]\n".format(section))
for option in sorted(root[section]):
value = root[section][option]
json_value = json.dumps(
value, cls=EnumEncoder, ensure_ascii=False)
# Discard removed options, but always keep
# all options of all extensions.
if (not section.startswith("extensions::")
and (not section in defaults or
not option in defaults[section])): continue
# Write options that remain at their default value
# (perhaps, but not necessarily unset) as commented out.
if (section in defaults
and option in defaults[section]
and value == defaults[section][option]):
f.write("# ")
f.write("{} = {}\n".format(option, json_value))

def write_to_file(self):
"""Write values of configuration options to file."""
Expand Down
5 changes: 4 additions & 1 deletion setup-aeidon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
shutil.copytree("data/headers", "aeidon/data/headers")
shutil.copytree("data/patterns", "aeidon/data/patterns")

with open("README.aeidon.md", "r") as f:
long_description = f.read()

setup(
name="aeidon",
version=get_aeidon_version(),
author="Osmo Salomaa",
author_email="otsaloma@iki.fi",
description="Reading, writing and manipulating text-based subtitle files",
long_description=open("README.aeidon.md", "r").read(),
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/otsaloma/gaupol",
license="GPL",
Expand Down
Loading