1 | import re
|
---|
2 |
|
---|
3 | from ....builtins import *
|
---|
4 |
|
---|
5 | from ...exceptions import *
|
---|
6 | from ...expressions import *
|
---|
7 | from ...streams import *
|
---|
8 |
|
---|
9 | ##
|
---|
10 | ## vmpool has records separated by a line of = characters
|
---|
11 | ##
|
---|
12 | def stream(stream, format='vmpool -list_all'):
|
---|
13 |
|
---|
14 | if format in ['vmpool -list_all', 'vmpool -listall']:
|
---|
15 | return LineSeparatorStream(stream, separator='================================================================================', header=1)
|
---|
16 | elif format in ['vmpool -list_scratch']:
|
---|
17 | return LineSeparatorStream(stream, separator='=============', header=2)
|
---|
18 | else:
|
---|
19 | raise ParseError, 'Unknown format %s' % (format)
|
---|
20 |
|
---|
21 |
|
---|
22 | ##
|
---|
23 | ## Parse a vmpool record
|
---|
24 | ##
|
---|
25 | ## vmpool -listall
|
---|
26 | ##
|
---|
27 | def parse(record, format='vmpool -listall', version=None, tz=None):
|
---|
28 |
|
---|
29 | pool = ExtendedDict()
|
---|
30 | re_pair = re.compile('^([^:]+):\s*(.*)$')
|
---|
31 |
|
---|
32 | if format in ['vmpool -list_all', 'vmpool -listall']:
|
---|
33 |
|
---|
34 | try:
|
---|
35 |
|
---|
36 | for line in record:
|
---|
37 |
|
---|
38 | match = re_pair.match(line)
|
---|
39 |
|
---|
40 | key = match.group(1)
|
---|
41 | value = match.group(2)
|
---|
42 | key = key.lower()
|
---|
43 | key = key.replace(' ', '_')
|
---|
44 | key = key.replace('/', '_')
|
---|
45 |
|
---|
46 | #
|
---|
47 | # Convert to integer value
|
---|
48 | #
|
---|
49 | if re_integer.match(value):
|
---|
50 | value = int(value)
|
---|
51 |
|
---|
52 | pool[key] = value
|
---|
53 |
|
---|
54 | return pool
|
---|
55 |
|
---|
56 | except Exception, e:
|
---|
57 |
|
---|
58 | raise ParseError, e
|
---|
59 |
|
---|
60 | elif format in ['vmpool -list_scratch']:
|
---|
61 |
|
---|
62 | try:
|
---|
63 |
|
---|
64 | return record[0]
|
---|
65 |
|
---|
66 | except Exception, e:
|
---|
67 |
|
---|
68 | raise ParseError, e
|
---|
69 |
|
---|
70 | else:
|
---|
71 |
|
---|
72 | raise ParseError, 'Unknown format %s' % (format)
|
---|
73 |
|
---|
74 |
|
---|