<commit_before>#-*- coding: utf-8 -*-

import cgi
import cgitb
import lib.univie as univie
import lib.gcalexport as gcalexport
import layout
import datetime

PROJECT_ROOT = '/univie2gcal_root'

def get_list_semesters():
	prefix = ["S", "W"]
	long_prefix = ["S", "W"]
	curmonth = int(datetime.datetime.now().strftime("%m"))
	
	if curmonth >= 2 and curmonth < 7:
		pi = 0
	else:
		pi = 1
	
	lst = []
	year = int(datetime.datetime.now().strftime("%Y"))
	for i in range(0,5):
		lst.append(prefix[pi] + str(year))
		if pi == 0:
			year = year - 1
		pi = (pi - 1) % 2
	
	return lst

def get_cgi_variable(form, name, default=None):
	if name in form:
		return form[name].value
	else:
		return default

def gcal(env, form):
	exp = gcalexport.gcal_exporter()
	output = "Hallo: " + exp.msg
	exp.export_to_google_calendar(None)
	
	page = {'status': '200 OK',
			'header': [('Content-type', 'text/html'), ('Content-Length', str(len(output)))],
			'content': output}
	return page

def ical(env, form):
	output = """BEGIN:VCALENDAR
PRODID:univie2ical
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-TIMEZONE:Europe/Vienna
BEGIN:VTIMEZONE
TZID:Europe/Vienna
X-LIC-LOCATION:Europe/Vienna
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE\n"""
	
	sem = get_cgi_variable(form, 'sem', (get_list_semesters()[0])[0])
	if 'courses' in form:
		course_numbers = form['courses'].value.split(',')
	else:
		course_numbers = []
	
	omit = []
	if 'omit' in form:
		omit = form['omit'].value.split(',')
	else:
		omit = []

	for cnr in course_numbers:
		c = univie.Course(str(cnr), sem)
		i = 0
		if c.error == None:
			for e in c.events:
				if layout.event_omitted(cnr, i, omit):
					i = i + 1
					continue
				output = output + e.to_ical_event().encode('utf-8') + "\n"
				i = i + 1

	output = output + "END:VCALENDAR"
	
	page = {'status': '200 OK',
			'header': [('Content-type', 'text/calendar'), ('Content-Length', str(len(output))), ('Content-Disposition', 'attachment; filename=mycalendar.ics;')],
			'content': output}
	return page

def main(env, form):
	output = ""
	
	lastError = ""
	new = ""
	
	sem = get_cgi_variable(form, 'sem', (get_list_semesters()[0])[0])
	if 'courses' in form:
		course_numbers = form['courses'].value.split(',')
	else:
		course_numbers = []
	
	omit = []
	if 'omit' in form:
		omit = form['omit'].value.split(',')
	else:
		omit = []
	
	try:
		if 'inp_cnr' in form:
			int(form['inp_cnr'].value)
			new = form['inp_cnr'].value
			if new in course_numbers:
				new = ""
			else:
				course_numbers.insert(0, str(form['inp_cnr'].value))
	except ValueError:
		lastError = "<div class='error'>&#9888; Error: course number must be an integer</div>"
	
	existing_course_numbers = []
	courses = []
	for cnr in course_numbers:
		c = univie.Course(str(cnr), sem)
		if c.error == None:
			existing_course_numbers.append(cnr)
			courses.append(c)
		else:
			lastError = "<div class='error'>&#9888; Error: " + c.error + "</div>"

	output = output + \
"""<!doctype html>
<html>
	<head>
		<link rel='stylesheet' type='text/css' media='handheld' href='""" + PROJECT_ROOT + """/css/main.css' />
		<link rel='stylesheet' type='text/css' media='handheld' href='""" + PROJECT_ROOT + """/css/mobile.css' />  
		<script type="text/javascript" src='""" + PROJECT_ROOT + """/main.js'></script>
		<title>univie2google-calendar</title>
	</head>
	<body>
		<div class='content'>
			<h1>Import univie-course:</h1>""" + lastError + """
			<form id='main_form' method='post' action='""" + env['SCRIPT_NAME'] + """/'>Select course: """
	if len(existing_course_numbers) == 0:
		output = output + """<select name='sem'>"""
		selected = " selected='selected'"
		for s in get_list_semesters():
			output = output + "<option"+ selected +" value='" + s + "'>"+ s +"</option>"
			selected = ""
		output = output + "</select>"
	else:<commit_msg>enabled mobile<commit_after>#-*- coding: utf-8 -*-

import cgi
import cgitb
import lib.univie as univie
import lib.gcalexport as gcalexport
import layout
import datetime

PROJECT_ROOT = '/univie2gcal_root'

def get_list_semesters():
	prefix = ["S", "W"]
	long_prefix = ["S", "W"]
	curmonth = int(datetime.datetime.now().strftime("%m"))
	
	if curmonth >= 2 and curmonth < 7:
		pi = 0
	else:
		pi = 1
	
	lst = []
	year = int(datetime.datetime.now().strftime("%Y"))
	for i in range(0,5):
		lst.append(prefix[pi] + str(year))
		if pi == 0:
			year = year - 1
		pi = (pi - 1) % 2
	
	return lst

def get_cgi_variable(form, name, default=None):
	if name in form:
		return form[name].value
	else:
		return default

def gcal(env, form):
	exp = gcalexport.gcal_exporter()
	output = "Hallo: " + exp.msg
	exp.export_to_google_calendar(None)
	
	page = {'status': '200 OK',
			'header': [('Content-type', 'text/html'), ('Content-Length', str(len(output)))],
			'content': output}
	return page

def ical(env, form):
	output = """BEGIN:VCALENDAR
PRODID:univie2ical
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-TIMEZONE:Europe/Vienna
BEGIN:VTIMEZONE
TZID:Europe/Vienna
X-LIC-LOCATION:Europe/Vienna
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE\n"""
	
	sem = get_cgi_variable(form, 'sem', (get_list_semesters()[0])[0])
	if 'courses' in form:
		course_numbers = form['courses'].value.split(',')
	else:
		course_numbers = []
	
	omit = []
	if 'omit' in form:
		omit = form['omit'].value.split(',')
	else:
		omit = []

	for cnr in course_numbers:
		c = univie.Course(str(cnr), sem)
		i = 0
		if c.error == None:
			for e in c.events:
				if layout.event_omitted(cnr, i, omit):
					i = i + 1
					continue
				output = output + e.to_ical_event().encode('utf-8') + "\n"
				i = i + 1

	output = output + "END:VCALENDAR"
	
	page = {'status': '200 OK',
			'header': [('Content-type', 'text/calendar'), ('Content-Length', str(len(output))), ('Content-Disposition', 'attachment; filename=mycalendar.ics;')],
			'content': output}
	return page

def main(env, form):
	output = ""
	
	lastError = ""
	new = ""
	
	sem = get_cgi_variable(form, 'sem', (get_list_semesters()[0])[0])
	if 'courses' in form:
		course_numbers = form['courses'].value.split(',')
	else:
		course_numbers = []
	
	omit = []
	if 'omit' in form:
		omit = form['omit'].value.split(',')
	else:
		omit = []
	
	try:
		if 'inp_cnr' in form:
			int(form['inp_cnr'].value)
			new = form['inp_cnr'].value
			if new in course_numbers:
				new = ""
			else:
				course_numbers.insert(0, str(form['inp_cnr'].value))
	except ValueError:
		lastError = "<div class='error'>&#9888; Error: course number must be an integer</div>"
	
	existing_course_numbers = []
	courses = []
	for cnr in course_numbers:
		c = univie.Course(str(cnr), sem)
		if c.error == None:
			existing_course_numbers.append(cnr)
			courses.append(c)
		else:
			lastError = "<div class='error'>&#9888; Error: " + c.error + "</div>"

	output = output + \
"""<!doctype html>
<html>
	<head>
		<link rel='stylesheet' type='text/css' media='all' href='""" + PROJECT_ROOT + """/css/main.css' />
		<link rel='stylesheet' type='text/css' media='handheld' href='""" + PROJECT_ROOT + """/css/mobile.css' />  
		<script type="text/javascript" src='""" + PROJECT_ROOT + """/main.js'></script>
		<title>univie2google-calendar</title>
	</head>
	<body>
		<div class='content'>
			<h1>Import univie-course:</h1>""" + lastError + """
			<form id='main_form' method='post' action='""" + env['SCRIPT_NAME'] + """/'>Select course: """
	if len(existing_course_numbers) == 0:
		output = output + """<select name='sem'>"""
		selected = " selected='selected'"
		for s in get_list_semesters():
			output = output + "<option"+ selected +" value='" + s + "'>"+ s +"</option>"
			selected = ""
		output = output + "</select>"
	else:
{'fraction_non_alphanumeric': 0.184152061219452, 'fraction_numerical': 0.022463589237225377, 'mean_word_length': 4.296078431372549, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, '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': '5761153', 'n_tokens_mistral': 3533, 'n_tokens_neox': 3090, 'n_words': 678}