Commit 52620763 authored by w-e-w's avatar w-e-w
Browse files

clean up message spam

parent 8fc0f5d2
Loading
Loading
Loading
Loading
+17 −11
Original line number Original line Diff line number Diff line
@@ -19,12 +19,13 @@ dump_cache_thread = None


def cache_db_to_dict(db_path):
def cache_db_to_dict(db_path):
    try:
    try:
        with sqlite3.connect(db_path) as conn:
        database_dict = {}
        database_dict = {}
        with sqlite3.connect(db_path) as conn:
            for table in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall():
            for table in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall():
                table_name = table[0]
                table_name = table[0]
                table_data = conn.execute(f"SELECT * FROM `{table_name}`").fetchall()
                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
        return database_dict
    except Exception as e:
    except Exception as e:
        print(e)
        print(e)
@@ -84,8 +85,10 @@ def cache(subsection):
        s = cache_data.get(subsection, {})
        s = cache_data.get(subsection, {})
        if not s:
        if not s:
            try:
            try:
                with cache_lock:
                    with sqlite3.connect(cache_db_path) as conn:
                    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:
            except Exception as e:
                print(e)
                print(e)
        cache_data[subsection] = s
        cache_data[subsection] = s
@@ -102,7 +105,8 @@ def cache(subsection):
                            cache_data = json.load(file)
                            cache_data = json.load(file)
                    except Exception:
                    except Exception:
                        os.replace(cache_filename, os.path.join(script_path, "tmp", "cache.json"))
                        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(
                            '[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache')
                        cache_data = {}
                        cache_data = {}


    s = cache_data.get(subsection, {})
    s = cache_data.get(subsection, {})
@@ -111,7 +115,7 @@ def cache(subsection):
    return s
    return s




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


@@ -120,7 +124,7 @@ def cached_data_for_file(subsection, title, filename, func):
        title (str): The title of the data entry in the subsection of the cache.
        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.
        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 (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:
    Returns:
        dict or None: The cached or generated data, or None if data generation fails.
        dict or None: The cached or generated data, or None if data generation fails.


@@ -144,7 +148,11 @@ def cached_data_for_file(subsection, title, filename, func):
            entry = None
            entry = None


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


@@ -154,9 +162,7 @@ def cached_data_for_file(subsection, title, filename, func):
                    with sqlite3.connect(cache_db_path) as conn:
                    with sqlite3.connect(cache_db_path) as conn:
                        insert_or_replace = f"INSERT OR REPLACE INTO `{subsection}` (path, mtime, value) VALUES (?, ?, ?)"
                        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(insert_or_replace, (title, ondisk_mtime, json.dumps(value)))
                        existing_cache = cache(subsection)
                        existing_cache[title] = {'mtime': ondisk_mtime, 'value': value}
                        existing_cache[title] = {'mtime': ondisk_mtime, 'value': value}
                        print(f'{title}: {value}')
                    return value
                    return value
            except Exception as e:
            except Exception as e:
                print(e)
                print(e)
+1 −1
Original line number Original line Diff line number Diff line
@@ -44,7 +44,7 @@ def sha256(filename, title, use_addnet_hash=False):
        else:
        else:
            subsection = "hashes"
            subsection = "hashes"
            calculate_hash = partial(calculate_sha256, filename)
            calculate_hash = partial(calculate_sha256, filename)
        return modules.cache.cached_data_for_file(subsection, title, filename, calculate_hash)
        return modules.cache.cached_data_for_file(subsection, title, filename, calculate_hash, f"Calculating sha256 for {filename}: ")


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