|
Revision 168, 2.0 kB
(checked in by abaxter, 3 years ago)
|
whitespace normalisation
|
| Line | |
|---|
| 1 |
import syck |
|---|
| 2 |
from pyramid import yamlRegistry as Y |
|---|
| 3 |
import mergetrees |
|---|
| 4 |
|
|---|
| 5 |
def merge(a, b, dir=None, partialbuild=False): |
|---|
| 6 |
|
|---|
| 7 |
if (isinstance(a, Y.sectionnav) and |
|---|
| 8 |
isinstance(b, Y.sectionnav) and dir is not None): |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
if a == b: |
|---|
| 14 |
b = [] |
|---|
| 15 |
if partialbuild: |
|---|
| 16 |
a = b |
|---|
| 17 |
else: |
|---|
| 18 |
mergetrees.merge(a, b, dir) |
|---|
| 19 |
|
|---|
| 20 |
elif isinstance(b, dict) and isinstance(a, dict): |
|---|
| 21 |
for bkey in b.keys(): |
|---|
| 22 |
if a.has_key(bkey): |
|---|
| 23 |
a[bkey] = merge(a[bkey], b[bkey], dir, |
|---|
| 24 |
partialbuild=partialbuild) |
|---|
| 25 |
else: |
|---|
| 26 |
a[bkey] = b[bkey] |
|---|
| 27 |
else: |
|---|
| 28 |
a = b |
|---|
| 29 |
return a |
|---|
| 30 |
|
|---|
| 31 |
class stackdict(dict): |
|---|
| 32 |
|
|---|
| 33 |
parent = None |
|---|
| 34 |
|
|---|
| 35 |
def __init__(self, parent=None): |
|---|
| 36 |
if parent is not None: |
|---|
| 37 |
self.parent = parent |
|---|
| 38 |
merge(self, parent) |
|---|
| 39 |
|
|---|
| 40 |
def popStack(self): |
|---|
| 41 |
return self.parent |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
if __name__ == "__main__": |
|---|
| 45 |
|
|---|
| 46 |
def keyval(text): |
|---|
| 47 |
''' Factory to convert a string into key:val pair (split on last space) |
|---|
| 48 |
''' |
|---|
| 49 |
t = text.split(' ') |
|---|
| 50 |
return { ''.join(t[:-1]) : t[-1] } |
|---|
| 51 |
|
|---|
| 52 |
x = ''' |
|---|
| 53 |
a: 1 |
|---|
| 54 |
b: |
|---|
| 55 |
x: 12 |
|---|
| 56 |
y: 13 |
|---|
| 57 |
z: 14 |
|---|
| 58 |
c: 3 |
|---|
| 59 |
''' |
|---|
| 60 |
|
|---|
| 61 |
y = ''' |
|---|
| 62 |
b: |
|---|
| 63 |
x: 15 |
|---|
| 64 |
z: |
|---|
| 65 |
p: 98 |
|---|
| 66 |
q: 99 |
|---|
| 67 |
c: 3P0 |
|---|
| 68 |
''' |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
xd = syck.load(x, Loader=Y.Loader, implicit_typing=False) |
|---|
| 72 |
yd = syck.load(y, Loader=Y.Loader, implicit_typing=False) |
|---|
| 73 |
|
|---|
| 74 |
z = merge(xd, yd) |
|---|
| 75 |
|
|---|
| 76 |
print z |
|---|
| 77 |
|
|---|
| 78 |
print '-------------------' |
|---|
| 79 |
|
|---|
| 80 |
d = stackdict() |
|---|
| 81 |
d['a'] = 1 |
|---|
| 82 |
d['b'] = {'x':1, 'y':2 } |
|---|
| 83 |
print d |
|---|
| 84 |
|
|---|
| 85 |
d = stackdict(d) |
|---|
| 86 |
d['a'] = 2 |
|---|
| 87 |
print d |
|---|
| 88 |
|
|---|
| 89 |
d = stackdict(d) |
|---|
| 90 |
d['b'] = {'x':2, 'y': {'p':4 } } |
|---|
| 91 |
d['c'] = 100 |
|---|
| 92 |
print d |
|---|
| 93 |
|
|---|
| 94 |
while 1: |
|---|
| 95 |
d = d.popStack() |
|---|
| 96 |
if d is None: |
|---|
| 97 |
break |
|---|
| 98 |
print d |
|---|