/
channels.py
69 lines (57 loc) · 2.45 KB
/
channels.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
from logbook import debug, info, warning
from db import Channel, Session
__author__ = 'ankhmorporkian'
class ChannelManager:
def __init__(self):
self.session = Session()
def add_channel(self, id, parent=None, name=None, links=None,
description=None, links_add=None, links_remove=None,
temporary=None, position=None, description_hash=None):
c = self.session.query(Channel).get(id)
if c:
debug("Updating channel {} ({})".format(c.name, id))
self.update_channel(id, parent, name, links, description, links_add,
links_remove, temporary, position,
description_hash)
else:
c = Channel(id=id,
parent_id=parent, name=name, links="", description=description,
links_add="",links_remove="",
temporary=temporary, position=position,
description_hash=description_hash)
self.session.add(c)
if parent is not None and parent != 0:
p = self.session.query(Channel).get(parent)
if p is None:
raise KeyError("Got a child channel without a root channel (?)")
def del_channel(self, id):
try:
c = self.session.query(Channel).get(id)
self.session.delete(c)
info("Deleted channel with ID {0}", id)
except KeyError:
warning("Server removed channel with ID {}, "
"but we haven't seen it before!", id)
except:
pass
def get_by_name(self, name):
return self.session.query(Channel).filter_by(name=name).first()
def get(self, id):
if not isinstance(id, int):
if not id.isdigit():
id = self.get_by_name(id).id
else:
id = int(id)
return self.session.query(Channel).get(id)
def update_channel(self,
*args): # id, parent, name, links, description, links_add,
#links_remove, temporary, position, description_hash):
pass
def add_from_message(self, message):
args = []
for m in (
'channel_id', 'parent', 'name', 'links', 'description',
'links_add',
'links_remove', 'temporary', 'position', 'description_hash'):
args.append(getattr(message, m, None))
self.add_channel(*args)