1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 logging.basicConfig(level=logging.INFO)
21 logger = logging.getLogger(__name__)
23 # Cache for redundant SQL queries
28 def key_exists(key: str) -> bool:
31 def set_all(key: str, rows: list, value: any):
32 logger.debug("key='%s',rows()=%d,value[]='%s' - CALLED!", key, len(rows), type(value))
33 if not isinstance(key, str):
34 raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'")
35 elif not key_exists(key):
36 logger.debug("Cache for key='%s' not initialized.", key)
40 logger.debug("Setting key='%s',sub[%s]='%s'", key, type(sub), sub)
41 if isinstance(sub, sqlite3.Row):
42 logger.debug("Setting key='%s',sub[%s]='%s',value[]='%s'", key, type(sub), sub, type(value))
43 _cache[key][sub[0]] = value
45 logger.warning("Unsupported type sub[]='%s'", type(sub))
49 def set_sub_key(key: str, sub: str, value: any):
50 logger.debug("key='%s',sub='%s',value[]='%s' - CALLED!", key, sub, type(value))
51 if not isinstance(key, str):
52 raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'")
53 elif not isinstance(sub, str):
54 raise ValueError(f"Parameter sub[]='{type(sub)}' is not of type 'str'")
55 elif not key_exists(key):
56 raise Exception(f"Cache for key='{key}' is not initialized, but function invoked")
58 logger.debug("Setting key='%s',sub='%s',value[]='%s' ...", key, sub, type(value))
59 _cache[key][sub] = value
63 def sub_key_exists(key: str, sub: str) -> bool:
64 logger.debug("key='%s',sub='%s' - CALLED!", key, sub)
65 if not isinstance(key, str):
66 raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'")
67 elif not isinstance(sub, str):
68 raise ValueError(f"Parameter sub[]='{type(sub)}' is not of type 'str'")
69 elif not key_exists(key):
70 raise Exception(f"Cache for key='{key}' is not initialized, but function invoked")
72 exists = sub in _cache[key]
74 logger.debug("exists='%s' - EXIT!", exists)