Back to index

obnam  1.1
Public Member Functions | Static Public Attributes
obnamlib.forget_policy.ForgetPolicy Class Reference

List of all members.

Public Member Functions

def parse
def last_in_each_period
def match

Static Public Attributes

dictionary periods
tuple rule_pat = re.compile(r'(?P<count>\d+)(?P<period>(h|d|w|m|y))')

Detailed Description

Parse and interpret a policy for what to forget and what to keep.

See documentation for the --keep option for details.

Definition at line 22 of file forget_policy.py.


Member Function Documentation

def obnamlib.forget_policy.ForgetPolicy.last_in_each_period (   self,
  period,
  genlist 
)

Definition at line 75 of file forget_policy.py.

00075 
00076     def last_in_each_period(self, period, genlist):
00077         formats = {
00078             'hourly': '%Y-%m-%d %H',
00079             'daily': '%Y-%m-%d',
00080             'weekly': '%Y-%W',
00081             'monthly': '%Y-%m',
00082             'yearly': '%Y',
00083         }
00084     
00085         matches = []
00086         for genid, dt in genlist:
00087             formatted = dt.strftime(formats[period])
00088             if not matches:
00089                 matches.append((genid, formatted))
00090             elif matches[-1][1] == formatted:
00091                 matches[-1] = (genid, formatted)
00092             else:
00093                 matches.append((genid, formatted))
00094         return [genid for genid, formatted in matches]

Here is the caller graph for this function:

def obnamlib.forget_policy.ForgetPolicy.match (   self,
  rules,
  genlist 
)
Match a parsed ruleset against a list of generations and times.

The ruleset should be of the form returned by the parse method.

genlist should be a list of generation identifiers and timestamps.
Identifiers can be anything, timestamps should be an instance
of datetime.datetime, with no time zone (it is ignored).

genlist should be in ascending order by time: oldest one first.

Return value is all those pairs from genlist that should be
kept (i.e., which match the rules).

Definition at line 95 of file forget_policy.py.

00095 
00096     def match(self, rules, genlist):
00097         '''Match a parsed ruleset against a list of generations and times.
00098         
00099         The ruleset should be of the form returned by the parse method.
00100         
00101         genlist should be a list of generation identifiers and timestamps.
00102         Identifiers can be anything, timestamps should be an instance
00103         of datetime.datetime, with no time zone (it is ignored).
00104         
00105         genlist should be in ascending order by time: oldest one first.
00106         
00107         Return value is all those pairs from genlist that should be
00108         kept (i.e., which match the rules).
00109         
00110         '''
00111 
00112         result_ids = set()
00113         for period in rules:
00114             genids = self.last_in_each_period(period, genlist)
00115             if rules[period]:
00116                 for genid in genids[-rules[period]:]:
00117                     result_ids.add(genid)
00118 
00119         return [(genid, dt) for genid, dt in genlist
00120                 if genid in result_ids]
00121 

Here is the call graph for this function:

def obnamlib.forget_policy.ForgetPolicy.parse (   self,
  optarg 
)
Parse the argument of --keep.

Return a dictionary indexed by 'hourly', 'daily', 'weekly',
'monthly', 'yearly', and giving the number of generations
to keep for each time period.

Definition at line 40 of file forget_policy.py.

00040 
00041     def parse(self, optarg):
00042         '''Parse the argument of --keep.
00043         
00044         Return a dictionary indexed by 'hourly', 'daily', 'weekly',
00045         'monthly', 'yearly', and giving the number of generations
00046         to keep for each time period.
00047         
00048         '''
00049         
00050         remaining = optarg
00051         m = self.rule_pat.match(remaining)
00052         if not m:
00053             raise obnamlib.Error('Forget policy syntax error: %s' % optarg)
00054 
00055         result = dict((y, None) for x, y in self.periods.iteritems())
00056         while m:
00057             count = int(m.group('count'))
00058             period = self.periods[m.group('period')]
00059             if result[period] is not None:
00060                 raise obnamlib.Error('Forget policy may not '
00061                                      'duplicate period (%s): %s' % 
00062                                      (period, optarg))
00063             result[period] = count
00064             remaining = remaining[m.end():]
00065             if not remaining:
00066                 break
00067             if not remaining.startswith(','):
00068                 raise obnamlib.Error('Forget policy must have rules '
00069                                      'separated by commas: %s' % optarg)
00070             remaining = remaining[1:]
00071             m = self.rule_pat.match(remaining)
00072         
00073         result.update((x, 0) for x, y in result.iteritems() if y is None)
00074         return result


Member Data Documentation

Initial value:
{
        'h': 'hourly',
        'd': 'daily',
        'w': 'weekly',
        'm': 'monthly',
        'y': 'yearly',
    }

Definition at line 30 of file forget_policy.py.

tuple obnamlib.forget_policy.ForgetPolicy.rule_pat = re.compile(r'(?P<count>\d+)(?P<period>(h|d|w|m|y))') [static]

Definition at line 38 of file forget_policy.py.


The documentation for this class was generated from the following file: