[chirp_devel] [PATCH 2 of 2] List unix serial ports in natural order. #753
Tom Hayward
Wed Apr 3 13:23:07 PDT 2013
# HG changeset patch
# User Tom Hayward <tom at tomh.us>
# Date 1365020577 25200
# Node ID aa5ee998b37ab70ee9985ae7becf72698867a4ed
# Parent 055a211f1857229789d4d999e04df82a5684d6cd
List unix serial ports in natural order. #753
diff -r 055a211f1857 -r aa5ee998b37a chirp/platform.py
--- a/chirp/platform.py Wed Apr 03 13:22:52 2013 -0700
+++ b/chirp/platform.py Wed Apr 03 13:22:57 2013 -0700
@@ -16,6 +16,7 @@
import os
import sys
import glob
+import re
from subprocess import Popen
try:
@@ -51,6 +52,11 @@
def _find_me():
return sys.modules["chirp.platform"].__file__
+def natural_sorted(l):
+ convert = lambda text: int(text) if text.isdigit() else text.lower()
+ natural_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
+ return sorted(l, key=natural_key)
+
class Platform:
"""Base class for platform-specific functions"""
@@ -277,12 +283,13 @@
os.system("firefox '%s'" % path)
def list_serial_ports(self):
- return sorted(glob.glob("/dev/ttyS*") +
- glob.glob("/dev/ttyUSB*") +
- glob.glob("/dev/ttyAMA*") +
- glob.glob("/dev/cu.*") +
- glob.glob("/dev/term/*") +
- glob.glob("/dev/tty.KeySerial*"))
+ ports = ["/dev/ttyS*",
+ "/dev/ttyUSB*",
+ "/dev/ttyAMA*",
+ "/dev/cu.*",
+ "/dev/term/*",
+ "/dev/tty.KeySerial*"]
+ return natural_sorted(sum([glob.glob(x) for x in ports], []))
def os_version_string(self):
try:
@@ -327,15 +334,7 @@
os.system("explorer %s" % path)
def list_serial_ports(self):
- def cmp(a, b):
- try:
- return int(a[3:]) - int(b[3:])
- except:
- return 0
-
- ports = [port for port, name, url in comports()]
- ports.sort(cmp=cmp)
- return ports
+ return natural_sorted([port for port, name, url in comports()])
def gui_open_file(self, start_dir=None, types=[]):
import win32gui
More information about the chirp_devel
mailing list