This commit is contained in:
Wolfgang Hottgenroth 2017-12-06 23:30:40 +01:00
commit a8b32fd3f3

128
wichteln.py Normal file
View File

@ -0,0 +1,128 @@
# -*- coding: utf-8 -*-
import random
import smtplib
DEBUG = False
def sendmail(sender, receiver, msg):
smtp = None
try:
smtp = smtplib.SMTP("localhost")
smtp.sendmail("nobody@hotti.de", receiver, msg);
except SMTPException:
print("Fehler beim Senden der Mail an {0}".format(self.name))
raise Exception("Nochmal von vorne")
finally:
if smtp:
smtp.close()
class Wichtelee(object):
def __init__(self, name, email, forbiddenNames):
self.name = name
self.email = email
self.forbiddenNames = forbiddenNames
self.receiverName = None
self.giverName = False
def check(self, receiverName):
return (receiverName != self.name) and (receiverName not in self.forbiddenNames)
def storeOther(self, receiver):
self.receiverName = receiver.name
receiver.giverName = self.name
def isReceiver(self):
return self.giverName
def inform(self):
infoLine = "{0} -> {1}".format(self.name, self.receiverName)
global notarMsg
notarMsg = notarMsg + infoLine + "\n"
if DEBUG:
print(infoLine)
msg = """From: Der Geist der Weihnacht <nobody@hottis.de>
To: {0} <{1}>
Subject: Wichteln
Du, {0}, darfst ein Geschenk für {2} besorgen.
Der Geist der Weihnacht
""".format(self.name, self.email, self.receiverName)
if DEBUG:
receiver = "woho@hottis.de"
else:
receiver = self.email
sendmail("nobody@hottis.de", receiver, msg)
wichtelees = [
Wichtelee("Wolfgang", "woho@hottis.de", ("Patty", "Matthias", "Anna")),
Wichtelee("Patty", "patty@hottis.de", ("Wolfgang", "Matthias", "Anna")),
Wichtelee("Matthias", "matthias@hottis.de", ("Wolfgang", "Patty", "Anna", "Robert")),
Wichtelee("Anna", "anna@hottis.de", ("Wolfgang", "Patty", "Matthias", "Lucia")),
Wichtelee("Thomas", "thomas@nober.de", ("Sandra")),
Wichtelee("Sandra", "sandra.nober@web.de", ("Thomas")),
Wichtelee("Philipp", "philipp@nicolai.eu", ("Barbara")),
Wichtelee("Barbara", "barbara@nicolai.eu", ("Philipp")),
Wichtelee("Robert", "robert@nober.de", ("Matthias")),
Wichtelee("Raphael", "raphael@nober.de", ("Nina")),
Wichtelee("Nina", "nina.massolle@gmx.de", ("Raphael")),
Wichtelee("Lucia", "luci@erbeck.net", ("Viktor", "Anna")),
Wichtelee("Viktor", "vik@erbeck.net", ("Lucia")),
Wichtelee("Gregor", "gregor@nober.de", ())
]
for first in wichtelees:
checked = []
while True:
n = random.randint(0, len(wichtelees)-1)
second = wichtelees[n]
if DEBUG:
print("First is {0}, Second is {1}".format(first.name, second.name))
if second.name not in checked:
checked.append(second.name)
if len(checked) == len(wichtelees):
raise Exception("no complete match possible, try again")
if second.isReceiver():
if DEBUG:
print(" Second receives already from {0}".format(second.giverName))
continue
if first.check(second.name):
if DEBUG:
print(" Match")
first.storeOther(second)
break
else:
if DEBUG:
print(" Forbidden")
notarMsg = """Subject: Wichtelergebnis
To: Papa <heribert-ludger@nober.de
From: Wolfgang <woho@hottis.de>
Bitte gut aufheben und keinem zeigen!
"""
for first in wichtelees:
first.inform()
if DEBUG:
notar = "woho@hottis.de"
else:
notar = "heribert-ludger@nober.de"
sendmail("woho@hottis.de", notar, notarMsg)