+# vim: tabstop=4 shiftwidth=4
+"""
+Walks through your blog root figuring out all the available monthly archives in
+your blogs. It generates html with this information and stores it in the
+$archivelinks variable which you can use in your head or foot templates.
+
+The vars available with typical example values are:
+ b 'Jun'
+ m '6'
+ Y '1978'
+ y '78'
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify,
+merge, publish, distribute, sublicense, and/or sell copies of the
+Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+Copyright 2004, 2005 Wari Wahab
+Copyright 2006 Benjamin Mako Hill
+"""
+__author__ = "Benjamin Mako Hill <mako@atdot.cc>"
+__version__ = "$Id: pyarchives.py 883 2006-03-24 03:43:42Z willhelm $"
+
+from Pyblosxom import tools
+import time, os
+
+class PyblArchives:
+ def __init__(self, request):
+ self._request = request
+ self._archives = None
+ self._years = []
+
+ def linearArchive(self):
+ if self._archives == None:
+ self.genLinearArchive()
+ return self._archives
+
+ def __str__(self):
+ return self.linearArchive()
+
+ def genLinearArchive(self):
+ config = self._request.getConfiguration()
+ data = self._request.getData()
+ root = config["datadir"]
+ archives = {}
+ archiveList = tools.Walk(self._request, root)
+ fulldict = {}
+ fulldict.update(config)
+ fulldict.update(data)
+
+ month_template = config.get('archive_template_month',
+ '<li><a href="%(base_url)s/%(Y)s/%(b)s">%(b)s/%(Y)s</a></li>')
+ year_template = config.get('archive_template_year',
+ '<li>%(Y)s<ul id="archive_year_contents_%(Y)s">%(month_contents)s</ul></li>')
+
+ for mem in archiveList:
+ timetuple = tools.filestat(self._request, mem)
+ timedict = {}
+ for x in ["B", "b", "m", "Y", "y"]:
+ timedict[x] = time.strftime("%" + x, timetuple)
+
+ fulldict.update(timedict)
+ if not archives.has_key(timedict['Y']):
+ archives[timedict['Y']] = {}
+ elif not archives[timedict['Y']].has_key(timedict['m']):
+ archives[timedict['Y']][timedict['m']] = (month_template % fulldict)
+
+ years = archives.keys()
+ years.sort()
+ years.reverse()
+ ordered_year_strings = []
+
+ # store years for possible use later
+ self._years = years
+
+ # we're not done with the dictionary, so please clear it
+ for x in ["B", "b", "m", "Y", "y"]:
+ del(fulldict[x])
+
+ for year in years:
+ month_templates = archives[year]
+ months = month_templates.keys()
+ months.sort()
+ months.reverse()
+ ordered_month_strings = []
+
+ for month in months:
+ ordered_month_strings.append(month_templates[month])
+
+ fulldict['month_contents'] = '\n'.join(ordered_month_strings)
+ fulldict['Y'] = year
+ ordered_year_strings.append(year_template % fulldict)
+
+ self._archives = '\n'.join(ordered_year_strings)
+
+ def linearYearlist(self):
+ config = self._request.getConfiguration()
+ template = config.get('archive_template_yearlist',
+ 'Element.hide(\'archive_year_contents_%s\');')
+ outgoing_lines = []
+ for year in self._years:
+ outgoing_lines.append(template % year)
+ return("\n".join(outgoing_lines))
+
+def cb_prepare(args):
+ request = args["request"]
+ data = request.getData()
+ archives = PyblArchives(request)
+ data["archivelinks"] = archives.linearArchive()
+ data["archiveyears"] = archives.linearYearlist()