Skip to content

Commit bb07c17

Browse files
committed
Merge pull request django-haystack#1340 from claudep/manage_commands
chg: migrate management commands to argparse
2 parents 6e559de + ae95d8c commit bb07c17

File tree

6 files changed

+137
-119
lines changed

6 files changed

+137
-119
lines changed

haystack/management/commands/build_solr_schema.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
from __future__ import absolute_import, division, print_function, unicode_literals
44

5-
import sys
6-
from optparse import make_option
7-
85
from django.core.exceptions import ImproperlyConfigured
96
from django.core.management.base import BaseCommand
107
from django.template import Context, loader
118

12-
from haystack import constants
9+
from haystack import connections, connection_router, constants
1310
from haystack.backends.solr_backend import SolrSearchBackend
1411

1512

1613
class Command(BaseCommand):
1714
help = "Generates a Solr schema that reflects the indexes."
18-
base_options = (
19-
make_option("-f", "--filename", action="store", type="string", dest="filename",
20-
help='If provided, directs output to a file instead of stdout.'),
21-
make_option("-u", "--using", action="store", type="string", dest="using", default=constants.DEFAULT_ALIAS,
22-
help='If provided, chooses a connection to work with.'),
23-
)
24-
option_list = BaseCommand.option_list + base_options
15+
16+
def add_arguments(self, parser):
17+
parser.add_argument(
18+
"-f", "--filename",
19+
help='If provided, directs output to a file instead of stdout.'
20+
)
21+
parser.add_argument(
22+
"-u", "--using", default=constants.DEFAULT_ALIAS,
23+
help='If provided, chooses a connection to work with.'
24+
)
2525

2626
def handle(self, **options):
2727
"""Generates a Solr schema that reflects the indexes."""
@@ -34,13 +34,14 @@ def handle(self, **options):
3434
self.print_stdout(schema_xml)
3535

3636
def build_context(self, using):
37-
from haystack import connections, connection_router
3837
backend = connections[using].get_backend()
3938

4039
if not isinstance(backend, SolrSearchBackend):
4140
raise ImproperlyConfigured("'%s' isn't configured as a SolrEngine)." % backend.connection_alias)
4241

43-
content_field_name, fields = backend.build_schema(connections[using].get_unified_index().all_searchfields())
42+
content_field_name, fields = backend.build_schema(
43+
connections[using].get_unified_index().all_searchfields()
44+
)
4445
return Context({
4546
'content_field_name': content_field_name,
4647
'fields': fields,
@@ -56,15 +57,14 @@ def build_template(self, using):
5657
return t.render(c)
5758

5859
def print_stdout(self, schema_xml):
59-
sys.stderr.write("\n")
60-
sys.stderr.write("\n")
61-
sys.stderr.write("\n")
62-
sys.stderr.write("Save the following output to 'schema.xml' and place it in your Solr configuration directory.\n")
63-
sys.stderr.write("--------------------------------------------------------------------------------------------\n")
64-
sys.stderr.write("\n")
65-
print(schema_xml)
60+
self.stderr.write("\n")
61+
self.stderr.write("\n")
62+
self.stderr.write("\n")
63+
self.stderr.write("Save the following output to 'schema.xml' and place it in your Solr configuration directory.\n")
64+
self.stderr.write("--------------------------------------------------------------------------------------------\n")
65+
self.stderr.write("\n")
66+
self.stdout.write(schema_xml)
6667

6768
def write_file(self, filename, schema_xml):
68-
schema_file = open(filename, 'w')
69-
schema_file.write(schema_xml)
70-
schema_file.close()
69+
with open(filename, 'w') as schema_file:
70+
schema_file.write(schema_xml)

haystack/management/commands/clear_index.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,32 @@
22

33
from __future__ import absolute_import, division, print_function, unicode_literals
44

5-
import sys
6-
from optparse import make_option
7-
85
from django.core.management.base import BaseCommand
96
from django.utils import six
107

8+
from haystack import connections
9+
1110

1211
class Command(BaseCommand):
1312
help = "Clears out the search index completely."
14-
base_options = (
15-
make_option('--noinput', action='store_false', dest='interactive', default=True,
13+
14+
def add_arguments(self, parser):
15+
parser.add_argument(
16+
'--noinput', action='store_false', dest='interactive', default=True,
1617
help='If provided, no prompts will be issued to the user and the data will be wiped out.'
17-
),
18-
make_option("-u", "--using", action="append", dest="using",
19-
default=[],
18+
)
19+
parser.add_argument(
20+
"-u", "--using", action="append", default=[],
2021
help='Update only the named backend (can be used multiple times). '
2122
'By default all backends will be updated.'
22-
),
23-
make_option('--nocommit', action='store_false', dest='commit',
23+
)
24+
parser.add_argument(
25+
'--nocommit', action='store_false', dest='commit',
2426
default=True, help='Will pass commit=False to the backend.'
25-
),
26-
)
27-
option_list = BaseCommand.option_list + base_options
27+
)
2828

2929
def handle(self, **options):
3030
"""Clears out the search index completely."""
31-
from haystack import connections
3231
self.verbosity = int(options.get('verbosity', 1))
3332
self.commit = options.get('commit', True)
3433

@@ -37,23 +36,21 @@ def handle(self, **options):
3736
using = connections.connections_info.keys()
3837

3938
if options.get('interactive', True):
40-
print()
41-
print("WARNING: This will irreparably remove EVERYTHING from your search index in connection '%s'." % "', '".join(using))
42-
print("Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.")
39+
self.stdout.write("WARNING: This will irreparably remove EVERYTHING from your search index in connection '%s'." % "', '".join(using))
40+
self.stdout.write("Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.")
4341

4442
yes_or_no = six.moves.input("Are you sure you wish to continue? [y/N] ")
45-
print
4643

4744
if not yes_or_no.lower().startswith('y'):
48-
print("No action taken.")
49-
sys.exit()
45+
self.stdout.write("No action taken.")
46+
return
5047

5148
if self.verbosity >= 1:
52-
print("Removing all documents from your index because you said so.")
49+
self.stdout.write("Removing all documents from your index because you said so.")
5350

5451
for backend_name in using:
5552
backend = connections[backend_name].get_backend()
5653
backend.clear(commit=self.commit)
5754

5855
if self.verbosity >= 1:
59-
print("All documents removed.")
56+
self.stdout.write("All documents removed.")

haystack/management/commands/haystack_info.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22

33
from __future__ import absolute_import, division, print_function, unicode_literals
44

5-
from django.core.management.base import NoArgsCommand
5+
from django.core.management.base import BaseCommand
66

7+
from haystack import connections
78

8-
class Command(NoArgsCommand):
9+
10+
class Command(BaseCommand):
911
help = "Provides feedback about the current Haystack setup."
1012

11-
def handle_noargs(self, **options):
13+
def handle(self, **options):
1214
"""Provides feedback about the current Haystack setup."""
13-
from haystack import connections
1415

1516
unified_index = connections['default'].get_unified_index()
1617
indexed = unified_index.get_indexed_models()
1718
index_count = len(indexed)
18-
print("Number of handled %s index(es)." % index_count)
19+
self.stdout.write("Number of handled %s index(es)." % index_count)
1920

2021
for index in indexed:
21-
print(" - Model: %s by Index: %s" % (index.__name__, unified_index.get_indexes()[index]))
22+
self.stdout.write(" - Model: %s by Index: %s" % (
23+
index.__name__, unified_index.get_indexes()[index])
24+
)

haystack/management/commands/rebuild_index.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,28 @@
55
from django.core.management import call_command
66
from django.core.management.base import BaseCommand
77

8-
from haystack.management.commands.clear_index import Command as ClearCommand
9-
from haystack.management.commands.update_index import Command as UpdateCommand
10-
11-
__all__ = ['Command']
12-
13-
_combined_options = list(BaseCommand.option_list)
14-
_combined_options.extend(option for option in UpdateCommand.base_options
15-
if option.get_opt_string() not in [i.get_opt_string() for i in _combined_options])
16-
_combined_options.extend(option for option in ClearCommand.base_options
17-
if option.get_opt_string() not in [i.get_opt_string() for i in _combined_options])
18-
198

209
class Command(BaseCommand):
2110
help = "Completely rebuilds the search index by removing the old data and then updating."
22-
option_list = _combined_options
11+
12+
def add_arguments(self, parser):
13+
parser.add_argument(
14+
'--noinput', action='store_false', dest='interactive', default=True,
15+
help='If provided, no prompts will be issued to the user and the data will be wiped out.'
16+
)
17+
parser.add_argument(
18+
'-u', '--using', action='append', default=[],
19+
help='Update only the named backend (can be used multiple times). '
20+
'By default all backends will be updated.'
21+
)
22+
parser.add_argument(
23+
'-k', '--workers', default=0, type=int,
24+
help='Allows for the use multiple workers to parallelize indexing. Requires multiprocessing.'
25+
)
26+
parser.add_argument(
27+
'--nocommit', action='store_false', dest='commit',
28+
default=True, help='Will pass commit=False to the backend.'
29+
)
2330

2431
def handle(self, **options):
2532
call_command('clear_index', **options)

0 commit comments

Comments
 (0)