1 | import re
|
---|
2 |
|
---|
3 | import time
|
---|
4 | import datetime
|
---|
5 | import pytz
|
---|
6 |
|
---|
7 | from ....builtins import *
|
---|
8 |
|
---|
9 | from ...exceptions import *
|
---|
10 | from ...expressions import *
|
---|
11 | from ...autotype import *
|
---|
12 | from ...streams import *
|
---|
13 |
|
---|
14 | ##
|
---|
15 | ## nbdevquery has records separated by blank lines
|
---|
16 | ##
|
---|
17 | def stream(stream, format='nbdevquery -listdv -U -stype BasicDisk'):
|
---|
18 |
|
---|
19 | if format in ['nbdevquery -listdv -U -stype BasicDisk', 'nbdevquery -listdv -U -stype AdvancedDisk', 'nbdevquery -listdv -U -stype PureDisk']:
|
---|
20 | return BlankLineStream(stream, header=0)
|
---|
21 | else:
|
---|
22 | raise ParseError, 'Unknown format %s' % (format)
|
---|
23 |
|
---|
24 |
|
---|
25 | ##
|
---|
26 | ## Parse a nbdevquery record
|
---|
27 | ##
|
---|
28 | ## nbdevquery -listdv -U -stype BasicDisk
|
---|
29 | ##
|
---|
30 | def parse(record, format='nbdevquery -listdv -U -stype BasicDisk', version=None, tz=None):
|
---|
31 |
|
---|
32 | re_pair = re.compile('^\s*([^:]+):\s*(.*)\s*$')
|
---|
33 | re_type_id = re.compile('^([0-9a-zA-Z\-_ ]+)\s+\((\d+)\)$')
|
---|
34 | re_id_type = re.compile('^(\d+)\s+\(([0-9a-zA-Z\-_ ]+)\)$')
|
---|
35 | re_in_parens = re.compile('^\((.*)\)$')
|
---|
36 | re_starts_with_time = re.compile('^time_.*$')
|
---|
37 | re_ends_with_time = re.compile('^.*_time$')
|
---|
38 | re_time_plus_epoch_seconds_in_parens = re.compile('^.*\d:\d\d:\d\d.*\((\d+)\)$')
|
---|
39 | re_epoch_seconds_plus_time_in_parens = re.compile('^(\d+)\s+\(.*\d:\d\d:\d\d.*\)$')
|
---|
40 |
|
---|
41 | re_trailing_whitespace = re.compile('\s+$')
|
---|
42 | re_strip_trailing_whitespace = re.compile('^(.*\S)\s*$')
|
---|
43 |
|
---|
44 | pool = ExtendedDict()
|
---|
45 | pool['flags'] = ExtendedDict()
|
---|
46 |
|
---|
47 | if format in ['nbdevquery -listdv -U -stype BasicDisk', 'nbdevquery -listdv -U -stype AdvancedDisk', 'nbdevquery -listdv -U -stype PureDisk']:
|
---|
48 |
|
---|
49 | try:
|
---|
50 |
|
---|
51 | for line in record:
|
---|
52 |
|
---|
53 | match = re_pair.match(line)
|
---|
54 |
|
---|
55 | key = match.group(1)
|
---|
56 | value = match.group(2)
|
---|
57 |
|
---|
58 | key = key.rstrip()
|
---|
59 | value = value.rstrip()
|
---|
60 |
|
---|
61 | key = re.sub('\s+', ' ', key)
|
---|
62 | key = key.lower()
|
---|
63 | key = key.replace(' ', '_')
|
---|
64 | key = key.replace('/', '_')
|
---|
65 | key = key.replace('(', '')
|
---|
66 | key = key.replace(')', '')
|
---|
67 | key = key.replace('%', '_pct')
|
---|
68 |
|
---|
69 | #
|
---|
70 | # convert to python datatypes
|
---|
71 | #
|
---|
72 | while True:
|
---|
73 |
|
---|
74 | #
|
---|
75 | # convert to integer value
|
---|
76 | #
|
---|
77 | if re_integer.match(value):
|
---|
78 | value = int(value)
|
---|
79 | break
|
---|
80 |
|
---|
81 | #
|
---|
82 | # convert to floating point value
|
---|
83 | #
|
---|
84 | if re_float.match(value):
|
---|
85 | value = float(value)
|
---|
86 | break
|
---|
87 |
|
---|
88 | #
|
---|
89 | # break if nothing matched
|
---|
90 | #
|
---|
91 | break
|
---|
92 |
|
---|
93 | #
|
---|
94 | # if flag, add value to flags
|
---|
95 | #
|
---|
96 | if key == 'flag':
|
---|
97 | pool.flags[value] = True
|
---|
98 | else:
|
---|
99 | pool[key] = value
|
---|
100 |
|
---|
101 | return pool
|
---|
102 |
|
---|
103 | except Exception, e:
|
---|
104 |
|
---|
105 | raise ParseError, e
|
---|
106 |
|
---|
107 | else:
|
---|
108 |
|
---|
109 | raise ParseError, 'Unknown format %s' % (format)
|
---|