changeset 3150:8ae94675b9f9

AttachFile: some cosmetical source changes
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 28 Feb 2008 21:34:22 +0100
parents 6749e003d7e2
children 863d90c05507
files MoinMoin/action/AttachFile.py MoinMoin/formatter/text_docbook.py MoinMoin/parser/text_rst.py
diffstat 3 files changed, 40 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Thu Feb 28 21:31:18 2008 +0100
+++ b/MoinMoin/action/AttachFile.py	Thu Feb 28 21:34:22 2008 +0100
@@ -45,21 +45,21 @@
 class AttachmentAlreadyExists(Exception):
     pass
 
+
 def getBasePath(request):
-    """ Get base path where page dirs for attachments are stored.
-    """
+    """ Get base path where page dirs for attachments are stored. """
     return request.rootpage.getPagePath('pages')
 
 
 def getAttachDir(request, pagename, create=0):
-    """ Get directory where attachments for page `pagename` are stored.
-    """
+    """ Get directory where attachments for page `pagename` are stored. """
     if request.page and pagename == request.page.page_name:
         page = request.page # reusing existing page obj is faster
     else:
         page = Page(request, pagename)
     return page.getPagePath("attachments", check_create=create)
 
+
 def absoluteName(url, pagename):
     """ Get (pagename, filename) of an attachment: link
         @param url: PageName/filename.ext or filename.ext (unicode)
@@ -74,6 +74,7 @@
     else:
         return u"/".join(pieces[:-1]), pieces[-1]
 
+
 def attachUrl(request, pagename, filename=None, **kw):
     # filename is not used yet, but should be used later to make a sub-item url
     if kw:
@@ -81,6 +82,7 @@
     else:
         qs = ''
     return "%s/%s%s" % (request.getScriptname(), wikiutil.quoteWikinameURL(pagename), qs)
+
     
 def getAttachUrl(pagename, filename, request, addts=0, escaped=0, do='get', drawing='', upload=False):
     """ Get URL that points to attachment `filename` of page `pagename`. """
@@ -137,19 +139,21 @@
         filename = filename.encode(config.charset)
     return os.path.join(getAttachDir(request, pagename, create=1), 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`.
-    """
+    """ Generate snippet with info on the attachment for page `pagename`. """
     _ = request.getText
 
     attach_dir = getAttachDir(request, pagename)
@@ -164,11 +168,11 @@
         }
     return "\n<p>\n%s\n</p>\n" % attach_info
 
+
 def add_attachment(request, pagename, target, filecontent, overwrite=0):
-    # replace illegal chars
-
     _ = request.getText
 
+    # replace illegal chars
     target = wikiutil.taintfilename(target)
 
     # set mimetype from extension, or from given mimetype
@@ -368,13 +372,15 @@
     if os.path.isdir(attach_dir):
         files = [fn.decode(config.charset) for fn in os.listdir(attach_dir)]
         files.sort()
-        return files
-    return []
+    else:
+        files = []
+    return files
 
 
 def _get_filelist(request, pagename):
     return _build_filelist(request, pagename, 1, 0)
 
+
 def _subdir_exception(zf):
     """
     Checks for the existance of one common subdirectory shared among
@@ -385,16 +391,17 @@
 
     b = zf.namelist()
     if not '/' in b[0]:
-        return False #No directory
+        return False # no directory
     slashoffset = b[0].index('/')
     directory = b[0][:slashoffset]
     for origname in b:
         if origname.rfind('/') != slashoffset or origname[:slashoffset] != directory:
-            return False #Multiple directories or different directory
+            return False # multiple directories or different directory
     names = {}
     for origname in b:
         names[origname] = origname[slashoffset+1:]
-    return names #Returns dict of {origname: safename}
+    return names # returns dict of {origname: safename}
+
 
 def error_msg(pagename, request, msg):
     request.theme.add_msg(msg, "error")
@@ -502,10 +509,6 @@
     'textcha': TextCha(request).render(),
 })
 
-#<dt>%(upload_label_mime)s</dt>
-#<dd><input type="text" name="mime" size="50"></dd>
-#    'upload_label_mime': _('MIME Type (optional)'),
-
     request.write('<h2>' + _("Attached Files") + '</h2>')
     request.write(_get_filelist(request, pagename))
 
@@ -521,8 +524,7 @@
 #############################################################################
 
 def execute(pagename, request):
-    """ Main dispatcher for the 'AttachFile' action.
-    """
+    """ Main dispatcher for the 'AttachFile' action. """
     _ = request.getText
 
     if action_name in request.cfg.actions_excluded:
@@ -619,6 +621,7 @@
     if msg:
         error_msg(pagename, request, msg)
 
+
 def upload_form(pagename, request, msg=''):
     _ = request.getText
 
@@ -633,6 +636,7 @@
     request.theme.send_footer(pagename)
     request.theme.send_closing_html()
 
+
 def do_upload(pagename, request, overwrite):
     _ = request.getText
 
@@ -716,6 +720,7 @@
     if ext == '.map':
         os.utime(getAttachDir(request, pagename), None)
 
+
 def del_file(pagename, request):
     _ = request.getText
 
@@ -735,6 +740,7 @@
 
     upload_form(pagename, request, msg=_("Attachment '%(filename)s' deleted.") % {'filename': filename})
 
+
 def move_file(request, pagename, new_pagename, attachment, new_attachment):
     _ = request.getText
 
@@ -764,6 +770,7 @@
         upload_form(pagename, request, msg=_("Page %(newpagename)s does not exists or you don't have enough rights.") % {
             'newpagename': new_pagename})
 
+
 def attachment_move(pagename, request):
     _ = request.getText
     if 'newpagename' in request.form:
@@ -782,6 +789,7 @@
     attachment = request.form.get('oldattachmentname')[0]
     move_file(request, pagename, new_pagename, attachment, new_attachment)
 
+
 def send_moveform(pagename, request):
     _ = request.getText
 
@@ -834,6 +842,7 @@
     request.theme.add_msg(formhtml, "dialog")
     return thispage.send_page()
 
+
 def get_file(pagename, request):
     import shutil
 
@@ -869,6 +878,7 @@
         # send data
         shutil.copyfileobj(open(fpath, 'rb'), request, 8192)
 
+
 def install_package(pagename, request):
     _ = request.getText
 
@@ -890,6 +900,7 @@
 
     upload_form(pagename, request, msg=msg)
 
+
 def unzip_file(pagename, request):
     _ = request.getText
     valid_pathname = lambda name: ('/' not in name) and ('\\' not in name)
@@ -920,8 +931,8 @@
             sum_size_over_all_valid_files = 0.0
             count_valid_files = 0
             namelist = _subdir_exception(zf)
-            if not namelist: #if it's not handled by _subdir_exception()
-                #Convert normal zf.namelist() to {origname:finalname} dict
+            if not namelist: # if it's not handled by _subdir_exception()
+                # convert normal zf.namelist() to {origname:finalname} dict
                 namelist = {}
                 for name in zf.namelist():
                     namelist[name] = name
@@ -973,6 +984,7 @@
 
     upload_form(pagename, request, msg=wikiutil.escape(msg))
 
+
 def send_viewfile(pagename, request):
     _ = request.getText
     fmt = request.html_formatter
@@ -1008,7 +1020,6 @@
             except IOError:
                 pass
 
-
         request.write(request.formatter.preformatted(1))
         # If we have text but no colorizing parser we try to decode file contents.
         content = open(fpath, 'r').read()
@@ -1083,8 +1094,7 @@
 #############################################################################
 
 def do_admin_browser(request):
-    """ Browser for SystemAdmin macro.
-    """
+    """ Browser for SystemAdmin macro. """
     from MoinMoin.util.dataset import TupleDataset, Column
     _ = request.getText
 
@@ -1093,7 +1103,6 @@
         Column('page', label=('Page')),
         Column('file', label=('Filename')),
         Column('size', label=_('Size'), align='right'),
-        #Column('action', label=_('Action')),
     ]
 
     # iterate over pages that might have attachments
@@ -1110,7 +1119,6 @@
                     Page(request, pagename).link_to(request, querystr="action=AttachFile"),
                     wikiutil.escape(filename.decode(config.charset)),
                     os.path.getsize(filepath),
-                    # '',
                 ))
 
     if data:
--- a/MoinMoin/formatter/text_docbook.py	Thu Feb 28 21:31:18 2008 +0100
+++ b/MoinMoin/formatter/text_docbook.py	Thu Feb 28 21:34:22 2008 +0100
@@ -414,10 +414,8 @@
         if not os.path.exists(fpath):
             return self.text("[attachment:%s]" % url)
         else:
-            return self.image(
-                title="attachment:%s" % url,
-                src=AttachFile.getAttachUrl(pagename, filename,
-                                            self.request, addts=1))
+            src = AttachFile.getAttachUrl(pagename, filename, self.request, addts=1)
+            return self.image(src=src, title="attachment:%s" % url)
 
     def attachment_drawing(self, url, text, **kw):
         _ = self.request.getText
@@ -430,11 +428,8 @@
         if not os.path.exists(fpath):
             return self.text("[drawing:%s]" % url)
         else:
-            return self.image(
-                alt=drawing,
-                src=AttachFile.getAttachUrl(pagename, filename, self.request,
-                                            addts=1),
-                html_class="drawing")
+            src = AttachFile.getAttachUrl(pagename, filename, self.request, addts=1)      
+            return self.image(alt=drawing, src=src, html_class="drawing")
 
 ### Images and Smileys ##############################################
 
--- a/MoinMoin/parser/text_rst.py	Thu Feb 28 21:31:18 2008 +0100
+++ b/MoinMoin/parser/text_rst.py	Thu Feb 28 21:34:22 2008 +0100
@@ -357,8 +357,7 @@
                     self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
                     self.add_wiki_markup()
                 # Attachment exists, just get a link to it.
-                node['refuri'] = AttachFile.getAttachUrl(self.request.page.page_name,
-                        link, self.request)
+                node['refuri'] = AttachFile.getAttachUrl(self.request.page.page_name, link, self.request)
                 if not [i for i in node.children if i.__class__ == docutils.nodes.image]:
                     node['classes'].append(prefix)
             elif prefix == 'wiki':
@@ -417,8 +416,7 @@
                 self.add_wiki_markup()
             # Attachment exists, get a link to it.
             # create the url
-            node['uri'] = AttachFile.getAttachUrl(self.request.page.page_name,
-                    attach_name, self.request, addts=1)
+            node['uri'] = AttachFile.getAttachUrl(self.request.page.page_name, attach_name, self.request, addts=1)
             if not node.hasattr('alt'):
                 node['alt'] = node.get('name', uri)
         html4css1.HTMLTranslator.visit_image(self, node)