From 3cf01339d279a4d587f84fe331bc2d776924815c Mon Sep 17 00:00:00 2001
From: David Gowers <00ai99@gmail.com>
Date: Mon, 30 Jan 2012 13:43:17 +1030
Subject: [PATCH] scrap prefix: Substitute values for date formats {likethis}
 when found.

Also added a tooltip giving a summary of format strings, to the scrap prefix entry field.

List of format strings, sans the {}:
    * year, month, day, hour, minute, second
    * min, sec (== minute and second respectively)
    * weekday, monthday (== month), yearday
    * monthname, dayname
    * monthname_long, dayname_long
    * ymd (year month day, as in '20120130')
    * hm_s (hour minute second, as in '1340:20')

Values are zero padded as appropriate:
    * minute, month, hour, day, second values to 2 places
    * yearday to 3 places)

invalid formats will be left alone --
ie. {what} will remain {what} in the output filename.
---
 gui/filehandling.py      |   43 +++++++++++++++++++++++++++++++++----------
 gui/preferenceswindow.py |   15 +++++++++++++--
 2 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/gui/filehandling.py b/gui/filehandling.py
index 7797a4c..4ffe575 100644
--- a/gui/filehandling.py
+++ b/gui/filehandling.py
@@ -110,9 +110,9 @@ class FileHandler(object):
         (_("JPEG 90% quality (*.jpg; *.jpeg)"), '.jpg', {'quality': 90}), #5
         ]
         self.ext2saveformat = {
-        '.ora': SAVE_FORMAT_ORA, 
-        '.png': SAVE_FORMAT_PNGSOLID, 
-        '.jpeg': SAVE_FORMAT_JPEG, 
+        '.ora': SAVE_FORMAT_ORA,
+        '.png': SAVE_FORMAT_PNGSOLID,
+        '.jpeg': SAVE_FORMAT_JPEG,
         '.jpg': SAVE_FORMAT_JPEG}
         self.config2saveformat = {
         'openraster': SAVE_FORMAT_ORA,
@@ -172,7 +172,7 @@ class FileHandler(object):
         dialog.show_all()
 
     def selected_save_format_changed_cb(self, widget):
-        """When the user changes the selected format to save as in the dialog, 
+        """When the user changes the selected format to save as in the dialog,
         change the extension of the filename (if existing) immediately."""
         dialog = self.save_dialog
         filename = dialog.get_filename()
@@ -392,7 +392,7 @@ class FileHandler(object):
     def open_scratchpad_dialog(self):
         dialog = gtk.FileChooserDialog(_("Open Scratchpad..."), self.app.drawWindow,
                                        gtk.FILE_CHOOSER_ACTION_OPEN,
-                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,                            
+                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                         gtk.STOCK_OPEN, gtk.RESPONSE_OK))
         dialog.set_default_response(gtk.RESPONSE_OK)
 
@@ -420,7 +420,7 @@ class FileHandler(object):
                 self.app.scratchpad_filename = dialog.get_filename().decode('utf-8')
                 self.open_scratchpad(self.app.scratchpad_filename)
         finally:
-            dialog.destroy()  
+            dialog.destroy()
 
     def save_cb(self, action):
         if not self.filename:
@@ -485,7 +485,7 @@ class FileHandler(object):
                     cfg = self.app.preferences['saving.default_format']
                     default_saveformat = self.config2saveformat[cfg]
                     if ext:
-                        try: 
+                        try:
                             saveformat = self.ext2saveformat[ext]
                         except KeyError:
                             saveformat = default_saveformat
@@ -494,7 +494,7 @@ class FileHandler(object):
 
                 desc, ext_format, options = self.saveformats[saveformat]
 
-                # 
+                #
                 if ext:
                     if ext_format != ext:
                         # Minor ugliness: if the user types '.png' but
@@ -537,7 +537,7 @@ class FileHandler(object):
     def save_autoincrement_file(self, filename, prefix, main_doc = True):
         # If necessary, create the folder(s) the scraps are stored under
         prefix_dir = os.path.dirname(prefix)
-        if not os.path.exists(prefix_dir): 
+        if not os.path.exists(prefix_dir):
             os.makedirs(prefix_dir)
 
         number = None
@@ -592,9 +592,32 @@ class FileHandler(object):
         return filename
 
     def get_scrap_prefix(self):
+        import time
+        import re
         prefix = self.app.preferences['saving.scrap_prefix']
         prefix = helpers.expanduser_unicode(prefix.decode('utf-8'))
         prefix = os.path.abspath(prefix)
+        current_time = time.localtime()
+        fmtdict = {}
+        for k in ('year','hour','min','sec'):
+            fmtdict[k] = '%02d' % getattr (current_time, 'tm_' + k)
+        fmtdict['minute'] = fmtdict['min']
+        fmtdict['second'] = fmtdict['sec']
+        fmtdict['year'] = current_time.tm_year
+        fmtdict['month'] = '%02d' % current_time.tm_mon
+        fmtdict['day'] = '%02d' % current_time.tm_mday
+        fmtdict['weekday'] = current_time.tm_wday
+        fmtdict['monthday'] = '%02d' % current_time.tm_mday
+        fmtdict['yearday'] = '%03d' % current_time.tm_yday
+        for k,v in {'dayname': '%a', 'dayname_long': '%A',
+                     'monthname': '%b', 'monthname_long': '%B',
+                     'ymd': '%Y%m%d', 'hm_s':'%H%M:%S'}.items():
+            fmtdict[k] = time.strftime(v, current_time)
+        #prefix = prefix.format (fmtdict)
+        def sub (match):
+            key = match.groups()[0]
+            return str(fmtdict.get (key, '{%s}' % key))
+        prefix = re.sub('\{([a-zA-Z_][a-zA-Z0-9_]+)\}', sub, prefix)
         if os.path.isdir(prefix):
             if not prefix.endswith(os.path.sep):
                 prefix += os.path.sep
@@ -627,7 +650,7 @@ class FileHandler(object):
             confpath = os.path.join(prefix, 'UserData')
         else:
             confpath = os.path.join(homepath, '.gimp-2.6')
-        return confpath       
+        return confpath
 
     def list_scraps(self):
         prefix = self.get_scrap_prefix()
diff --git a/gui/preferenceswindow.py b/gui/preferenceswindow.py
index aba8ca0..f40d0b4 100644
--- a/gui/preferenceswindow.py
+++ b/gui/preferenceswindow.py
@@ -192,7 +192,7 @@ class Window(windowing.Dialog):
         l = gtk.Label(_('Default file format:'))
         l.set_alignment(0.0, 0.5)
         combo = self.defaultsaveformat_combo = gtk.combo_box_new_text()
-        self.defaultsaveformat_values = [filehandling.SAVE_FORMAT_ORA, 
+        self.defaultsaveformat_values = [filehandling.SAVE_FORMAT_ORA,
             filehandling.SAVE_FORMAT_PNGSOLID, filehandling.SAVE_FORMAT_JPEG]
         for saveformat in self.defaultsaveformat_values:
             format_desc = self.app.filehandler.saveformats[saveformat][0]
@@ -207,10 +207,21 @@ class Window(windowing.Dialog):
         l.set_markup(_('<b>Save Next Scrap</b>'))
         table.attach(l, 0, 3, current_row, current_row + 1, xopt, yopt)
         current_row += 1
-
+        tooltips = gtk.Tooltips()
         l = gtk.Label(_('Path and filename prefix:'))
         l.set_alignment(0.0, 0.5)
         self.prefix_entry = gtk.Entry()
+        tooltips.set_tip(self.prefix_entry, _('Special format strings you can use:\n'
+                         ' {year}\t\t\t\tYear\n {month}\t\t\tMonth, 01-12\n'
+                         ' {day}\t\t\t\tDay, 01-31\n {hour}\t\t\t\tHour, 00-24\n'
+                         ' {minute}\t\t\tMinute, 00-59\n {second}\t\t\tSecond, 00-59\n'
+                         ' {weekday}\t\t\tDay of week, 1-6\n {yearday}\t\t\tDay of year, 001-366\n'
+                         ' {dayname}\t\t\tName of day(short), eg. "Sat"\n'
+                         ' {dayname_long}\t\tName of day(long), eg. "Saturday"\n'
+                         ' {monthname}\t\tName of month(short), eg. "Jan"\n'
+                         ' {monthname_long}\tName of month(long), eg. "January"\n'
+                         ' {ymd}\t\t\t\t{year}{month}{day}, eg. 20120130\n'
+                         ' {hm_s}\t\t\t\t{hour}{minute}:{second}, eg. 1413:24\n'))
         self.prefix_entry.connect('changed', self.prefix_entry_changed_cb)
         table.attach(l, 1, 2, current_row, current_row + 1, xopt, yopt)
         table.attach(self.prefix_entry, 2, 3, current_row, current_row + 1, xopt, yopt)
-- 
1.7.8.4

