<commit_before>#!/usr/bin/env python
import os
import datetime
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from weekview.models import EventCategory, Event
from django.contrib.auth.models import User

def get_categories(filename, username):
	catdict = {}
	file = open(filename)
	for line in file.readlines():
		name = line.split('|')[0]
		if not name in catdict:
			category = EventCategory.objects.get(name__iexact=name) # FIXME: add user check
			if category is not None:
				catdict[name.lower()] = category
			else:
				raise Exception('Error in data', 'DB does not contain category: '+name)
	return catdict

def parse_date(str):
	datestr, timestr = str.split(' ')
	day,month,year = datestr.split('.')
	hour,minute = timestr.split(':')
	return datetime.datetime(int(year),int(month),int(day),int(hour),int(minute),0)

def get_user(username):
	return User.objects.get(username__iexact=username)

def import_file(filename, username):
	catdict = get_categories(filename, username)
	user = get_user(username)
	file = open(filename)
	for line in file.readlines():
		line = line.replace("\n", '')
		catstr, beginstr, endstr = line.split('|')

		event = Event()
		event.user = user
		event.category = catdict[catstr.lower()]
		event.begin = parse_date(beginstr)
		event.end = parse_date(endstr)
		print event.__str__() + "\n"
		event.save()

import_file('data.txt', 'deggis')
<commit_msg>Improved data_importer script a little.<commit_after>#!/usr/bin/env python
import os
import datetime
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from weekview.models import EventCategory, Event
from django.contrib.auth.models import User

def get_categories(filename, username):
	catdict = {}
	file = open(filename)
	for line in file.readlines():
		name = line.split('|')[0]
		if not name in catdict:
			try:
				category = EventCategory.objects.get(name__iexact=name) # FIXME: add user check
				catdict[name.lower()] = category
			except Exception as inst:
				print "Category '"+name+"' was not found!"
				raise Exception('Error in data', 'DB does not contain category: '+name)
	return catdict

def parse_date(str):
	datestr, timestr = str.split(' ')
	year,month,day = datestr.split('.')
	hour,minute = timestr.split(':')
	try:
		return datetime.datetime(int(year),int(month),int(day),int(hour),int(minute),0)
	except Exception as inst:
		print "Invalid datestring: '"+str+"', got: "+inst.__str__()

def get_user(username):
	return User.objects.get(username__iexact=username)

def import_file(filename, username):
	catdict = get_categories(filename, username)
	user = get_user(username)
	file = open(filename)
	print """Beginning to import data. All needed categories were found!

"""
	for line in file.readlines():
		line = line.replace("\n", '')
		catstr, beginstr, endstr = line.split('|')

		event = Event()
		event.user = user
		event.category = catdict[catstr.lower()]
		event.begin = parse_date(beginstr)
		event.end = parse_date(endstr)
		print event.__str__()
		event.save()
	print """

There was many datas and the datas were good!

Byebye."""

import_file('data.txt', 'deggis')

{'fraction_non_alphanumeric': 0.1407766990291262, 'fraction_numerical': 0.0012944983818770227, 'mean_word_length': 5.037109375, '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': '5126969', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1034, 'n_words': 262}