Attachment 'Sorter-1.3.py'
Download 1 #-*- coding: utf-8 -*-
2 """
3 Sorter to sort one level of input
4 ===================================
5
6 This parser is adapted from SortText and used to sort text lines which are delimited by a special symbol. The
7 sort direction and the column to sort can be specified. For the sort column the type can be specified as "text"
8 or "date". The delimiters are omitted in the final output.
9
10 Defaults are
11 delimiter=''
12 column='1'
13 column_type='text'
14 sort_direction='up'
15
16 delimiter is the empty string so that a normal text search is done by default.
17
18 Install
19 -------
20 Put it in your wiki/data/plugin/parser/
21
22
23 Example
24 -------
25
26 {{{
27 #!Sorter column=2,column_type=date,sort_dir=up,date_format=%d.%m.%Y
28 * | B | 1.1.2005
29 * | A | 1.2.2006
30 * | D | 4.1.2005
31 * | C | 5.4.2000
32 }}}
33
34 Result::
35 * C 5.4.2000
36 * B 1.1.2005
37 * D 4.1.2005
38 * A 1.2.2006
39
40
41 Legal
42 -----
43 @copyright 2005 WilliRichert <w.richert@gmx.net>
44 @copyright 2005 ReimarBauer <R.Bauer@fz-juelich.de>
45
46
47 This program is free software; you can redistribute it and/or modify
48 it under the terms of the GNU General Public License as published by
49 the Free Software Foundation; either version 2 of the License, or
50 (at your option) any later version.
51
52 This program is distributed in the hope that it will be useful,
53 but WITHOUT ANY WARRANTY; without even the implied warranty of
54 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55 GNU General Public License for more details.
56
57 You should have received a copy of the GNU General Public License
58 along with this program; if not, write to the Free Software
59 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
60 """
61
62
63
64 Dependencies = []
65 import string, time
66 from MoinMoin.parser import wiki
67 from MoinMoin import wikiutil
68
69 class Parser:
70
71 def __init__(self, raw, request, **kw):
72 self.raw = raw
73 self.request = request
74 self.form = request.form
75 self._ = request.getText
76
77 self.kw = {
78 'delimiter': '',
79 'column': '1',
80 'column_type': 'text',
81 'date_format':'%Y-%m-%d',
82 'sort_dir': 'up'
83 }
84
85
86 for arg in kw.get('format_args','').split(','):
87 if arg.find('=') > -1:
88 key, value=arg.split('=')
89 self.kw[key]=wikiutil.escape(value, quote=1)
90
91
92 def format(self, formatter):
93
94 Dict = {}
95 raw = self.raw.split('\n')
96
97 delim = self.kw['delimiter']
98 data = []
99 for line in raw:
100 data.append(line.split(delim))
101
102 up = self.kw['sort_dir']=='up'
103 def _cmp(a, b):
104 if self.kw['column_type']=="date":
105 col, df = int(self.kw['column']), self.kw['date_format']
106 a = time.strptime(a[col].strip(), df)
107 b = time.strptime(b[col].strip(), df)
108
109 if not up:
110 a,b=b,a
111
112 return cmp(a, b)
113
114 data.sort(_cmp)
115
116 raw = []
117 for line in data:
118 raw.append("".join(line))
119
120 wikiizer = wiki.Parser(string.join(raw,"\n"),self.request)
121 wikiizer.format(formatter)
122
123
124
125
126
127
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.