annotate MoinMoin/macro/Hits.py @ 2455:fe71235f1f7c

ParameterParser: changed to return fixed_count, arg_dict, putting positional args into arg_dict[pos], more tests
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 21 Jul 2007 23:00:25 +0200
parents adf5ec4874ed
children 9065adc695ca
rev   line source
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
2 """
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
3 MoinMoin - Hits Macro
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
4
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
5 This macro is used to show the cummulative hits of the wikipage where the Macro is called from.
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
6 Optional you could count how much this or all pages was changed or viewed.
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
7
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
8 [[Hits([all=(0,1)],[filter=(VIEWPAGE,SAVEPAGE)]]
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
9
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
10 all: if set to 1 then cummulative hits over all wiki pages is returned. Default is 0
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
11 filter: if set to SAVEPAGE then the saved pages are counted. Default is VIEWPAGE.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2054
diff changeset
12
2453
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
13 @copyright: 2004-2007 MoinMoin:ReimarBauer,
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
14 2005 BenjaminVrolijk
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
15 @license: GNU GPL, see COPYING for details.
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
16 """
2453
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
17
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
18 Dependencies = ['time'] # do not cache
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
19
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
20 from MoinMoin import wikiutil
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
21 from MoinMoin.logfile import eventlog
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
22
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
23 class Hits:
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
24 def __init__(self, macro, args):
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
25 self.request = macro.request
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
26 self.formatter = macro.formatter
2453
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
27 self.this_page = macro.formatter.page.page_name
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
28 argParser = wikiutil.ParameterParser("%(all)s%(filter)s")
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
29 try:
2455
fe71235f1f7c ParameterParser: changed to return fixed_count, arg_dict, putting positional args into arg_dict[pos], more tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2453
diff changeset
30 self.fixed_count, self.arg_dict = argParser.parse_parameters(args)
2453
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
31 self.error = None
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
32 except ValueError, err:
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
33 self.error = str(err)
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
34
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
35 def renderInText(self):
2453
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
36 if self.error:
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
37 text = "Hits macro: %s" % self.error
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
38 else:
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
39 text = "%d" % self.getHits()
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
40 return self.formatter.text(text)
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
41
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
42 def getHits(self):
2453
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
43 event_filter = self.arg_dict["filter"]
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
44 if not event_filter:
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
45 event_filter = "VIEWPAGE"
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
46 count_all_pages = self.arg_dict["all"]
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
47
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
48 event_log = eventlog.EventLog(self.request)
2453
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
49 event_log.set_filter([event_filter])
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
50 count = 0
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
51 for event in event_log.reverse():
2453
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
52 pagename = event[2].get('pagename')
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
53 if count_all_pages or pagename == self.this_page:
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
54 count += 1
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
55
2453
adf5ec4874ed fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
56 return count
2054
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
57
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
58 def execute(macro, args):
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
59 """ Temporary glue code to use with moin current macro system """
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
60 return Hits(macro, args).renderInText()
9bea36ddd065 macro/Hits: shows hits of a page (refactored version from MacroMarket)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
61