Revision f2b91949

b/lib/utils/text.py
453 453
  return rlist
454 454

  
455 455

  
456
def EscapeAndJoin(slist, sep=","):
457
  """Encode a list in a way parsable by UnescapeAndSplit.
458

  
459
  @type slist: list of strings
460
  @param slist: the strings to be encoded
461
  @rtype: string
462
  @return: the encoding of the list oas a string
463

  
464
  """
465
  return sep.join([re.sub("\\" + sep, "\\\\" + sep,
466
                          re.sub(r"\\", r"\\\\", v)) for v in slist])
467

  
468

  
456 469
def CommaJoin(names):
457 470
  """Nicely join a set of identifiers.
458 471

  
b/test/py/ganeti.utils.text_unittest.py
445 445
      b = ["a", "b" + sep + "c", "d" + sep + "e" + sep + "f", "g"]
446 446
      self.failUnlessEqual(utils.UnescapeAndSplit(sep.join(a), sep=sep), b)
447 447

  
448
class TestEscapeAndJoin(unittest.TestCase):
449
  def verifyParsesCorrect(self, args):
450
    for sep in [",", "+", ".", ":"]:
451
      self.assertEqual(utils.UnescapeAndSplit(
452
          utils.EscapeAndJoin(args, sep=sep),
453
          sep=sep), args)
454

  
455
  def test(self):
456
    self.verifyParsesCorrect(["a", "b", "c"])
457
    self.verifyParsesCorrect(["2.10.0", "12345"])
458
    self.verifyParsesCorrect(["2.10.0~alpha1", "12345"])
459
    self.verifyParsesCorrect(["..:", ",,+"])
460
    self.verifyParsesCorrect(["a\\", "b\\\\", "c"])
461
    self.verifyParsesCorrect(["a"])
462
    self.verifyParsesCorrect(["+"])
463
    self.verifyParsesCorrect(["\\"])
464
    self.verifyParsesCorrect(["\\\\"])
465

  
448 466

  
449 467
class TestCommaJoin(unittest.TestCase):
450 468
  def test(self):

Also available in: Unified diff