Skip to content

Commit

Permalink
lense -> nmget
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Dec 26, 2022
1 parent a03bc82 commit e9f3acc
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions scripts/update-policy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict
from collections.abc import Mapping
from itertools import chain
from typing import Any, Mapping

import oyaml
import requests
Expand All @@ -24,25 +25,53 @@ def increase_indent(self, flow=False, indentless=False):
return super(OYAMLDumper, self).increase_indent(flow, False)


def lense(obj, *args):
if len(args) == 0 and obj is not None:
return obj

if not isinstance(obj, Mapping):
return None

return lense(obj.get(args[0], None), *args[1:])
def nmget(
o: Mapping[str, Any],
key_path: str,
def_val: Any = None,
path_delimiter: str = ".",
null_as_default: bool = True,
) -> Any:
"""
A short-hand for retrieving a value from nested mappings
("nested-mapping-get"). At each level it checks if the given "path"
component in the given key exists and return the default value whenever
fails.
Example:
>>> o = {'a':{'b':1}, 'x': None}
>>> nmget(o, 'a', 0)
{'b': 1}
>>> nmget(o, 'a.b', 0)
1
>>> nmget(o, 'a/b', 0, '/')
1
>>> nmget(o, 'a.c', 0)
0
>>> nmget(o, 'x', 0)
0
>>> nmget(o, 'x', 0, null_as_default=False)
None
"""
pieces = key_path.split(path_delimiter)
while pieces:
p = pieces.pop(0)
if o is None or p not in o:
return def_val
o = o[p]
if o is None and null_as_default:
return def_val
return o


def filter_syscalls(processor_arch, syscalls_item):
if syscalls_item["action"] != "SCMP_ACT_ALLOW":
return []

excludes = lense(syscalls_item, "excludes", "arches")
excludes = nmget(syscalls_item, "excludes.arches")
if excludes and processor_arch in excludes:
return []

includes = lense(syscalls_item, "includes", "arches")
includes = nmget(syscalls_item, "includes.arches")
if includes and not processor_arch in includes:
return []

Expand Down

0 comments on commit e9f3acc

Please sign in to comment.