Attachment 'AgPics.py'

Download

   1 #!/usr/bin/env python
   2 # -*- coding: utf-8 -*-
   3 """
   4  MoinMoin AgPics Parser
   5  
   6  @author: Yun, Jonghyouk <ageldama@gmail.com> 2007-03-22
   7 """
   8 
   9 Dependencies = []
  10 import os, sys, string, re
  11 import glob, os.path, stat
  12 import Image
  13 from datetime import datetime
  14 import time
  15 import Image
  16 from MoinMoin.action import AttachFile
  17 from MoinMoin import wikiutil, config
  18 from MoinMoin.Page import Page
  19 from MoinMoin.parser import wiki
  20 
  21 ### utf8 only?
  22 import sys; reload(sys)
  23 sys.setdefaultencoding('utf8')
  24 
  25 ### Constants
  26 ## gallery2에서 제일 작은 미리보기.
  27 thumbnail_prefix  = "thumbnail_"
  28 thumbnail_regex   = re.compile("^thumbnail_.*")
  29 thumbnail_size    = [256, 256]
  30 ## gallery2에서 중간크기 미리보기.
  31 webnail_prefix    = "webnail_"
  32 webnail_regex     = re.compile("^webnail_.*")
  33 ## 이미지 파일의 정규식.
  34 image_regex      = re.compile(".*\.(jpg|jpeg|gif|png)$", re.I)
  35 
  36 ### helpers
  37 def list_all_files(path):
  38   """특정 디렉토리의 모든 파일명을 되돌린다."""
  39   return glob.glob(path+"/*")
  40 
  41 def filter_filename_matches(regex, files, is_except=False):
  42   """정규식에 각 파일이름들이 매치하는지(혹은 아닌지)에 따라 그를 걸러낸다"""
  43   if is_except:
  44     inv = lambda b: not b
  45   else:
  46     inv = lambda b: b
  47   return filter(lambda f: inv(regex.match(os.path.basename(f))), files)
  48 
  49 def filter_image_files(files):
  50   return filter_filename_matches(image_regex, files)
  51 
  52 def filter_except_thumbnails(files):
  53   return filter_filename_matches(thumbnail_regex, files, True)
  54 
  55 def filter_except_webnails(files):
  56   return filter_filename_matches(webnail_regex, files, True)
  57 
  58 def get_attach_url(page, request, file):
  59   #return page.url(request)+"?action=AttachFile&do=get&target="+wikiutil.url_quote(file)
  60   return AttachFile.getAttachUrl(page.page_name, file, request)
  61 
  62 def filter_only_images(files):
  63   return filter_except_webnails(filter_except_thumbnails(filter_image_files(files)))
  64 
  65 def desc_by_mtime(files):
  66   ## mtime 기준으로 **역순** 정렬하기!
  67   l = [ (os.stat(f)[stat.ST_MTIME], f) for f in files ]
  68   l.sort(lambda l, r: cmp(r[0],l[0]))
  69   return [ f[1] for f in l ]
  70 
  71 def attach_path(page, request):
  72   return AttachFile.getAttachDir(request, page.page_name, create=1)
  73 
  74 def thumbnail_filename(file):
  75   return thumbnail_prefix+os.path.basename(file)
  76 
  77 ### AgPics Parser Class
  78 class Parser:
  79   def __init__(self, raw, request, **kw):
  80     self.raw = raw
  81     self.request = request
  82     self.form = request.form
  83     self._ = request.getText
  84     self.kw = {}
  85     for arg in kw.get('format_args','').split(','):
  86       if arg.find('=') > -1:
  87         key, value=arg.split('=')
  88         self.kw[key]=wikiutil.escape(value, quote=1)
  89         
  90   def format(self, formatter):
  91     page    = formatter.page
  92     request = self.request
  93     cur_pagename  =page.page_name
  94     cur_pageurl   =page.url(request)
  95     ## 각 파일들에 대해서...
  96     attchpath = attach_path(page, request)
  97     all_files = list_all_files(attchpath)
  98     img_files = filter_only_images(all_files)
  99     request.write("<ul>")
 100     for f in desc_by_mtime(img_files):
 101       ## 파일이름과 url
 102       abs_filename  = f
 103       filename      = os.path.basename(f)
 104       thumbfn       = thumbnail_filename(filename)
 105       abs_thumbfn   = os.path.dirname(f)+'/'+ thumbfn
 106       file_url      = get_attach_url(page, request, filename)
 107       ## stat!
 108       st = os.stat(f)
 109       ctime = datetime.fromtimestamp(st[stat.ST_CTIME])
 110       mtime = datetime.fromtimestamp(st[stat.ST_MTIME])
 111       ## 섬네일 없거나 원본보다 오래되었으면 만들기...
 112       mtime_orig = st[stat.ST_MTIME]#os.stat(abs_filename)[stat.ST_MTIME]
 113       try:
 114         mtime_thumb = os.stat(abs_thumbfn)[stat.ST_MTIME]
 115       except:
 116         mtime_thumb = 0
 117       img = Image.open(f)
 118       if mtime_orig > mtime_thumb:
 119         img_thumb = img.copy()
 120         img_thumb.thumbnail(thumbnail_size)
 121         img_thumb.save(abs_thumbfn, img_thumb.format)
 122       ## 내용출력.
 123       request.write("<li>")
 124       request.write("<a href='"+file_url+"'>")
 125       request.write("<img src='%s' alt='%s'></a><br>" % (get_attach_url(page, request, thumbfn), filename))
 126       request.write(" name(%s) size(%dx%d)<br>" % (filename, img.size[0], img.size[1]))
 127       request.write(" ctime(%s)<br> mtime(%s)" % (str(ctime), str(mtime)))
 128       request.write("<br>")
 129     request.write("</ul>")
 130 
 131 
 132 ##EOF

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-03-23 01:50:04, 4.3 KB) [[attachment:AgPics.py]]
 All files | Selected Files: delete move to page copy to page

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