]> www.infradead.org Git - users/dhowells/kafs-utils.git/commitdiff
Add a convenience module for partition ID handling
authorDavid Howells <dhowells@redhat.com>
Thu, 10 Apr 2014 10:17:25 +0000 (11:17 +0100)
committerDavid Howells <dhowells@redhat.com>
Thu, 10 Apr 2014 10:17:25 +0000 (11:17 +0100)
Add a convenience module for partition ID handling.  One function translates
"/vicepX[Y]", "vicepX[Y]", "X[Y]" and "N" into a numeric ID and range checks
it.  Another function translates a numeric ID back into "/vicepX[Y]".

Make the argument parser use it.

Signed-off-by: David Howells <dhowells@redhat.com>
suite/argparse.py
suite/lib/partition.py [new file with mode: 0644]

index 6384a378420ae9658baf9bf520f069d68240adc4..812f8b1b11a1a677e4dcc97c00052f052f380da9 100644 (file)
@@ -38,24 +38,8 @@ def get_file_name(switch, params):
     return params
 
 def get_partition_id(switch, params):
-    part = params[0]
-    if part.startswith("/vice"):
-        part = part[5:]
-    elif part.startswith("vice"):
-        part = part[4:]
-
-    if part.isnumeric():
-        part = int(part)
-    elif len(part) == 1 and part >= "a" and part <= "z":
-        part = bytes(part)[0] - 97
-    elif len(part) == 2 and part[0] >= "a" and part[0] <= "z" and part[1] >= "a" and part[1] <= "z":
-        part = (bytes(part)[0] - 97) * 26 | bytes(part)[1] - 97
-    else:
-        raise RuntimeError("Unparseable partition ID '" + params[0] + "'")
-
-    if part > 255:
-        raise RuntimeError("Partition ID '" + params[0] + "' out of range")
-    return part
+    from afs.lib.partition import part2id
+    return part2id(params[0])
 
 def get_auth(switch, params):
     return params
diff --git a/suite/lib/partition.py b/suite/lib/partition.py
new file mode 100644 (file)
index 0000000..6af4e97
--- /dev/null
@@ -0,0 +1,54 @@
+#
+# AFS Volume management toolkit: Partition name handling
+# -*- coding: utf-8 -*-
+#
+
+__copyright__ = """
+Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
+Written by David Howells (dhowells@redhat.com)
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public Licence version 2 as
+published by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public Licence for more details.
+
+You should have received a copy of the GNU General Public Licence
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""
+
+def part2id(name):
+    """Convert a partition name or number string into a numeric ID"""
+    if name.isnumeric():
+        n = int(name)
+    else:
+        if name.startswith("/vicep"):
+            name = name[6:]
+        elif name.startswith("vicep"):
+            name = name[5:]
+
+        if len(name) == 1 and name >= "a" and name <= "z":
+            n = int(name, 36) - 10
+        elif (len(name) == 2 and
+              name[0] >= "a" and name[0] <= "z" and
+              name[1] >= "a" and name[1] <= "z"):
+            n = (int(name[0], 36) - 10) * 26 + (int(name[1], 36) - 10)
+            n += 26
+        else:
+            raise RuntimeError("Unparseable partition ID '" + params[0] + "'")
+
+    if n < 0 or n > 255:
+        raise RuntimeError("Partition ID '" + params[0] + "' out of range")
+    return n
+
+def id2part(n):
+    """Convert a numeric ID into a partition name string"""
+    if n < 26:
+        return "/vicep{:c}".format(n + 97)
+    else:
+        n -= 26
+        return "/vicep{:c}{:c}".format(n / 26 + 97, n % 26 + 97)