Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renamed meta to metadata and other cleanups #90

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/git-using-lib/git_using_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class GitPromiseTypeModule(PromiseModule):
def __init__(self, **kwargs):
super().__init__("git_promise_module", "0.0.3", **kwargs)

def validate_promise(self, promiser, attributes, meta):
def validate_promise(self, promiser, attributes, metadata):
if not promiser.startswith("/"):
raise ValidationError(f"File path '{promiser}' must be absolute")
for name, value in attributes.items():
Expand All @@ -15,7 +15,7 @@ def validate_promise(self, promiser, attributes, meta):
if name == "repo" and type(value) is not str:
raise ValidationError(f"'repo' must be string for git promise types")

def evaluate_promise(self, promiser, attributes, meta):
def evaluate_promise(self, promiser, attributes, metadata):
folder = promiser
url = attributes["repo"]

Expand Down
4 changes: 2 additions & 2 deletions examples/gpg/gpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def gpg_key_present(self, homedir, user_id):
proc.communicate()
self.log_error(f"Timed out querying for gpg key '{user_id}'")

def validate_promise(self, promiser, attributes, meta):
def validate_promise(self, promiser, attributes, metadata):
if not promiser.startswith("/"):
raise ValidationError(
f"Promiser '{promiser}' for 'gpg_keys' promise must be an absolute path"
Expand All @@ -113,7 +113,7 @@ def validate_promise(self, promiser, attributes, meta):
f"Required attribute 'keylist' missing for 'gpg_keys' promise"
)

def evaluate_promise(self, promiser, attributes, meta):
def evaluate_promise(self, promiser, attributes, metadata):
keylist_json = self.clean_storejson_output(attributes["keylist"])
self.log_verbose(f"keylist_json is '{keylist_json}'")

Expand Down
4 changes: 2 additions & 2 deletions examples/rss/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self):
super().__init__("rss_promise_module", "0.0.3")


def validate_promise(self, promiser, attributes, meta):
def validate_promise(self, promiser, attributes, metadata):
# check promiser type
if type(promiser) is not str:
raise ValidationError("invalid type for promiser: expected string")
Expand Down Expand Up @@ -43,7 +43,7 @@ def validate_promise(self, promiser, attributes, meta):
raise ValidationError(f"Invalid value '{select}' for attribute select: must be newest, oldest or random")


def evaluate_promise(self, promiser, attributes, meta):
def evaluate_promise(self, promiser, attributes, metadata):
# get attriute feed
feed = attributes['feed']

Expand Down
4 changes: 2 additions & 2 deletions examples/site-up/site_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class SiteUpPromiseTypeModule(PromiseModule):
def __init__(self):
super().__init__("site_up_promise_module", "0.0.2")

def validate_promise(self, promiser, attributes, meta):
def validate_promise(self, promiser, attributes, metadata):
if not self.is_url_valid(promiser):
raise ValidationError(f"URL '{promiser}' is invalid")

def evaluate_promise(self, url, attributes, meta):
def evaluate_promise(self, url, attributes, metadata):
ssl_ctx = ssl.create_default_context()
if (
"skip_ssl_verification" in attributes
Expand Down
16 changes: 8 additions & 8 deletions libraries/python/cfengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,10 @@ def _handle_init(self):
_put_response(self._response, self._out, self._record_file)

def _handle_validate(self, promiser, attributes, request):
meta = {"promise_type": request.get("promise_type")}
metadata = {"promise_type": request.get("promise_type")}
try:
self.validate_attributes(promiser, attributes, meta)
returned = self.validate_promise(promiser, attributes, meta)
self.validate_attributes(promiser, attributes, metadata)
returned = self.validate_promise(promiser, attributes, metadata)
if returned is None:
# Good, expected
self._result = Result.VALID
Expand Down Expand Up @@ -370,9 +370,9 @@ def _handle_validate(self, promiser, attributes, request):

def _handle_evaluate(self, promiser, attributes, request):
self._result_classes = None
meta = {"promise_type": request.get("promise_type")}
metadata = {"promise_type": request.get("promise_type")}
try:
results = self.evaluate_promise(promiser, attributes, meta)
results = self.evaluate_promise(promiser, attributes, metadata)

# evaluate_promise should return either a result or a (result, result_classes) pair
if type(results) == str:
Expand Down Expand Up @@ -448,16 +448,16 @@ def prepare_promiser_and_attributes(self, promiser, attributes):
"""Override if you want to modify promiser or attributes before validate or evaluate"""
return (promiser, attributes)

def validate_attributes(self, promiser, attributes, meta):
def validate_attributes(self, promiser, attributes, metadata):
"""Override this if you want to prevent automatic validation"""
return self._validate_attributes(promiser, attributes)

def validate_promise(self, promiser, attributes, meta):
def validate_promise(self, promiser, attributes, metadata):
"""Must override this or use validation through self.add_attribute()"""
if not self._has_validation_attributes:
raise NotImplementedError("Promise module must implement validate_promise")

def evaluate_promise(self, promiser, attributes, meta):
def evaluate_promise(self, promiser, attributes, metadata):
raise NotImplementedError("Promise module must implement evaluate_promise")

def protocol_terminate(self):
Expand Down
4 changes: 2 additions & 2 deletions promise-types/ansible/ansible_promise.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ def prepare_promiser_and_attributes(self, promiser, attributes):
safe_promiser = promiser.replace(",", "_")
return (safe_promiser, attributes)

def validate_promise(self, promiser: str, attributes: Dict, meta: Dict):
def validate_promise(self, promiser: str, attributes: Dict, metadata: Dict):
if not ANSIBLE_AVAILABLE:
raise ValidationError("Ansible Python module not available")

def evaluate_promise(
self, safe_promiser: str, attributes: Dict, meta: Dict
self, safe_promiser: str, attributes: Dict, metadata: Dict
) -> Tuple[str, List[str]]:
model = self.create_attribute_object(safe_promiser, attributes)

Expand Down
2 changes: 1 addition & 1 deletion promise-types/git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def depth_must_be_zero_or_more(v):
self.add_attribute("update", bool, default=True)
self.add_attribute("version", str, default="HEAD")

def evaluate_promise(self, promiser: str, attributes: Dict, meta: Dict):
def evaluate_promise(self, promiser: str, attributes: Dict, metadata: Dict):
safe_promiser = promiser.replace(",", "_")
attributes.setdefault("destination", promiser)
model = self.create_attribute_object(promiser, attributes)
Expand Down
4 changes: 2 additions & 2 deletions promise-types/groups/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self):
self._name_regex = re.compile(r"^[a-z_][a-z0-9_-]*[$]?$")
self._name_maxlen = 32

def validate_promise(self, promiser, attributes, meta):
def validate_promise(self, promiser, attributes, metadata):
# check promiser value
if self._name_regex.match(promiser) is None:
self.log_warning(
Expand Down Expand Up @@ -115,7 +115,7 @@ def validate_promise(self, promiser, attributes, meta):
% (duplicates, promiser)
)

def evaluate_promise(self, promiser, attributes, meta):
def evaluate_promise(self, promiser, attributes, metadata):
# keep track of any repairs or failed repairs
failed_repairs = 0
repairs = 0
Expand Down
4 changes: 2 additions & 2 deletions promise-types/http/http_promise_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HTTPPromiseModule(PromiseModule):
def __init__(self, name="http_promise_module", version="2.0.0", **kwargs):
super().__init__(name, version, **kwargs)

def validate_promise(self, promiser, attributes, meta):
def validate_promise(self, promiser, attributes, metadata):
if "url" in attributes:
url = attributes["url"]
if type(url) != str:
Expand Down Expand Up @@ -91,7 +91,7 @@ def target_fh(self, file_info):
yield open(os.devnull, "wb")


def evaluate_promise(self, promiser, attributes, meta):
def evaluate_promise(self, promiser, attributes, metadata):
url = attributes.get("url", promiser)
method = attributes.get("method", "GET")
headers = attributes.get("headers", dict())
Expand Down
4 changes: 2 additions & 2 deletions promise-types/iptables/iptables.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def must_be_non_negative(v):
self.add_attribute("rules", dict)
self.add_attribute("executable", str, default="iptables")

def validate_promise(self, promiser: str, attributes: dict, meta: dict):
def validate_promise(self, promiser: str, attributes: dict, metadata: dict):
command = attributes["command"]

denied_attrs = self._collect_denied_attributes_of_command(command, attributes)
Expand All @@ -182,7 +182,7 @@ def validate_promise(self, promiser: str, attributes: dict, meta: dict):
if command != "flush" and attributes.get("chain") == "ALL":
raise ValidationError("Chain 'ALL' is only available for command 'flush'")

def evaluate_promise(self, promiser: str, attributes: dict, meta: dict):
def evaluate_promise(self, promiser: str, attributes: Dict, metadata: Dict):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for using typing.Dict instead of dict?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't mean anything special by it, I just copy-pasted the function signature from one of the other ones (git promise type I think).

It could make sense to always use the typing ones, make everything look consistent, and they have some more expressive ones. But I don't really have a strong opinion.

safe_promiser = promiser.replace(",", "_")

model = Model(
Expand Down
2 changes: 1 addition & 1 deletion promise-types/systemd/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def prepare_promiser_and_attributes(self, promiser, attributes):
return (safe_promiser, attributes)

def evaluate_promise(
self, safe_promiser: str, attributes: Dict, meta: Dict
self, safe_promiser: str, attributes: Dict, metadata: Dict
) -> Tuple[str, List[str]]:
model = self.create_attribute_object(safe_promiser, attributes)
# get the status of the service
Expand Down