3 # This file is part of avahi.
5 # avahi is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation; either version 2 of the
8 # License, or (at your option) any later version.
10 # avahi is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
13 # License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with avahi; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 locale.setlocale(locale.LC_ALL, '')
26 class ServiceTypeDatabase:
27 """ServiceTypeDatabase maps service types to descriptions"""
29 def __init__(self, filename = "@pkglibdatadir@/service-types.db"):
31 self.db = @DBM@.open(filename, "r")
33 l = locale.getlocale(locale.LC_MESSAGES)
40 self.suffixes += (l[0] + "@" + l[1], )
42 self.suffixes += (l[0], )
51 self.suffixes += (k + "@" + l[1], )
53 self.suffixes += (k, )
56 self.suffixes = tuple(map(lambda x: "["+x+"]", self.suffixes)) + ("", )
58 def __getitem__(self, key):
60 for suffix in self.suffixes:
62 return self.db[key + suffix]
70 return list(self.iteritems())
72 def has_key(self, key):
74 for suffix in self.suffixes:
76 if self.db.has_key(key + suffix):
81 def __contains__(self, item):
83 for suffix in self.suffixes:
85 if item+suffix in self.db:
96 if re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+', key) and not re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+\[.*\]', key):
108 self.__len__ = lambda : count
111 def get(self, key, default=None):
120 return ((key, self[key]) for key in self)
124 return self.__iter__()
126 def itervalues(self):
128 return (self[key] for key in self)
136 return list(self.itervalues())
138 if __name__ == "__main__":
140 b = ServiceTypeDatabase()
143 print b["_http._tcp"]
145 print b["_webdav._tcp"]
146 print b["_webdavs._tcp"]
148 print b.get("gurki._tcp")
151 for key, _ in zip(b, xrange(3)):
155 for key, _ in zip(b.iteritems(), xrange(3)):
159 for key, _ in zip(b.itervalues(), xrange(3)):