View All Posts. MiCHiLU.com powered by Django ;-)

[Python]: pyhabu global.cfgをオプションで渡すpatch

開発用とプロダクション用とで、globalセクションを簡単に分けられるので便利。 includeに渡されたYAMLでupdateします。

#プロダクション用
$ runhabu.py config/spam.cfg
...
#debug用
$ runhabu.py config/spam.cfg --include=config/debug.cfg
...

config/spam.cfg

global:
  timezone: Asia/Tokyo
  log: log/pyhabu_log
  loglevel: CRITICAL

pipeline:
  rss_fetcher:
    - module: subscription.config
      config:
        feed:
          - http://d.hatena.ne.jp/johzan/rss2
        file:
    - module: filter.grep
      config:
        str:
          title: 巡回
    - module: filter.select_anchor
    - module: publisher.rssfeeder
      config:
        title: 常山日記 - 巡回
        link: http://d.hatena.ne.jp/johzan/searchdiary?word=%BD%E4%B2%F3
        file: published/feeds/johzan_junkai.xml

config/debug.cfg

global:
  timezone: Asia/Tokyo
  log: stdout
  loglevel: DEBUG

include_option.diff

Index: habu/__init__.py
===================================================================
--- habu/__init__.py        (revision 87)
+++ habu/__init__.py        (working copy)
@@ -166,8 +166,10 @@
         self.scheduler = None
         self.servers = []

-    def load(self, yamlStream):
+    def load(self, yamlStream, includeStream=None):
         config = yaml.load(yamlStream)
+        if includeStream:
+            config.update(yaml.load(includeStream))
         self.environ = config.setdefault("global", None)
         self.lineConfigs = config.setdefault("pipeline", [])
         self._loadAfter(config)
Index: runhabu.py
===================================================================
--- runhabu.py      (revision 87)
+++ runhabu.py      (working copy)
@@ -1,3 +1,5 @@
+#! /usr/bin/env python
+
 from twisted.internet import reactor, defer
 import habu
 import habu.log as log
@@ -42,7 +44,7 @@
                                          "download-module=", "download-url=",
                                          "log-level=", "log-file=",
                                          "proxy-host=", "proxy-port=",
-                                         "pid-file="])
+                                         "pid-file=", "include="])
     except:
         printUsage(args[0])

@@ -88,12 +90,19 @@
             bag["url"] = arg
         elif opt == "--pid-file":
             bag["pid-file"] = arg
+        elif opt == "--include":
+            bag["include"] = arg
+            if not os.path.exists(bag["include"]):
+                printUsage(args[0])

     return cfgFile, bag

 def prepare(cfgFile, bag):
     mgr = habu.Habu()
-    mgr.load(open(cfgFile).read())
+    cfg = (open(cfgFile).read(),)
+    if bag.get("include"):
+        cfg = cfg + (open(bag.get("include")).read(),)
+    mgr.load(*cfg)
     mgr.addPluginPath(bag.get("plugin-path", None))
     mgr.setProxy(bag.get("proxy-host", None), bag.get("proxy-port", 0))
     if not mgr.getPipelines():
Thu, 25 Oct 2007 16:10:58 +0900 source edit
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.1 Japan License.
View All Posts. MiCHiLU.com powered by Django ;-)