Attachment 'SymLink.py'

Download

   1 from MoinMoin.action import AttachFile
   2 import os
   3 
   4 #  I saw another module defining a class so as to manage global variables.
   5 class Globs:
   6     
   7     global_string = ''
   8 
   9 #  I saw another module defining a class so as to manage default config
  10 #  parameters. I don't have any at the moment, but I might, so I'll just
  11 #  use a placeholder parameter.
  12 class Params:
  13 
  14     something_unused_required_by_last_line = 0 
  15 
  16 def execute(macro, args):
  17     #  This is what I'll return
  18     html = []
  19 
  20     #  Chop of up received args
  21     sargs = args.split(',')
  22     #  Symlink source (i.e. the file that already exists) is first arg.
  23     source = sargs[0]
  24     #  Get name of current page, so as to be able to work out path to attachments dir
  25     current_pagename = macro.formatter.page.page_name
  26     #  Get attachments dir
  27     attachment_path = AttachFile.getAttachDir(macro.request, current_pagename, create=1)
  28     #  Work out the absolute name of the symlink to create
  29     destination = attachment_path + '/' + os.path.basename(source)
  30 
  31     #  Complain if source is not absolute
  32     if source[0] != '/':
  33         message(source + ': is not absolute')
  34         
  35     #  Complain if source does not exist
  36     elif not os.path.exists(source):
  37         message(source + ': does not exist')
  38 
  39     #  If destination exists, is symlink and points to right thing, accept it as it is.
  40     elif os.path.lexists(destination) and os.path.islink(destination) and os.readlink(destination) == source:
  41         pass
  42 
  43     #  If destination exists, is symlink but does not point the right thing, delete and recreate it.
  44     elif os.path.lexists(destination) and os.path.islink(destination):
  45         message(source + ': symlink will be redirected')
  46         os.unlink(destination)
  47         os.symlink(source, destination)
  48 
  49     #  If destination exists but is not a symlink, complain.
  50     elif os.path.lexists(destination):
  51         message(destination + ': already exists but is not symlink')
  52 
  53     #  If destination does not exist then create it.
  54     else:
  55         os.symlink(source, destination)
  56 
  57     #  If any any error text was collected append it to what we'll pass back
  58     if Globs.global_string:
  59         html.append(u'<table><tr><td colspan="5" style="border-width: 0px;">')
  60         html.append(u'<font color="#aa0000">%s</font>' % Globs.global_string)
  61         html.append(u'</td></tr></table>')
  62 
  63     #  return it
  64     return macro.formatter.rawHTML(u'\n'.join(html))
  65 
  66 def message(string):
  67     Globs.global_string += u'PageComment: %s\n' % string

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] (2013-10-31 09:35:08, 2.4 KB) [[attachment:SymLink.py]]
 All files | Selected Files: delete move to page copy to page

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