| 1 | import re
|
|---|
| 2 |
|
|---|
| 3 | from ....builtins import *
|
|---|
| 4 |
|
|---|
| 5 | from ...exceptions import *
|
|---|
| 6 | from ...expressions import *
|
|---|
| 7 | from ...streams import *
|
|---|
| 8 |
|
|---|
| 9 | ##
|
|---|
| 10 | ## bpstulist has records separated by blank lines
|
|---|
| 11 | ##
|
|---|
| 12 | def stream(stream, format='bpstulist -L'):
|
|---|
| 13 |
|
|---|
| 14 | if format in ['bpstulist -L']:
|
|---|
| 15 | return BlankLineStream(stream, header=1)
|
|---|
| 16 | else:
|
|---|
| 17 | raise ParseError, 'Unknown format %s' % (format)
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | ##
|
|---|
| 21 | ## Parse a bpstulist record
|
|---|
| 22 | ##
|
|---|
| 23 | ## bpstulist -L
|
|---|
| 24 | ##
|
|---|
| 25 | def parse(record, format='bpstulist -L', version=None, tz=None):
|
|---|
| 26 |
|
|---|
| 27 | stunit = ExtendedDict()
|
|---|
| 28 |
|
|---|
| 29 | re_pair = re.compile('^\s*([^:]+):\s*(.*)\s*$')
|
|---|
| 30 | re_type_id = re.compile('^([0-9a-zA-Z\-_ ]+)\s+\((\d+)\)$')
|
|---|
| 31 | re_quoted_string = re.compile('^[\'"].*[\'"]$')
|
|---|
| 32 |
|
|---|
| 33 | if format == 'bpstulist -L':
|
|---|
| 34 |
|
|---|
| 35 | try:
|
|---|
| 36 |
|
|---|
| 37 | for line in record:
|
|---|
| 38 |
|
|---|
| 39 | match = re_pair.match(line)
|
|---|
| 40 |
|
|---|
| 41 | key = match.group(1)
|
|---|
| 42 | value = match.group(2)
|
|---|
| 43 | key = key.lower()
|
|---|
| 44 | key = key.replace(' ', '_')
|
|---|
| 45 | key = key.replace('/', '_')
|
|---|
| 46 |
|
|---|
| 47 | #
|
|---|
| 48 | # convert text values to python datatypes
|
|---|
| 49 | #
|
|---|
| 50 | while True:
|
|---|
| 51 |
|
|---|
| 52 | #
|
|---|
| 53 | # convert to integer value
|
|---|
| 54 | #
|
|---|
| 55 | match = re_integer.match(value)
|
|---|
| 56 | if match:
|
|---|
| 57 | value = int(value)
|
|---|
| 58 | break
|
|---|
| 59 |
|
|---|
| 60 | #
|
|---|
| 61 | # remove quotes from quoted string
|
|---|
| 62 | #
|
|---|
| 63 | match = re_quoted_string.match(value)
|
|---|
| 64 | if match:
|
|---|
| 65 | value = value[1:-1]
|
|---|
| 66 | break
|
|---|
| 67 |
|
|---|
| 68 | #
|
|---|
| 69 | # separate type and id
|
|---|
| 70 | #
|
|---|
| 71 | match = re_type_id.match(value)
|
|---|
| 72 | if match:
|
|---|
| 73 | idkey = '%s_id' % (key)
|
|---|
| 74 | value = match.group(1)
|
|---|
| 75 | stunit[idkey] = int(match.group(2))
|
|---|
| 76 | break
|
|---|
| 77 |
|
|---|
| 78 | #
|
|---|
| 79 | # separate robot type, id, and number
|
|---|
| 80 | #
|
|---|
| 81 | if key == 'robot_type_number':
|
|---|
| 82 |
|
|---|
| 83 | robot_type, robot_number = value.split(' / ')
|
|---|
| 84 |
|
|---|
| 85 | match = re_type_id.match(robot_type)
|
|---|
| 86 | if match:
|
|---|
| 87 | robot_type = match.group(1)
|
|---|
| 88 | stunit['robot_type_id'] = match.group(2)
|
|---|
| 89 |
|
|---|
| 90 | stunit['robot_type'] = robot_type
|
|---|
| 91 | stunit['robot_number'] = robot_number
|
|---|
| 92 |
|
|---|
| 93 | break
|
|---|
| 94 |
|
|---|
| 95 | #
|
|---|
| 96 | # separate robot type and id
|
|---|
| 97 | #
|
|---|
| 98 | if key == 'robot_type':
|
|---|
| 99 | if value == '(not robotic)':
|
|---|
| 100 | value = None
|
|---|
| 101 | break
|
|---|
| 102 |
|
|---|
| 103 | #
|
|---|
| 104 | # break out of enclosing while loop
|
|---|
| 105 | #
|
|---|
| 106 | break
|
|---|
| 107 |
|
|---|
| 108 | stunit[key] = value
|
|---|
| 109 |
|
|---|
| 110 | return stunit
|
|---|
| 111 |
|
|---|
| 112 | except Exception, e:
|
|---|
| 113 |
|
|---|
| 114 | raise ParseError, e
|
|---|
| 115 |
|
|---|
| 116 | else:
|
|---|
| 117 |
|
|---|
| 118 | raise ParseError, 'Unknown format %s' % (format)
|
|---|
| 119 |
|
|---|