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
Support python dict in sysctl_create #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,18 +31,22 @@ | |
def create(sysctl_dict, sysctl_file): | ||
"""Creates a sysctl.conf file from a YAML associative array | ||
|
||
:param sysctl_dict: a YAML-formatted string of sysctl options eg "{ 'kernel.max_pid': 1337 }" | ||
:param sysctl_dict: a dict or YAML-formatted string of sysctl | ||
options eg "{ 'kernel.max_pid': 1337 }" | ||
:type sysctl_dict: str | ||
:param sysctl_file: path to the sysctl file to be saved | ||
:type sysctl_file: str or unicode | ||
:returns: None | ||
""" | ||
try: | ||
sysctl_dict_parsed = yaml.safe_load(sysctl_dict) | ||
except yaml.YAMLError: | ||
log("Error parsing YAML sysctl_dict: {}".format(sysctl_dict), | ||
level=ERROR) | ||
return | ||
if type(sysctl_dict) is not dict: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might consider using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. importing collections.Mapping raises a warning under Py3.7, and should be collections.abc.Mapping. We might need a conditional import. |
||
try: | ||
sysctl_dict_parsed = yaml.safe_load(sysctl_dict) | ||
except yaml.YAMLError: | ||
log("Error parsing YAML sysctl_dict: {}".format(sysctl_dict), | ||
level=ERROR) | ||
return | ||
else: | ||
sysctl_dict_parsed = sysctl_dict | ||
|
||
with open(sysctl_file, "w") as fd: | ||
for key, value in sysctl_dict_parsed.items(): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would probably be more flexible if it used:
as it would allow dict like objects to also be used. If you wanted to be really flexible, then you could use:
Then it would allow any object that supports the dictionary specific methods (e.g. getattr, iter, etc.) to be used.