From 741757a3c3cb437adcd38028111c9dbd9cf57463 Mon Sep 17 00:00:00 2001 From: Dan Mick Date: Fri, 12 Jul 2013 21:23:14 -0700 Subject: [PATCH] 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 --- src/pybind/ceph_argparse.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/pybind/ceph_argparse.py b/src/pybind/ceph_argparse.py index 830667480d4f7..b2f8956c8a530 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): -- 2.39.5