changeset 1911:0b2ad3099fcf

AttachFile.size to reduce code duplication, fixed docstring
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 25 Mar 2007 23:14:29 +0200
parents 5c3dffe2abf1
children 901ff624d942
files MoinMoin/action/AttachFile.py MoinMoin/action/info.py
diffstat 2 files changed, 17 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Sun Mar 25 22:37:45 2007 +0200
+++ b/MoinMoin/action/AttachFile.py	Sun Mar 25 23:14:29 2007 +0200
@@ -15,15 +15,14 @@
     4. /pathname/fname, do=view[&mimetype=type]:create a page
        to view the content of the file
 
-    To insert an attachment into the page, use the "attachment:" pseudo
-    schema.
+    To insert an attachment into the page, use the "attachment:" pseudo schema.
 
-    @copyright: 2001 by Ken Sugino (sugino@mediaone.net)
-    @copyright: 2001-2004 by Jürgen Hermann <jh@web.de>
-    @copyright: 2005 MoinMoin:ReimarBauer
-    @copyright: 2005 MoinMoin:AlexanderSchremmer
-    @copyright: 2005 DiegoOngaro at ETSZONE (diego@etszone.com)
-    @copyright: 2006 MoinMoin:ReimarBauer
+    @copyright: 2001 by Ken Sugino (sugino@mediaone.net),
+                2001-2004 by Jürgen Hermann <jh@web.de>,
+                2005 MoinMoin:AlexanderSchremmer,
+                2005 DiegoOngaro at ETSZONE (diego@etszone.com),
+                2005-2006 MoinMoin:ReimarBauer,
+                2007 MoinMoin:ThomasWaldmann
     @license: GNU GPL, see COPYING for details.
 """
 
@@ -146,13 +145,20 @@
         @rtype: string (in config.charset encoding)
         @return: complete path/filename of attached file
     """
-    return os.path.join(getAttachDir(request, pagename), filename).encode(config.charset)
+    if isinstance(filename, unicode):
+        filename = filename.encode(config.charset)
+    return os.path.join(getAttachDir(request, pagename), filename)
 
 def exists(request, pagename, filename):
     """ check if page <pagename> has a file <filename> attached """
     fpath = getFilename(request, pagename, filename)
     return os.path.exists(fpath)
 
+def size(request, pagename, filename):
+    """ return file size of file attachment """
+    fpath = getFilename(request, pagename, filename)
+    return os.path.getsize(fpath)
+
 def info(pagename, request):
     """ Generate snippet with info on the attachment for page `pagename`.
     """
@@ -878,7 +884,6 @@
     if not filename:
         return # error msg already sent in _access_file
 
-    attachment_path = getAttachDir(request, pagename)
     single_file_size = request.cfg.unzip_single_file_size
     attachments_file_space = request.cfg.unzip_attachments_space
     attachments_file_count = request.cfg.unzip_attachments_count
@@ -890,7 +895,7 @@
         fsize = 0.0
         fcount = 0
         for file in files:
-            fsize += float(os.stat(getFilename(request, pagename, file))[6]) # in byte
+            fsize += float(size(request, pagename, file))
             fcount += 1
 
         available_attachments_file_space = attachments_file_space - fsize
--- a/MoinMoin/action/info.py	Sun Mar 25 22:37:45 2007 +0200
+++ b/MoinMoin/action/info.py	Sun Mar 25 23:14:29 2007 +0200
@@ -133,12 +133,7 @@
                 size = 0
                 if line.action != 'ATTDEL':
                     from MoinMoin.action import AttachFile
-                    page_dir = AttachFile.getAttachDir(request, pagename)
-                    filepath = os.path.join(page_dir, filename.encode(config.charset))
-                    try:
-                        size = os.path.getsize(filepath)
-                    except:
-                        pass
+                    size = AttachFile.size(request, pagename, filename)
                     if line.action == 'ATTNEW':
                         actions.append(render_action(_('view'), {'action': 'AttachFile', 'do': 'view', 'target': '%s' % filename}))
                     elif line.action == 'ATTDRW':