@@ -60,6 +60,12 @@ Our command might look like this:
60
60
61
61
|
62
62
63
+ .. code-block :: console
64
+
65
+ $> python manage.py backup list
66
+ Default backup routines:
67
+ database(filename={database}.json, databases=['default'])
68
+
63
69
.. _inheritance :
64
70
65
71
Inheritance
@@ -72,8 +78,8 @@ files to our site. This means we'll want to add a media backup routine to the ba
72
78
.. note ::
73
79
74
80
Inheritance also works for commands defined using the Typer-style function based interface.
75
- Just import Command from the upstream command module and subclass it just as you would if you
76
- had defined it using the class based interface .
81
+ Import the root Typer _ app from the upstream command module and pass it as an argument
82
+ to Typer _ when you create the root app in your overriding command module .
77
83
78
84
Say our app tree looks like this:
79
85
@@ -147,10 +153,18 @@ backup batch:
147
153
148
154
.. code-block :: bash
149
155
156
+ $> python manage.py backup list
157
+ Default backup routines:
158
+ database(filename={database}.json, databases=[' default' ])
159
+ media(filename=media.tar.gz)
150
160
# backup media only
151
- $ python manage.py backup media
161
+ $> python manage.py backup media
162
+ Backing up ./media to ./media.tar.gz
152
163
# or backup database and media
153
- $ python manage.py backup
164
+ $> python manage.py backup
165
+ Backing up database [default] to: ./default.json
166
+ [.............................................]
167
+ Backing up ./media to ./media.tar.gz
154
168
155
169
.. warning ::
156
170
@@ -327,41 +341,93 @@ Overriding Groups
327
341
~~~~~~~~~~~~~~~~~
328
342
329
343
Some commands might have deep nesting of subcommands and groups. If you want to override a
330
- group or subcommand of a group down a chain of commands you simply need to access the
331
- :class: `~django_typer.CommandGroup ` instance of the group you want to override or extend:
344
+ group or subcommand of a group down a chain of commands you would need to access the
345
+ :class: `~django_typer.Typer ` instance of the group you want to override or extend:
332
346
333
- .. code-block :: python
347
+ .. tabs ::
348
+
349
+ .. tab :: Django-style
350
+
351
+ .. code-block :: python
352
+
353
+ from somewhere.upstream.management.commands.command import Command
354
+
355
+ # add a command to grp2 which is a subgroup of grp1
356
+ @Command.grp1.grp2.command ()
357
+ def my_command (): # remember self is optional
358
+ pass
359
+
360
+ # add a subgroup to grp2 which is a subgroup of grp1
361
+ @Command.grp1.grp2.group ()
362
+ def grp3 ():
363
+ pass
364
+
365
+ .. tab :: Typer-style
366
+
367
+ .. code-block :: python
334
368
335
- from somewhere.upstream.management.commands.command import Command
369
+ from somewhere.upstream.management.commands.command import app
336
370
337
- # add a command to grp2 which is a subgroup of grp1
338
- @Command .grp1.grp2.command ()
339
- def my_command (): # remember self is optional
340
- pass
371
+ # add a command to grp2 which is a subgroup of grp1
372
+ @app .grp1.grp2.command ()
373
+ def my_command (): # remember self is optional
374
+ pass
341
375
342
- # add a subgroup to grp2 which is a subgroup of grp1
343
- @Command .grp1.grp2.group ()
344
- def grp3 ():
345
- pass
376
+ # add a subgroup to grp2 which is a subgroup of grp1
377
+ @app .grp1.grp2.group ()
378
+ def grp3 ():
379
+ pass
346
380
347
381
You may even override the initializer of a predefined group:
348
382
349
- .. code-block :: python
383
+ .. tabs ::
384
+
385
+ .. tab :: Django-style
386
+
387
+ .. code-block :: python
388
+
389
+ from somewhere.upstream.management.commands.command import Command
390
+
391
+ # override the initializer (typer callback) of grp1 on Command,
392
+ # this will not alter the child groups of grp1 (grp2, grp3, etc.)
393
+ @Command.grp1.initialize ()
394
+ def grp1_init (self ):
395
+ pass
396
+
397
+ @Command.group ()
398
+ def grp1 (self ):
399
+ """
400
+ This would override grp1 entirely and remove all subcommands
401
+ and groups.
402
+ """
403
+
404
+ .. tab :: Typer-style
405
+
406
+ .. code-block :: python
407
+
408
+ from somewhere.upstream.management.commands.command import app
409
+
410
+ # override the initializer (typer callback) of grp1 on app,
411
+ # this will not alter the child groups of grp1 (grp2, grp3, etc.)
412
+ @app.grp1.initialize ()
413
+ def grp1_init ():
414
+ pass
350
415
351
- from somewhere.upstream.management.commands.command import Command
416
+ @app.group ()
417
+ def grp1 ():
418
+ """
419
+ This would override grp1 entirely and remove all subcommands
420
+ and groups.
421
+ """
352
422
353
- # override the initializer (typer callback) of grp1 on Command,
354
- # this will not alter the child groups of grp1 (grp2, grp3, etc.)
355
- @Command.grp1.initialize ()
356
- def grp1_init (self ):
357
- pass
423
+ .. tip ::
358
424
359
- @Command. group()
360
- def grp1 ( self ):
361
- """
362
- This would override grp1 entirely and remove all subcommands
363
- and groups.
364
- """
425
+ If a group or command has not been directly defined on a Command class, django-typer will do
426
+ a ` breadth first search < https://en.wikipedia.org/wiki/Breadth-first_search >`_ of the command
427
+ tree and fetch the first group or subcommand that matches the name of the attribute. This
428
+ means that you do not necessarily have to walk the command hierarchy
429
+ (i.e. `` Command.grp1.grp2.grp3.cmd ``), if there is only one cmd you can simply write
430
+ `` Command.cmd ``. However, using the strict hierarchy will be robust to future changes.
365
431
366
432
When Do Plugins Make Sense?
367
433
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -377,4 +443,4 @@ projects its often a good idea to break your code into apps that are as self con
377
443
Plugins can be a good way to organize commands in a code base that follows this pattern. It
378
444
also allows for deployments that install a subset of those apps and is therefore a good way to
379
445
organize commands in code bases that serve as a framework for a particular kind of site or that
380
- support selecting the features to install.
446
+ support selecting the features to install by the inclusion or exclusion of specific apps .
0 commit comments