<file_sep># -*- coding: utf-8 -*-
"""
Created on Wed Nov 28 21:21:08 2018

@author: melan
"""

from Perform_Analysis import get_commits, read_file


#Testing the code
import unittest

class TestCommits(unittest.TestCase):

    def setUp(self):
        self.data = read_file('C:\\Users\\melan\\OneDrive\\Documents\\CA4_10390914\\changes_python.log')

    def test_number_of_lines(self):
        self.assertEqual(5255, len(self.data))

    def test_number_of_commits(self):
        commits = get_commits(self.data)
        commits = get_commits(self.data)
        self.assertEqual(422, len(commits))
        self.assertEqual('Thomas', commits[0].author)
        self.assertEqual('Jimmy', commits[420].author)
        
        

if __name__ == '__main__':
    unittest.main()<file_sep># -*- coding: utf-8 -*-
"""
Created on Wed Nov 28 22:11:35 2018

@author: melan
"""

class Commit(object):

    def __init__(self, revision, author, date, time, number_of_lines, changed_path=[], comment=[]):
        self.revision = revision
        self.author = author
        self.date = date
        self.time = time
        self.number_of_lines = number_of_lines
        self.changed_path = changed_path
        self.comment = comment

    def __repr__(self):
        return self.revision + ',' + self.author + \
                ',' + self.date + ',' + self.time + ',' + str(self.number_of_lines) + \
				',' + ' '.join(self.comment) + '\n'

def get_commits(data):
    sep = 72*'-'
    commits = []
    index = 0
    while index < len(data):
        try:
            # parse each of the commits and put them into a list of commits
            details = data[index + 1].split('|')
            # the author with spaces at end removed.
            commit = Commit(details[0].strip(),
                details[1].strip(),
                details[2].strip().split(' ')[0],
                details[2].strip().split(' ')[1],
                int(details[3].strip().split(' ')[0]))
            change_file_end_index = data.index('', index + 1)
            commit.changed_path = data[index + 3 : change_file_end_index]
            commit.comment = data[change_file_end_index + 1 : 
                    change_file_end_index + 1 + commit.number_of_lines]
            # add details to the list of commits.
            commits.append(commit)
            index = data.index(sep, index + 1)
        except IndexError:
            index = len(data)
    return commits

def read_file(any_file):
    # use strip to strip out spaces and trim the line.
    return [line.strip() for line in open(any_file, 'r')]

def save_commits(commits, any_file):
    my_file = open(any_file, 'w')
    my_file.write("revision,author,date,time,number_of_lines,comment\n")
    for commit in commits:
        my_file.write(str(commit))
    my_file.close()

if __name__ == '__main__':
    # open the file - and read all of the lines.
    changes_file = 'C:\\Users\\melan\\OneDrive\\Documents\\CA4_10390914\\changes_python.log'
    data = read_file(changes_file)
    print(len(data))
    commits = get_commits(data)
    print(len(commits))
    print(commits[0])
    print(commits[0].author)
    save_commits(commits, 'changes.csv')
    
#interesting stat no.1 
authorNames = set(get_commits("author"))
len(authorNames)

#interesting stat no.2
import pandas as pd

df = pd.DataFrame([get_commits])

df = pd.DataFrame.from_dict(get_commits["author"])
ax = df.plot(kind='bar', title = 'No of comments per author', figsize=(20,10))
ax.set_xlabel('Users',fontsize=14)
ax.set_ylabel('Frequency',fontsize=14)

#interesting stat no.3