]> git.meshlink.io Git - catta/blob - avahi-python/avahi/ServiceTypeDatabase.py.in
get rid of a lot of old svn cruft
[catta] / avahi-python / avahi / ServiceTypeDatabase.py.in
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
20 import @DBM@
21 import locale
22 import re
23
24 locale.setlocale(locale.LC_ALL, '')
25
26 class ServiceTypeDatabase:
27
28     def __init__(self, filename = "@pkglibdir@/service-types.db"):
29
30         self.db = @DBM@.open(filename, "r")
31
32         l = locale.getlocale(locale.LC_MESSAGES)
33
34         self.suffixes = ()
35
36         if not l[0] is None:
37
38             if not l[1] is None:
39                 self.suffixes += (l[0] + "@" + l[1], )
40
41             self.suffixes += (l[0], )
42
43             i = l[0].find("_")
44
45             if i >= 0:
46
47                 k = l[0][:i]
48                 
49                 if not l[1] is None:
50                     self.suffixes += (k + "@" + l[1], )
51
52                 self.suffixes += (k, )
53             
54             
55         self.suffixes = tuple(map(lambda x:  "["+x+"]", self.suffixes)) + ("", )
56
57     def __getitem__(self, key):
58
59         for suffix in self.suffixes:
60             try:
61                 return self.db[key + suffix]
62             except KeyError:
63                 pass
64
65         raise KeyError()
66
67     def items(self):
68
69         items = []
70         @FIRST_KEY@
71         @CHECK_KEY@
72             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):
73                 localized_service_name = self[key]
74                 items.append((key, localized_service_name))
75             @NEXT_KEY@
76         return items
77
78     def has_key(self, key):
79
80         for suffix in self.suffixes:
81
82             if self.db.has_key(key + suffix):
83                 return True
84             
85         return False
86
87     def __contains__(self, item):
88
89         for suffix in self.suffixes:
90
91             if item+suffix in self.db:
92                 return True
93
94         return False
95         
96
97         
98 if __name__ == "__main__":
99     
100     b = ServiceTypeDatabase()
101     print b.items()
102
103     print b["_http._tcp"]
104     print b["_ftp._tcp"]
105     print b["_webdav._tcp"]
106     print b["_webdavs._tcp"]
107
108     print b["gurki._tcp"]