« 07.15 Pylons で使う paster command を作ってみる | ココ | 07.16 インデックスだけでは済まなかった »
2009年7月15日
Pylons で使う paster command を作ってみる(2)
というわけでー(ざこば師匠風に)。
ShellCommandクラスをざっと見たけどなんかさっぱりわけワカメ(死語)だったんで、なんとか必要なパーツをかき集めてみる。プロジェクト名が rssret として、ま、まずは軽くrssret/commands/__init__.py と RSSフィードの自動取得のためのスクリプトとして、autoupdates.pyを作る。
from paste.script.command import Command
from paste.deploy import appconfig
import os
from datetime import datetime, timedeltafrom rssret.config.environment import load_environment
from rssret.model.rssuri import RssUri
from rssret.model.feeditem import FeedItemclass AutoUpdatesCommand(Command):
# Parser configuration
summary = "--NO SUMMARY--"
usage = "--NO USAGE--"
group_name = "myapp"
parser = Command.standard_parser(verbose=False)def command(self):
heredir = os.path.dirname(__file__)
conffile = (len(self.args) and self.args[0]) or 'development.ini'while not os.path.exists(os.path.join(heredir, conffile)):
heredir = os.path.join(heredir, '../')conf = appconfig("config:%s" % os.path.join(heredir, conffile))
load_environment(conf.global_conf, conf.local_conf)from rssret.model.meta import Session as s
tm_ago = datetime.now() - timedelta(minutes=10)
for r in s.query(RssUri).filter(RssUri.last_accessed<tm_ago).all():
print "Checking %s ..." % r.title.encode('utf-8')
u = r.retrieveItems()
s.commit()if u.filter(FeedItem.created_date>tm_ago).count() > 0:
print " New feed item found"print "Update done at %s." % datetime.now()
ポイントは、
- load_environment()を使う
- load_environment()にはpylons.configは使わなくて、paste.appconfigを使う
- DB engineを保持しているmodel.meta.Sessionは、load_environment()の後にインポートする
とりあえず、これでcommand()の中でModelが不自由なく使える。
paster commandの作り方の中には、setup.pyに[paste.paster_command]\nrssupdates = rssret.commands.autoupdates:AutoUpdatesCommandを入れると書いてあったけど、手っ取り早く使えるようにするためには、egg-infoのentry_points.txtとpaster_plugins.txtの編集をするのでも十分(setup.pyに書いておけば、easy_installでインストールしたときに適用されるけど、インストールが必要)。
$ vi rssret.egg-info/entry_points.txt
[paste.app_factory]
main = rssret.config.middleware:make_app[paste.app_install]
main = pylons.util:PylonsInstaller[paste.paster_command]
rssupdates = rssret.commands.autoupdates:AutoUpdatesCommand
Railsの、script/runner よりかは起動のオーバーヘッドがマシな感じがする。
By ただ at 23:26 カテゴリー ; PinMarch Samples , プログラミングとか
« 07.15 Pylons で使う paster command を作ってみる |
07月の記事
| 07.16 インデックスだけでは済まなかった »
トラックバック
このエントリーのトラックバックURL:
http://pinmarch.sakura.ne.jp/mt/mt-tb.cgi/1442