reconstruction of code from what i've been able to dig up
[pyblosxom-hierarchical_archives] / expandingarchives.py
1 # vim: tabstop=4 shiftwidth=4
2 """
3 Walks through your blog root figuring out all the available monthly archives in
4 your blogs.  It generates html with this information and stores it in the
5 $archivelinks variable which you can use in your head or foot templates.
6
7 The vars available with typical example values are:
8     b      'Jun'
9     m      '6'
10     Y      '1978'
11     y      '78'
12
13 Permission is hereby granted, free of charge, to any person
14 obtaining a copy of this software and associated documentation
15 files (the "Software"), to deal in the Software without restriction,
16 including without limitation the rights to use, copy, modify,
17 merge, publish, distribute, sublicense, and/or sell copies of the
18 Software, and to permit persons to whom the Software is furnished
19 to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be
22 included in all copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
26 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 SOFTWARE.
32
33 Copyright 2004, 2005 Wari Wahab
34 Copyright 2006 Benjamin Mako Hill
35 """
36 __author__ = "Benjamin Mako Hill <mako@atdot.cc>"
37 __version__ = "$Id: pyarchives.py 883 2006-03-24 03:43:42Z willhelm $"
38
39 from Pyblosxom import tools
40 import time, os
41
42 class PyblArchives:
43     def __init__(self, request):
44         self._request = request
45         self._archives = None
46         self._years = []
47
48     def linearArchive(self):
49         if self._archives == None:
50             self.genLinearArchive()
51         return self._archives
52         
53     def __str__(self):
54         return self.linearArchive()
55
56     def genLinearArchive(self):
57         config = self._request.getConfiguration()
58         data = self._request.getData()
59         root = config["datadir"]
60         archives = {}
61         archiveList = tools.Walk(self._request, root)
62         fulldict = {}
63         fulldict.update(config)
64         fulldict.update(data)
65       
66         month_template = config.get('archive_template_month', 
67                          '<li><a href="%(base_url)s/%(Y)s/%(b)s">%(b)s/%(Y)s</a></li>')
68         year_template = config.get('archive_template_year',
69                         '<li>%(Y)s<ul id="archive_year_contents_%(Y)s">%(month_contents)s</ul></li>')
70
71         for mem in archiveList:
72             timetuple = tools.filestat(self._request, mem)
73             timedict = {}
74             for x in ["B", "b", "m", "Y", "y"]:
75                 timedict[x] = time.strftime("%" + x, timetuple)
76
77             fulldict.update(timedict)
78             if not archives.has_key(timedict['Y']):
79                 archives[timedict['Y']] = {}
80             elif not archives[timedict['Y']].has_key(timedict['m']):
81                 archives[timedict['Y']][timedict['m']] = (month_template % fulldict)
82
83         years = archives.keys()
84         years.sort()
85         years.reverse()
86         ordered_year_strings = []
87         
88         # store years for possible use later
89         self._years = years
90
91         # we're not done with the dictionary, so please clear it
92         for x in ["B", "b", "m", "Y", "y"]:
93             del(fulldict[x])
94         
95         for year in years:
96             month_templates = archives[year]
97             months = month_templates.keys()
98             months.sort()
99             months.reverse()
100             ordered_month_strings = []
101
102             for month in months:
103                 ordered_month_strings.append(month_templates[month])
104             
105             fulldict['month_contents'] = '\n'.join(ordered_month_strings)
106             fulldict['Y'] = year
107             ordered_year_strings.append(year_template % fulldict)
108             
109         self._archives = '\n'.join(ordered_year_strings)
110
111     def linearYearlist(self):
112         config = self._request.getConfiguration()
113         template = config.get('archive_template_yearlist',
114                              'Element.hide(\'archive_year_contents_%s\');')
115         outgoing_lines = []
116         for year in self._years:
117             outgoing_lines.append(template % year)
118         return("\n".join(outgoing_lines))
119
120 def cb_prepare(args):
121     request = args["request"]
122     data = request.getData()
123     archives = PyblArchives(request)
124     data["archivelinks"] = archives.linearArchive()
125     data["archiveyears"] = archives.linearYearlist()

Benjamin Mako Hill || Want to submit a patch?