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

[WIP] osx_defaults: Add dict support #3420

Closed
wants to merge 4 commits into from

Conversation

aiotter
Copy link

@aiotter aiotter commented Sep 22, 2021

SUMMARY

Add dict support to osx_default.
Fixes #238.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

osx_defaults.py

ADDITIONAL INFORMATION

Example usage:

- osx_defaults:
    domain: com.apple.finder
    key: FXInfoPanesExpanded
    type: dict
    value: {General: true, OpenWith: true}
    state: present

- osx_defaults:
    domain: com.apple.finder
    key: FXInfoPanesExpanded
    dict_add: MetaData
    type: bool
    value: yes
    state: present

CAUTION: This PR contains a bug. To make it fixed, I need some help. For more detail: #238 (comment)

@ansibullbot ansibullbot added WIP Work in progress feature This issue/PR relates to a feature request module module new_contributor Help guide this first time contributor plugins plugin (any type) system labels Sep 22, 2021
@ansibullbot
Copy link
Collaborator

The test botmeta failed with 1 error:

.github/BOTMETA.yml:0:0: Author aiotter not mentioned as active or inactive maintainer for plugins/modules/system/osx_defaults.py (mentioned are: Akasurde, kyleabenson, martinm82, danieljaouen, indrajitr, notok)

The test ansible-test sanity --test validate-modules [explain] failed with 2 errors:

plugins/modules/system/osx_defaults.py:0:0: doc-type-does-not-match-spec: Argument 'dict_add' in argument_spec defines type as 'str' but documentation defines type as 'string'
plugins/modules/system/osx_defaults.py:0:0: invalid-documentation: DOCUMENTATION.options.dict_add.type: not a valid value for dictionary value @ data['options']['dict_add']['type']. Got 'string'

The test ansible-test sanity --test validate-modules [explain] failed with 2 errors:

plugins/modules/system/osx_defaults.py:0:0: doc-type-does-not-match-spec: Argument 'dict_add' in argument_spec defines type as 'str' but documentation defines type as 'string'
plugins/modules/system/osx_defaults.py:0:0: invalid-documentation: DOCUMENTATION.options.dict_add.type: not a valid value for dictionary value @ data['options']['dict_add']['type']. Got 'string'

The test ansible-test sanity --test validate-modules [explain] failed with 2 errors:

plugins/modules/system/osx_defaults.py:0:0: doc-type-does-not-match-spec: Argument 'dict_add' in argument_spec defines type as 'str' but documentation defines type as 'string'
plugins/modules/system/osx_defaults.py:0:0: invalid-documentation: DOCUMENTATION.options.dict_add.type: not a valid value for dictionary value @ data['options']['dict_add']['type']. Got 'string'

The test ansible-test sanity --test validate-modules [explain] failed with 2 errors:

plugins/modules/system/osx_defaults.py:0:0: doc-type-does-not-match-spec: Argument 'dict_add' in argument_spec defines type as 'str' but documentation defines type as 'string'
plugins/modules/system/osx_defaults.py:0:0: invalid-documentation: DOCUMENTATION.options.dict_add.type: not a valid value for dictionary value @ data['options']['dict_add']['type']. Got 'string'

click here for bot help

@aiotter
Copy link
Author

aiotter commented Sep 22, 2021

It seems parsing the result of defaults export by plistlib can work. I'll take a look into this solution when I have some time.

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.

@decadent
Copy link

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.

@aiotter
Copy link
Author

aiotter commented Sep 22, 2021

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.
@aiotter
Copy link
Author

aiotter commented Sep 22, 2021

Used six.PY2 to switch the code.
Now I'm looking into failed CI.

@felixfontein felixfontein added backport-3 check-before-release PR will be looked at again shortly before release and merged if possible. labels Sep 22, 2021
if PY2:
self.current_value = plistlib.readPlistFromString(out)
else:
self.current_value = plistlib.loads(out.encode('utf-8'))
Copy link
Collaborator

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).

Copy link
Author

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?

Copy link
Author

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'))

Copy link
Collaborator

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.

@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Oct 6, 2021
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:
Copy link
Contributor

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.

@russoz
Copy link
Collaborator

russoz commented Mar 12, 2022

Hi @aiotter how do you want to proceed with this PR?

@russoz
Copy link
Collaborator

russoz commented Apr 15, 2022

needs_info

@ansibullbot ansibullbot added the needs_info This issue requires further information. Please answer any outstanding questions label Apr 15, 2022
@ansibullbot
Copy link
Collaborator

@aiotter This pullrequest is waiting for your response. Please respond or the pullrequest will be closed.

click here for bot help

@aiotter
Copy link
Author

aiotter commented May 23, 2022

Sorry that I have no time to carry on this PR.
If there are someone that want to implement this, you can add some tests so that it will be merged.

@aiotter aiotter closed this May 23, 2022
@felixfontein
Copy link
Collaborator

@aiotter thanks for your work on this PR! I hope that someone will pick it up and complete it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
check-before-release PR will be looked at again shortly before release and merged if possible. feature This issue/PR relates to a feature request module module needs_info This issue requires further information. Please answer any outstanding questions new_contributor Help guide this first time contributor plugins plugin (any type) stale_ci CI is older than 7 days, rerun before merging system WIP Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

osx_defaults - Add dict support
6 participants