return key in _cache
def set_all(key: str, rows: list, value: any):
- # NOISY-DEBUG: print(f"DEBUG: key='{key}',rows()={len(rows)},value[]={type(value)} - CALLED!")
+ # DEBUG: print(f"DEBUG: key='{key}',rows()={len(rows)},value[]={type(value)} - CALLED!")
if type(key) != str:
raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
elif not key_exists(key):
- # NOISY-DEBUG: print(f"DEBUG: Cache for key='{key}' not initialized.")
+ # DEBUG: print(f"DEBUG: Cache for key='{key}' not initialized.")
_cache[key] = {}
for sub in rows:
- # NOISY-DEBUG: print(f"DEBUG: Setting key='{key}',sub[{type(sub)}]='{sub}'")
+ # DEBUG: print(f"DEBUG: Setting key='{key}',sub[{type(sub)}]='{sub}'")
if isinstance(sub, tuple):
_cache[key][sub[0]] = value
else:
print(f"WARNING: Unsupported type row[]='{type(row)}'")
- # NOISY-DEBUG: print("DEBUG: EXIT!")
+ # DEBUG: print("DEBUG: EXIT!")
def set_sub_key(key: str, sub: str, value: any):
if type(key) != str:
return type(var) in {int, str, float, bool} or var == None
def fetch_instances(domain: str, origin: str, software: str, script: str, path: str = None):
+ # DEBUG: print(f"DEBUG: domain={domain},origin={origin},software={software},path={path} - CALLED!")
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
elif domain == "":
raise ValueError(f"Parameter 'domain' cannot be empty")
- # DEBUG: print("DEBUG: domain,origin,software,path:", domain, origin, software, path)
if not is_instance_registered(domain):
# DEBUG: print("DEBUG: Adding new domain:", domain, origin)
add_instance(domain, origin, script, path)
# Skip "None" types as tidup() cannot parse them
continue
- # DEBUG: print(f"DEBUG: instance[{type(instance}]={instance} - BEFORE")
+ # DEBUG: print(f"DEBUG: instance='{instance}' - BEFORE")
instance = tidyup_domain(instance)
- # DEBUG: print(f"DEBUG: instance[{type(instance}]={instance} - AFTER")
+ # DEBUG: print(f"DEBUG: instance='{instance}' - AFTER")
if instance == "":
print("WARNING: Empty instance after tidyup_domain(), domain:", domain)
"Origin": domain
})
- # DEBUG: print(f"DEBUG: fetched()={len(fetched))}")
+ # DEBUG: print(f"DEBUG: fetched()={len(fetched)}")
if len(fetched) == 0:
# DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
break
elif domain == "":
raise ValueError(f"Parameter 'domain' cannot be empty")
- # NOISY-DEBUG: # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
+ # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
if not cache.key_exists("is_registered"):
- # NOISY-DEBUG: # DEBUG: print(f"DEBUG: Cache for 'is_registered' not initialized, fetching all rows ...")
+ # DEBUG: print(f"DEBUG: Cache for 'is_registered' not initialized, fetching all rows ...")
try:
cursor.execute("SELECT domain FROM instances")
# Is cache found?
registered = cache.sub_key_exists("is_registered", domain)
- # NOISY-DEBUG: # DEBUG: print(f"DEBUG: registered='{registered}' - EXIT!")
+ # DEBUG: print(f"DEBUG: registered='{registered}' - EXIT!")
return registered
def add_instance(domain: str, origin: str, originator: str, path: str = None):
for instance in fetched:
# just in case
- if instance["isSuspended"]:
+ if instance["isSuspended"] and not has_element(blocks["suspended"], "domain", instance):
blocks["suspended"].append(
{
"domain": tidyup_domain(instance["host"]),
offset = offset + step
for instance in fetched:
- if instance["isBlocked"]:
+ if instance["isBlocked"] and not has_element(blocks["blocked"], "domain", instance):
blocks["blocked"].append({
"domain": tidyup_domain(instance["host"]),
"reason": None
# DEBUG: print(f"DEBUG: domain='{domain}',path='{path}',headers()={len(headers)},timeout={timeout} - CALLED!")
if type(domain) != str:
raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
+ elif domain == "":
+ raise ValueError("Parameter 'domain' cannot be empty")
elif type(path) != str:
raise ValueError(f"Parameter path[]='{type(path)}' is not 'str'")
+ elif path == "":
+ raise ValueError("Parameter 'path' cannot be empty")
try:
# DEBUG: print(f"DEBUG: Sending request to '{domain}{path}' ...")
# DEBUG: print(f"DEBUG: response[]='{type(response)}' - EXXIT!")
return response
+
+def has_element(elements: list, key: str, value: any) -> bool:
+ # DEBUG: print(f"DEBUG: element()={len(element)},key='{key}',value[]='{type(value)}' - CALLED!")
+ if type(key) != str:
+ raise ValueError(f"Parameter key[]='{type(key)}' is not 'str'")
+ elif key == "":
+ raise ValueError("Parameter 'key' cannot be empty")
+
+ has = False
+ # DEBUG: print(f"DEBUG: Checking elements()={len(elements)} ...")
+ for element in elements:
+ # DEBUG: print(f"DEBUG: element[]='{type(element)}'")
+ if type(element) != dict:
+ raise ValueError(f"element[]='{type(element)}' is not 'dict'")
+ elif not key in element:
+ raise KeyError(f"Cannot find key='{key}'")
+ elif element[key] == value:
+ has = True
+ break
+
+ # DEBUG: print(f"DEBUG: has={has} - EXIT!")
+ return has
}
def set(key: str, domain: str, value: any):
- # NOISY-DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',value[]='{type(value)}' - CALLED!")
+ # DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',value[]='{type(value)}' - CALLED!")
if type(key) != str:
raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
elif key == "":
# Set it
_pending[key][domain] = value
- # DEBUG: print("DEBUG: EXIT!")
+ # DEBUG: # DEBUG: print("DEBUG: EXIT!")
def has_pending_instance_data(domain: str) -> bool:
# DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
raise Exception(f"Domain '{domain}' has no pending instance data, but function invoked")
# DEBUG: print(f"DEBUG: Updating instance data for domain='{domain}' ...")
- sql_string = ''
+ sql_string = ""
fields = list()
for key in _pending:
# DEBUG: print("DEBUG: key:", key)
fields.append(time.time())
fields.append(domain)
- if sql_string == '':
+ if sql_string == "":
raise ValueError(f"No fields have been set, but method invoked, domain='{domain}'")
# DEBUG: print(f"DEBUG: sql_string='{sql_string}',fields()={len(fields)}")
"reason" : None
})
else:
- print(f"DEBUG: Updating block last seen for blocker='{blocker}',blocked='{blocked}' ...")
+ # DEBUG: print(f"DEBUG: Updating block last seen for blocker='{blocker}',blocked='{blocked}' ...")
fba.update_last_seen(blocker, blocked, block_level)
# DEBUG: print("DEBUG: Committing changes ...")
fba.block_instance(blocker, blocking, reason, block_level)
if block_level == "reject":
- blockdict.append(
- {
- "blocked": blocked,
- "reason" : reason
- })
+ blockdict.append({
+ "blocked": blocked,
+ "reason" : reason
+ })
else:
# DEBUG: print(f"DEBUG: Updating block last seen and reason for blocker='{blocker}',blocking='{blocking}' ...")
fba.update_last_seen(blocker, blocking, block_level)
fba.block_instance(blocker, blocked, reason, block_level)
if block_level == "reject":
- blockdict.append(
- {
- "blocked": blocked,
- "reason" : reason
- })
+ blockdict.append({
+ "blocked": blocked,
+ "reason" : reason
+ })
else:
- print(f"DEBUG: Updating block last seen and reason for blocker='{blocker}',blocked='{blocked}' ...")
+ # DEBUG: print(f"DEBUG: Updating block last seen and reason for blocker='{blocker}',blocked='{blocked}' ...")
fba.update_last_seen(blocker, blocked, block_level)
fba.update_block_reason(reason, blocker, blocked, block_level)
# DEBUG: print(f"DEBUG: blocker='{blocker}' is blocking '{blocked}' for unknown reason at this point")
fba.block_instance(blocker, blocked, "unknown", "reject")
- blockdict.append(
- {
- "blocked": blocked,
- "reason" : None
- })
+ blockdict.append({
+ "blocked": blocked,
+ "reason" : None
+ })
else:
- print(f"DEBUG: Updating block last seen for blocker='{blocker}',blocked='{blocked}' ...")
+ # DEBUG: print(f"DEBUG: Updating block last seen for blocker='{blocker}',blocked='{blocked}' ...")
fba.update_last_seen(blocker, blocked, "reject")
if "public_comment" in peer: