On Mon, Oct 7, 2013 at 9:55 AM, Klaus Aehlig <[email protected]> wrote: > With utils.UnescapeAndSplit, we have a function to parse > arbitrary non-empty string lists encoded as strings. Also > provde the appropriate encoding function.
s/provde/provide > > Signed-off-by: Klaus Aehlig <[email protected]> > --- > lib/utils/text.py | 13 +++++++++++++ > test/py/ganeti.utils.text_unittest.py | 18 ++++++++++++++++++ > 2 files changed, 31 insertions(+) > > diff --git a/lib/utils/text.py b/lib/utils/text.py > index e768588..a69af17 100644 > --- a/lib/utils/text.py > +++ b/lib/utils/text.py > @@ -453,6 +453,19 @@ def UnescapeAndSplit(text, sep=","): > return rlist > > > +def EscapeAndJoin(slist, sep=","): > + """Encode a list in a way parsable by UnescapeAndSplit. > + > + @type slist: list of strings > + @param slist: the strings to be encoded > + @rtype: string > + @return: the encoding of the list oas a string > + > + """ > + return sep.join([re.sub("\\" + sep, "\\\\" + sep, > + re.sub(r"\\", r"\\\\", v)) for v in slist]) > + > + > def CommaJoin(names): > """Nicely join a set of identifiers. > > diff --git a/test/py/ganeti.utils.text_unittest.py > b/test/py/ganeti.utils.text_unittest.py > index b99bcfd..d10a3b7 100755 > --- a/test/py/ganeti.utils.text_unittest.py > +++ b/test/py/ganeti.utils.text_unittest.py > @@ -445,6 +445,24 @@ class TestUnescapeAndSplit(unittest.TestCase): > b = ["a", "b" + sep + "c", "d" + sep + "e" + sep + "f", "g"] > self.failUnlessEqual(utils.UnescapeAndSplit(sep.join(a), sep=sep), b) > > +class TestEscapeAndJoin(unittest.TestCase): > + def verifyParsesCorrect(self, args): > + for sep in [",", "+", ".", ":"]: > + self.assertEqual(utils.UnescapeAndSplit( > + utils.EscapeAndJoin(args, sep=sep), > + sep=sep), args) > + > + def test(self): > + self.verifyParsesCorrect(["a", "b", "c"]) > + self.verifyParsesCorrect(["2.10.0", "12345"]) > + self.verifyParsesCorrect(["2.10.0~alpha1", "12345"]) > + self.verifyParsesCorrect(["..:", ",,+"]) > + self.verifyParsesCorrect(["a\\", "b\\\\", "c"]) > + self.verifyParsesCorrect(["a"]) > + self.verifyParsesCorrect(["+"]) > + self.verifyParsesCorrect(["\\"]) > + self.verifyParsesCorrect(["\\\\"]) > + > > class TestCommaJoin(unittest.TestCase): > def test(self): > -- > 1.8.4 > LGTM, thanks. Michele -- Google Germany GmbH Dienerstr. 12 80331 München Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Geschäftsführer: Graham Law, Christine Elizabeth Flores
