1#!@PYTHON@ 2# -*-python-*- 3# This file is part of avahi. 4# 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. 9# 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. 14# 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 18# USA. 19 20import @DBM@ 21import locale 22import re 23 24locale.setlocale(locale.LC_ALL, '') 25 26class ServiceTypeDatabase: 27 """ServiceTypeDatabase maps service types to descriptions""" 28 29 def __init__(self, filename = "@pkglibdatadir@/service-types.db"): 30 31 self.db = @DBM@.open(filename, "r") 32 33 l = locale.getlocale(locale.LC_MESSAGES) 34 35 self.suffixes = () 36 37 if not l[0] is None: 38 39 if not l[1] is None: 40 self.suffixes += (l[0] + "@" + l[1], ) 41 42 self.suffixes += (l[0], ) 43 44 i = l[0].find("_") 45 46 if i >= 0: 47 48 k = l[0][:i] 49 50 if not l[1] is None: 51 self.suffixes += (k + "@" + l[1], ) 52 53 self.suffixes += (k, ) 54 55 56 self.suffixes = tuple(map(lambda x: "["+x+"]", self.suffixes)) + ("", ) 57 58 def __getitem__(self, key): 59 60 for suffix in self.suffixes: 61 try: 62 return self.db[key + suffix] 63 except KeyError: 64 pass 65 66 raise KeyError() 67 68 def items(self): 69 70 return list(self.iteritems()) 71 72 def has_key(self, key): 73 74 for suffix in self.suffixes: 75 76 if self.db.has_key(key + suffix): 77 return True 78 79 return False 80 81 def __contains__(self, item): 82 83 for suffix in self.suffixes: 84 85 if item+suffix in self.db: 86 return True 87 88 return False 89 90 91 def __iter__(self): 92 93 @FIRST_KEY@ 94 @CHECK_KEY@ 95 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): 97 yield key 98 99 @NEXT_KEY@ 100 101 def __len__(self): 102 103 count = 0 104 for _ in self: 105 106 count+=1 107 108 self.__len__ = lambda : count 109 return len(self) 110 111 def get(self, key, default=None): 112 113 if key in self: 114 return self[key] 115 else: 116 return default 117 118 def iteritems(self): 119 120 return ((key, self[key]) for key in self) 121 122 def iterkeys(self): 123 124 return self.__iter__() 125 126 def itervalues(self): 127 128 return (self[key] for key in self) 129 130 def keys(self): 131 132 return list(self) 133 134 def values(self): 135 136 return list(self.itervalues()) 137 138if __name__ == "__main__": 139 140 b = ServiceTypeDatabase() 141 print b.items() 142 143 print b["_http._tcp"] 144 print b["_ftp._tcp"] 145 print b["_webdav._tcp"] 146 print b["_webdavs._tcp"] 147 148 print b.get("gurki._tcp") 149 print len(b) 150 151 for key, _ in zip(b, xrange(3)): 152 153 print key 154 155 for key, _ in zip(b.iteritems(), xrange(3)): 156 157 print key 158 159 for key, _ in zip(b.itervalues(), xrange(3)): 160 161 print key 162