blob: f21770ba77910a7c15d3408aa948974776935154 [file] [log] [blame]
Rich Lane62a56492013-05-10 15:28:04 -07001#!/usr/bin/python
2# Copyright 2013, Big Switch Networks, Inc.
3#
4# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
5# the following special exception:
6#
7# LOXI Exception
8#
9# As a special exception to the terms of the EPL, you may distribute libraries
10# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
11# that copyright and licensing notices generated by LoxiGen are not altered or removed
12# from the LoxiGen Libraries and the notice provided below is (i) included in
13# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
14# documentation for the LoxiGen Libraries, if distributed in binary form.
15#
16# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
17#
18# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
19# a copy of the EPL at:
20#
21# http://www.eclipse.org/legal/epl-v10.html
22#
23# Unless required by applicable law or agreed to in writing, software
24# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26# EPL for the specific language governing permissions and limitations
27# under the EPL.
28
29import os
30
31_test_data_dir = os.path.dirname(os.path.realpath(__file__))
32
33def list_files():
34 """
35 Return a list of the data files in this directory
36
37 These strings are suitable to be passed to read().
38 """
39
40 result = []
41 for dirname, dirnames, filenames in os.walk(_test_data_dir):
42 dirname = os.path.relpath(dirname, _test_data_dir)
43 for filename in filenames:
44 if filename.endswith('.data') and not filename.startswith('.'):
45 result.append(dirname + '/' + filename)
46 return sorted(result)
47
48def read(name):
49 """
50 Read, parse, and return a test data file
51
52 @param name Filename relative to the test_data directory
53 @returns A hash from section to the string contents
54
55 A section named "binary" is treated specially: it's treated
56 as a hex dump and parsed into a binary string.
57 """
58
59 section_lines = {}
60 cur_section = None
61
62 with open(os.path.join(_test_data_dir, name)) as f:
63 for line in f:
Rich Lanea0ee6362013-05-16 16:35:35 -070064 line = line.rstrip().partition('#')[0].rstrip()
Rich Lane62a56492013-05-10 15:28:04 -070065 if line == '':
66 continue
67 elif line.startswith('--'):
68 cur_section = line[2:].strip()
69 if cur_section in section_lines:
70 raise Exception("section %s already exists in the test data file")
71 section_lines[cur_section] = []
72 elif cur_section:
73 section_lines[cur_section].append(line)
74 data = { section: '\n'.join(lines) for (section, lines) in section_lines.items() }
75
76 # Special case: convert 'binary' section into binary
77 # The string '00 11\n22 33' results in "\x00\x11\x22\x33"
78 if 'binary' in data:
79 hex_strs = data['binary'].split()
80 data['binary'] = ''.join(map(lambda x: chr(int(x, 16)), hex_strs))
81
82 return data