From abe22597873a735c1b84b3dc74bee8e6b9cb0e58 Mon Sep 17 00:00:00 2001 From: richard Date: Wed, 21 Aug 2002 07:07:27 +0000 Subject: [PATCH] In preparing to turn back on link/unlink journal events (by default these are turned off) I've: - fixed back_anydbm so it can journal those events again (had broken it with recent changes) - changed the serialisation format for dates and intervals to use a numbers-only (and sign for Intervals) string instead of tuple-of-ints. Much smaller. git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@981 57a73879-2fb5-44c3-a270-3262357dd7e2 --- roundup/backends/back_anydbm.py | 42 +++++++++------- roundup/date.py | 86 +++++++++++++++++++++------------ 2 files changed, 79 insertions(+), 49 deletions(-) diff --git a/roundup/backends/back_anydbm.py b/roundup/backends/back_anydbm.py index 27d8ae7..ffbe3ed 100644 --- a/roundup/backends/back_anydbm.py +++ b/roundup/backends/back_anydbm.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -#$Id: back_anydbm.py,v 1.61 2002-08-19 02:53:27 richard Exp $ +#$Id: back_anydbm.py,v 1.62 2002-08-21 07:07:27 richard Exp $ ''' This module defines a backend that saves the hyperdatabase in a database chosen by anydbm. It is guaranteed to always be available in python @@ -342,9 +342,9 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database): if isinstance(prop, Password): d[k] = str(v) elif isinstance(prop, Date) and v is not None: - d[k] = v.get_tuple() + d[k] = v.serialise() elif isinstance(prop, Interval) and v is not None: - d[k] = v.get_tuple() + d[k] = v.serialise() else: d[k] = v return d @@ -605,25 +605,30 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database): def doSaveJournal(self, classname, nodeid, action, params): # handle supply of the special journalling parameters (usually # supplied on importing an existing database) - if params.has_key('creator'): - journaltag = self.user.get(params['creator'], 'username') - del params['creator'] + if isinstance(params, type({})): + if params.has_key('creator'): + journaltag = self.user.get(params['creator'], 'username') + del params['creator'] + else: + journaltag = self.journaltag + if params.has_key('created'): + journaldate = params['created'].serialise() + del params['created'] + else: + journaldate = date.Date().serialise() + if params.has_key('activity'): + del params['activity'] + + # serialise the parameters now + if action in ('set', 'create'): + params = self.serialise(classname, params) else: journaltag = self.journaltag - if params.has_key('created'): - journaldate = params['created'].get_tuple() - del params['created'] - else: - journaldate = date.Date().get_tuple() - if params.has_key('activity'): - del params['activity'] - - # serialise the parameters now - if action in ('set', 'create'): - params = self.serialise(classname, params) + journaldate = date.Date().serialise() # create the journal entry entry = (nodeid, journaldate, journaltag, action, params) + print 'doSaveJournal', entry if __debug__: print >>hyperdb.DEBUG, 'doSaveJournal', entry @@ -1907,6 +1912,9 @@ class IssueClass(Class, roundupdb.IssueClass): # #$Log: not supported by cvs2svn $ +#Revision 1.61 2002/08/19 02:53:27 richard +#full database export and import is done +# #Revision 1.60 2002/08/19 00:23:19 richard #handle "unset" initial Link values (!) # diff --git a/roundup/date.py b/roundup/date.py index a7f212a..e180e6c 100644 --- a/roundup/date.py +++ b/roundup/date.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: date.py,v 1.23 2002-07-18 23:07:08 richard Exp $ +# $Id: date.py,v 1.24 2002-08-21 07:07:27 richard Exp $ __doc__ = """ Date, time and time interval handling. @@ -75,6 +75,10 @@ class Date: >>> Date("14:25", -5) + + The date format 'yyyymmddHHMMSS' (year, month, day, hour, + minute, second) is the serialisation format returned by the serialise() + method, and is accepted as an argument on instatiation. ''' def __init__(self, spec='.', offset=0): """Construct a date given a specification and a time zone offset. @@ -210,17 +214,23 @@ class Date: return str def set(self, spec, offset=0, date_re=re.compile(r''' - (((?P\d\d\d\d)-)?((?P\d\d?)-(?P\d\d?))?)? # yyyy-mm-dd - (?P\.)? # . - (((?P\d?\d):(?P\d\d))?(:(?P\d\d))?)? # hh:mm:ss - (?P.+)? # offset - ''', re.VERBOSE)): + (((?P\d\d\d\d)-)?((?P\d\d?)-(?P\d\d?))?)? # yyyy-mm-dd + (?P\.)? # . + (((?P\d?\d):(?P\d\d))?(:(?P\d\d))?)? # hh:mm:ss + (?P.+)? # offset + ''', re.VERBOSE), serialised_re=re.compile(''' + (?P\d{4})(?P\d{2})(?P\d{2}) # yyyymmdd + (?P\d{2})(?P\d{2})(?P\d{2}) # HHMMSS + ''', re.VERBOSE)): ''' set the date to the value in spec ''' - m = date_re.match(spec) + m = serialised_re.match(spec) if not m: - raise ValueError, _('Not a date spec: [[yyyy-]mm-dd].[[h]h:mm[:ss]]' - '[offset]') + m = date_re.match(spec) + if not m: + raise ValueError, _('Not a date spec: [[yyyy-]mm-dd].' + '[[h]h:mm[:ss]][offset]') + info = m.groupdict() # get the current date/time using the offset @@ -245,7 +255,7 @@ class Date: self.year, self.month, self.day, self.hour, self.minute, \ self.second, x, x, x = time.gmtime(ts) - if info['o']: + if info.get('o', None): self.applyInterval(Interval(info['o'])) def __repr__(self): @@ -262,6 +272,10 @@ class Date: return (self.year, self.month, self.day, self.hour, self.minute, self.second, 0, 0, 0) + def serialise(self): + return '%4d%02d%02d%02d%02d%02d'%(self.year, self.month, + self.day, self.hour, self.minute, self.second) + class Interval: ''' Date intervals are specified using the suffixes "y", "m", and "d". The @@ -290,6 +304,10 @@ class Interval: days (or over/underflow from hours/mins/secs) will do that, and days-per-month and leap years are accounted for. Leap seconds are not. + The interval format 'syyyymmddHHMMSS' (sign, year, month, day, hour, + minute, second) is the serialisation format returned by the serialise() + method, and is accepted as an argument on instatiation. + TODO: more examples, showing the order of addition operation ''' def __init__(self, spec, sign=1): @@ -311,7 +329,7 @@ class Interval: r = cmp(getattr(self, attr), getattr(other, attr)) if r: return r return 0 - + def __str__(self): """Return this interval as a string.""" sign = {1:'+', -1:'-'}[self.sign] @@ -325,35 +343,32 @@ class Interval: l.append('%d:%02d'%(self.hour, self.minute)) return ' '.join(l) - def set(self, spec, interval_re = re.compile(''' - \s* - (?P[-+])? # + or - - \s* - ((?P\d+\s*)y)? # year - \s* - ((?P\d+\s*)m)? # month - \s* - ((?P\d+\s*)w)? # week - \s* - ((?P\d+\s*)d)? # day - \s* - (((?P\d+):(?P\d+))?(:(?P\d+))?)? # time - \s* - ''', re.VERBOSE)): + def set(self, spec, interval_re=re.compile(''' + \s*(?P[-+])? # + or - + \s*((?P\d+\s*)y)? # year + \s*((?P\d+\s*)m)? # month + \s*((?P\d+\s*)w)? # week + \s*((?P\d+\s*)d)? # day + \s*(((?P\d+):(?P\d+))?(:(?P\d+))?)? # time + \s*''', re.VERBOSE), serialised_re=re.compile(''' + (?P[+-])(?P\d{4})(?P\d{2})(?P\d{2}) + (?P\d{2})(?P\d{2})(?P\d{2})''', re.VERBOSE)): ''' set the date to the value in spec ''' self.year = self.month = self.week = self.day = self.hour = \ self.minute = self.second = 0 self.sign = 1 - m = interval_re.match(spec) + m = serialised_re.match(spec) if not m: - raise ValueError, _('Not an interval spec: [+-] [#y] [#m] [#w] ' - '[#d] [[[H]H:MM]:SS]') + m = interval_re.match(spec) + if not m: + raise ValueError, _('Not an interval spec: [+-] [#y] [#m] [#w] ' + '[#d] [[[H]H:MM]:SS]') info = m.groupdict() for group, attr in {'y':'year', 'm':'month', 'w':'week', 'd':'day', 'H':'hour', 'M':'minute', 'S':'second'}.items(): - if info[group] is not None: + if info.getr(group, None) is not None: setattr(self, attr, int(info[group])) if self.week: @@ -416,8 +431,12 @@ class Interval: return s def get_tuple(self): - return (self.year, self.month, self.day, self.hour, self.minute, - self.second) + return (self.sign, self.year, self.month, self.day, self.hour, + self.minute, self.second) + + def serialise(self): + return '%s%4d%02d%02d%02d%02d%02d'%(self.sign, self.year, self.month, + self.day, self.hour, self.minute, self.second) def test(): @@ -442,6 +461,9 @@ if __name__ == '__main__': # # $Log: not supported by cvs2svn $ +# Revision 1.23 2002/07/18 23:07:08 richard +# Unit tests and a few fixes. +# # Revision 1.22 2002/07/14 06:05:50 richard # . fixed the date module so that Date(". - 2d") works # -- 2.30.2