changeset 4:ccc40ffe14fc

text_x_arnica: factored out select_files from format
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sat, 08 Mar 2008 18:50:11 +0100
parents c9ab05e6d499
children 413a64b78621
files data/plugin/parser/text_x_arnica.py
diffstat 1 files changed, 79 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/parser/text_x_arnica.py	Fri Mar 07 21:46:28 2008 +0100
+++ b/data/plugin/parser/text_x_arnica.py	Sat Mar 08 18:50:11 2008 +0100
@@ -439,6 +439,7 @@
         }
 
     def print_help(self):
+        """ print help """
         self.request.write('''
 <br>
 {{{<br>
@@ -455,7 +456,78 @@
  * [[image1.jpg|alias]] <br>
  * [[image2.jpg|alias]] <br>
 }}}''')
+        
+    def select_files(self, formatter):
+        """ select files """
+        self.pagename = formatter.page.page_name
+        if self.target_page:
+            self.pagename = self.target_page
+        
+        self.quoted_pagename = wikiutil.quoteWikinameURL(self.pagename)
+        self.static_path = AttachFile.getAttachDir(self.request, self.pagename, create=1)
 
+        self.static_url = AttachFile.getAttachUrl(self.pagename, '', self.request)
+        Image_Dict = {}
+        quotes = self.get_quotes()
+
+        if self.only_items == '1':
+            all_files = quotes['image']
+            result = []
+            for attfile in all_files:
+                if os.path.exists(os.path.join(self.static_path, attfile)):
+                    result.append(attfile)
+            all_files = result
+
+            if self.sort_by_alias == '1':
+                result = []
+                alias_text = quotes['alias']
+
+                i = 0
+                for attfile in all_files:
+                    infile = os.path.join(self.static_path, attfile)
+                    Image_Dict[alias_text[i]] = attfile
+                    i += 1
+
+                keys = Image_Dict.keys()
+                keys.sort()
+                for txt in keys:
+                    result.append(Image_Dict[txt])
+                all_files = result
+        else:
+            all_files = os.listdir(self.static_path)
+        
+        if self.filter != '.':
+            result = []
+            for test in all_files:
+                if re.match(self.filter, test):
+                    result.append(test)
+            all_files = result
+            
+        if self.sort_by_name == '1' and self.only_items == '0':
+            all_files.sort()
+
+        if self.sort_by_date == '1':
+            for attfile in all_files:
+                infile = os.path.join(self.static_path, attfile)
+                ft_file = "%s%s" % (str(os.path.getmtime(infile)), str(randint(0, 65535)))
+                Image_Dict[ft_file] = attfile   
+            keys = Image_Dict.keys()
+            keys.sort()
+            result = []
+            for txt in keys:
+                result.append(Image_Dict[txt])
+            all_files = result
+            
+        Image_Dict.clear()
+        
+        if self.reverse_sort == '1':
+            all_files.reverse()
+
+        if all_files:
+            self.get_files(self.static_path, all_files, quotes)        
+        
+        return all_files
+    
     def format(self, formatter):
         """ does the format """
         _ = self._
@@ -464,88 +536,17 @@
             self.print_help()
             return
 
-        ImageDict = {}
-        quotes = self.get_quotes()
-        current_pagename = formatter.page.page_name
-
         if self.target_page and (not self.request.user.may.read(self.target_page) or not Page(self.request, self.target_page).exists()):
             self.request.write(_("Page %(pagename)s does not exists or you don't have enough rights." % {"pagename": self.target_page}))
             return
 
-        if self.target_page:
-            current_pagename = self.target_page
-        self.pagename = current_pagename
-
-        self.quoted_pagename = wikiutil.quoteWikinameURL(self.pagename)
-        attachment_path = AttachFile.getAttachDir(self.request, current_pagename, create=1)
-
-        self.static_path = attachment_path
-        self.static_url = AttachFile.getAttachUrl(self.pagename, '', self.request)
-
-        if self.only_items == '1':
-            all_files = quotes['image']
-            result = []
-            for attfile in all_files:
-                infile = os.path.join(attachment_path, attfile)
-                if os.path.exists(infile):
-                    result.append(attfile)
-            all_files = result
-
-            if self.sort_by_alias == '1':
-                new_ordered_files = []
-                alias_text = quotes['alias']
-
-                i = 0
-                for attfile in all_files:
-                    infile = os.path.join(attachment_path, attfile)
-                    ImageDict[alias_text[i]] = attfile
-                    i += 1
-
-                keys = ImageDict.keys()
-                keys.sort()
-                for txt in keys:
-                    new_ordered_files.append(ImageDict[txt])
-
-                all_files = new_ordered_files
-                ImageDict.clear()
-        else:
-            all_files = os.listdir(attachment_path)
-
-        if self.filter != '.':
-            result = []
-            for test in all_files:
-                if re.match(self.filter, test):
-                    result.append(test)
-            all_files = result
-
-        if not all_files:
-            self.request.write("<br><br><h1>No matching image file found!</h1>")
+        if not self.select_files(formatter):
+            self.request.write(_("No matching image file found!"))
             return
-
-        if self.sort_by_name == '1' and self.only_items == '0':
-            all_files.sort()
-
-        if self.sort_by_date == '1':
-            for attfile in all_files:
-                infile = os.path.join(attachment_path, attfile)
-                ft_file = "%s%s" % (str(os.path.getmtime(infile)), str(randint(0, 65535)))
-                ImageDict[ft_file] = attfile
-
-            keys = ImageDict.keys()
-            keys.sort()
-            file_mdate = []
-            for txt in keys:
-                file_mdate.append(ImageDict[txt])
-            all_files = file_mdate
-            ImageDict.clear()
-
-        if self.reverse_sort == '1':
-            all_files.reverse()
-
-        self.get_files(attachment_path, all_files, quotes)
-
+        
         if self.template_itemlist == '1':
-            self.request.write('Copy the following listitems into the script. Replace alias with the label you want. Afterwards disable template_itemlist by setting it to 0:<BR>')
+            self.request.write(_("Copy the following listitems into the script. Replace alias with the label you want. Afterwards disable template_itemlist by setting it to 0:"))
+            self.request.write('<br>')
             for attfile in self.full:
                 self.request.write(' * [[%(attfile)s|%(alias)s]]<br>\n' % {
                                    'attfile': attfile,
@@ -594,13 +595,13 @@
                     os.unlink(webf)
 
             if not os.path.exists(webf) or not os.path.exists(thumbf):
-                infile = os.path.join(attachment_path, attfile)
+                infile = os.path.join(self.static_path, attfile)
                 im = Image.open(infile)
 
                 if not os.path.exists(webf):
                     im.thumbnail((int(self.webnail_width), int(self.webnail_width)), Image.ANTIALIAS)
                     if self.image_for_webnail == '1':
-                        os.link(os.path.join(attachment_path, attfile), webf)
+                        os.link(os.path.join(self.static_path, attfile), webf)
                     else:
                         im.save(webf, this_imgtype)
                 if not os.path.exists(thumbf):