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

safely convert ini parameters to bool, int, list and dict #104

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
14 changes: 13 additions & 1 deletion pyramid_celery/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ def get_route_config(parser, section):
return config


def safe_conversion(value):
"""convert a string to a more specific type"""
if value.lower() in ("true", "false"):
return bool(value)
Comment on lines +97 to +98

Choose a reason for hiding this comment

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

For this, I suggest using pyramid.settings.asbool to consider the full range of supported "bool-like" values.

Copy link
Author

Choose a reason for hiding this comment

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

I did consider this, however, this is trying to replicate what you might use in celeryconfig.py, but with values you would find in the ini file.

For example in celeryconfig.py you might write
task_acks_late = True
so in the ini you would need to write
task_acks_late = true

Pyramid asbool will also convert integer and other values to a boolean which may or may not work similarly to setting them celeryconfig.py

Choose a reason for hiding this comment

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

Good point about the integer. Better to stay safe with the explicit bool-like strings. 👍

try:
if float(value).is_integer():
return int(value)
return float(value)
except ValueError:
return value


class INILoader(celery.loaders.base.BaseLoader):
ConfigParser = configparser.SafeConfigParser

Expand All @@ -106,7 +118,7 @@ def read_configuration(self, fail_silently=True):
config_dict = {}

for key, value in self.parser.items('celery'):
config_dict[key] = value
config_dict[key] = safe_conversion(value)

Choose a reason for hiding this comment

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

Might need to consider applying this to other structures below?
For example, a lot of Task result backend settings tend to have very nested dict definitions.


if celery_version.major > 6:
# TODO: Check for invalid settings
Expand Down