-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnzeige.py
85 lines (75 loc) · 3.03 KB
/
Anzeige.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os,re,logging
log = logging.getLogger(__name__)
class Anzeige:
truestrings=['ja','true','yes','Ja','JA','Yes','YES']
def __init__(self,directory):
self.directory=directory
self.title=''
self.text=''
self.price=''
self.category=''
self.place=''
self.isReserved=False
self.pictures=[]
files = sorted(os.listdir(directory))
if 'text.txt' in files:
self.__readText()
else:
raise Exception("Datei text.txt konnte nicht gefunden werden.")
pictures=0
for filename in sorted(files):
if re.match('^\.',filename): continue
if filename == 'text.txt': continue
pictures+=1
if pictures<=3:
self.__readPicture(filename)
else:
log.warning('- ignoring picture '+filename+' for '+self.title)
def __to_boolean(self,string):
if string in self.truestrings:
return True
else:
return False
def __readText(self):
lines=0
with open(os.path.join(self.directory,'text.txt')) as f:
for line in f:
lines += 1
if lines <= 5:
match = re.search('^(Titel|Preis|Kategorie|Ort|Reserviert): (.*)',line)
if match:
if match.group(1) == 'Titel': self.title = match.group(2)
elif match.group(1) == 'Preis': self.price = match.group(2)
elif match.group(1) == 'Kategorie': self.category = match.group(2)
elif match.group(1) == 'Ort': self.place = match.group(2)
elif match.group(1) == 'Reserviert': self.isReserved = self.__to_boolean(match.group(2))
else: raise Exception('Unbekanntes Element '+match.group(1)+' in '+os.path.join(directory,'text.txt')+'.')
else:
raise Exception('Die ersten 5 Zeilen sind für Titel, Preis, Kategorie, Ort und Reserviert.')
else:
self.text += line
def __readPicture(self,filename):
filebase,extension = os.path.splitext(filename)
extension = extension[1:] # remove initial dot
if extension not in ['jpg','jpeg','JPG','JPEG','gif','GIF','png','PNG']:
raise Exception(filename+' with'+extension+' does not apear to be an picture. please check.')
if len(self.pictures)<3:
self.pictures.append(os.path.join(self.directory,filename))
else:
raise Exception("only 3 pictures are allowed.")
def __repr__(self):
return '\n'.join((('title: '+self.title,
'price: '+self.price,
'category: '+self.category,
'place: '+self.place,
'isReserved: '+self.isReserved,
'text: '+self.text,
'pictures: '+', '.join(self.pictures))))
def __getitem__(self, key):
if key == 'title': return str(self.title)
elif key == 'price': return self.price
elif key == 'category': return self.category
elif key == 'place': return self.place
elif key == 'isReserved': return self.isReserved
elif key == 'text': return self.text
elif key == 'pictures': return self.pictures