<commit_before>from _ast import Import

__author__ = 'vahid'

from timesheet.commands import Command
from timesheet import config
import sys
import csv
from datetime import datetime
from timesheet.models import Subject


class ImportCommand(Command):
    name = 'import'
    description = 'Import csv file with columns: subject, task, start, end'

    def __init__(self, *args, **kw):
        super(ImportCommand, self).__init__(*args, **kw)
        self.subjects = {}

    def add_arguments(self):
        self.parser.add_argument('input', nargs='?', help="Input filename. if omitted, the standard input will be used")

    def process_row(self, row):
        subject_name, task_name, start_time, end_time = row
        start_time = datetime.strptime(start_time, config.datetime_format)
        end_time = datetime.strptime(end_time, config.datetime_format)

        if not subject_name in self.subjects:
            self.subjects[subject_name] = Subject.ensure(subject_name)


        print subject_name, task_name, start_time, end_time

    def do_job(self):

        if self.args.input:
            csv_file = open(self.args.input)
        else:
            csv_file = sys.stdin

        try:
            reader = csv.reader(csv_file)
            for row in reader:
                self.process_row(row)
        finally:
            if csv_file and csv_file != sys.stdin:
                csv_file.close()

<commit_msg>import csv<commit_after>from _ast import Import

__author__ = 'vahid'

import sys
import csv
from timesheet.commands import Command
from timesheet import config
from timesheet.models import Subject, DBSession, Task
from datetime import datetime


class ImportCommand(Command):
    name = 'import'
    description = 'Import csv file with columns: subject, task, start, end'

    def __init__(self, *args, **kw):
        super(ImportCommand, self).__init__(*args, **kw)
        self.subjects = {}

    def add_arguments(self):
        self.parser.add_argument('input', nargs='?', help="Input filename. if omitted, the standard input will be used")

    def process_row(self, row):
        subject_name, task_name, start_time, end_time = row
        start_time = datetime.strptime(start_time, config.datetime_format)
        end_time = datetime.strptime(end_time, config.datetime_format)

        if not subject_name in self.subjects:
            self.subjects[subject_name] = Subject.ensure(subject_name)
            DBSession.commit()

        task = Task(title=task_name, start_time=start_time, end_time=end_time)
        self.subjects[subject_name].tasks.append(task)
        print 'Adding %s' % task
        DBSession.commit()

    def do_job(self):

        if self.args.input:
            csv_file = open(self.args.input)
        else:
            csv_file = sys.stdin

        try:
            reader = csv.reader(csv_file)
            for row in reader:
                self.process_row(row)
        finally:
            if csv_file and csv_file != sys.stdin:
                csv_file.close()
{'fraction_non_alphanumeric': 0.0913907284768212, 'fraction_numerical': 0.0, 'mean_word_length': 2.6751824817518246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6486392', 'n_tokens_mistral': 913, 'n_tokens_neox': 874, 'n_words': 233}