summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Mick <dan.mick@inktank.com>2013-07-12 21:23:14 -0700
committerDan Mick <dan.mick@inktank.com>2013-07-12 21:42:38 -0700
commit741757a3c3cb437adcd38028111c9dbd9cf57463 (patch)
tree7a9e83fc53091010667d6b23a41de736bc7f9af8
parent089dfe8e9e98890f14bc824f7b877f209113e46e (diff)
downloadceph-741757a3c3cb437adcd38028111c9dbd9cf57463.tar.gz
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 <dan.mick@inktank.com>
-rw-r--r--src/pybind/ceph_argparse.py27
1 files changed, 19 insertions, 8 deletions
diff --git a/src/pybind/ceph_argparse.py b/src/pybind/ceph_argparse.py
index 830667480d4..b2f8956c8a5 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 '<string{0}>'.format(b)
class CephSocketpath(CephArgtype):