--- a/pvl/dns/serial.py Fri Jul 05 02:32:00 2013 +0300
+++ b/pvl/dns/serial.py Tue Sep 10 12:12:02 2013 +0300
@@ -14,31 +14,62 @@
SERIAL_LEN = 10
def format_serial (date, count) :
+ """
+ Format serial as string.
+
+ >>> format_serial(datetime.date(2013, 9, 10), 0)
+ '2013091000'
+ >>> format_serial(datetime.date(2013, 9, 10), 1)
+ '2013091001'
+ """
+
+ assert 0 <= count <= 99
+
return SERIAL_FMT.format(date=date.strftime(DATE_FMT), count=count)
-def next_count (value, date, count) :
+def next_serial (date, count) :
"""
Return serial with next count.
+
+ >>> next_serial(datetime.date(2013, 9, 10), 1)
+ '2013091002'
+ >>> next_serial(datetime.date(2013, 9, 10), 99)
+ '2013091100'
"""
- count += 1
# check
- if count > 99 :
- serial = str(value + 1)
+ if count < 99 :
+ return format_serial(date, count + 1)
+ else:
+ serial = str(int(format_serial(date, count)) + 1)
log.warn("Serial count rollover: %s, %s; fallback -> %s", date, count, serial)
return serial
- return format_serial(date, count)
-
-def update_serial (serial) :
+def update_serial (serial, today=None) :
"""
Parse given serial number (string), returning an updated serial, based on date.
+
+ Raises ValueError on invalid (non-numeric) serial.
+
+ >>> print update_serial('', today=datetime.date(2013, 9, 10))
+ 2013091001
+ >>> print update_serial('13', today=datetime.date(2013, 9, 10))
+ 14
+ >>> print update_serial('2013083501', today=datetime.date(2013, 9, 10))
+ 2013083502
+ >>> print update_serial('2013083102', today=datetime.date(2013, 9, 10))
+ 2013091001
+ >>> print update_serial('2013091002', today=datetime.date(2013, 9, 10))
+ 2013091003
+ >>> print update_serial('2013091102', today=datetime.date(2013, 9, 10))
+ 2013091103
"""
- today = datetime.date.today()
+ if not today:
+ today = datetime.date.today()
# handle
if not serial :
@@ -89,17 +120,15 @@
log.info("Updating today's count: %s, %s", date, count)
# handle count rollover
- return next_count(value, date, count)
+ return next_serial(date, count)
elif date > today :
# keep, update count
- serial = next_count(value, date, count)
+ serial = next_serial(date, count)
log.warn("Serial in future; incrementing count: %s, %s -> %s", date, count, serial)
return serial
else :
- raise Exception("Invalid serial: %s:%s", old_date, old_count)
-
-
+ raise Exception("Impossible serial: %s:%s", date, count)