-
Notifications
You must be signed in to change notification settings - Fork 280
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
Key ordering with locale, take #2 #288
Key ordering with locale, take #2 #288
Conversation
…ware. Support sorting by locale with strcoll(). Properly handle case and accents.
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') | ||
except locale.Error: | ||
self.skipTest('locale en_US.UTF-8 not available') | ||
locale.setlocale(locale.LC_ALL, (None, None)) |
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.
Can you briefly explain why you added this? Is the cleanup on line 334 redundant?
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.
The cleanup is still needed after the whole test ran.
Setting the locale to en_US
is just a test for locale availability. But because the default without locale option (as in the first of the for test .run
s below) doesn't set any locale, that en_US
would still be active. Therefore we need to reset that to it's initial state first, then run the 2 tests with default settings, then the 2 with locale setting, then cleanup.
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.
Thanks!
I understood the second one (line 339), but I thought the cleanup wasn't needed, then. Actually it's still needed because yamllint calls locale.setlocale()
too (in cli.py
).
Could you add a tiny comment above self.addCleanup()
, just to make sure we remember it? E.g. # Clean up after yamllint called locale.setlocale() in cli.py:
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 added some comments and moved the addCleanup
to a more appropriate spot regarding the flow in this test.
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') | ||
self.addCleanup(locale.setlocale, locale.LC_ALL, loc) | ||
self.skipTest('no UTF-8 locale available') | ||
self.addCleanup(locale.setlocale, locale.LC_ALL, (None, None)) |
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 now understand more how the whole locale
module works, so I improved this test case (unrelated to my changes) as well. This should now work for any available UTF-8
locale, I guess - and also reset to the "default" from before better then with getlocale
.
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.
Interesting: 'C.UTF-8'
→ (None, 'UTF-8')
Thanks!
@@ -350,6 +352,10 @@ def test_run_with_locale(self): | |||
os.path.join(self.wd, 'c.yaml'))) | |||
self.assertEqual(ctx.returncode, 0) | |||
|
|||
# the next two runs use setlocale() inside, | |||
# so we need to clean up afterwards |
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.
👍
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') | ||
self.addCleanup(locale.setlocale, locale.LC_ALL, loc) | ||
self.skipTest('no UTF-8 locale available') | ||
self.addCleanup(locale.setlocale, locale.LC_ALL, (None, None)) |
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.
Interesting: 'C.UTF-8'
→ (None, 'UTF-8')
Thanks!
Only.... that it doesn't do what I thought it would :/ Did a little more testing - it's basically the same as I will try to add some environment without locales to Travis to prevent the whole regression from happening again. |
While I am really interested about key ordering and I want to add a feature like this to ansible/ansible-lint#578 the current implementation worries me a little because it does not take in consideration that different yaml files would need custom ordering. One good example is the fact that Ansible files would want to have the "name" key the first key, and that is clearly not alphabetical. Using alphabetical ordering would make no sense at all. I know this is a complex issue but it would be nice to have a bug where we can plan. As I use yamllint in tandem with ansible-lint, I am also considering integrating them, so users would not have to run both. |
I am currently using yaml files for translations - in that use-case it makes perfect sense to sort the keys alphabetical. I agree, though, that e.g. for various types of config files, custom ordering rules would be great (I'm thinking As for different files needing different rules: Not sure whether that's possible right now, I guess not. That might be a completely independent issue: Defining rules individually by file name (or some kind of pattern).
You could just create an issue for it? ;) Or better - a PR to implement it? |
@wolfgangwalther It is more complex than this, read the linked ticket and preferably see if you can help YAML spec owners to help us. A comment on the spec ticket could help, even a message on their freenode channel |
If yaml schemas would become a thing, that would be a great way to apply different configs to files. However, in the meantime you could still implement a "custom-key-order" rule already, as it doesn't depend on different configs directly. This way you would already be half-way there. |
Compared to the PR before, this does not set a default locale at all. If the
locale
config option is not used, nosetlocale()
is executed, so no error possible.