9991-improved-log... 1 import os
9991-improved-log... 2
genesis 3 class Channel(object):
genesis 4 def __init__(self, server, name):
genesis 5 self.server = server
genesis 6 self.name = name
genesis 7 self.members = set()
genesis 8 self._topic = ""
genesis 9 self._key = None
genesis 10 if self.server.statedir:
genesis 11 self._state_path = "%s/%s" % (
genesis 12 self.server.statedir,
genesis 13 name.replace("_", "__").replace("/", "_"))
genesis 14 self._read_state()
genesis 15 else:
genesis 16 self._state_path = None
genesis 17
genesis 18 def add_member(self, client):
genesis 19 self.members.add(client)
genesis 20
genesis 21 def get_topic(self):
genesis 22 return self._topic
genesis 23
genesis 24 def set_topic(self, value):
genesis 25 self._topic = value
genesis 26 self._write_state()
genesis 27
genesis 28 topic = property(get_topic, set_topic)
genesis 29
genesis 30 def get_key(self):
genesis 31 return self._key
genesis 32
genesis 33 def set_key(self, value):
genesis 34 self._key = value
genesis 35 self._write_state()
genesis 36
genesis 37 key = property(get_key, set_key)
genesis 38
genesis 39 def remove_client(self, client):
genesis 40 self.members.discard(client)
genesis 41 if not self.members:
genesis 42 self.server.remove_channel(self)
genesis 43
genesis 44 def _read_state(self):
genesis 45 if not (self._state_path and os.path.exists(self._state_path)):
genesis 46 return
genesis 47 data = {}
genesis 48 exec(open(self._state_path), {}, data)
genesis 49 self._topic = data.get("topic", "")
genesis 50 self._key = data.get("key")
genesis 51
genesis 52 def _write_state(self):
genesis 53 if not self._state_path:
genesis 54 return
genesis 55 (fd, path) = tempfile.mkstemp(dir=os.path.dirname(self._state_path))
genesis 56 fp = os.fdopen(fd, "w")
genesis 57 fp.write("topic = %r\n" % self.topic)
genesis 58 fp.write("key = %r\n" % self.key)
genesis 59 fp.close()
genesis 60 os.rename(path, self._state_path)
genesis 61
genesis 62