Skip to content

Commit 65a99b4

Browse files
committed
Extract base class for handling --extra-settings
1 parent 630ffd7 commit 65a99b4

File tree

3 files changed

+85
-82
lines changed

3 files changed

+85
-82
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import warnings
2+
from typing import Any, Optional
3+
4+
from django.core.management.base import BaseCommand, CommandParser
5+
6+
from .utils import load_extra_settings
7+
from .constants import SETTING_NAME_PREFIX
8+
9+
10+
class CommandWithExtraSettings(BaseCommand):
11+
"""
12+
Base class for handling `--extra-settings`.
13+
14+
Derived classes must call `handle_extra_settings` at the top of their
15+
`handle` method. For example:
16+
17+
class Command(CommandWithExtraSettings):
18+
def handle(self, **options: Any) -> None:
19+
super().handle_extra_settings(**options)
20+
...
21+
"""
22+
23+
def add_arguments(self, parser: CommandParser) -> None:
24+
super().add_arguments(parser)
25+
26+
extra_settings_group = parser.add_mutually_exclusive_group()
27+
extra_settings_group.add_argument(
28+
'--config',
29+
action='store',
30+
default=None,
31+
help="The path to an additional django-style config file to load "
32+
"(this spelling is deprecated in favour of '--extra-settings')",
33+
)
34+
extra_settings_group.add_argument(
35+
'--extra-settings',
36+
action='store',
37+
default=None,
38+
help="The path to an additional django-style settings file to load. "
39+
f"{SETTING_NAME_PREFIX}* settings discovered in this file will "
40+
"override those from the default Django settings.",
41+
)
42+
43+
def handle_extra_settings(
44+
self,
45+
*,
46+
config: Optional[str] = None,
47+
extra_settings: Optional[str],
48+
**_: Any
49+
) -> Optional[str]:
50+
"""
51+
Load extra settings if there are any.
52+
53+
Returns the filename (if any) of the extra settings that have been loaded.
54+
"""
55+
56+
if config is not None:
57+
warnings.warn(
58+
"Use of '--config' is deprecated in favour of '--extra-settings'.",
59+
category=DeprecationWarning,
60+
)
61+
extra_settings = config
62+
63+
# Configuration overrides
64+
if extra_settings is not None:
65+
load_extra_settings(extra_settings)
66+
67+
return extra_settings

django_lightweight_queue/management/commands/queue_configuration.py

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,14 @@
1-
import warnings
21
from typing import Any
32

4-
from django.core.management.base import BaseCommand, CommandParser
5-
63
from ... import app_settings
7-
from ...utils import get_backend, get_queue_counts, load_extra_settings
8-
from ...constants import SETTING_NAME_PREFIX
4+
from ...utils import get_backend, get_queue_counts
5+
from ...command_utils import CommandWithExtraSettings
96
from ...cron_scheduler import get_cron_config
107

118

12-
class Command(BaseCommand):
13-
def add_arguments(self, parser: CommandParser) -> None:
14-
extra_settings_group = parser.add_mutually_exclusive_group()
15-
extra_settings_group.add_argument(
16-
'--config',
17-
action='store',
18-
default=None,
19-
help="The path to an additional django-style config file to load "
20-
"(this spelling is deprecated in favour of '--extra-settings')",
21-
)
22-
extra_settings_group.add_argument(
23-
'--extra-settings',
24-
action='store',
25-
default=None,
26-
help="The path to an additional django-style settings file to load. "
27-
f"{SETTING_NAME_PREFIX}* settings discovered in this file will "
28-
"override those from the default Django settings.",
29-
)
30-
9+
class Command(CommandWithExtraSettings):
3110
def handle(self, **options: Any) -> None:
32-
extra_config = options.pop('config')
33-
if extra_config is not None:
34-
warnings.warn(
35-
"Use of '--config' is deprecated in favour of '--extra-settings'.",
36-
category=DeprecationWarning,
37-
)
38-
options['extra_settings'] = extra_config
39-
40-
# Configuration overrides
41-
extra_settings = options['extra_settings']
42-
if extra_settings is not None:
43-
load_extra_settings(extra_settings)
11+
super().handle_extra_settings(**options)
4412

4513
print("django-lightweight-queue")
4614
print("========================")

django_lightweight_queue/management/commands/queue_runner.py

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
import warnings
21
from typing import Any, Dict, Optional
32

43
import daemonize
54

65
from django.apps import apps
7-
from django.core.management.base import (
8-
BaseCommand,
9-
CommandError,
10-
CommandParser,
11-
)
6+
from django.core.management.base import CommandError, CommandParser
127

138
from ...types import QueueName
14-
from ...utils import (
15-
get_logger,
16-
get_backend,
17-
get_middleware,
18-
load_extra_settings,
19-
)
9+
from ...utils import get_logger, get_backend, get_middleware
2010
from ...runner import runner
21-
from ...constants import SETTING_NAME_PREFIX
11+
from ...command_utils import CommandWithExtraSettings
2212
from ...machine_types import Machine, PooledMachine, DirectlyConfiguredMachine
2313

2414

25-
class Command(BaseCommand):
15+
class Command(CommandWithExtraSettings):
2616
def add_arguments(self, parser: CommandParser) -> None:
17+
super().add_arguments(parser)
18+
2719
parser.add_argument(
2820
'--pidfile',
2921
action='store',
@@ -58,22 +50,6 @@ def add_arguments(self, parser: CommandParser) -> None:
5850
default=None,
5951
help="Only run the given queue, useful for local debugging",
6052
)
61-
extra_settings_group = parser.add_mutually_exclusive_group()
62-
extra_settings_group.add_argument(
63-
'--config',
64-
action='store',
65-
default=None,
66-
help="The path to an additional django-style config file to load "
67-
"(this spelling is deprecated in favour of '--extra-settings')",
68-
)
69-
extra_settings_group.add_argument(
70-
'--extra-settings',
71-
action='store',
72-
default=None,
73-
help="The path to an additional django-style settings file to load. "
74-
f"{SETTING_NAME_PREFIX}* settings discovered in this file will "
75-
"override those from the default Django settings.",
76-
)
7753
parser.add_argument(
7854
'--exact-configuration',
7955
action='store_true',
@@ -83,17 +59,9 @@ def add_arguments(self, parser: CommandParser) -> None:
8359
" '--of'.",
8460
)
8561

86-
def validate_and_normalise(self, options: Dict[str, Any]) -> None:
87-
extra_config = options.pop('config')
88-
if extra_config is not None:
89-
warnings.warn(
90-
"Use of '--config' is deprecated in favour of '--extra-settings'.",
91-
category=DeprecationWarning,
92-
)
93-
options['extra_settings'] = extra_config
94-
62+
def validate_and_normalise(self, options: Dict[str, Any], had_extra_settings: bool) -> None:
9563
if options['exact_configuration']:
96-
if not options['extra_settings']:
64+
if not had_extra_settings:
9765
raise CommandError(
9866
"Must provide a value for '--extra-settings' when using "
9967
"'--exact-configuration'.",
@@ -127,19 +95,19 @@ def validate_and_normalise(self, options: Dict[str, Any]) -> None:
12795
def handle(self, **options: Any) -> None:
12896
logger = get_logger('dlq.master')
12997

130-
self.validate_and_normalise(options)
98+
extra_settings = super().handle_extra_settings(**options)
99+
100+
self.validate_and_normalise(
101+
options,
102+
had_extra_settings=extra_settings is not None,
103+
)
131104

132105
def touch_filename(name: str) -> Optional[str]:
133106
try:
134107
return options['touchfile'] % name
135108
except TypeError:
136109
return None
137110

138-
# Configuration overrides
139-
extra_config = options['extra_settings']
140-
if extra_config is not None:
141-
load_extra_settings(extra_config)
142-
143111
logger.info("Starting queue master")
144112

145113
# Ensure children will be able to import our backend

0 commit comments

Comments
 (0)