#! /usr/bin/env python
# -*- coding: utf-8 -*-


# Use this to create repeating issues. It should run once per day with
# the tracker directory as its first argument. The script then ensures
# that for each of the repeating events described below, there are
# corresponding issues with the appropriate due dates in the tracker.
#
# By default, only the upcoming issue is added to the tracker. That
# is, if you have weekly event, the script will not clutter your
# tracker with e.g. 52 issues with due dates for each week, but will
# only add one issue for the next week. If it is then called again in
# the next week, it will add an issue for the week after that etc.
#
# The events are configured in the list of python dictionaries below.
# The keys title, priority, keyword and message should be self
# explanatory. Note that there is no support for multiple keywords
# yet, but adding this should be trivial.
#
# When an issue is added to the tracker, it always starts in the
# 'waiting' state. The 'revives' key can be used to specify the revive
# date relative to the due date. So if you set "revives": -10, then
# the issue will change to 'needs attention' 10 days before it is due.
#
# To configure the periodicity, you can use the keys months,
# days_of_month and days_of_week. The event will be repeated on every
# date that matches all specified criteria. So if you specify only
# "days_of_month": [1,20] it will be added for the 1st and 20th of
# every month. If you add "months": [1,12] this will only happen in
# January and December. Had you specified *only* "months": [1,12] the
# event would be added to every day in January and December. Note that
# for days_of_week you should use the 3 letter abbreviations instead
# of numbers, i.e. "Mon", "Tue" etc.
#


issues = [
    { "title": "Monthly issue 1",
      "priority": "normal",
      "keyword": "PC",
      "days_of_month": [10,5],
      "months": [4,8,12],
      "revives": -5,
      "message": """
This creates issues with due dates on the 10th and 5th
of the months 4,8 and 12 (April, August and December).
"""

    { "title": "Monthly issue 2",
      "priority": "normal",
      "keyword": "Privat",
      "days_of_month": [25,4],
      "revives": -2,
      "message": """
This creates issues with due dates on the 25th and 4th
of every month.
"""},

    { "title": "Weekly Issue",
      "priority": "normal",
      "keyword": "Pulse",
      "days_of_week": ["Mon", "Tue"],
      "days_of_month": [3, 16],
      "revives": -9,
      "message": """
This creates issues whenever the 3th or 16th is a Monday
or Tuesday, no matter what month."""},
    ]



#### BEGIN MAIN PROGRAM ####


# Roundup boto uses several deprecated modules
import warnings
warnings.filterwarnings("ignore", "", DeprecationWarning, "roundup")

import sys
from roundup import instance
from roundup.date import Date,Interval
from copy import deepcopy

# open the instance
if len(sys.argv) != 2:
    print 'You need to specify an instance home dir'
instance_home = sys.argv[1]
instance = instance.open(instance_home)
db = instance.open('admin')

# Remove time from current date
now = Date(Date(".").pretty("%Y-%m-%d"))

# Loop through next 2 months and add issues
for offset in [Interval("%dd" % x) for x in range(1,60)]:
    date = now + offset
    day_of_month = int(date.pretty("%d"))
    month = int(date.pretty("%m"))
    day_of_week = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ][int(date.pretty("%w"))]

    # Loop through issues
    for issue in issues:
        # Check if the issue is "due" for the current date
        if (issue.has_key("days_of_month") and \
            not day_of_month in issue["days_of_month"]) or \
            (issue.has_key("days_of_week") and \
            not day_of_week in issue["days_of_week"]) or \
            (issue.has_key("months") and \
             not month in issue["months"]):
            continue
        if (not issue.has_key("days_of_week")) and \
           (not issue.has_key("days_of_month")) and \
           (not issue.has_key("months")):
            print "Warning! Issue has no dates: "+issue["title"]
            continue

        # Check whether we already added enough of it
        if not issue.has_key("add_max"):
            issue["add_max"] = 1
        if issue["add_max"] == 0:
            continue
        issue["add_max"] -= 1

        # Check if the issue is already existing
        if db.issue.filter(None, { "autogen_id": ("%s %d" % (date.pretty("%Y-%m-%d"), hash(issue["title"])))}):
            continue

        # Add message
        if issue.has_key("message"):
            msg = [db.msg.create(
                    author = db.user.lookup("admin"),
                    date = Date("."),
                    content = issue["message"]) ]
        else:
            msg = None

        # Add issue
        revives = date + Interval("%dd" % issue["revives"])
        if revives > Date("."):
            db.issue.create( \
                title = issue["title"],
                status = db.status.lookup("waiting"),
                revives = revives,
                due_date = date,
                messages = msg,
                priority = db.priority.lookup(issue["priority"]),
                keyword = [ db.keyword.lookup(issue["keyword"])],
                assignedto = db.user.lookup("nikratio"),
                autogen_id=("%s %d" % (date.pretty("%Y-%m-%d"), hash(issue["title"]))))
        else:
            db.issue.create( \
                title = issue["title"],
                status = db.status.lookup("needs attention"),
                due_date = date,
                messages = msg,
                priority = db.priority.lookup(issue["priority"]),
                keyword = [ db.keyword.lookup(issue["keyword"])],
                assignedto = db.user.lookup("nikratio"),
                autogen_id=("%s %d" % (date.pretty("%Y-%m-%d"), hash(issue["title"]))))
    # End issue loop
# end date loop


db.commit()
db.close()
