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

feat/improve configuration #75

Merged
merged 80 commits into from
Apr 21, 2022
Merged

Conversation

onerandomusername
Copy link
Member

@onerandomusername onerandomusername commented Sep 19, 2021

  • feat: rewrite the entire configuration system to use marshmallow and attrs
  • config: remove config_default.toml since its now part of the classes

Closes #22

@onerandomusername onerandomusername added p: high High Priority s: needs review Ready for review and merge t: enhancement Changes or improvements to existing features a: bot Catch all for issues regarding the bot labels Sep 19, 2021
@codecov
Copy link

codecov bot commented Sep 19, 2021

Codecov Report

Merging #75 (f0ec067) into main (4b07917) will decrease coverage by 3.08%.
The diff coverage is 61.79%.

❗ Current head f0ec067 differs from pull request most recent head 4644d76. Consider uploading reports for the commit 4644d76 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #75      +/-   ##
==========================================
- Coverage   70.90%   67.81%   -3.09%     
==========================================
  Files          33       35       +2     
  Lines        1619     2082     +463     
  Branches      199      286      +87     
==========================================
+ Hits         1148     1412     +264     
- Misses        439      610     +171     
- Partials       32       60      +28     
Impacted Files Coverage Δ
modmail/__main__.py 0.00% <0.00%> (ø)
modmail/extensions/meta.py 73.91% <ø> (ø)
modmail/extensions/extension_manager.py 35.15% <22.22%> (+0.79%) ⬆️
modmail/extensions/configuration_manager.py 38.60% <38.60%> (ø)
modmail/bot.py 38.28% <50.00%> (-0.57%) ⬇️
tests/modmail/conftest.py 75.00% <57.14%> (-11.96%) ⬇️
modmail/config.py 67.13% <66.66%> (-29.79%) ⬇️
tests/modmail/test_config.py 95.94% <95.94%> (ø)
modmail/__init__.py 100.00% <100.00%> (ø)
modmail/utils/embeds.py 81.08% <100.00%> (-0.50%) ⬇️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d47e097...4644d76. Read the comment docs.

@onerandomusername onerandomusername force-pushed the feat/improve-configuration branch 6 times, most recently from fe7a268 to 0b511e3 Compare September 21, 2021 22:22
@onerandomusername
Copy link
Member Author

This pr is now feature frozen, a full system to be able to change settings while the bot is running will be added later, the rudimentary system provided here is good enough for now.

Copy link
Member

@Shivansh-007 Shivansh-007 left a comment

Choose a reason for hiding this comment

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

It would be great to have some documentation on how to work with the config as a developer (adding, removing, meta), otherwise it works pretty well. I haven't tested out this via discord as I off it for a while. Secondly, I am not really familiar with attr (I should give it a shot, probably after exams).

.pre-commit-config.yaml Show resolved Hide resolved
pyproject.toml Outdated
Comment on lines 25 to 27
environs = "~=9.3.3"
marshmallow = "~=3.13.0"
python-dotenv = "^0.19.0"
Copy link
Member

Choose a reason for hiding this comment

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

Why do we have two deps which do the samething?

Copy link
Member Author

Choose a reason for hiding this comment

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

environs is being used here in order to set the logging level, #118 will make this unnecessary.

Copy link
Member

Choose a reason for hiding this comment

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

still stands

Copy link
Member Author

Choose a reason for hiding this comment

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

ROOT_LOG_LEVEL = env.log_level("MODMAIL_LOG_LEVEL", logging.TRACE)
FMT = "%(asctime)s %(levelname)10s %(name)15s - [%(lineno)5d]: %(message)s"

Used here, but made irrelevant by #118 so I'm not fixing it here.

modmail/config.py Outdated Show resolved Hide resolved
modmail/config.py Outdated Show resolved Hide resolved
modmail/config.py Outdated Show resolved Hide resolved
modmail/config.py Outdated Show resolved Hide resolved
modmail/config.py Outdated Show resolved Hide resolved
modmail/config.py Outdated Show resolved Hide resolved
modmail/config.py Outdated Show resolved Hide resolved
modmail/config.py Show resolved Hide resolved
Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
…oken from being changed

Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
when converting to a marshmallow schema, desert does not use class defined Meta classes

Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
if any modmail configuration file is edited, a pre-commit hook will run to check that the autogenerated files are up-to-date

Signed-off-by: onerandomusername <genericusername414+git@gmail.com>
@onerandomusername onerandomusername force-pushed the feat/improve-configuration branch 3 times, most recently from c17131b to f015b56 Compare November 21, 2021 16:52
Copy link
Member

@Shivansh-007 Shivansh-007 left a comment

Choose a reason for hiding this comment

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

half reviewed, gtg so can't finish

.pre-commit-config.yaml Show resolved Hide resolved


_log_optional_deps = logging.getLogger("modmail.optional_dependencies")
_silence = " You can silence these alerts by ignoring the `modmail.optional_dependencies` logger"
Copy link
Member

Choose a reason for hiding this comment

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

Ignoring optional_dependencies where? Be a bit more concise, also am not sure if this is really highlighted since the user's won't be diving deep into the code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not even sure this logger is necessary, tbh

Copy link
Member Author

Choose a reason for hiding this comment

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

The only people who would silence this are developers, and I wonder if its even necessary. I might yank the warning entirely.

try:
import dotenv
except ModuleNotFoundError: # pragma: nocover
_log_optional_deps.notice("dotenv was unable to be imported." + _silence)
Copy link
Member

Choose a reason for hiding this comment

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

f-string?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not worth it since silence is always at the end.

Per this comment, I may be removing that warning anyways.


@functools.lru_cache
def _get_config_directory() -> pathlib.Path:
Copy link
Member

Choose a reason for hiding this comment

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

Its also worth checking if the user supplied a --config arg in sys.argv. This would in cases when you are working on multiple scenarios and don't need to constantly keep changing the config while having the others in background.

Using --config you won't need to change them and can directly pass the config in.

Not really the write place, it would better fit in USER_CONFIG_FILES.

Copy link
Member Author

Choose a reason for hiding this comment

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

Might add it in a seperate pr, I don't want to add this here since it would involve adding a cli to the bot's entrypoint.

While I'm aware that's not hard, I don't want to reach much further out of the scope of this pr.

]


class BetterPartialEmojiConverter(discord.ext.commands.converter.EmojiConverter):
Copy link
Member

Choose a reason for hiding this comment

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

could probably be moved to a file called patches.py since it likely to be used elsewhere in the code.

Comment on lines +82 to +103
kw = {}
kw["default"] = field.default if field.default is not marshmallow.missing else None
kw["name"] = field.name
kw["type"] = field.type

kw["metadata"] = field.metadata
kw["field"] = field

kw["frozen"] = field.on_setattr is attr.setters.frozen or frozen

metadata_table: config.ConfigMetadata = field.metadata[config.METADATA_TABLE]
kw[config.METADATA_TABLE] = metadata_table
kw["description"] = metadata_table.description
kw["canonical_name"] = metadata_table.canonical_name
kw["extended_description"] = metadata_table.extended_description
kw["hidden"] = metadata_table.hidden

kw["discord_converter"] = metadata_table.discord_converter
kw["discord_converter_attribute"] = metadata_table.discord_converter_attribute

if nested is not None:
kw["nested"] = nested
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be more readable if we form the dict at once?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so, I felt like this was more readable.

I meant to add documentation to each key here in the form of comments, which I guess i didn't do.

await responses.send_positive_response(ctx, response)

@config_group.command(name="get", aliases=("show",))
async def get_config(self, ctx: Context, option: KeyConverter) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

get shouldn't work if the value is hidden, like in the case of bot.token

Copy link
Member Author

Choose a reason for hiding this comment

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

Will fix when renovating the configuration manager in a new pr.

@@ -35,7 +33,7 @@ def __init__(self: discord.Embed, description: str = None, **kwargs): # noqa: N
if ("description" in kwargs or description is not None) and "content" in kwargs:
raise TypeError("Description and content are aliases for the same field, but both were provided.")

colour = kwargs.pop("color", kwargs.pop("colour", DEFAULT_COLOR))
colour = kwargs.pop("color", kwargs.pop("colour", config().user.colours.base_embed_color))
Copy link
Member

Choose a reason for hiding this comment

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

bot.config

Copy link
Member Author

Choose a reason for hiding this comment

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

bot is not available in this context.

@@ -34,8 +34,9 @@ def determine_bot_mode() -> int:
The configuration system uses true/false values, so we need to turn them into an integer for bitwise.
"""
bot_mode = 0
_config = config()
Copy link
Member

Choose a reason for hiding this comment

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

bot.config

Copy link
Member Author

Choose a reason for hiding this comment

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

bot is not available in this context.

pyproject.toml Outdated
Comment on lines 25 to 27
environs = "~=9.3.3"
marshmallow = "~=3.13.0"
python-dotenv = "^0.19.0"
Copy link
Member

Choose a reason for hiding this comment

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

still stands

@coveralls
Copy link
Collaborator

coveralls commented Jan 2, 2022

Coverage Status

Coverage decreased (-2.8%) to 75.0% when pulling 4644d76 on feat/improve-configuration into d47e097 on main.

@onerandomusername onerandomusername merged commit 10a6468 into main Apr 21, 2022
@onerandomusername onerandomusername deleted the feat/improve-configuration branch April 21, 2022 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: bot Catch all for issues regarding the bot p: high High Priority review: do not merge s: needs review Ready for review and merge t: enhancement Changes or improvements to existing features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite configuration system
4 participants