]> oss.titaniummirror.com Git - webber.git/blobdiff - webber.py
rss_feed.py: generate "lastBuildDate" in GMT, not local time
[webber.git] / webber.py
index db3ea7e3af444fdf20d1e5baef8a7d6a677bdb68..c90cdb97b3805c35b03a1983ef6d51124b2e09c7 100644 (file)
--- a/webber.py
+++ b/webber.py
@@ -1,5 +1,5 @@
 # -*- coding: iso-8859-1 -*-
-import sys, os, optparse, fnmatch, stat, re, time, types
+import sys, os, optparse, fnmatch, stat, re, time, codecs
 from config import Holder
 
 
@@ -63,49 +63,69 @@ class File(Holder):
                Holder.__init__(self, **kw)
                files[kw["rel_path"]] = self
                self.render = None
+               self.contents = None
                mtime = os.stat(self.path)[stat.ST_MTIME]
                self.mtime = mtime
                self.ctime = mtime
                #print self.keys()
 
        reKeywords = re.compile(r'(\S+)\s*:\s*(.*)')
-       #reIsoDate = re.compile(r'(\d\d\d\d)-(\d\d)-(\d\d)')
-
-       def read_keywords(self, terminate_line=""):
-               """Opens the file and reads "key: value" pairs on the top of it. Returns
-               the open file handle for further processing by some plugins/read_*.py code."""
-               f = open(self.path)
-               while True:
-                       s = f.readline().strip()
-                       if s==terminate_line:
-                               break
-                       m = self.reKeywords.match(s)
-                       if not m:
-                               warning("%s: wrong 'key: value' line '%s'" % (self.rel_path, s))
-                               break
-                       key = m.group(1).lower()
-                       val = m.group(2)
-
-                       if key == "mtime":
-                               val = iso_to_time(val)
-
-                       if key == "ctime":
-                               val = iso_to_time(val)
-
-                       if key == "title":
-                               if not self.has_key("linktitle"):
-                                       self["linktitle"] = val
-
-                       #print self.rel_path, key, val
-                       self[key] = val
-               return f
+
+       def read(self, terminate_line=""):
+               f = codecs.open(self.path, "r", self.input_encoding)
+
+               # Read keywords
+               read_keywords = True
+               txt = []
+               for s in f.readlines():
+                       if read_keywords:
+                               s = s.strip()
+                               #print "kwd:", s
+                               if s == terminate_line:
+                                       read_keywords = False
+                                       continue
+
+                               m = self.reKeywords.match(s)
+                               if not m:
+                                       warning("%s: wrong 'key: value' line '%s'" % (self.rel_path, s))
+                                       break
+                               key = m.group(1).lower()
+                               val = m.group(2)
+
+                               if key == "mtime":
+                                       val = iso_to_time(val)
+
+                               if key == "ctime":
+                                       val = iso_to_time(val)
+
+                               if key == "title":
+                                       if not self.has_key("linktitle"):
+                                               self["linktitle"] = val
+
+                               #print self.rel_path, key, val
+                               self[key] = val
+
+                               continue
+                       #print "txt:", s.rstrip().encode("iso-8859-1")
+                       txt.append(s)
+
+               # Warn about a bogus time entries
+               if self.mtime < self.ctime:
+                       log('%s: modification time cannot be before creation time' % self.rel_path)
+                       self.ctime = self.mtime
+
+               # Warn about long titles / long linktitles
+               if len(self.linktitle) > 20:
+                       log('%s: define a shorter linktitle' % self.rel_path)
+
+               self.contents = "".join(txt)
 
 
 _get_file_for_cache = {}
 def get_file_for(name):
        """webber.files is an hash of File objects, but keyed on the real file name.
        This function returns a File object for a specific linktitle."""
-       
+
        try:
                return _get_file_for_cache[name]
        except:
@@ -242,7 +262,7 @@ def get_program_directory():
 #      5... Debug
 #
 def log(s, level=4):
-       if level>4:
+       if level > 4:
                indent = " " * (level-4)
        else:
                indent = ""
@@ -293,7 +313,7 @@ def info(s):
 # At startup:
 #      addoptions           allow plugins to add command-line options
 #      checkconfig          check configuration
-#      start                
+#      start
 # While reading files:
 #      read                 ask any reader (plugins!) to read the file
 #      filter               ask anybody to filter the contents
@@ -317,13 +337,11 @@ hooks = {}
 def load_plugins():
        """Loads all plugins in the plugins directory."""
        sys.path.append(os.path.join(get_program_directory(), "plugins"))
+       if cfg.has_key("plugin_dirs"):
+               for s in cfg.plugin_dirs:
+                       sys.path.append(s)
        for s in cfg.plugins:
-               #print "import:", s
-               #try:
                exec "import %s" % s
-               #except:
-               #       print "Could not import plugin '%s'" % s
-               #       sys.exit(1)
 
 
 def set_hook(name, last=False):
@@ -415,7 +433,7 @@ def iso_to_time(val):
                try:
                        t = time.strptime(val, "%Y-%m-%d")
                except ValueError:
-                       warning("%s: wrong ISO format in '%s'" % (self.rel_path, s))
+                       warning("wrong ISO format in '%s'" % val)
        return int(time.mktime(t))
 
 @set_function("format_date")
@@ -487,7 +505,7 @@ def walk_tree(dirpath):
                direc.inheritFrom(cfg)
 
                if not rel_path: rel_path = "."
-               log("reading directory %s" % rel_path, level=4)
+               log("reading directory %s" % rel_path, level=5)
 
                for s in os.listdir(dirpath):
                        full_path = os.path.join(dirpath, s)
@@ -510,6 +528,13 @@ def walk_tree(dirpath):
                                if ok:
                                        #print "FILE", s
                                        rel_path = relpath(cfg.in_dir, full_path)
+                                       # Allow paths to be specified in exclude_files:
+                                       for e in cfg.exclude_files:
+                                               if fnmatch.fnmatch(rel_path, e):
+                                                       log("ignoring file %s" % rel_path, level=7)
+                                                       ok = False
+                                                       break
+                               if ok:
                                        log("reading file %s" % rel_path, level=5)
                                        file = File(
                                                path = full_path,
@@ -518,7 +543,7 @@ def walk_tree(dirpath):
                                        )
                                        file.inheritFrom(direc)
                                        read_file(direc, file)
-                                               
+
        walk(dirpath)
 
 
@@ -567,7 +592,7 @@ def run_macros(file, contents):
                        kw["file"] = file
                        f = macros[name]
                        s = f(kw)
-                       if type(s) == types.UnicodeType:
+                       if isinstance(s, unicode):
                                s = s.encode("utf-8")
                        return s
                else:
@@ -575,21 +600,27 @@ def run_macros(file, contents):
        s = reMacro.sub(do_macro, contents)
        #print s
        return s
-       
+
 
 def scan_files():
        info("Scanning files ...")
 
        for s in files:
                file = files[s]
-               try:
-                       # Just check if the file has contents
-                       contents = file.contents
-               except:
+               if not file.has_key("contents"):
                        continue
+#              try:
+#                      # Just check if the file has contents
+#                      contents = file.contents
+#              except:
+#                      continue
 
                direc = directories[file.direc]
 
+               # Output-Filename "berechnen"
+               if file.render and file.render == "html":
+                       file.out_path = os.path.splitext(s)[0] + ".html"
+
                run_hooks("scan",
                        direc=direc,
                        file=file)
@@ -635,9 +666,6 @@ def render_files():
                        continue
                file.contents = contents
 
-               # Output-Filename "berechnen"
-               file.out_path = os.path.splitext(fname_in)[0] + ".html"
-
        for fname_in in files:
                file = files[fname_in]
                current_file = file
@@ -713,7 +741,7 @@ def addoptions(params):
 
        return parser
 
-       
+
 @set_hook("checkconfig", last=True)
 def checkconfig(params):
        # Ensure absolute paths that end in '/'.