-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[WIP] osx_defaults: Add dict support #3420
Conversation
The test
The test
The test
The test
The test
|
It seems parsing the result of In [13]: import subprocess, plistlib
...: plistlib.loads(subprocess.run(['defaults', 'export', 'com.apple.finder', '-'], capture_output=True).stdout)
Out[13]:
{'AppleShowAllFiles': True,
'ComputerViewSettings': {'CustomViewStyleVersion': 1,
'WindowState': {'ContainerShowSidebar': True, ... Apparently I forgot adding myself as a maintainer. I'll do it later. And also fix type error on document. |
Note that plistlib is different in Python 3 and 2, and Macos has 2 by default—which Ansible has to use as a consequence. While you seem to be using version 3. It might be possible to create a parser/encoder for the defaults' dict format, considering that it's not too complex—but of course that's more involved than just calling a library. |
Thank you @decadent! It seems we can use plistlib.readPlistFromString on python2.7. Tested and worked. >>> import subprocess, plistlib
>>> plistlib.readPlistFromString(subprocess.check_output(['defaults', 'export', 'com.apple.finder', '-']))
{'RecentMoveAndCopyDestinations': ['file:///Users/aiotter/Documents/', ... But in this case we still have to take it into consideration of the case we are running ansible on python3. I'm not familiar with the python2 compatibility. Is there some standard way in ansible to switch the code based on version of the python, or can I just do it with sys.version_info? |
This commit contains a bug that all the dict manipulation flags the result `unchanged`. Need to be fixed.
075e2d0
to
4cb8abe
Compare
Used six.PY2 to switch the code. |
if PY2: | ||
self.current_value = plistlib.readPlistFromString(out) | ||
else: | ||
self.current_value = plistlib.loads(out.encode('utf-8')) |
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.
It might be a good idea to move this into a static function (that can easily be tested), and add unit tests for this. These are run with all Python versions Ansible supports, which will give you some confidence that this does what it is supposed to on all Python versions. Unit tests for this module would have to do into tests/unit/plugins/modules/system/test_osx_defaults.py (which doesn't exist yet).
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.
I'm not very experienced with unit tests in Python. I read some of tests/unit/plugins/modules/system/* but it seems difficult to understand. Is there good document for this?
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.
move this into a static function
Like this?
def convert_plist_xml_to_object(xml):
if six.PY2:
return plistlib.readPlistFromString(xml)
else:
return plistlib.loads(xml.encode('utf-8'))
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.
You can find some information here: https://docs.ansible.com/ansible/latest/dev_guide/testing_units.html#structuring-unit-tests
And yes, such a function would help.
default: string | ||
array_add: | ||
description: | ||
- Add new elements to the array for a key which has an array as its value. | ||
type: bool | ||
default: no | ||
dict_add: |
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.
Worth to add as well an example in EXAMPLES
below.
Hi @aiotter how do you want to proceed with this PR? |
needs_info |
@aiotter This pullrequest is waiting for your response. Please respond or the pullrequest will be closed. |
Sorry that I have no time to carry on this PR. |
@aiotter thanks for your work on this PR! I hope that someone will pick it up and complete it! |
SUMMARY
Add dict support to osx_default.
Fixes #238.
ISSUE TYPE
COMPONENT NAME
osx_defaults.py
ADDITIONAL INFORMATION
Example usage:
CAUTION: This PR contains a bug. To make it fixed, I need some help. For more detail: #238 (comment)