¶ zqarchives.py

2011-08-31 23:22

# -*- coding: utf-8 -*-

# vim: tabstop=4 shiftwidth=4
- zoomq 100419 fixed for export month/year with count
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.

You can format the output with the key "archive_template".

A config.py example:

    py['archive_template'] = '
  • %(m)s/%(y)s
  • ' Displays the archives as list items, with a month number slash year number, like 06/78. 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 """ __author__ = "Wari Wahab - wari at wari dot per dot sg" __version__ = "$Id: zqarchives.py,v 2872b22e2ace 2011/10/27 07:08:25 zoomquiet+hg $" __url__ = "http://blog.zoomquiet.org/pyblosxom/techic/PyBlosxom/plugins/zqarchives.html" from Pyblosxom import tools import time, os def verify_installation(request): config = request.get_configuration() if not config.has_key("archive_template"): print "missing optional config property 'archive_template' which " print "allows you to specify how the archive links are created. " print "refer to pyarchive plugin documentation for more details." return 1 class PyblArchives: def __init__(self, request): self._request = request self._archives = None def __str__(self): if self._archives == None: self.gen_linear_archive() return self._archives def gen_linear_archive(self): config = self._request.get_configuration() data = self._request.get_data() root = config["datadir"] archives = {} archive_list = tools.walk(self._request, root) fulldict = {} fulldict.update(config) fulldict.update(data) template = config.get('archive_template', '%(y)s.%(m)s') #%(Y)s-%(b)s #print fulldict["base_url"] for mem in archive_list: 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'] + timedict['m']): archives[timedict['Y'] + timedict['m']] = [template % fulldict,1] else: archives[timedict['Y'] + timedict['m']][1] += 1 archives[timedict['Y'] + timedict['m']][0] = template % fulldict #print archives #return arc_keys = archives.keys() arc_keys.sort() arc_keys.reverse() yearmonth = {} result = [] #base archives walk and count every year's mounth for key in arc_keys: yearname = key[:-2] if yearname in yearmonth.keys(): yearmonth[yearname][0] += archives[key][1] yearmonth[yearname][1].append(archives[key]) else: yearmonth[yearname] = [archives[key][1],[]] yearmonth[yearname][1].append(archives[key]) #print yearmonth["2007"] mon_keys = yearmonth.keys() mon_keys.sort() mon_keys.reverse() #print mon_keys for year in mon_keys: #print "%s%s"%(year,yearmonth[year][0]) monode = yearmonth[year][1] result.append("
  • %s(%d)
  • "%(fulldict["base_url"],year,year,yearmonth[year][0])) if 1==len(monode): #print "%s%s"%(monode[0][0],monode[0][1]) result.append("
  • %s(%d)
  • "%(monode[0][0],monode[0][1])) else: for m in monode: #print m #print "%s%s"%(m[0],m[1]) result.append("
  • %s(%d)
  • "%(m[0],m[1])) #result.append("%s%s"%(month[0],month[1])) #print result self._archives = '\n'.join(result) def cb_prepare(args): request = args["request"] data = request.get_data() data["archivelinks"] = PyblArchives(request)

  • § 写于: Wed, 31 Aug 2011 | 永久链接;源文: rdf ,rss ,raw | 分类: /techic/PyBlosxom/plugins §
    [MailMe] [Print] Creative Commons License

    作品Zoom.Quiet创作,采用知识共享署名-相同方式共享 2.5 中国大陆许可协议进行许可。 基于zoomquiet.org上的作品创作。