~ubuntu-branches/ubuntu/oneiric/muse-el/oneiric

« back to all changes in this revision

Viewing changes to contrib/pyblosxom/metadate.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael W. Olson (GNU address)
  • Date: 2008-01-09 15:51:46 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080109155146-0wwzermvvzs9rqzo
Tags: 3.11-3ubuntu1
* Merge with with Debian unstable (LP: #137284). Remaining Ubuntu changes:
  - Keep manual.
  - Set Ubuntu MOTU to be Maintainer

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Purpose:
 
3
 
 
4
  If an entry contains a #postdate metatag, metadate.py makes
 
5
  pyblosxom use that as the entry's date instead of the file's mtime.
 
6
 
 
7
To use:
 
8
 
 
9
  Place this file in your pyblosxom plugins directory.
 
10
 
 
11
  If you have previously assigned a value to py['load_plugins'] in
 
12
  your config.py file, add this module to that list.
 
13
 
 
14
  Tag an entry with a metadata line like so:
 
15
    #postdate YYYY-MM-DD HH-MM
 
16
 
 
17
Developer Info:
 
18
 
 
19
  There really isn't a clean way to implement this (as far as I can
 
20
  tell).  The cb_filestat callback isn't passed the entry, and none of
 
21
  the other callbacks are passed the mtime, and we really need them
 
22
  both to do this properly.
 
23
 
 
24
  The ugly--but functional--solution to this problem is to go into the
 
25
  file and dig the metadata out directly.  That is what I have done
 
26
  here.
 
27
 
 
28
  Since the entries are now being opened twice each, this could result
 
29
  in decently-sized (relatively speaking) performance hit.  Consider
 
30
  yourself warned.
 
31
 
 
32
License:
 
33
 
 
34
  Permission is hereby granted, free of charge, to any person
 
35
  obtaining a copy of this software and associated documentation files
 
36
  (the"Software"), to deal in the Software without restriction,
 
37
  including without limitation the rights to use, copy, modify, merge,
 
38
  publish, distribute, sublicense, and/or sell copies of the Software,
 
39
  and to permit persons to whom the Software is furnished to do so,
 
40
  subject to the following conditions:
 
41
 
 
42
  The above copyright notice and this permission notice shall be
 
43
  included in all copies or substantial portions of the Software.
 
44
 
 
45
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
46
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
47
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
48
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 
49
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 
50
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
51
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
52
  SOFTWARE.
 
53
 
 
54
Copyright 2005 Cameron Desautels
 
55
"""
 
56
 
 
57
import os, re, time
 
58
 
 
59
__author__ = 'Cameron Desautels <cam@apt2324.com>'
 
60
__version__ = 'metadate.py, v 0.1'
 
61
__description__ = 'Implements overriding of mtime with #postdate metadata.'
 
62
 
 
63
DATEFORMAT = re.compile('([0-9]{4})-([0-1][0-9])-([0-3][0-9]) ([0-2][0-9]):([0-5][0-9])')
 
64
 
 
65
def cb_filestat(args):
 
66
    stattuple = args['mtime']
 
67
 
 
68
    try:
 
69
        story = open(args['filename']).readlines()
 
70
    except IOError:
 
71
        raise IOError
 
72
 
 
73
    if story:
 
74
        story.pop(0) #throw away the title
 
75
 
 
76
        while story:
 
77
            metamatch = re.match(r'#(\w+)\s+(.*)', story[0])
 
78
            if metamatch:
 
79
                if metamatch.groups()[0] == 'postdate':
 
80
                    datematch = DATEFORMAT.match(metamatch.groups()[1].strip())
 
81
                    if datematch:
 
82
                        # year, month, day, hour, minute
 
83
                        mtime = time.mktime((int(datematch.groups()[0]),int(datematch.groups()[1]), \
 
84
                                             int(datematch.groups()[2]),int(datematch.groups()[3]), \
 
85
                                             int(datematch.groups()[4]),0,0,0,-1))
 
86
 
 
87
                        args['mtime'] = tuple(list(stattuple[:8]) + [mtime] + list(stattuple[9:]))
 
88
 
 
89
                    # whether the postdate line was correct or malformed, we found it, so quit
 
90
                    break
 
91
                else:
 
92
                    # that wasn't the right metadata line, so chuck it and keep going
 
93
                    story.pop(0)
 
94
            else:
 
95
                # quit when all metadata has been examined
 
96
                break
 
97
 
 
98
    return args