#!/usr/bin/env python
'''github-pr-stats

Usage:
   github-pr-stats [options] <user> [<repo>]
   github-pr-stats --version
   github-pr-stats (-h | --help)

Options:
   -h --help                     Show this screen.
      --version                  Print the program's installed version.
      --basic                    Basic statistics about merged/closed rate.
      --days-open                Analyze number of days opened.
      --comments                 Analyze number of comments created.
      --day-created              Analyze day of the week opened.
      --day-closed               Analyze day of the week closed.
      --hour-created             Analyze hour of the day opened.
      --hour-closed              Analyze hour of the day closed.
      --week-created             Analyze week opened.
      --week-closed              Analyze week closed.
      --user-creating            Analyze user who opened.
      --user-closing             Analyze user who closed.
      --labels                   Analyze attached labels.
      --additions                Analyze number of lines added.
      --deletions                Analyze number of lines deleted.
      --bucketSize=<bucketSize>  The size of the display groups for line
                                 additions and deletions. [default: 10]
      --since=<date>             Only consider pull requests opened on or after
                                 this date.
      --until=<date>             Only consider pull requests opened before this
                                 date.
'''

# May you recognize your weaknesses and share your strengths.
# May you share freely, never taking more than you give.
# May you find love and love everyone you find.

import signal
import sys
from collections import defaultdict

from docopt import docopt
from envoy import run
from github3 import authorize

from github_pr_stats import VERSION
from github_pr_stats.github_pr_stats import analyze

# Stack traces are ugly; why would we want to print one on ctrl-c?
def nice_sigint(signal, frame):
   print("")
   sys.exit(0)
signal.signal(signal.SIGINT, nice_sigint)

arguments = docopt(__doc__, version='github-pr-stats %s' % VERSION)

try:
   arguments['--bucketSize'] = int(arguments['--bucketSize'])
except ValueError:
   sys.stderr.write('bucketSize must be an integer!\n')
   sys.exit(1)

# Use a stored OAuth token so we don't have to ask for the user's password
# every time (or save the password on disk!).
token = run('git config --global github-pr-stats.token').std_out.strip()
if not token:
   from getpass import getpass
   user = password = ''

   while not user:
      user = raw_input('Username: ')
   while not password:
      password = getpass('Password: ')

   auth = authorize(user, password,
                    scopes=['repo'],
                    note='github-pr-stats',
                    note_url='https://github.com/xiongchiamiov/github-pr-stats')
   token = auth.token
   # We need to un-unicode token for now.
   # https://github.com/kennethreitz/envoy/issues/34
   run("git config --global github-pr-stats.token '%s'" % str(token))

config = defaultdict(bool)
if arguments['--basic']:
   config['basicStats'] = True
if arguments['--days-open']:
   config['daysOpen'] = True
if arguments['--comments']:
   config['comments'] = True
if arguments['--day-created']:
   config['dayOfWeekCreated'] = True
if arguments['--day-closed']:
   config['dayOfWeekClosed'] = True
if arguments['--hour-created']:
   config['hourOfDayCreated'] = True
if arguments['--hour-closed']:
   config['hourOfDayClosed'] = True
if arguments['--week-created']:
   config['weekCreated'] = True
if arguments['--week-closed']:
   config['weekClosed'] = True
if arguments['--user-creating']:
   config['userCreating'] = True
if arguments['--user-closing']:
   config['userClosing'] = True
if arguments['--labels']:
   config['labels'] = True
if arguments['--additions']:
   config['additions'] = True
if arguments['--deletions']:
   config['deletions'] = True
# Did we not get *any* data-specifying options?  Default to everything.
if not config:
   config = defaultdict(lambda: True)

analyze(token, config, arguments['<user>'], arguments['<repo>'], \
        since=arguments['--since'], until=arguments['--until'], \
        bucketSize=arguments['--bucketSize'])

