Attachment 'EmbedObject.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - EmbedObject Macro
   4 
   5     PURPOSE:
   6         This macro is used to embed an object into a wiki page. Optionally, the
   7         size of the object can get adjusted. Further keywords are dependent on
   8         the kind of application.
   9 
  10     CALLING SEQUENCE:
  11         [[EmbedObject(attachment[,width=width][,height=height])]]
  12 
  13     SUPPORTED MIMETYPES:  
  14          application/x-shockwave-flash
  15          application/x-dvi
  16          application/postscript
  17          application/pdf
  18          application/ogg
  19          application/vnd.visio
  20          
  21          image/x-ms-bmp
  22          image/svg+xml
  23          image/tiff
  24          image/x-photoshop
  25 
  26          audio/mpeg
  27          audio/midi
  28          audio/x-wav
  29                          
  30          video/fli
  31          video/mpeg
  32          video/quicktime
  33          video/x-msvideo
  34                          
  35          chemical/x-pdb
  36 
  37          x-world/x-vrml  
  38            
  39     INPUTS:
  40         attachment: name of attachment
  41 
  42     KEYWORD PARAMETERS:
  43         
  44         Dependent on the mimetype class a different set of keywords is used from the defaults
  45 
  46            width = ""
  47            height = ""
  48            type = mime_type
  49            play = false
  50            loop = false
  51            quality = high
  52            op = true
  53            repeat = false
  54            autostart = false
  55            menu = true
  56       
  57 
  58         All do use width, height, mime_type   
  59         
  60         in addition:
  61            'video' do use  repeat, autostart, menu, op
  62            'audio' do use   play, repeat, autostart, op, hidden
  63                    the default width is 60 and default height is 20
  64            'application' do use play, menu, autostart
  65         
  66     
  67     EXAMPLE:
  68         [[EmbedObject]]
  69         [[EmbedObject(example.swf)]]
  70         [[EmbedObject(example.pdf]]
  71         [[EmbedObject(example.svg]]
  72         [[EmbedObject(example.mp3]]
  73         [[EmbedObject(example.vss]]
  74          
  75         [[EmbedObject(example.swf,width=637,height=392)]]
  76         [[EmbedObject(SlideShow/example.swf,width=637,height=392)]]
  77         [[EmbedObject(SlideShow/example.swf,width=637,height=392)]]
  78         [[EmbedObject(SlideShow/example.swf,width=637,height=392,play=true,loop=false)]]
  79         [[EmbedObject(SlideShow/example.swf,width=637,height=392,quality=low)]]
  80 
  81  
  82     PROCEDURE:
  83         If the attachment file isn't uploaded yet the attachment line will be shown.
  84         If you give only one size argument, e.g. width only, the other one will be calculated.
  85 
  86         By the swftools it is possible to get the swf size returned. I don't know if it is 
  87         possible to get sizes for svg, pdf and others detected too, that's the reason why 
  88         I haven't added it by now.
  89 
  90         Please add needed mimetypes as objects.
  91     
  92     RESTRICTIONS:
  93         some mimetypes do ignore all used keywords. May be they do use different names.        
  94 
  95 
  96     MODIFICATION HISTORY:
  97         @copyright: 2006 by Reimar Bauer (R.Bauer@fz-juelich.de)      
  98         @license: GNU GPL, see COPYING for details.
  99         initial version: 1.5.0-1
 100         svg was added by AndrewArmstrong
 101         2006-05-04 TomSi: added mp3 support
 102         2006-05-09 RB code refactored, fixed a taintfilename bug
 103         2006-06-29 visio from OwenJones added but not tested,
 104                    RB code reviewed, taintfile removed
 105         2006-10-01 RB code refactored
 106         2006-10-05 RB bug fixed closing " at height added
 107         2006-10-08 RB type is needed on some platforms, some more keywords added
 108 """
 109 import os, mimetypes
 110 
 111 from MoinMoin import wikiutil
 112 from MoinMoin.action import AttachFile
 113 
 114 class EmbedObject:
 115 
 116     def __init__(self, macro, args):
 117         self.macro = macro
 118         self.request = macro.request
 119         self.formatter = macro.formatter
 120         self.args = args
 121 
 122         self.width = ""
 123         self.height = ""
 124         self.play = "false"
 125         self.loop = "false"
 126         self.quality = "high"
 127         self.op = "true"
 128         self.repeat = "false"
 129         self.autostart = "false"
 130         self.align = "center"
 131         self.hidden = "false"
 132         self.menu = "true"
 133 
 134         if args:
 135             args = args.split(',')
 136             args = [arg.strip() for arg in args]
 137         else:
 138             args = []
 139 
 140         kw_count = 0
 141         argc = len(args)
 142         for arg in self.args.split(','):
 143             if arg.find('=') > -1:
 144                 kw_count += 1
 145                 key, value = arg.split('=')
 146                 setattr(self, key, wikiutil.escape(value.strip(), quote=1))
 147 
 148         argc -= kw_count
 149 
 150         if not argc:
 151            msg = 'Not enough arguments to EmbedObject macro! Try [[EmbedObject(attachment [,width=width] [,height=heigt])]]'
 152            return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
 153         else:
 154             self.target = args[0]
 155 
 156     def embed(self, mime_type, file):
 157         mtype = mime_type.split('/')
 158 
 159         if mtype[0] == 'video':
 160             return '''
 161 <OBJECT>
 162 <EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" REPEAT="%(repeat)s" AUTOSTART="%(autostart)s" OP="%(op)s" MENU="%(menu)s" TYPE="%(type)s"></EMBED>
 163 </OBJECT>''' % {
 164     "width": self.width,
 165     "height": self.height,
 166     "file": file,
 167     "repeat": self.repeat,
 168     "autostart": self.autostart,
 169     "op": self.op,
 170     "type": mime_type,
 171     "menu": self.menu,
 172 }
 173 
 174         if mtype[0] in ['image', 'chemical', 'x-world']:
 175             return '''
 176 <OBJECT>
 177 <EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" TYPE="%(type)s"></EMBED>
 178 </OBJECT>''' % {
 179     "width": self.width,
 180     "height": self.height,
 181     "file": file,
 182     "type": mime_type,
 183 }
 184 
 185         if mtype[0] == 'audio':
 186             if self.width == "":
 187                 self.width = "60"
 188             if self.height == "":
 189                 self.height = "20"
 190             return '''
 191 <OBJECT>
 192 <EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" REPEAT="%(repeat)s" AUTOSTART="%(autostart)s" OP="%(op)s" PLAY="%(play)s" HIDDEN="%(hidden)s" TYPE="%(type)s"></EMBED>
 193 </OBJECT>''' % {
 194    "width": self.width,
 195    "height": self.height,
 196    "file": file,
 197    "play": self.play,
 198    "repeat": self.repeat,
 199    "autostart": self.autostart,
 200    "op": self.op,
 201    "hidden": self.hidden,
 202    "type": mime_type,
 203 }
 204 
 205         if mtype[0] == 'application':
 206             return '''
 207 <OBJECT>
 208 <EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s" AUTOSTART="%(autostart)s" PLAY="%(play)s" LOOP="%(loop)s" MENU="%(menu)s" TYPE="%(type)s"> </EMBED>
 209 </OBJECT>''' % {
 210     "width": self.width,
 211     "height": self.height,
 212     "file": file,
 213     "autostart": self.autostart,
 214     "play": self.play,
 215     "loop": self.loop,
 216     "type": mime_type,
 217     "menu": self.menu,
 218 }
 219 
 220     def render(self):
 221         _ = self.request.getText
 222 
 223         pagename, attname = AttachFile.absoluteName(self.target, self.formatter.page.page_name)
 224         attachment_fname = AttachFile.getFilename(self.request, pagename, attname)
 225 
 226         if not os.path.exists(attachment_fname):
 227             linktext = _('Upload new attachment "%(filename)s"')
 228             return wikiutil.link_tag(self.request,
 229                 ('%s?action=AttachFile&rename=%s' % (
 230                 wikiutil.quoteWikinameURL(pagename),
 231                 wikiutil.url_quote_plus(attname))),
 232                 linktext % {'filename': attname})
 233 
 234         url = AttachFile.getAttachUrl(pagename, attname, self.request)
 235         mime_type, enc = mimetypes.guess_type(attname)
 236 
 237         if mime_type in ["application/x-shockwave-flash",
 238                          "application/x-dvi",
 239                          "application/postscript",
 240                          "application/pdf",
 241                          "application/ogg",
 242                          "application/vnd.visio",
 243 
 244                          "image/x-ms-bmp",
 245                          "image/svg+xml",
 246                          "image/tiff",
 247                          "image/x-photoshop",
 248 
 249                          "audio/mpeg",
 250                          "audio/midi",
 251                          "audio/x-wav",
 252 
 253                          "video/fli",
 254                          "video/mpeg",
 255                          "video/quicktime",
 256                          "video/x-msvideo",
 257 
 258                          "chemical/x-pdb",
 259 
 260                          "x-world/x-vrml",
 261                        ]:
 262 
 263             return self.embed(mime_type, url)
 264 
 265         else:
 266             msg = 'Not supported mimetype %(mimetype)s ' % {"mimetype": mime_type}
 267             return "%s%s%s" % (self.macro.formatter.sysmsg(1),
 268                        self.macro.formatter.text(msg),
 269                        self.macro.formatter.sysmsg(0))
 270 
 271 
 272 def execute(macro, args):
 273     return EmbedObject(macro, args).render()

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-10-08 08:22:31, 8.5 KB) [[attachment:EmbedObject.py]]
  • [get | view] (2006-10-08 08:22:14, 14.0 KB) [[attachment:EmbedObject_20061008_patch.txt]]
  • [get | view] (2006-10-01 18:09:12, 13.3 KB) [[attachment:EmbedObject_patch_20061001.txt]]
  • [get | view] (2006-10-05 07:50:34, 13.3 KB) [[attachment:EmbedObject_patch_20061005.txt]]
 All files | Selected Files: delete move to page copy to page

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