From: Dan Mick Date: Sat, 13 Jul 2013 04:23:14 +0000 (-0700) Subject: ceph_argparse.py: allow valid char RE arg to CephString X-Git-Tag: v0.67-rc1~59^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=741757a3c3cb437adcd38028111c9dbd9cf57463;p=ceph.git ceph_argparse.py: allow valid char RE arg to CephString Change badchars to goodchars (no one was using badchars); allow goodchars to be a RE character class of valid characters for the param. First use: crush item names. Signed-off-by: Dan Mick --- diff --git a/src/pybind/ceph_argparse.py b/src/pybind/ceph_argparse.py index 830667480d4f..b2f8956c8a53 100644 --- a/src/pybind/ceph_argparse.py +++ b/src/pybind/ceph_argparse.py @@ -15,6 +15,7 @@ Foundation. See file COPYING. import copy import json import os +import re import socket import stat import sys @@ -175,21 +176,31 @@ class CephFloat(CephArgtype): class CephString(CephArgtype): """ - String; pretty generic. + String; pretty generic. goodchars is a RE char class of valid chars """ - def __init__(self, badchars=''): - self.badchars = badchars + def __init__(self, goodchars=''): + from string import printable + try: + re.compile(goodchars) + except: + raise ValueError('CephString(): "{0}" is not a valid RE'.\ + format(goodchars)) + self.goodchars = goodchars + self.goodset = frozenset( + [c for c in printable if re.match(goodchars, c)] + ) def valid(self, s, partial=False): - for c in self.badchars: - if c in s: - raise ArgumentFormat("bad char {0} in {1}".format(c, s)) + sset = set(s) + if self.goodset and not sset <= self.goodset: + raise ArgumentFormat("invalid chars {0} in {1}".\ + format(''.join(sset - self.goodset), s)) self.val = s def __str__(self): b = '' - if len(self.badchars): - b = '(without chars in {0})'.format(self.badchars) + if self.goodchars: + b += '(goodchars {0})'.format(self.goodchars) return ''.format(b) class CephSocketpath(CephArgtype):