1 """Library code for language-independent D-Bus-related code generation.
3 The master copy of this library is in the telepathy-glib repository -
4 please make any changes there.
7 # Copyright (C) 2006-2008 Collabora Limited
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 from string import ascii_letters, digits
27 NS_TP = "http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"
29 _ASCII_ALNUM = ascii_letters + digits
32 def camelcase_to_lower(s):
38 for i in range(1,len(s)):
41 if (i+1) < len(s) and s[i+1].islower():
42 out += "_" + s[i].lower()
46 out += "_" + s[i].lower()
54 def camelcase_to_upper(s):
55 return camelcase_to_lower(s).upper()
58 def cmp_by_name(node1, node2):
59 return cmp(node1.getAttributeNode("name").nodeValue,
60 node2.getAttributeNode("name").nodeValue)
63 def escape_as_identifier(identifier):
64 """Escape the given string to be a valid D-Bus object path or service
65 name component, using a reversible encoding to ensure uniqueness.
67 The reversible encoding is as follows:
69 * The empty string becomes '_'
70 * Otherwise, each non-alphanumeric character is replaced by '_' plus
71 two lower-case hex digits; the same replacement is carried out on
72 the first character, if it's a digit
78 # A bit of a fast path for strings which are already OK.
79 # We deliberately omit '_' because, for reversibility, that must also
81 if (identifier.strip(_ASCII_ALNUM) == '' and
82 identifier[0] in ascii_letters):
85 # The first character may not be a digit
86 if identifier[0] not in ascii_letters:
87 ret = ['_%02x' % ord(identifier[0])]
91 # Subsequent characters may be digits or ASCII letters
92 for c in identifier[1:]:
96 ret.append('_%02x' % ord(c))
101 def get_by_path(element, path):
102 branches = path.split('/')
105 # Is the current branch an attribute, if so, return the attribute value
107 return element.getAttribute(branch[1:])
109 # Find matching children for the branch
112 children.append(element.parentNode)
114 for x in element.childNodes:
115 if x.localName == branch:
119 # If this is not the last path element, recursively gather results from
121 if len(branches) > 1:
123 add = get_by_path(x, '/'.join(branches[1:]))
124 if isinstance(add, list):
134 def get_docstring(element):
136 for x in element.childNodes:
137 if x.namespaceURI == NS_TP and x.localName == 'docstring':
139 if docstring is not None:
140 docstring = docstring.toxml().replace('\n', ' ').strip()
141 if docstring.startswith('<tp:docstring>'):
142 docstring = docstring[14:].lstrip()
143 if docstring.endswith('</tp:docstring>'):
144 docstring = docstring[:-15].rstrip()
145 if docstring in ('<tp:docstring/>', ''):
150 def get_descendant_text(element_or_elements):
151 if not element_or_elements:
153 if isinstance(element_or_elements, list):
154 return ''.join(map(get_descendant_text, element_or_elements))
156 for x in element_or_elements.childNodes:
157 if x.nodeType == x.TEXT_NODE:
158 parts.append(x.nodeValue)
159 elif x.nodeType == x.ELEMENT_NODE:
160 parts.append(get_descendant_text(x))
163 return ''.join(parts)
166 class _SignatureIter:
167 """Iterator over a D-Bus signature. Copied from dbus-python 0.71 so we
168 can run genginterface in a limited environment with only Python
171 def __init__(self, string):
172 self.remaining = string
175 if self.remaining == '':
178 signature = self.remaining
183 for marker in range(0, end):
184 cur_sig = signature[marker]
188 elif cur_sig == '{' or cur_sig == '(':
189 if block_type == None:
192 if block_type == cur_sig:
193 block_depth = block_depth + 1
196 if block_type == '{':
197 block_depth = block_depth - 1
204 if block_type == '(':
205 block_depth = block_depth - 1
217 self.remaining = signature[end:]
218 return Signature(signature[0:end])
221 class Signature(str):
222 """A string, iteration over which is by D-Bus single complete types
223 rather than characters.
226 return _SignatureIter(self)
230 s = s.replace('&', '&').replace("'", ''').replace('"', '"')
231 return s.replace('<', '<').replace('>', '>')