#!/usr/bin/env python
#
# Fred C. <github-fred@hidzz.com>
#
import os
import shlex
import subprocess
import sys
import yaml

from datetime import datetime

BANNER_INFO_FILE = 'banner.yaml'
DATAFILE = 'commits.dat'
SUNDAY = 6

def is_pushday(filename, today=datetime.now().date()):

  try:
    with open('banner.yaml') as fdin:
      banner = yaml.load(fdin)
  except (IOError, yaml.YAMLError) as err:
    raise StandardError(err)

  # Check if all the fields are present.
  for field in ('start_date', 'message'):
    if field not in banner:
      raise StandardError('The field "%s" is missing' % field)

  try:
    start_date = datetime.strptime(banner['start_date'], '%m/%d/%Y')
    start_date = start_date.date()
    if start_date.weekday() != SUNDAY:
      raise ValueError('The date must start on a sunday')
  except ValueError as err:
    raise StandardError(err)

  message = banner['message'].splitlines()
  # Check for the number of lines and the length of all the lines.
  if len(message) != 7:
    raise StandardError('Only 7 lines are allowed (there is 7 days/week)')
  if not all([len(message[0]) == len(l) for l in message[1:]]):
    raise StandardError('All the lines must have the same length')

  # Build the data structure.
  commit = {}
  number_of_days = len(message[0]) * 7
  day = (today - start_date).days
  push = True if message[day % 7][day / len(message)] == '*' else False

  return (push, day, number_of_days)


def main():
  push, day, days_left = is_pushday(BANNER_INFO_FILE)
  msg = "Push: %s, Day: %s, Days left: %s" % (push, day, days_left)

  try:
    with open(DATAFILE, 'a') as fdout:
      fdout.write(msg)
      fdout.write('\n')

  except IOError as err:
    print err
  else:
    if push:
      command = lambda cmd: subprocess.check_call(shlex.split(cmd))
      command('git commit -am "%s"' % msg )
      command('git push origin')


if __name__ == '__main__':
  main()

@@ -865,16 +865,96 @@
 r(err)%0A%0A
+  if today %3C start_date:%0A    raise StandardError('Today is before start date')%0A%0A
   messag
@@ -1294,47 +1294,28 @@
   # 
-Build the data structure.%0A  commit = %7B%7D
+Is today a push day?
 %0A  n
@@ -1329,16 +1329,31 @@
 f_days =
+ len(message) *
  len(mes
@@ -1364,12 +1364,8 @@
 %5B0%5D)
- * 7
 %0A  d
@@ -1395,16 +1395,25 @@
 e).days%0A
+  try:%0A  
   push =
@@ -1475,16 +1475,92 @@
 se False
+%0A  except IndexError:%0A    raise StandardError('The banner is fully printed')
 %0A%0A  retu
@@ -1604,16 +1604,25 @@
 main():%0A
+  try:%0A  
   push, 
@@ -1667,16 +1667,88 @@
 O_FILE)%0A
+  except StandardError as err:%0A    print err%0A    sys.exit(os.EX_OSERR)%0A%0A
   msg = 
@@ -1807,17 +1807,16 @@
 s_left)%0A
-%0A
   try:%0A 
@@ -1900,17 +1900,16 @@
 e('%5Cn')%0A
-%0A
   except

Check dates bounderies; improving messages