From 22ef0dd5a0082775c746b178e9b7b6023057503b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20K=C3=A1cha?= <ph@cesnet.cz>
Date: Wed, 21 Feb 2024 15:36:52 +0100
Subject: [PATCH] Bring the code to Py 3

---
 dmenu-win.py   |  8 ++++----
 dmenu-xdg.py   | 12 ++++++------
 i3-switcher.py |  7 ++++---
 ipc.py         | 10 +++++-----
 4 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/dmenu-win.py b/dmenu-win.py
index 7b84d81..fb9ac61 100755
--- a/dmenu-win.py
+++ b/dmenu-win.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 # -*- coding: utf-8 -*-
 
 import sys
@@ -9,7 +9,7 @@ def main(argv):
     sock = ipc.i3()
     windows = ipc.getwin(sock.command(ipc.TREE), activews=False)
     dmwindows = {}
-    for w in windows.itervalues():
+    for w in windows.values():
         line = "%2d: %s" % (w["wsnum"], w["name"])
         dup = 2
         while line in dmwindows:
@@ -17,9 +17,9 @@ def main(argv):
             dup += 1
         dmwindows[line] = w["window"]
     dmenu = subprocess.Popen(['dmenu']+argv[1:], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
-    dmenulist = "\n".join(sorted(dmwindows.iterkeys())).encode("utf-8")
+    dmenulist = "\n".join(sorted(dmwindows.keys())).encode("utf-8")
     dmenustdout = dmenu.communicate(dmenulist)[0]
-    picked = dmenustdout.rstrip("\n").decode("utf-8")
+    picked = dmenustdout.rstrip(b"\n").decode("utf-8")
     if picked:
         sock.command(ipc.COMMAND, '[id="%d"] focus' % dmwindows[picked])
 
diff --git a/dmenu-xdg.py b/dmenu-xdg.py
index b8694ad..6d5c031 100755
--- a/dmenu-xdg.py
+++ b/dmenu-xdg.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 # -*- coding: utf-8 -*-
 
 import os
@@ -26,7 +26,7 @@ def uniq_list(l):
 
 
 def quote(s):
-    res = re.sub(r'[`$\\]', lambda(match): '\\'+match.group(0) , s)
+    res = re.sub(r'[`$\\]', lambda match: '\\'+match.group(0) , s)
     res = u"\"" + res + u"\"" if res!=s or s==u"" else s
     return res
 
@@ -55,10 +55,10 @@ def main(argv):
             continue
 
         generic = dsk.getGenericName()
-        if not isinstance(generic, unicode):
+        if not isinstance(generic, str):
             generic = generic.decode("utf-8")
         name = dsk.getName()
-        if not isinstance(name, unicode):
+        if not isinstance(name, str):
             name = name.decode("utf-8")
         dsk.name = name # ugly, sure
         label = name
@@ -72,9 +72,9 @@ def main(argv):
         pick[label] = dsk
 
     dmenu = subprocess.Popen(['dmenu']+argv[1:], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
-    dmenulist = "\n".join(sorted(pick.iterkeys())).encode("utf-8")
+    dmenulist = "\n".join(sorted(pick.keys())).encode("utf-8")
     dmenustdout = dmenu.communicate(dmenulist)[0]
-    picked = dmenustdout.rstrip("\n").decode("utf-8")
+    picked = dmenustdout.rstrip(b"\n").decode("utf-8")
     if picked:
         try:
             dsk = pick[picked]
diff --git a/i3-switcher.py b/i3-switcher.py
index b0451e5..3a44fa5 100755
--- a/i3-switcher.py
+++ b/i3-switcher.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 # -*- coding: utf-8 -*-
 
 import sys
@@ -58,11 +58,12 @@ def main(argv=None):
         clients = ipc.getwin(i3command.command(ipc.TREE))
         sortedclients = sorted(clients.values(), key=itemgetter("wsnum", "order"), reverse=True)
         for c in sortedclients:
+            name = c.get("name") or str(c)
             out.append({
                 u"name": u"client",
                 u"instance": str(c["id"]),
-                u"full_text": unicode(c["name"]),
-                u"short_text": unicode(c["name"])[0:20]+u"…",
+                u"full_text": name,
+                u"short_text": name[0:20]+u"…",
                 u"color": u"#FFFFFF" if c["focused"] else u"#AAAAAA"
             })
 
diff --git a/ipc.py b/ipc.py
index 7a1d44c..9ff3fd4 100755
--- a/ipc.py
+++ b/ipc.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 # -*- coding: utf-8 -*-
 
 import socket
@@ -25,7 +25,7 @@ class i3(object):
 
     def send(self, msg_type, payload=''):
         pld = payload.encode('utf-8')
-        msg = 'i3-ipc' + struct.pack('II', len(pld), msg_type) + pld
+        msg = b'i3-ipc' + struct.pack('II', len(pld), msg_type) + pld
         self.socket.sendall(msg)
 
 
@@ -43,7 +43,7 @@ class i3(object):
             chunk = self.socket.recv(rest)
             data.append(chunk)
             rest -= len(chunk)
-        return "".join(data)
+        return b"".join(data)
 
 
     def close(self):
@@ -75,14 +75,14 @@ def getwin(tree, activews=True):
         stack.extend(node.get("floating_nodes", []))
         stack.extend(node.get("list", []))
     if activews:
-        for w in winlist.keys():
+        for w in list(winlist.keys()):
             if winlist[w]["wsnum"] != wsactive:
                 del winlist[w]
     return winlist
 
 
 def printjson(d):
-    print json.dumps(d, indent=2, sort_keys=True)
+    print(json.dumps(d, indent=2, sort_keys=True))
 
 
 if __name__=='__main__':
-- 
GitLab