~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/conch/ssh/sexpy.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
#
 
5
 
 
6
def parse(s):
 
7
    s = s.strip()
 
8
    expr = []
 
9
    while s:
 
10
        if s[0] == '(':
 
11
            newSexp = []
 
12
            if expr:
 
13
                expr[-1].append(newSexp)
 
14
            expr.append(newSexp)
 
15
            s = s[1:]
 
16
            continue
 
17
        if s[0] == ')':
 
18
            aList = expr.pop()
 
19
            s=s[1:]
 
20
            if not expr:
 
21
                assert not s
 
22
                return aList
 
23
            continue
 
24
        i = 0
 
25
        while s[i].isdigit(): i+=1
 
26
        assert i
 
27
        length = int(s[:i])
 
28
        data = s[i+1:i+1+length]
 
29
        expr[-1].append(data)
 
30
        s=s[i+1+length:]
 
31
    assert 0, "this should not happen"
 
32
 
 
33
def pack(sexp):
 
34
    s = ""
 
35
    for o in sexp:
 
36
        if type(o) in (type(()), type([])):
 
37
            s+='('
 
38
            s+=pack(o)
 
39
            s+=')'
 
40
        else:
 
41
            s+='%i:%s' % (len(o), o)
 
42
    return s