Source

tgext.command /

Filename Size Date modified Message
tgext
2.3 KB
79 B
910 B
TurboGears Extension Command
============================

This extension (tgext.command) is an attempt to make it
easier to create new TG command-line commands.

Current features:

* DBSession setup (or session-less if `with_session = False` on the
  command class)
* il8n setup (if `lang` is defined in the config-file)
* Beaker cache setup
* rollback-on-exception, commit on regular exit

TGExt.Command
-------------

TurboGears uses the Paste commands system to create command-line entry
points that, for example, set up your database or start your server.
When you get to larger projects, however, you will often have other
things you need to do "in the context of your application" from the
command line, such as periodic imports of data, or cron'd database
management tasks.

Paste's command system is well documented, but it can take quite a bit
of poking around to find out how to get SQLAlchemy, and TurboGears
configured so that code that looks in tg.config gets the right values,
and SQLAlchemy has access to your models.

.. code-block:: bash

    $ easy_install tgext.command

To use the extension, you will create a BaseCommand class like this
that will be shared by all of your command-line scripts.

.. code-block:: python

    from tgext.command import tgcommand
    class BaseCommand( tgcommand.TGCommand ):
        def import_model( self ):
            from example import model
            return model

This command-class would then serve as the base command for each of
your "real" commands.  For example, a command that iterates through
all users showing their user_name property:

.. code-block:: python

    class Hello(BaseCommand):
        def db_command( self, engine ):
            from example.model import User,Group, DBSession
            for user in DBSession.query( User ):
                print 'User',user.user_name

As with regular Paste commands you have to register your TGCommands
in your application's setup.py (in the setup() call), like so:

.. code-block:: ini

    entry_points="""
    [paste.paster_command]
    hellocommand = example.commands.hello:Hello
    """

You will need to re-run setup.py develop to get the command to be
available).  Note that a simple approach which does not support
PID-file exclusion, paster registration/command-line-parsing and
the like is documented in :ref:`cli_script`.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.