Use pathclass.Path.open.

This commit is contained in:
Ethan Dalool 2020-09-20 18:25:12 -07:00
parent c6d7450028
commit 342062dd99
3 changed files with 14 additions and 11 deletions

View file

@ -86,7 +86,7 @@ def load_file(filepath, defaults):
needs_rewrite = False needs_rewrite = False
if user_config_exists: if user_config_exists:
with open(path.absolute_path, 'r', encoding='utf-8') as handle: with path.open('r', encoding='utf-8') as handle:
user_config = json.load(handle) user_config = json.load(handle)
(config, needs_rewrite) = layer_json(config, user_config) (config, needs_rewrite) = layer_json(config, user_config)
else: else:

View file

@ -36,11 +36,13 @@ def equal_file(filename1, filename2, *args, **kwargs):
''' '''
Given two files, return True if they have the same quickid hash. Given two files, return True if they have the same quickid hash.
''' '''
filename1 = pathclass.Path(filename1).absolute_path file1 = pathclass.Path(filename1)
filename2 = pathclass.Path(filename2).absolute_path file2 = pathclass.Path(filename2)
if os.path.getsize(filename1) != os.path.getsize(filename2): file1.assert_is_file()
file2.assert_is_file()
if file1.size != file2.size:
return False return False
with open(filename1, 'rb') as handle1, open(filename2, 'rb') as handle2: with file1.open('rb') as handle1, file2.open('rb') as handle2:
return equal_handle(handle1, handle2, *args, **kwargs) return equal_handle(handle1, handle2, *args, **kwargs)
def matches_handle(handle, other_id): def matches_handle(handle, other_id):
@ -65,8 +67,8 @@ def matches_file(filename, other_id):
Given a file and a quickid hash, return True if the file matches Given a file and a quickid hash, return True if the file matches
that hash. that hash.
''' '''
filename = pathclass.Path(filename).absolute_path file = pathclass.Path(filename)
with open(filename, 'rb') as handle: with file.open('rb') as handle:
return matches_handle(handle, other_id) return matches_handle(handle, other_id)
def quickid_handle(handle, hashtype='md5', chunk_size=CHUNK_SIZE): def quickid_handle(handle, hashtype='md5', chunk_size=CHUNK_SIZE):
@ -96,8 +98,9 @@ def quickid_file(filename, *args, **kwargs):
''' '''
Return the quickid hash for this file. Return the quickid hash for this file.
''' '''
filename = pathclass.Path(filename).absolute_path file = pathclass.Path(filename)
with open(filename, 'rb') as handle: file.assert_is_file()
with file.open('rb') as handle:
return quickid_handle(handle, *args, **kwargs) return quickid_handle(handle, *args, **kwargs)
def main(argv): def main(argv):

View file

@ -438,7 +438,7 @@ def copy_file(
def handlehelper(path, mode): def handlehelper(path, mode):
try: try:
handle = open(path.absolute_path, mode) handle = path.open(mode)
return handle return handle
except PermissionError as exception: except PermissionError as exception:
if callback_permission_denied is not None: if callback_permission_denied is not None:
@ -548,7 +548,7 @@ def hash_file(
checked_bytes = 0 checked_bytes = 0
file_size = os.path.getsize(path.absolute_path) file_size = os.path.getsize(path.absolute_path)
handle = open(path.absolute_path, 'rb') handle = path.open('rb')
with handle: with handle:
while True: while True:
chunk = handle.read(chunk_size) chunk = handle.read(chunk_size)