Skip to main content

My collection of things for working with Django.

Project description

My collection of things for working with Django.

Latest release 20241119: New DjangoSpecificSubCommand(CSBaseCommand.SubCommandClass) to include support for pure Django BaseCommands.

Class BaseCommand(cs.cmdutils.BaseCommand, django.core.management.base.BaseCommand)

A drop in class for django.core.management.base.BaseCommand which subclasses cs.cmdutils.BaseCommand.

This lets me write management commands more easily, particularly if there are subcommands.

This is a drop in in the sense that you still make a management command in nearly the same way:

from cs.djutils import BaseCommand

class Command(BaseCommand):

and manage.py will find it and run it as normal. But from that point on the style is as for cs.cmdutils.BaseCommand:

  • no aegparse setup
  • direct support for subcommands as methods
  • succinct option parsing, if you want command line options

A simple command looks like this:

class Command(BaseCommand):

    def main(self, argv):
        ... do stuff based on the CLI args `argv` ...

A command with subcommands looks like this:

class Command(BaseCommand):

    def cmd_this(self, argv):
        ... do the "this" subcommand ...

    def cmd_that(self, argv):
        ... do the "that" subcommand ...

If want some kind of app/client specific "overcommand" composed from other management commands you can import them and make them subcommands of the overcommand:

from .other_command import Command as OtherCommand

class Command(BaseCommand):

    # provide it as the "other" subcommand
    cmd_other = OtherCommand

Option parsing is inline in the command. self comes presupplied with a .options attribute which is an instance of cs.cmdutils.BaseCommandOptions (or some subclass).

Parsing options is simple:

class Command(BaseCommand):

    def cmd_this(self, argv):
        options = self.options
        # parsing options:
        #
        # boolean -x option, makes options.x
        #
        # --thing-limit n option taking an int
        # makes options.thing_limit
        # help text is "Thing limit."
        #
        # a --mode foo option taking a string
        # makes options.mode
        # help text is "The run mode."
        options.popopts(
            argv,
            x=None,
            thing_limit_=int,
            mode_='The run mode.',
        )
        ... now consult options.x or whatever
        ... argv is now the remaining arguments after the options

Usage summary:

Usage: base [common-options...] [options...]
  A drop in class for `django.core.management.base.BaseCommand`
  which subclasses `cs.cmdutils.BaseCommand`.
  Subcommands:
    help [common-options...] [-l] [subcommand-names...]
      Print help for subcommands.
      This outputs the full help for the named subcommands,
      or the short help for all subcommands if no names are specified.
      -l  Long help even if no subcommand-names provided.
    info [common-options...] [field-names...]
      Recite general information.
      Explicit field names may be provided to override the default listing.
    shell [common-options...]
      Run a command prompt via cmd.Cmd using this command's subcommands.

BaseCommand.SubCommandClass

BaseCommand.add_arguments(self, parser): Add the Options.COMMON_OPT_SPECS to the argparse parser. This is basicly to support the Django call_command function.

BaseCommand.handle(*, argv, **options): The Django BaseComand.handle method. This creates another instance for argv and runs it.

BaseCommand.run_from_argv(argv): Intercept django.core.management.base.BaseCommand.run_from_argv. Construct an instance of cs.djutils.DjangoBaseCommand and run it.

Class DjangoSpecificSubCommand(cs.cmdutils.SubCommand)

A subclass of cs.cmdutils.SubCOmmand with additional support for Django's BaseCommand.

DjangoSpecificSubCommand.__call__(self, argv: List[str]): Run this SubCommand with argv. This calls Django's BaseCommand.run_from_argv for pure Django commands.

DjangoSpecificSubCommand.is_pure_django_command: Whether this subcommand is a pure Django BaseCommand.

DjangoSpecificSubCommand.usage_text(self, *, cmd=None, **kw): Return the usage text for this subcommand.

Release Log

Release 20241119: New DjangoSpecificSubCommand(CSBaseCommand.SubCommandClass) to include support for pure Django BaseCommands.

Release 20241111: Rename DjangoBaseCommand to just BaseCommand so that we go from cs.djutils import BaseCommand. Less confusing.

Release 20241110: Initial PyPI release with DjangoBaseCommand, cs.cmdutils.BaseCommand subclass suppplanting django.core.management.base.BaseCommand.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs_djutils-20241119.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cs_djutils-20241119-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file cs_djutils-20241119.tar.gz.

File metadata

  • Download URL: cs_djutils-20241119.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.6

File hashes

Hashes for cs_djutils-20241119.tar.gz
Algorithm Hash digest
SHA256 a9ccdae7d57aa0d67b378a58ea4dd4a87afa21f9ed0b56b33f51953baf698f3b
MD5 4a94a6c36006d0290ec8681b3bd0840d
BLAKE2b-256 0da01356f7e523ec7edc7de4a2212a46f1bca46b110fd2bbfb85b981fd6627d3

See more details on using hashes here.

File details

Details for the file cs_djutils-20241119-py3-none-any.whl.

File metadata

  • Download URL: cs_djutils-20241119-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.6

File hashes

Hashes for cs_djutils-20241119-py3-none-any.whl
Algorithm Hash digest
SHA256 d20f93a62030b0658f26a7527a095512035a9add473d12f302b62dbc9a9d3e8e
MD5 3b886e819f5d44dadc95390ea2234889
BLAKE2b-256 8bdf165262b7b5d0cdcd1aa721a831c895f02bc380ddc91cfc523e6d8cf1712c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page