[discinfo] Use context manager for file access

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-05-04 15:28:36 +02:00
parent 2113b6475c
commit 8ef9478879

View File

@ -45,23 +45,22 @@ def write_discinfo(file_path, description, arch, disc_numbers=None, timestamp=No
raise TypeError("Invalid type: disc_numbers type is %s; expected: <list>" % type(disc_numbers)) raise TypeError("Invalid type: disc_numbers type is %s; expected: <list>" % type(disc_numbers))
if not timestamp: if not timestamp:
timestamp = "%f" % time.time() timestamp = "%f" % time.time()
f = open(file_path, "w") with open(file_path, "w") as f:
f.write("%s\n" % timestamp) f.write("%s\n" % timestamp)
f.write("%s\n" % description) f.write("%s\n" % description)
f.write("%s\n" % arch) f.write("%s\n" % arch)
if disc_numbers: if disc_numbers:
f.write("%s\n" % ",".join([str(i) for i in disc_numbers])) f.write("%s\n" % ",".join([str(i) for i in disc_numbers]))
f.close()
return timestamp return timestamp
def read_discinfo(file_path): def read_discinfo(file_path):
result = {} result = {}
f = open(file_path, "r") with open(file_path, "r") as f:
result["timestamp"] = f.readline().strip() result["timestamp"] = f.readline().strip()
result["description"] = f.readline().strip() result["description"] = f.readline().strip()
result["arch"] = f.readline().strip() result["arch"] = f.readline().strip()
disc_numbers = f.readline().strip() disc_numbers = f.readline().strip()
if not disc_numbers: if not disc_numbers:
result["disc_numbers"] = None result["disc_numbers"] = None
elif disc_numbers == "ALL": elif disc_numbers == "ALL":