<commit_before>#!/usr/bin/env python
#coding: utf-8

from git import git
from objects import *
from color   import *
from dateutil import parser as dateparser

def get_commits():
    """シェルからコミットログを取得してCommitオブジェクトにしてリストで返す"""
    commits = []
    commits_string = git("log")

    # analyzing
    commitlines = commits_string.split("\n\n")
    commits_string_list = []
    while 0 < len(commitlines):
        commits_string_list.append(
                    "\n".join(commitlines[0:2]))
        commitlines = commitlines[2:]

    # parse
    for commitline in commits_string_list:
        commitdata = parse_commit(commitline)
        commits.append(
            Commit(**commitdata))

    return commits

def get_latest_commit():
    """最新のコミットのCommitオブジェクトを返す"""
    commits = []
    commits_string = git("log", "-n", "1")

    # analyzing
    commitlines = commits_string.split("\n\n")
    commits_string_list = []
    while 0 < len(commitlines):
        commits_string_list.append(
                    "\n".join(commitlines[0:2]))
        commitlines = commitlines[2:]

    # parse
    for commitline in commits_string_list:
        commitdata = parse_commit(commitline)
        commits.append(
            Commit(**commitdata))

    return commits[0]

def parse_commit(commitstring):
    """シェルから取得したコミットログをパースして""" \
    """コミットのハッシュ値、コミットした者、日付・時間、コミットコメント及び""" \
    """マージした場合はsourceとdestinationを返す"""
    commitlines = commitstring.split("\n")

    # 1 row
    commithash = commitlines[0].replace("commit ","")

    # 2 row
    merge_source = merge_dest = None
    author = None
    if "Merge" in commitlines[1]:
        # parse merge data
        merge_source = commitlines[1].split(" ")[1]
        merge_dest = commitlines[1].split(" ")[2]
    else:
        # parse author
        author = commitlines[1].replace("Author: ","")

    # 3 row
    if "Author" in commitlines[2]:
        # parse author
        author = commitlines[2].replace("Author: ","")
    else:
        # parse date
        date = dateparser.parse(commitlines[2].replace("Date:   ",""))

    # 4 row
    if "Date" in commitlines[3]:
        # parse date
        date = dateparser.parse(commitlines[3].replace("Date:   ",""))
    else:
        # parse comment
        comment = " ".join([i.strip() for i in commitlines[3:]])

    # 5 row
    if "Merge" in commitlines[1]:
        # comment -> under 4 row
        comment = " ".join([i.strip() for i in commitlines[4:]])

    return {"commithash": commithash,
            "merge_data": (merge_source, merge_dest) if merge_source != None else None,
            "author"    : author,
            "date"      : date,
            "comment"   : comment}
        

def output_todolist(todo_container, sortby=None, nocolor=False):
    """ToDoが入っているcontainerを指定してToDoをリスト表示する。"""
    """第二引数にsortするattributeを指定すればsortedの状態で表示される。"""

    author_length = list(sorted([len(i.author) for i in todo_container]))[-1]
    index_length = len(str(len(todo_container)))+1
    timeformat = core.isoformat.replace("-"," ")

    header = "Date"  .ljust(21)+ \
             "Author".ljust(author_length+2)+ \
             "Stat"  .ljust(8)+ \
             "#"     .ljust(index_length)+ \
             "Content"
    title = "%(created_at)s  %(author)s  %(status)s  " \
                                  " %(index)s%(content)s"

    print header
    print "-"*core.terminal_width()

    sort_attributes = ["index","created_at",\
                        "status-open","status-closed","commit"]
    todo_container = zip(range(len(todo_container)),todo_container)

    if (sortby is not None) and (sortby not in sort_attributes):
        raise AttributeError("this attribute not defined to sort attribute.")

    if sortby == "index":
        pass

    elif sortby == "created_at":
        todo_container = map(
                (lambda created_at: [(j,d)
                   for j,d in todo_container if d.created_at == created_at][0]),
                sorted([t.created_at for i,t in todo_container],reverse=True)
        )

    elif sortby == "status-open":<commit_msg>Dropped space from header<commit_after>#!/usr/bin/env python
#coding: utf-8

from git import git
from objects import *
from color   import *
from dateutil import parser as dateparser

def get_commits():
    """シェルからコミットログを取得してCommitオブジェクトにしてリストで返す"""
    commits = []
    commits_string = git("log")

    # analyzing
    commitlines = commits_string.split("\n\n")
    commits_string_list = []
    while 0 < len(commitlines):
        commits_string_list.append(
                    "\n".join(commitlines[0:2]))
        commitlines = commitlines[2:]

    # parse
    for commitline in commits_string_list:
        commitdata = parse_commit(commitline)
        commits.append(
            Commit(**commitdata))

    return commits

def get_latest_commit():
    """最新のコミットのCommitオブジェクトを返す"""
    commits = []
    commits_string = git("log", "-n", "1")

    # analyzing
    commitlines = commits_string.split("\n\n")
    commits_string_list = []
    while 0 < len(commitlines):
        commits_string_list.append(
                    "\n".join(commitlines[0:2]))
        commitlines = commitlines[2:]

    # parse
    for commitline in commits_string_list:
        commitdata = parse_commit(commitline)
        commits.append(
            Commit(**commitdata))

    return commits[0]

def parse_commit(commitstring):
    """シェルから取得したコミットログをパースして""" \
    """コミットのハッシュ値、コミットした者、日付・時間、コミットコメント及び""" \
    """マージした場合はsourceとdestinationを返す"""
    commitlines = commitstring.split("\n")

    # 1 row
    commithash = commitlines[0].replace("commit ","")

    # 2 row
    merge_source = merge_dest = None
    author = None
    if "Merge" in commitlines[1]:
        # parse merge data
        merge_source = commitlines[1].split(" ")[1]
        merge_dest = commitlines[1].split(" ")[2]
    else:
        # parse author
        author = commitlines[1].replace("Author: ","")

    # 3 row
    if "Author" in commitlines[2]:
        # parse author
        author = commitlines[2].replace("Author: ","")
    else:
        # parse date
        date = dateparser.parse(commitlines[2].replace("Date:   ",""))

    # 4 row
    if "Date" in commitlines[3]:
        # parse date
        date = dateparser.parse(commitlines[3].replace("Date:   ",""))
    else:
        # parse comment
        comment = " ".join([i.strip() for i in commitlines[3:]])

    # 5 row
    if "Merge" in commitlines[1]:
        # comment -> under 4 row
        comment = " ".join([i.strip() for i in commitlines[4:]])

    return {"commithash": commithash,
            "merge_data": (merge_source, merge_dest) if merge_source != None else None,
            "author"    : author,
            "date"      : date,
            "comment"   : comment}
        

def output_todolist(todo_container, sortby=None, nocolor=False):
    """ToDoが入っているcontainerを指定してToDoをリスト表示する。"""
    """第二引数にsortするattributeを指定すればsortedの状態で表示される。"""

    author_length = list(sorted([len(i.author) for i in todo_container]))[-1]
    index_length = len(str(len(todo_container)))+1
    timeformat = core.isoformat.replace("-"," ")

    header = "Date"  .ljust(21)+ \
             "Author".ljust(author_length+2)+ \
             "Stat"  .ljust(6)+ \
             "#"     .ljust(index_length)+ \
             "Content"
    title = "%(created_at)s  %(author)s  %(status)s" \
                                  "%(index)s%(content)s"

    print header
    print "-"*core.terminal_width()

    sort_attributes = ["index","created_at",\
                        "status-open","status-closed","commit"]
    todo_container = zip(range(len(todo_container)),todo_container)

    if (sortby is not None) and (sortby not in sort_attributes):
        raise AttributeError("this attribute not defined to sort attribute.")

    if sortby == "index":
        pass

    elif sortby == "created_at":
        todo_container = map(
                (lambda created_at: [(j,d)
                   for j,d in todo_container if d.created_at == created_at][0]),
                sorted([t.created_at for i,t in todo_container],reverse=True)
        )

    elif sortby == "status-open":
{'fraction_non_alphanumeric': 0.13516496994971175, 'fraction_numerical': 0.009567030540905188, 'mean_word_length': 2.4653633659158523, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5140199', 'n_tokens_mistral': 2776, 'n_tokens_neox': 2641, 'n_words': 530}