Skip to content

Commit

Permalink
EvaluationContext.variables as AttrDict (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmvincent committed Apr 17, 2023
1 parent a33a326 commit cf451e3
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions policykit/policyengine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
logger = logging.getLogger(__name__)
db_logger = logging.getLogger("db")

class AttrDict(dict):
"""
For accessing variables using attribute-style access
e.g. variables.slack_channel_id
"""
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self


class EvaluationLogAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
Expand Down Expand Up @@ -50,7 +59,7 @@ def __init__(self, proposal):
self.action = proposal.action

self.policy = proposal.policy
self.variables = {}
self.variables = AttrDict()
self.proposal = proposal

# Can't use logger in filter step because proposal isn't saved yet
Expand All @@ -73,7 +82,7 @@ def __init__(self, proposal):
self.metagov = Metagov(proposal)

# Make policy variables available in the evaluation context
setattr(self, "variables", { v.name : v.value for v in self.policy.variables.all() or []})
setattr(self, "variables", AttrDict({ v.name : v.value for v in self.policy.variables.all() or []}))

class PolicyEngineError(Exception):
"""Base class for exceptions raised from the policy engine"""
Expand Down

0 comments on commit cf451e3

Please sign in to comment.