Scripting within Pyramid (i.e. no server)

I have good experience with PHP and Python frameworks for scripting, so now I'm taking a step towards Pyramid.

I would like to know what the “correct” way to run a script in Pyramid is. That is, how should I configure it so that it is part of the application and has access to config and, therefore, to the database, but does not start through pasteur (or any other WSGI).

As an example, let's say I have a web application that, when the user is offline, captures Facebook updates through the web service. I want to write a script to poll this service and store it in a database ready for next login.

How do I do this in terms of:

  • Adding variables to ini file
  • Running the script correctly

I understand the basics of Python modules and packages; however, I do not quite understand the Configurator / Paster / package configuration, in which I suspect the answer lies.

thank

Update:

Thanks, this looks like what I'm looking for. I note that you need to follow a specific structure (for example, have pivot and parser attributes) and that a 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?

Now I'm stuck on how to get the settings myself. For example, in init.py you can do this:

 engine = engine_from_config(settings, 'sqlalchemy.')

What do I need to do to convert the configuration file to settings?

EDIT: (simpler) way to do it in Pylons:   Run Pylons controller as a standalone application?

+3
source share
4

paster , ini , . "serve" wsgi . , commands.

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

setup.py.

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

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

, pshell .

+1

, . , -:

 #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)

 #--------------------------------------------------------------------------
+1

, , :

paster request development.ini /url_to_your_view
0

All Articles