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

Provide default values for mirror config options #1740

Merged
merged 3 commits into from
May 29, 2024

Conversation

flyinghyrax
Copy link
Contributor

@flyinghyrax flyinghyrax commented May 27, 2024

(...finally.)

This should be a much more focused PR for addressing issue #1702 without so much additional refactoring.

I've changed the configuration files embedded in the package like so:

  • Renamed the existing "default.conf" to "example.conf". This file is still used when the -c argument is passed file that doesn't exist to create a configuration.
  • Added "defaults.conf", containing only default values for all [mirror] configuration options except for directory, with minimal annotation.

I changed BandersnatchConfig:

  • It's now a subclass of ConfigParser. Technically this violates the Liskov substitution principal by giving the subtype a different signature for __init__. I have mixed feelings about that, but this seemed like a much cleaner way to customize ConfigParser and removed many instances of BandersnatchConfig().config or similar.
  • It always reads the embedded defaults file unless explicitly disabled via a keyword argument (useful for some existing unit tests). The user config file is read second if specified, so values from the user config overwrite values from the defaults file. This leaves the configuration singleton with default values set for any mirror option the user doesn't configure.

Some notable effects of this include:

  • It is no longer meaningful to use ConfigParser.has_option on the 'mirror' section. Instead, you have to check whether the options value is empty or None.
  • Specifying a default/fallback value when calling .get on the 'mirror' section will have no effect, because the option will already be present in the ConfigParser mappings.

Happy to tweak which options are required/have defaults and dynamic fallback behaviors for things like root_uri, if that's something we want to change at this point.

Fixes #1702
Fixes #990

Copy link

codecov bot commented May 27, 2024

Codecov Report

Attention: Patch coverage is 85.83333% with 17 lines in your changes are missing coverage. Please review.

Project coverage is 83.69%. Comparing base (4d020e8) to head (74986a2).
Report is 96 commits behind head on main.

Files Patch % Lines
src/bandersnatch/configuration.py 80.00% 11 Missing and 1 partial ⚠️
src/bandersnatch/main.py 61.53% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1740      +/-   ##
==========================================
+ Coverage   79.69%   83.69%   +4.00%     
==========================================
  Files          31       33       +2     
  Lines        4324     4380      +56     
  Branches      780      783       +3     
==========================================
+ Hits         3446     3666     +220     
+ Misses        721      531     -190     
- Partials      157      183      +26     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@flyinghyrax flyinghyrax marked this pull request as ready for review May 27, 2024 21:17
This splits the existing 'default.conf' config file shipped with the package into two
similar files: "defaults.conf" and "example.conf". "example.conf" is an exact copy of the
previous "default.conf". The new "defaults.conf" is a stripped-down version containing
only default values for all mirror configuration options except "mirror.directory".

BandersnatchConfig is changed to *always* read defaults.conf, then read the user config
file if one is specified. This leaves the ConfigParser populated with default values for
any mirror options that aren't set by the user (except mirror.directory).

Notable ripple effects for this include:
- It is no longer meaningful to check ConfigParser.has_option with the 'mirror' section.
  Instead, you have to check whether the options value is empty or None.
- Specifying a default/fallback value when calling .get on the 'mirror' section will
  have no effect, because the option will already be present in the ConfigParser mappings.

As (mostly) an implementation detail, BandersnatchConfig is changed to be a subclass
of ConfigParser. The BandersnatchConfig singleton can be used anywhere a ConfigParser
instance is expected without having to use '.config' to access a nested ConfigParser.

Fixes pypa#1702
Fixes pypa#990
Add default values for options that are no longer required.
Copy link
Contributor

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

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

Looks like a nice refactor and cleanup. Many thanks!

src/bandersnatch/configuration.py Show resolved Hide resolved
src/bandersnatch/configuration.py Outdated Show resolved Hide resolved
@cooperlees cooperlees merged commit f405f48 into pypa:main May 29, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants