Commit 4df4465f authored by w-e-w's avatar w-e-w
Browse files

rework prefix message

parent 52620763
Loading
Loading
Loading
Loading
+7 −18
Original line number Diff line number Diff line
@@ -24,8 +24,7 @@ def cache_db_to_dict(db_path):
            for table in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall():
                table_name = table[0]
                table_data = conn.execute(f"SELECT * FROM `{table_name}`").fetchall()
                database_dict[table_name] = {row[0]: {"mtime": row[1], "value": json.loads(row[2])} for row in
                                             table_data}
                database_dict[table_name] = {row[0]: {"mtime": row[1], "value": json.loads(row[2])} for row in table_data}
        return database_dict
    except Exception as e:
        print(e)
@@ -87,8 +86,7 @@ def cache(subsection):
            try:
                with cache_lock:
                    with sqlite3.connect(cache_db_path) as conn:
                        conn.execute(
                            f'CREATE TABLE IF NOT EXISTS `{subsection}` (path TEXT PRIMARY KEY, mtime REAL, value TEXT)')
                        conn.execute(f'CREATE TABLE IF NOT EXISTS `{subsection}` (path TEXT PRIMARY KEY, mtime REAL, value TEXT)')
            except Exception as e:
                print(e)
        cache_data[subsection] = s
@@ -103,19 +101,16 @@ def cache(subsection):
                    try:
                        with open(cache_filename, "r", encoding="utf8") as file:
                            cache_data = json.load(file)
                    except Exception:
                    except Exception as e:
                        os.replace(cache_filename, os.path.join(script_path, "tmp", "cache.json"))
                        print(
                            '[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache')
                        print(f'[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache" {e}')
                        cache_data = {}

    s = cache_data.get(subsection, {})
    cache_data[subsection] = s

    return s


def cached_data_for_file(subsection, title, filename, func, func_message: str = None):
def cached_data_for_file(subsection, title, filename, func):
    """
    Retrieves or generates data for a specific file, using a caching mechanism.

@@ -124,7 +119,6 @@ def cached_data_for_file(subsection, title, filename, func, func_message: str =
        title (str): The title of the data entry in the subsection of the cache.
        filename (str): The path to the file to be checked for modifications.
        func (callable): A function that generates the data if it is not available in the cache.
        func_message (str): when non-blank, prints {func_message}{func()} if func is called
    Returns:
        dict or None: The cached or generated data, or None if data generation fails.

@@ -148,11 +142,7 @@ def cached_data_for_file(subsection, title, filename, func, func_message: str =
            entry = None

    if not entry or 'value' not in entry:
        if func_message:
            print(f"{func_message}", end="")
        value = func()
        if func_message:
            print(value)
        if value is None:
            return None

@@ -160,8 +150,7 @@ def cached_data_for_file(subsection, title, filename, func, func_message: str =
            try:
                with cache_lock:
                    with sqlite3.connect(cache_db_path) as conn:
                        insert_or_replace = f"INSERT OR REPLACE INTO `{subsection}` (path, mtime, value) VALUES (?, ?, ?)"
                        conn.execute(insert_or_replace, (title, ondisk_mtime, json.dumps(value)))
                        conn.execute(f"INSERT OR REPLACE INTO `{subsection}` (path, mtime, value) VALUES (?, ?, ?)", (title, ondisk_mtime, json.dumps(value)))
                    existing_cache[title] = {'mtime': ondisk_mtime, 'value': value}
                    return value
            except Exception as e:
+11 −6
Original line number Diff line number Diff line
import hashlib
import os.path
from functools import partial

from modules import shared
import modules.cache
@@ -39,12 +38,9 @@ def sha256_from_cache(filename, title, use_addnet_hash=False):
def sha256(filename, title, use_addnet_hash=False):
    if shared.opts.experimental_sqlite_cache:
        if use_addnet_hash:
            subsection = "hashes-addnet"
            calculate_hash = partial(calculate_addnet_hash, filename)
            return modules.cache.cached_data_for_file("hashes-addnet", title, filename, calculate_hash_with_message(calculate_addnet_hash, filename))
        else:
            subsection = "hashes"
            calculate_hash = partial(calculate_sha256, filename)
        return modules.cache.cached_data_for_file(subsection, title, filename, calculate_hash, f"Calculating sha256 for {filename}: ")
            return modules.cache.cached_data_for_file("hashes", title, filename, calculate_hash_with_message(calculate_sha256, filename))

    hashes = cache("hashes-addnet") if use_addnet_hash else cache("hashes")

@@ -93,3 +89,12 @@ def addnet_hash_safetensors(b):
def calculate_addnet_hash(filename):
    with open(filename, "rb") as f:
        return addnet_hash_safetensors(f)


def calculate_hash_with_message(hash_function, filename):
    def wrapped_function():
        print(f"Calculating sha256 for {filename}: ", end='')
        result = hash_function(filename)
        print(result)
        return result
    return wrapped_function