""" filter.py is a pyblosxom plugin filter.py filters visible entries based on a simple regex search on the path. This allows for custom entry views at each level of your blog. For example: news only, alerts only, user entries only filter.py looks for a file in the current working data root. The file name should be specified in py['filter_file']. The file will contain a simple directive and regular expression to match. Config file format -- : directive := allow|deny regex := std. python regex allow means that only matches are allowed. deny means that only non-matches are allowed. Only the first line is read from the configuration file. This is based heavily on the default filelist handler in pyblosxom. version = "0.03/10JUN2008" TODO: * Add multiple lines/rules if it seems useful * Add a reasonable boiler plate to cb_filelist Example Usage: 1. Place filter.py in your plugins directory 2. Add it to your config.py 3. Let's say you have three directories under your data directory: news, users, development, users/foo, users/bar, users/stupid Ex 1: You only want news entries to show up when you browse to your blog root: echo 'allow:[datadir-root]/news/' > [datadir-root]/regex.filter Now only entries from your news will show up. Ex 2: You want everything to show up in user/ except entries from user stupid: echo 'deny:[datadir-root]/users/stupid' > [datadir-root]/users/regex.filter So now when you browse to '/' you see only news entries. When you browse to '/users' you see entries from everyone but 'stupid'. Remember, you can use full regular expressions and not just paths. Also take note that I use 'search' instead of 'match'. This means .*s aren't required on the edges, but may mean that you end up matching more than you meant to. If this needs any clarification, feel free to email me. Contributors: * willg - http://www.bluesock.org/~willg/ - Bringing this up-to-date since I haven't used pybloxsom in a while Thanks!! * Bill Powell - http://billpowellisalive.com/ - For adding support for coexistence with tags.py """ __author__ = "will drewry " import time, os, re from Pyblosxom import tools from Pyblosxom.pyblosxom import FileEntry def cb_filelist(args): dont_filter = False request = args["request"] data = request.getData() config = request.getConfiguration() # Co-exist with tags.py plugin if config.has_key('tag_url'): filter_tags = re.compile(config['tag_url']) if filter_tags.match(data['url']): return # Load regex filter from data['root_datadir'] filter_expr = '' filter_cmd = '' regex_filename = config.get('filter_file', 'regex.filter') try: filter_file = open(data['root_datadir'] + '/' + regex_filename, 'r') filter_cmd = filter_file.readline() filter_cmd = filter_cmd.strip() filter_file.close() try: (filter_action, filter_expr) = filter_cmd.split(':') except ValueError: dont_filter = True except IOError: dont_filter = True # Compile regex from file filter = re.compile(filter_expr) if data['bl_type'] == 'dir': filelist = tools.Walk(request, data['root_datadir'], int(config['depth'])) elif data['bl_type'] == 'file': filelist = [data['root_datadir']] else: filelist = [] entrylist = [] for ourfile in filelist: entry = FileEntry(request, ourfile, data['root_datadir']) # Test file again configured regex and add if it is allowed if dont_filter == True: entrylist.append((entry._mtime, entry)) elif filter_action == 'deny' and filter.search(ourfile) == None: entrylist.append((entry._mtime, entry)) elif filter_action == 'allow' and filter.search(ourfile) != None: entrylist.append((entry._mtime, entry)) # this sorts entries by mtime in reverse order. entries that have # no mtime get sorted to the top. entrylist.sort() entrylist.reverse() entrylist = [x[1] for x in entrylist] # Match dates with files if applicable if data['pi_yr']: month = (data['pi_mo'] in tools.month2num.keys() and tools.month2num[data['pi_mo']] or data['pi_mo']) matchstr = "^" + data["pi_yr"] + month + data["pi_da"] valid_list = [x for x in entrylist if re.match(matchstr, x['fulltime'])] else: valid_list = entrylist return valid_list