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.
  • [get | view] (2006-02-16 14:59:57, 14.6 KB) [[attachment:Calendar-20060216.py]]
  • [get | view] (2007-05-28 09:42:03, 2.7 KB) [[attachment:ExcelPastedTable.py]]
  • [get | view] (2005-04-12 19:22:18, 6.0 KB) [[attachment:Gantt-1.3.3-2.py]]
  • [get | view] (2007-03-24 02:05:26, 3.9 KB) [[attachment:Literate_parser-0.7_Moin-1.3.tgz]]
  • [get | view] (2007-03-24 02:08:08, 4.8 KB) [[attachment:Literate_parser-0.7_Moin-1.3.zip]]
  • [get | view] (2005-03-11 13:50:49, 12.3 KB) [[attachment:MySQL.py]]
  • [get | view] (2005-09-11 08:09:44, 1.6 KB) [[attachment:SortText-1.3.5-1.py]]
  • [get | view] (2005-11-21 08:40:10, 2.8 KB) [[attachment:Sorter-1.3.py]]
  • [get | view] (2005-06-02 13:02:06, 1.2 KB) [[attachment:colorer.py]]
  • [get | view] (2006-01-04 16:10:31, 0.6 KB) [[attachment:gettext.py]]
  • [get | view] (2004-10-19 13:05:05, 0.7 KB) [[attachment:html-parser-1.2.py]]
  • [get | view] (2005-02-17 10:46:56, 0.6 KB) [[attachment:html.py]]
  • [get | view] (2005-12-06 21:09:48, 1.3 KB) [[attachment:matlab.py]]
  • [get | view] (2005-01-20 07:42:34, 0.4 KB) [[attachment:nocamelcase.py]]
  • [get | view] (2005-11-28 16:55:23, 2.3 KB) [[attachment:php-1.3.4-1]]
  • [get | view] (2005-12-18 22:36:37, 15.0 KB) [[attachment:sctable-1.3.5-4.py]]
  • [get | view] (2004-12-31 04:41:23, 1.6 KB) [[attachment:textil.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.