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 | ## bpmedialist has records separated by blank lines
|
---|
16 | ##
|
---|
17 | def stream(stream, format='bpmedialist -mlist'):
|
---|
18 |
|
---|
19 | if format in ['bpmedialist -mlist']:
|
---|
20 | return BlankLineStream(stream, header=2)
|
---|
21 | else:
|
---|
22 | raise ParseError, 'Unknown format %s' % (format)
|
---|
23 |
|
---|
24 |
|
---|
25 | ##
|
---|
26 | ## Parse a bpmedialist record
|
---|
27 | ##
|
---|
28 | ## bpmedialist -L
|
---|
29 | ##
|
---|
30 | def parse(record, format='bpmedialist -L', 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_unix_time_hack = re.compile('^.*\((\d+)\)$')
|
---|
35 | re_trailing_whitespace = re.compile('\s+$')
|
---|
36 |
|
---|
37 | media = ExtendedDict()
|
---|
38 | media['flags'] = ExtendedDict()
|
---|
39 |
|
---|
40 | if format == 'bpmedialist -L':
|
---|
41 |
|
---|
42 | try:
|
---|
43 |
|
---|
44 | for line in record:
|
---|
45 |
|
---|
46 | for pair in line.split(', '):
|
---|
47 |
|
---|
48 | key, value = pair.split(' = ')
|
---|
49 |
|
---|
50 | #
|
---|
51 | # convert text values to python datatypes
|
---|
52 | #
|
---|
53 | while True:
|
---|
54 |
|
---|
55 | if key in ['allocated', 'last_written', 'expiration', 'last_read']:
|
---|
56 | match = re_unix_time_hack.match(value)
|
---|
57 | value = datetime.datetime.fromtimestamp(float(match.group(1)), tz)
|
---|
58 | break
|
---|
59 |
|
---|
60 | if key in ['density']:
|
---|
61 | match = re_type_id.match(value)
|
---|
62 | idkey = '%s_id' % (key)
|
---|
63 | value = match.group(1)
|
---|
64 | media[idkey] = int(match.group(2))
|
---|
65 | break
|
---|
66 |
|
---|
67 | if key in ['status']:
|
---|
68 |
|
---|
69 | value = int(value, 16)
|
---|
70 | status = ExtendedDict()
|
---|
71 |
|
---|
72 | if value & 0x200 == 0x200:
|
---|
73 | media.flags.multiplexed = True
|
---|
74 | else:
|
---|
75 | media.flags.multiplexed = False
|
---|
76 |
|
---|
77 | if value & 0x080 == 0x080:
|
---|
78 | media.flags.imported = True
|
---|
79 | else:
|
---|
80 | media.flags.imported = False
|
---|
81 |
|
---|
82 | if value & 0x040 == 0x040:
|
---|
83 | media.flags.multiple_retention_levels = True
|
---|
84 | else:
|
---|
85 | media.flags.multiple_retention_levels = False
|
---|
86 |
|
---|
87 | if value & 0x008 == 0x008:
|
---|
88 | media.flags.full = True
|
---|
89 | else:
|
---|
90 | media.flags.full = False
|
---|
91 |
|
---|
92 | if value & 0x004 == 0x004:
|
---|
93 | media.flags.unreported = True
|
---|
94 | else:
|
---|
95 | media.flags.unreported = False
|
---|
96 |
|
---|
97 | if value & 0x002 == 0x002:
|
---|
98 | media.flags.suspended = True
|
---|
99 | else:
|
---|
100 | media.flags.suspended = False
|
---|
101 |
|
---|
102 | if value & 0x001 == 0x001:
|
---|
103 | media.flags.frozen = True
|
---|
104 | else:
|
---|
105 | media.flags.frozen = False
|
---|
106 |
|
---|
107 | if value & 0x000 == 0x000:
|
---|
108 | media.flags.active = True
|
---|
109 | else:
|
---|
110 | media.flags.active = False
|
---|
111 |
|
---|
112 | break
|
---|
113 |
|
---|
114 | if value in['*NULL*']:
|
---|
115 | value = None
|
---|
116 | break
|
---|
117 |
|
---|
118 | value = autotype(value)
|
---|
119 | break
|
---|
120 |
|
---|
121 | media[key] = value
|
---|
122 |
|
---|
123 | return media
|
---|
124 |
|
---|
125 | except Exception, e:
|
---|
126 |
|
---|
127 | for line in record:
|
---|
128 | print line
|
---|
129 |
|
---|
130 | raise ParseError, e
|
---|
131 |
|
---|
132 | else:
|
---|
133 |
|
---|
134 | raise ParseError, 'Unknown format %s' % (format)
|
---|
135 |
|
---|