From: David Howells Date: Thu, 10 Apr 2014 10:17:25 +0000 (+0100) Subject: Add a convenience module for partition ID handling X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=ebdfc57fda7bcf425b3935b035bd5866f79e28c0;p=users%2Fdhowells%2Fkafs-utils.git Add a convenience module for partition ID handling 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 --- diff --git a/suite/argparse.py b/suite/argparse.py index 6384a37..812f8b1 100644 --- a/suite/argparse.py +++ b/suite/argparse.py @@ -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 index 0000000..6af4e97 --- /dev/null +++ b/suite/lib/partition.py @@ -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)