Question

I have a fair bit of experience with PHP frameworks and Python for scripting so am now taking the step to Pyramid.

I'd like to know what is the 'correct' way to run a script in Pyramid. That is, how should I set it up so that it is part of the application and has access to config and thus database but does not run through paster (or whatever WSGI).

As an example, say I have a web application which while a user is offline grabs Facebook updates through a web service. I want to write a script to poll that service and store in the database ready for next login.

How should I do this in terms of:

  1. Adding variables in the ini file
  2. Starting the script correctly

I understand the basics of Python modules and packages; however I don't fully understand Configurator/Paster/package setup, wherein I suspect the answer lies.

Thanks

Update:

Thanks, this seems along the lines of what I am looking for. I note that you have to follow a certain structure (eg have summary and parser attributes set) and that the function called command() will always be run. My test code now looks something like this:

class AwesomeCommand(Command):

max_args = 2
min_args = 2

usage = "NAME"
# These are required
summary = "Say hello!"
group_name = "My Package Name"
# Required:
parser = Command.standard_parser(verbose=True)


def command(self):

    # Load the config file/section
    config_file, section_name = self.args
    # What next?

I'm now stuck as to how to get the settings themselves. For example, in init.py you can do this:

 engine = engine_from_config(settings, 'sqlalchemy.')

What do I need to do to transform the config file into the settings?

EDIT: The (simpler) way to do this in Pylons is here: Run Pylons controller as separate app?

Was it helpful?

Solution

OTHER TIPS

paster starts an application given an ini file that describes that application. the "serve" command is a built in command for starting a wsgi application and serving it. BUT, you can write other commands.

from paste.script.command import Command
class AwesomeCommand(Command):
    def command(self):
        print "the awesome thing it does"

and then register them as entry points in your setup.py.

setup(...
entry_points="""
  [paste.app_factory]
  .....

  [paste.global_paster_command]
  myawesome-command = mypackage.path.to.command:AwesomeCommand    """)

pyramid adds it's own commands this way like the pshell command.

After going to the pylons discuss list, I came up with an answer. Hope this helps somebody:

 #Bring in pyramid application--------------------------------------

 import pyramid
 from paste.deploy import appconfig
 config_file = '/path_to_config_file/configname.ini'

 name = 'app_name'
 config_name = 'config:%s' % config_file
 here_dir = os.getcwd()

 conf = appconfig(config_name, name, relative_to=here_dir)

 from main_package import main
 app = main(conf.global_conf, **conf.local_conf)

 #--------------------------------------------------------------------------

you need to make view for that action and then run it using:

paster request development.ini /url_to_your_view
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top