import os
import re
import pprint
+from datetime import date
from nagios.object.verify import NagiosVerifyError, NagiosObjectVerifier
if match is not None:
return None
- re_time = r'(?:(?:(?:[01][0-9]|2[0-3]):[0-5][0-9])|24:00)'
- re_timerange = re_time + r'\s*-\s*' + re_time
+ re_time = r'(?:(?:(?:[01][0-9]|2[0-3]):[0-5][0-9])|24:00)'
+ re_timerange = re_time + r'\s*-\s*' + re_time
re_timeranges = r'(' + re_timerange + r')(?:,(' + re_timerange + r'))*'
re_weekdays = r'(monday|tuesday|wednesday|thursday|friday|saturday|sunday)'
+ re_isodate = r'(\d\d\d\d)-(0[1-9]|1[0-2])-([0-2][1-9]|3[01])'
+ pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
+
+ # first search for a simple weekday
regex = r'^' + re_weekdays + r'\s+' + re_timeranges + r'$'
#self.logger.debug( "Timeperiod Regex {0!r}.\n\t{1!r}".format( regex, line ) )
-
match = re.search( regex, line )
if match is not None:
- pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
key = match.group(1)
value = []
for v in match.groups()[1:-1]:
value.append(v)
return ( key, value )
+ # Now search single ISO formatted date or a range of two ISO formatted dates
+ regex = r'^' + re_isodate + r'(?:\s*-\s*' + re_isodate + r')?\s+' + re_timeranges + r'$'
+ self.logger.debug( "Timeperiod Regex {0!r}.\n\t{1!r}".format( regex, line ) )
+ match = re.search( regex, line )
+ if match is not None:
+
+ liste = []
+ for k in match.groups():
+ liste.append(k)
+ self.logger.debug( "Found matches:\n\t{0}".format( pp.pformat( liste ) ) )
+
+ year = int(match.group(1))
+ month = int(match.group(2))
+ day = int(match.group(3))
+
+ try:
+ date_start = date( year, month, day )
+ except ValueError as e:
+ self.logger.warn( "Invalid date found in line {0!r}.".format( line ) )
+ return None
+
+ key = date_start.isoformat()
+
+ # It's a date range ...
+ if match.group(4) is not None:
+
+ year = int(match.group(4))
+ month = int(match.group(5))
+ day = int(match.group(6))
+
+ try:
+ date_end = date( year, month, day )
+ except ValueError as e:
+ self.logger.warn( "Invalid date found in line {0!r}.".format( line ) )
+ return None
+
+ key = key + ' - ' + date_end.isoformat()
+
+ # Formatting the value ...
+ value = []
+ for v in match.groups()[6:-1]:
+ value.append(v)
+ return ( key, value )
+
# Currently only a stub
return None