From 930fb35dbabf16cdaa13d495d408c15667155534 Mon Sep 17 00:00:00 2001 From: Cedric Leporcq Date: Sat, 31 Jul 2021 21:26:04 +0200 Subject: [PATCH] Move checksum function to filesystem --- dozo/database.py | 21 --------------------- dozo/filesystem.py | 8 ++++---- dozo/geolocation.py | 2 ++ 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/dozo/database.py b/dozo/database.py index 4372677..ddd2638 100644 --- a/dozo/database.py +++ b/dozo/database.py @@ -4,7 +4,6 @@ Methods for interacting with information Dozo caches about stored media. from builtins import map from builtins import object -import hashlib import json import os import sys @@ -125,26 +124,6 @@ class Db(object): """ return key in self.hash_db - def checksum(self, file_path, blocksize=65536): - """Create a hash value for the given file. - - See http://stackoverflow.com/a/3431835/1318758. - - :param str file_path: Path to the file to create a hash for. - :param int blocksize: Read blocks of this size from the file when - creating the hash. - :returns: str or None - """ - hasher = hashlib.sha256() - with open(file_path, 'rb') as f: - buf = f.read(blocksize) - - while len(buf) > 0: - hasher.update(buf) - buf = f.read(blocksize) - return hasher.hexdigest() - return None - def get_hash(self, key): """Get the hash value for a given key. diff --git a/dozo/filesystem.py b/dozo/filesystem.py index 657cd89..56f8ef5 100644 --- a/dozo/filesystem.py +++ b/dozo/filesystem.py @@ -185,7 +185,6 @@ class FileSystem(object): return folder_name - def get_part(self, item, mask, metadata, db, subdirs): """Parse a specific folder's name given a mask and metadata. @@ -582,6 +581,7 @@ class FileSystem(object): dest_directory = os.path.join(destination, os.path.dirname(file_path)) dest_path = os.path.join(destination, file_path) + self.create_directory(dest_directory) result = self.sort_file(src_path, dest_path, remove_duplicates) if result: @@ -687,7 +687,7 @@ class FileSystem(object): for image in images: if not os.path.isfile(image): continue - checksum1 = db.checksum(image) + checksum1 = self.checksum(image) # Process files # media = get_media_class(src_path, False, self.logger) # TODO compare metadata @@ -697,7 +697,7 @@ class FileSystem(object): moved_imgs = set() for img_path in ci.find_similar(image, similarity): similar = True - checksum2 = db.checksum(img_path) + checksum2 = self.checksum(img_path) # move image into directory name = os.path.splitext(os.path.basename(image))[0] directory_name = 'similar_to_' + name @@ -748,7 +748,7 @@ class FileSystem(object): img_path = os.path.join(dirname, subdir, file_name) if os.path.isdir(img_path): continue - checksum = db.checksum(img_path) + checksum = self.checksum(img_path) dest_path = os.path.join(dirname, os.path.basename(img_path)) result = self.move_file(img_path, dest_path, checksum, db) if not result: diff --git a/dozo/geolocation.py b/dozo/geolocation.py index da75e0b..5b85189 100644 --- a/dozo/geolocation.py +++ b/dozo/geolocation.py @@ -81,6 +81,7 @@ def get_prefer_english_names(): __PREFER_ENGLISH_NAMES__ = bool(config['Geolocation']['prefer_english_names']) return __PREFER_ENGLISH_NAMES__ + def place_name(lat, lon, db, cache=True, logger=logging.getLogger()): lookup_place_name_default = {'default': __DEFAULT_LOCATION__} if(lat is None or lon is None): @@ -131,6 +132,7 @@ def place_name(lat, lon, db, cache=True, logger=logging.getLogger()): return lookup_place_name + def lookup_osm(lat, lon, logger=logging.getLogger()): prefer_english_names = get_prefer_english_names()