Attachment 'ShowAmes.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Show Ames
   4 
   5     This macro is used to show correct data submission of ames files
   6     using a table showing some info of the file.
   7 
   8     comment: developed in 1.7, not ported to the macro parser yet, because
   9              compatibility to 1.5.8 / 1.6 is wanted.
  10 
  11     dependency: needs the nappy package from
  12                 http://home.badc.rl.ac.uk/astephens/software/nappy
  13 
  14     @copyright: 2007 MoinMoin:ReimarBauer
  15     @license: GNU GPL, see COPYING for details.
  16 """
  17 
  18 Dependencies = ['time'] # do not cache
  19 
  20 import nappy, os
  21 from MoinMoin import wikiutil
  22 from MoinMoin.action import AttachFile
  23 from MoinMoin.util.dataset import TupleDataset, Column
  24 from MoinMoin.widget.browser import DataBrowserWidget
  25 
  26 
  27 def execute(macro, args):
  28     request = macro.request
  29     formatter = macro.formatter
  30     _ = request.getText
  31 
  32     if args is None:
  33         extension = '.nas'
  34     else:
  35         extension = args
  36 
  37     COLUMNS = 1
  38 
  39     # initialize colums of output table
  40     data = TupleDataset()
  41     data.columns = []
  42     for dummy in range(COLUMNS):
  43         data.columns.extend([
  44             Column('file', label='file'),
  45             Column('date', label='date'),
  46             Column('short name', label='short name', align='center'),
  47             Column('unit', label='unit', align='center'),
  48             Column('first - last data val', label='first - last data val', align='center'),
  49             Column('', label='')
  50         ])
  51     # last column should be hidden    
  52     data.columns[-1].hidden = 1
  53     pagename = formatter.page.page_name
  54     files = AttachFile._get_files(request, pagename)
  55     attach_dir = AttachFile.getAttachDir(request, pagename)
  56     for file in files:
  57         result = []
  58         tmp_date = []
  59         tmp_short_name = []
  60         tmp_units = []
  61         tmp_first_last_data = []
  62         if file.lower().endswith(extension.lower()):
  63             # trying to open the ames file
  64             try:
  65                 myfile = nappy.openNAFile(os.path.join(attach_dir, file))
  66             except:
  67                 # Iam not sure if someone other as a python programmer
  68                 # helps reading the traceback to find the failure 
  69                 # in the file 
  70                 result.append("<p>%s<br>%s</p>" % (wikiutil.escape(file), "ames file header is not correct"))
  71                 for i in range(5): result.append('ERR')
  72                 data.addRow(tuple(result))
  73                 continue
  74 
  75             # trying to read the data of the ames file
  76             try:
  77                 myfile.readData()
  78             except:
  79                 result.append("<p>%s<br>%s</p>" % (wikiutil.escape(file), "is not of proper format"))
  80                 for i in range(5): result.append('ERR')
  81                 data.addRow(tuple(result))
  82                 continue
  83 
  84             url = AttachFile.getAttachUrl(pagename, file, request, escaped=1)
  85             link = "%s%s%s" % (formatter.url(1, url),
  86                                formatter.text(file),
  87                                formatter.url(0))
  88 
  89             result.append(link)
  90             # getting the dict of ames file
  91             myfile.getNADict()
  92             # read the date key
  93             y, m, d = myfile.naDict["DATE"]
  94             tmp_date.append("%d-%d-%d" % (y, m, d))
  95             # get the independent var
  96             independent_var = myfile.getIndependentVariables()
  97             tmp_short_name.append(independent_var[0][0])
  98             if independent_var[0][1]:
  99                 tmp_units.append(independent_var[0][1])
 100             else:
 101                 tmp_units.append('')
 102             # get start and end value of independent parameter    
 103             start = myfile.naDict["X"][0]
 104             endof = myfile.naDict["X"][-1]
 105             tmp_first_last_data.append("%d - %d" % (start, endof))
 106             # get the dependent var's    
 107             dependent_var = myfile.getVariables()
 108             n_dependent = len(dependent_var)
 109             i = 0
 110             while i < n_dependent:
 111                 tmp_short_name.append(dependent_var[i][0])
 112                 tmp_units.append(dependent_var[i][1])
 113                 # get start and end value of dependent parameters
 114                 start = myfile.naDict["V"][i][0]
 115                 endof = myfile.naDict["V"][i][-1]
 116                 tmp_first_last_data.append("%d - %d" % (start, endof))
 117                 i += 1
 118             # fill the table    
 119             result.append(formatter.linebreak(0).join(tmp_date))
 120             result.append(formatter.linebreak(0).join(tmp_short_name))
 121             result.append(formatter.linebreak(0).join(tmp_units))
 122             result.append(formatter.linebreak(0).join(tmp_first_last_data))
 123             data.addRow(tuple(result))
 124     if not data:
 125         return '<p>extension: %s: %s</p>' % (extension, _("No attachments stored for %(pagename)s") % {'pagename': wikiutil.escape(pagename)})
 126     browser = DataBrowserWidget(request)
 127     browser.setData(data)
 128     return browser.toHTML()

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] (2007-09-10 14:37:38, 4.8 KB) [[attachment:ShowAmes.py]]
  • [get | view] (2007-09-10 14:45:49, 9.3 KB) [[attachment:showames.png]]
 All files | Selected Files: delete move to page copy to page

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