Skip to content

Configuration Refactor - #496

Merged
d0ugal merged 10 commits into
mkdocs:masterfrom
d0ugal:config-refactor
May 9, 2015
Merged

d0ugal merged 10 commits into
mkdocs:masterfrom
d0ugal:config-refactor

Conversation

@d0ugal

@d0ugal d0ugal commented May 3, 2015

Copy link
Copy Markdown
Contributor

This change breaks up the config parsing into a set of validators. This
makes it somewhat easier to follow and add extra checks. As a result we
now also catch a few config errors we wouldn't have before.

The code probably still needs some tidying up, but it works well.

@d0ugal d0ugal added the Cleanup label May 3, 2015
@d0ugal d0ugal added this to the 0.13.0 milestone May 3, 2015
@d0ugal
d0ugal force-pushed the config-refactor branch 2 times, most recently from 1757d90 to 3d76ecf Compare May 3, 2015 08:44
@landscape-bot

Copy link
Copy Markdown

Code Health
Repository health increased by 1% when pulling 3d76ecf on d0ugal:config-refactor into 7fa41ab on mkdocs:master.

@waylan

waylan commented May 4, 2015

Copy link
Copy Markdown
Contributor

This looks like a great start. Obviously, the failing tests will need to be cleaned up, but I think this has a lot of potential. As things progress, more stuff can be moved into the validators. For example, the code which splits the markdown_extensions from the extension configs (currently in build.py) could go in a validator. Although, that raises an interesting question: To do so, an additional config setting would need to be created which holds the extension configs. Is there a way to make that setting private so that it cannot be set by the user in their config file (setting it would raise an error), but it would be available internally? I suppose a custom validator could be created for it which raises such an error, but how do you make sure that validator runs before the validator on markdown_extensions which would then populate that private setting? Really just thinking out loud here.

@d0ugal
d0ugal force-pushed the config-refactor branch from 3d76ecf to 450f650 Compare May 4, 2015 07:43
@d0ugal

d0ugal commented May 4, 2015

Copy link
Copy Markdown
Contributor Author

For example, the code which splits the markdown_extensions from the extension configs (currently in build.py) could go in a validator.

That's an interesting idea, I think it makes a lot of sense to do this. We probably need to rename validators tho', perhaps they could just be called ConfigOptions as they do validation and post processing too.

To do so, an additional config setting would need to be created which holds the extension configs. Is there a way to make that setting private so that it cannot be set by the user in their config file (setting it would raise an error), but it would be available internally?

I could do with a better way of handling this too.

https://github.com/mkdocs/mkdocs/pull/496/files#diff-75ea449ab275270869c65fcd82bdbcf8R15

At the moment if the user provided this it would be overwritten, given it is a reference to the config file the user is providing they can't ever give me anything useful here :)

It would be easy to handle something like this in the current code by making a validator that errors if it does exist and then handle the required work in a post_process function which is called after all the validation is done.

I suppose a custom validator could be created for it which raises such an error, but how do you make sure that validator runs before the validator on markdown_extensions which would then populate that private setting? Really just thinking out loud here.

I needed a two pass, so the post_process is a hacky way to add this. It would look something like...

class MarkdownExtension(BaseValidator):

    def check(self, value):

        if value is not None:

            raise ValidationError("..")

    def post_process(self, config, key_name):

        config['markdown_extensions_extra'] = {}

However, I wonder if the concepts should be separated, perhaps into two dictionaries one that holds the config, one that holds internal state and then a session object could bind them together and be passed around.

@d0ugal
d0ugal force-pushed the config-refactor branch from 450f650 to b619e64 Compare May 4, 2015 08:11
@waylan waylan mentioned this pull request May 6, 2015
@d0ugal
d0ugal force-pushed the config-refactor branch 5 times, most recently from 3688e7a to 2cda316 Compare May 8, 2015 05:54
d0ugal added 2 commits May 8, 2015 07:03
This change breaks up the config parsing into a set of validators. This
makes it somewhat easier to follow and add extra checks. As a result we
now also catch a few config errors we wouldn't have before.

The code probably still needs some tidying up, but it works well.
@d0ugal
d0ugal force-pushed the config-refactor branch from 7d67cf7 to e34a516 Compare May 8, 2015 06:03
@d0ugal

d0ugal commented May 8, 2015

Copy link
Copy Markdown
Contributor Author

I think I'm happy enough with this to land it now. @waylan or anyone else want to review before I do?

I expect this will need follow up work, but it should all be much more minor.

Comment thread mkdocs/config/config_options.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You have this backwards. The private method should be the one called internally and the public method should be the one overridden by subclasses. Switch validate and _validate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The reason for this, is that I didn't want to have to do the following on each subclass.

def validate(self, value):
    value = super(CLASS, self)._validate(value)
    if value is None:
        return

So, validate only calls the private subclassed method if the value isn't None.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I do feel like I am missing an obvious way to do this in a better way, but I can't quite think of it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What I think he meant was that the subclasses should override a method called validate. Flip the names of what is exposed and what is not. Subclasses should not override private methods. You shouldn't have to use super as you are still using things the same.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, of course. I get it. Sorry, it's been a long day. That makes so much more sense, not sure why I didn't do this before.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The original recommendation was based on the original code. It wasn't understood that there was an aversion to call _validate. I personally find overriding _validate more evil than calling it, but personal preference and all that ;). I think it just depends on how people approach the language, my background in coding (C/C++) steers me towards the latter. But I get some people in python view _ as don't touch, unless your using a named tuple, where they were forced to expose the class methods that way :).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you are curious, I scope it like this and it's the pattern used in most projects I'm involved with.

  • Underscore class methods within the class and subclasses only
  • Underscore functions (defined outside a class) used within the file only
  • Underscore files (like _file.py) within the project only.

I rarely used double underscore anywhere, other than __init__ an so on.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, I don't care much for do_* names either. But in this case, validate defines how the validator works, but do_validation runs the validation, which seems reasonable. You could use run_validation or run_validators. Just run works too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for explaining. I realize object oriented approaches can be completely different in other languages. As there is no strict rule on how to use underscore methods, I guess it really depends on the community of authors you work with. It's nice to understand different approaches. I will definitely keep this approach in mind when looking at other people's Python projects moving forward.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@waylan Yup. makes sense. I might just go with one of those :)

@facelessuser You are welcome, there are few hard rules here so my way is just one approach. pylint will complain if you access private properties on an object tho, that's one of the reasons for my bias.

@d0ugal
d0ugal force-pushed the config-refactor branch 2 times, most recently from 0010b09 to 5d21d84 Compare May 8, 2015 21:19
Comment thread mkdocs/config/config_options.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typo: run_validation not run_validatuon -- needs to be fixed in some the subclasses too.

I guess I expected this method to be called validate and the method currently called validate to be run_validation, but whatever. I guess run_validation and post_validation is more consistent.

@d0ugal
d0ugal force-pushed the config-refactor branch 3 times, most recently from a116b84 to 89071bf Compare May 9, 2015 08:47
@d0ugal

d0ugal commented May 9, 2015

Copy link
Copy Markdown
Contributor Author

@waylan @facelessuser Thanks both for your input. I'm going to merge this and continue iterating on it in master over time. I think it's a big enough improvement and a big enough PR that I don't want to continue in a branch much longer and make bigger.

However, I'd still be very interested in more feedback - so if you have anything, either comment here or open a new issue. Thanks again!

This name should explain what it does a bit more.
@d0ugal
d0ugal force-pushed the config-refactor branch from 89071bf to 68d3045 Compare May 9, 2015 08:54
@d0ugal
d0ugal force-pushed the config-refactor branch from 3262952 to 3af3448 Compare May 9, 2015 09:42
@d0ugal
d0ugal force-pushed the config-refactor branch from 0db4cf0 to 8fbebc4 Compare May 9, 2015 10:55
d0ugal added a commit that referenced this pull request May 9, 2015
@d0ugal
d0ugal merged commit be9da8d into mkdocs:master May 9, 2015
@d0ugal
d0ugal deleted the config-refactor branch May 24, 2015 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants