summaryrefslogtreecommitdiff
path: root/tools/libtpcodegen.py
diff options
context:
space:
mode:
authorSimon McVittie <simon.mcvittie@collabora.co.uk>2013-01-03 12:33:09 +0000
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2013-01-03 13:56:16 +0000
commitf4634b2caf1f92f27a507207ff181d3a7927871f (patch)
tree2d1567d69145244ad97cd76cc83d550184bbc63a /tools/libtpcodegen.py
parent8058faf2407867dcda1aca35616dd23496b1b814 (diff)
downloadtelepathy-glib-f4634b2caf1f92f27a507207ff181d3a7927871f.tar.gz
Make the code generation tools work under either Python 2 or 3
They have been verified to produce identical output in _gen. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=56758 Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk> Reviewed-by: Xavier Claessens <xavier.claessens@collabora.co.uk>
Diffstat (limited to 'tools/libtpcodegen.py')
-rw-r--r--tools/libtpcodegen.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/tools/libtpcodegen.py b/tools/libtpcodegen.py
index 7e9eb9a50..99de66340 100644
--- a/tools/libtpcodegen.py
+++ b/tools/libtpcodegen.py
@@ -21,6 +21,7 @@ please make any changes there.
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
+import sys
from string import ascii_letters, digits
@@ -28,6 +29,20 @@ NS_TP = "http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"
_ASCII_ALNUM = ascii_letters + digits
+if sys.version_info[0] >= 3:
+ def u(s):
+ """Return s, which must be a str literal with no non-ASCII characters.
+ This is like a more restricted form of the Python 2 u'' syntax.
+ """
+ return s.encode('ascii').decode('ascii')
+else:
+ def u(s):
+ """Return a Unicode version of s, which must be a str literal
+ (a bytestring) in which each byte is an ASCII character.
+ This is like a more restricted form of the u'' syntax.
+ """
+ return s.decode('ascii')
+
def file_set_contents(filename, contents):
try:
os.remove(filename)
@@ -38,13 +53,15 @@ def file_set_contents(filename, contents):
except OSError:
pass
- open(filename + '.tmp', 'w').write(contents)
+ open(filename + '.tmp', 'wb').write(contents)
os.rename(filename + '.tmp', filename)
def cmp_by_name(node1, node2):
return cmp(node1.getAttributeNode("name").nodeValue,
node2.getAttributeNode("name").nodeValue)
+def key_by_name(node):
+ return node.getAttributeNode("name").nodeValue
def escape_as_identifier(identifier):
"""Escape the given string to be a valid D-Bus object path or service
@@ -168,6 +185,9 @@ class _SignatureIter:
self.remaining = string
def next(self):
+ return self.__next__()
+
+ def __next__(self):
if self.remaining == '':
raise StopIteration