X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=webber.py;h=f3e0f63fec5c57e0d50dc37870d4630869e4c02d;hb=c6d30c7f18e835e0b99032d01b1f8acd7f077ec6;hp=abb13d474004e1d8242c7232fa360a88917c4cc3;hpb=5b4e747f947e1a4c757fbcaed1f6d977374b208e;p=webber.git diff --git a/webber.py b/webber.py index abb13d4..f3e0f63 100644 --- 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 @@ -69,36 +69,55 @@ class File(Holder): #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: xxx"') + + self.contents = "".join(txt) _get_file_for_cache = {} @@ -188,11 +207,16 @@ def relpath(base_path, target): def get_link_from(source, dest): #print "get_link_from", source, dest - source = get_file_for(source) + #print source + if not isinstance(source, File): + source = get_file_for(source) if not source: + print "NO SOURCE" return "." - dest = get_file_for(dest) + if not isinstance(dest, File): + dest = get_file_for(dest) if not dest: + print "NO DEST" return "." rel_path = relpath(directories[source.direc].abs_path, directories[dest.direc].abs_path) try: @@ -417,10 +441,21 @@ def iso_to_time(val): def format_date(timestamp): return time.strftime(cfg.date_format, time.localtime(timestamp)) +@set_function("get_time") +def get_time(): + return format_date(time.time()) + @set_function("get_current_file") def get_current_file(): return current_file +@set_function("get_path_to_root") +def get_path_to_root(): + rel_path = relpath(directories[current_file.direc].abs_path, directories[''].abs_path) + rel_path = os.path.join(rel_path, os.path.split("")[1]) + if rel_path[-1] == "/": + rel_path = rel_path[:-1] + return rel_path @@ -478,7 +513,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) @@ -501,6 +536,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, @@ -558,7 +600,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: